Replaces doctests test by pytest tests
This commit is contained in:
parent
85eb74ba5f
commit
dfeff7a50c
|
@ -19,50 +19,6 @@ def get_product(code, from_file=False):
|
|||
"""
|
||||
Call Open Food Facts API to get data of a single product
|
||||
|
||||
:Tests ONLINE:
|
||||
# >>> prod_beurre = get_product('3017760000109')
|
||||
# >>> prod_oreo = get_product('8410000810004')
|
||||
|
||||
# >>> prod_false is False
|
||||
# True
|
||||
|
||||
# >>> prod_string is False
|
||||
# True
|
||||
|
||||
:Tests OFFLINE:
|
||||
>>> prod_beurre = get_product('3017760000109', True)
|
||||
>>> prod_oreo = get_product('8410000810004', True)
|
||||
>>> prod_false = get_product('1664', True)
|
||||
File load error : sample/product-1664.json
|
||||
|
||||
>>> prod_string = get_product('string', True)
|
||||
File load error : sample/product-string.json
|
||||
|
||||
>>> print(prod_beurre['product_name'])
|
||||
Le Véritable Petit Beurre
|
||||
|
||||
>>> print(prod_beurre['nutrition_grades'])
|
||||
e
|
||||
|
||||
>>> print(prod_beurre['categories_tags'])
|
||||
['en:sugary-snacks', 'en:biscuits-and-cakes', \
|
||||
'en:biscuits', 'fr:petits-beurres']
|
||||
|
||||
>>> print(prod_oreo['code'])
|
||||
8410000810004
|
||||
|
||||
>>> print(prod_oreo['url'])
|
||||
https://fr.openfoodfacts.org/product/8410000810004/
|
||||
|
||||
>>> print(prod_oreo['product_name'])
|
||||
Biscuit Oreo
|
||||
|
||||
>>> print(prod_oreo['nutrition_grades'])
|
||||
e
|
||||
|
||||
>>> print(prod_oreo['categories_tags'])
|
||||
['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', \
|
||||
'en:chocolate-biscuits', 'es:sandwich-cookies']
|
||||
"""
|
||||
|
||||
ERR_FILE = "File load error : {}"
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
import function as target
|
||||
|
||||
@pytest.fixture()
|
||||
def product_offline():
|
||||
prod_beurre = target.get_product('3017760000109', True)
|
||||
prod_oreo = target.get_product('8410000810004', True)
|
||||
prod_false = target.get_product('1664', True)
|
||||
prod_string = target.get_product('string', True)
|
||||
|
||||
return (prod_beurre, prod_oreo, prod_false, prod_string)
|
||||
|
||||
@pytest.fixture()
|
||||
def product_online():
|
||||
prod_beurre = target.get_product('3017760000109')
|
||||
prod_oreo = target.get_product('8410000810004')
|
||||
return (prod_beurre, prod_oreo)
|
||||
|
||||
def test_get_product_stdout(capsys):
|
||||
target.get_product('1664', True)
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "File load error : sample/product-1664.json\n"
|
||||
|
||||
target.get_product('string', True)
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "File load error : sample/product-string.json\n"
|
||||
|
||||
def test_get_product(product_offline):
|
||||
# :Tests OFFLINE:
|
||||
prod_beurre, prod_oreo, prod_false, prod_string = product_offline
|
||||
assert prod_beurre['product_name'] == 'Le Véritable Petit Beurre'
|
||||
assert prod_beurre['nutrition_grades'] == 'e'
|
||||
assert prod_beurre['categories_tags'] == [
|
||||
'en:sugary-snacks',
|
||||
'en:biscuits-and-cakes',
|
||||
'en:biscuits',
|
||||
'fr:petits-beurres'
|
||||
]
|
||||
|
||||
# better use the file in sample directory
|
||||
assert prod_oreo == {'categories_tags': [
|
||||
'en:sugary-snacks',
|
||||
'en:biscuits-and-cakes',
|
||||
'en:biscuits',
|
||||
'en:chocolate-biscuits',
|
||||
'es:sandwich-cookies'
|
||||
],
|
||||
'code': '8410000810004',
|
||||
'nutrition_grades': 'e',
|
||||
'product_name': 'Biscuit Oreo',
|
||||
'url':'https://fr.openfoodfacts.org/product/8410000810004/'}
|
||||
|
||||
assert not prod_false
|
||||
assert not prod_string
|
Loading…
Reference in New Issue