bug fix tests

This commit is contained in:
smilerz 2021-04-05 21:43:33 -05:00
parent 2fe6788ce5
commit 436a070730
2 changed files with 2332 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,50 +1,81 @@
import json import json
import pytest import pytest
from django.urls import reverse from django.urls import reverse
from ._recipes import (AMERICAS_TEST_KITCHEN) from ._recipes import (ALLRECIPES, AMERICAS_TEST_KITCHEN, CHEF_KOCH, COOKPAD,
COOKS_COUNTRY, DELISH, FOOD_NETWORK, GIALLOZAFFERANO, JOURNAL_DES_FEMMES,
MADAME_DESSERT, MARMITON, TASTE_OF_HOME, TUDOGOSTOSO)
IMPORT_SOURCE_URL = 'api_recipe_from_source'
IMPORT_SOURCE_URL = 'data_import_source' DATA_DIR = "cookbook/tests/other/test_data/"
# These were chosen arbitrarily from: # These were chosen arbitrarily from:
# Top 10 recipe websites listed here https://www.similarweb.com/top-websites/category/food-and-drink/cooking-and-recipes/ # Top 10 recipe websites listed here https://www.similarweb.com/top-websites/category/food-and-drink/cooking-and-recipes/
# plus the test that previously existed # plus the test that previously existed
# custom scraper that was created # plus the custom scraper that was created
# TODO thoughtfully add recipes that test specific scenerios # TODO thoughtfully add recipes that test specific scenerios
@pytest.mark.parametrize("arg", [ @pytest.mark.parametrize("arg", [
['a_u', 403], ['a_u', 302],
['g1_s1', 404], ['g1_s1', 302],
['u1_s1', 200], ['u1_s1', 400],
['a1_s1', 404], ['a1_s1', 400],
['g1_s2', 404],
['u1_s2', 404],
['a1_s2', 404],
]) ])
def test_import_permission(arg, request): def test_import_permission(arg, request):
c = request.getfixturevalue(arg[0]) c = request.getfixturevalue(arg[0])
assert c.get(reverse(IMPORT_SOURCE_URL)).status_code == arg[1] assert c.get(reverse(IMPORT_SOURCE_URL)).status_code == arg[1]
# TODO this test is really bad, need to find a better solution, also pytest does not like those paths
# def test_ld_json():
# with scopes_disabled(): @pytest.mark.parametrize("arg", [
# test_list = [ ALLRECIPES,
# {'file': 'resources/websites/ld_json_1.html', 'result_length': 3237}, AMERICAS_TEST_KITCHEN,
# {'file': 'resources/websites/ld_json_2.html', 'result_length': 1525}, CHEF_KOCH,
# {'file': 'resources/websites/ld_json_3.html', 'result_length': 1644}, COOKPAD,
# {'file': 'resources/websites/ld_json_4.html', 'result_length': 1744}, COOKS_COUNTRY,
# {'file': 'resources/websites/ld_json_itemList.html', 'result_length': 3222}, DELISH,
# {'file': 'resources/websites/ld_json_multiple.html', 'result_length': 1621}, FOOD_NETWORK,
# {'file': 'resources/websites/micro_data_1.html', 'result_length': 1094}, GIALLOZAFFERANO,
# {'file': 'resources/websites/micro_data_2.html', 'result_length': 1453}, JOURNAL_DES_FEMMES,
# {'file': 'resources/websites/micro_data_3.html', 'result_length': 1163}, MADAME_DESSERT,
# {'file': 'resources/websites/micro_data_4.html', 'result_length': 4411}, MARMITON,
# ] TASTE_OF_HOME,
# TUDOGOSTOSO
# for test in test_list: ])
# with open(test['file'], 'rb') as file: def test_recipe_import(arg, u1_s1):
# print(f'Testing {test["file"]} expecting length {test["result_length"]}') for f in arg['file']:
# parsed_content = json.loads(get_from_html(file.read(), 'test_url', None).content) with open(DATA_DIR + f, 'r', encoding='UTF-8') as d:
# assert len(str(parsed_content)) == test['result_length'] response = u1_s1.post(
# file.close() reverse(IMPORT_SOURCE_URL),
{
'data': d.read(),
'url': arg['url'],
'mode': 'source'
},
files={'foo': 'bar'}
)
recipe = json.loads(response.content)['recipe_json']
for key in list(set(arg) - set(['file', 'url'])):
if type(arg[key]) == list:
assert len(recipe[key]) == len(arg[key])
if key == 'keywords':
valid_keywords = [i['text'] for i in arg[key]]
for k in recipe[key]:
assert k['text'] in valid_keywords
elif key == 'recipeIngredient':
valid_ing = ["{:g}{}{}{}{}".format(
i['amount'],
i['unit']['text'],
i['ingredient']['text'],
i['note'],
i['original'])
for i in arg[key]]
for i in recipe[key]:
assert "{:g}{}{}{}{}".format(
i['amount'],
i['unit']['text'],
i['ingredient']['text'],
i['note'],
i['original']) in valid_ing
else:
assert recipe[key] == arg[key]