update tests to reflect API changes

This commit is contained in:
smilerz
2024-04-24 12:26:05 -05:00
parent ebcc814abf
commit a047d36bf1
5 changed files with 51 additions and 91 deletions

View File

@@ -1,3 +1,5 @@
import time
import pytest
from django.contrib import auth
from django_scopes import scope
@@ -22,8 +24,7 @@ def recipes(space_1):
@pytest.fixture
def makenow_recipe(request, space_1):
onhand_user = auth.get_user(request.getfixturevalue(
request.param.get('onhand_users', 'u1_s1')))
onhand_user = auth.get_user(request.getfixturevalue(request.param.get('onhand_users', 'u1_s1')))
recipe = RecipeFactory.create(space=space_1)
for food in Food.objects.filter(ingredient__step__recipe=recipe.id):
@@ -40,11 +41,9 @@ def user1(u1_s1, u2_s1, space_1):
return user1
@pytest.mark.parametrize("makenow_recipe", [
({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'}),
], indirect=['makenow_recipe'])
@pytest.mark.parametrize("makenow_recipe", [({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'})], indirect=['makenow_recipe'])
def test_makenow_onhand(recipes, makenow_recipe, user1, space_1):
request = type('', (object,), {'space': space_1, 'user': user1})()
request = type('', (object, ), {'space': space_1, 'user': user1})()
search = RecipeSearch(request, makenow='true')
with scope(space=space_1):
search = search.get_queryset(Recipe.objects.all())
@@ -52,83 +51,64 @@ def test_makenow_onhand(recipes, makenow_recipe, user1, space_1):
assert search.first().id == makenow_recipe.id
@pytest.mark.parametrize("makenow_recipe", [
({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'}),
], indirect=['makenow_recipe'])
@pytest.mark.parametrize("makenow_recipe", [({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'})], indirect=['makenow_recipe'])
def test_makenow_ignoreshopping(recipes, makenow_recipe, user1, space_1):
request = type('', (object,), {'space': space_1, 'user': user1})()
request = type('', (object, ), {'space': space_1, 'user': user1})()
search = RecipeSearch(request, makenow='true')
with scope(space=space_1):
food = Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id).first()
food = Food.objects.filter(ingredient__step__recipe=makenow_recipe.id).first()
food.onhand_users.clear()
assert search.get_queryset(Recipe.objects.all()).count() == 0
food.ignore_shopping = True
food.save()
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, ignore_shopping=True).count() == 1
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, ignore_shopping=True).count() == 1
search = search.get_queryset(Recipe.objects.all())
assert search.count() == 1
assert search.first().id == makenow_recipe.id
@pytest.mark.parametrize("makenow_recipe", [
({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'}),
], indirect=['makenow_recipe'])
@pytest.mark.parametrize("makenow_recipe", [({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'})], indirect=['makenow_recipe'])
def test_makenow_substitute(recipes, makenow_recipe, user1, space_1):
request = type('', (object,), {'space': space_1, 'user': user1})()
request = type('', (object, ), {'space': space_1, 'user': user1})()
search = RecipeSearch(request, makenow='true')
with scope(space=space_1):
food = Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id).first()
food = Food.objects.filter(ingredient__step__recipe=makenow_recipe.id).first()
onhand_user = food.onhand_users.first()
food.onhand_users.clear()
assert search.get_queryset(Recipe.objects.all()).count() == 0
food.substitute.add(FoodFactory.create(
space=space_1, onhand_users=[onhand_user]))
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, substitute__isnull=False).count() == 1
food.substitute.add(FoodFactory.create(space=space_1, onhand_users=[onhand_user]))
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, substitute__isnull=False).count() == 1
search = search.get_queryset(Recipe.objects.all())
assert search.count() == 1
assert search.first().id == makenow_recipe.id
@pytest.mark.parametrize("makenow_recipe", [
({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'}),
], indirect=['makenow_recipe'])
@pytest.mark.parametrize("makenow_recipe", [({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'})], indirect=['makenow_recipe'])
def test_makenow_child_substitute(recipes, makenow_recipe, user1, space_1):
request = type('', (object,), {'space': space_1, 'user': user1})()
request = type('', (object, ), {'space': space_1, 'user': user1})()
search = RecipeSearch(request, makenow='true')
with scope(space=space_1):
food = Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id).first()
food = Food.objects.filter(ingredient__step__recipe=makenow_recipe.id).first()
onhand_user = food.onhand_users.first()
food.onhand_users.clear()
food.substitute_children = True
food.save()
assert search.get_queryset(Recipe.objects.all()).count() == 0
new_food = FoodFactory.create(
space=space_1, onhand_users=[onhand_user])
new_food = FoodFactory.create(space=space_1, onhand_users=[onhand_user])
new_food.move(food, node_location)
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(
ingredient__step__recipe=makenow_recipe.id, numchild__gt=0).count() == 1
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, numchild__gt=0).count() == 1
search = search.get_queryset(Recipe.objects.all())
assert search.count() == 1
assert search.first().id == makenow_recipe.id
@pytest.mark.parametrize("makenow_recipe", [
({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'}),
], indirect=['makenow_recipe'])
@pytest.mark.parametrize("makenow_recipe", [({'onhand_users': 'u1_s1'}), ({'onhand_users': 'u2_s1'})], indirect=['makenow_recipe'])
def test_makenow_sibling_substitute(recipes, makenow_recipe, user1, space_1):
request = type('', (object,), {'space': space_1, 'user': user1})()
request = type('', (object, ), {'space': space_1, 'user': user1})()
search = RecipeSearch(request, makenow='true')
with scope(space=space_1):
food = Food.objects.filter(ingredient__step__recipe=makenow_recipe.id).first()
@@ -137,14 +117,17 @@ def test_makenow_sibling_substitute(recipes, makenow_recipe, user1, space_1):
food.substitute_siblings = True
food.save()
assert search.get_queryset(Recipe.objects.all()).count() == 0
new_parent = FoodFactory.create(space=space_1)
new_sibling = FoodFactory.create(space=space_1, onhand_users=[onhand_user])
new_sibling.move(new_parent, node_location)
food.move(new_parent, node_location)
# force refresh from database, treebeard bypasses ORM
# force refresh from database, treebeard bypasses ORM after short pause
time.sleep(1)
food = Food.objects.get(id=food.id)
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9
assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, depth=2).count() == 1
search = search.get_queryset(Recipe.objects.all())
assert search.count() == 1
assert search.first().id == makenow_recipe.id

View File

@@ -3,11 +3,11 @@ from django.urls import reverse
@pytest.mark.parametrize("arg", [
['a_u', 302],
['g1_s1', 302],
['a_u', 403],
['g1_s1', 403],
['u1_s1', 200],
['a1_s1', 200],
['g1_s2', 302],
['g1_s2', 403],
['u1_s2', 404],
['a1_s2', 404],
])
@@ -17,7 +17,7 @@ def test_external_link_permission(arg, request, ext_recipe_1_s1):
@pytest.mark.parametrize("arg", [
['a_u', 302],
['a_u', 403],
['g1_s1', 200],
['u1_s1', 200],
['a1_s1', 200],

View File

@@ -2,19 +2,26 @@ import pytest
from django.urls import reverse
def test_index():
# TODO add appropriate test
pass
@pytest.mark.parametrize("arg", [
['a_u', 302],
['g1_s1', 302],
['u1_s1', 302],
['a1_s1', 302],
])
def test_index(arg, request, ext_recipe_1_s1):
c = request.getfixturevalue(arg[0])
assert c.get(reverse('index')).status_code == arg[1]
def test_search():
# TODO add appropriate test
pass
def test_view():
# TODO add appropriate test
pass
@pytest.mark.parametrize("arg", [
['a_u', 302],
['g1_s1', 200],
['u1_s1', 200],
['a1_s1', 200],
])
def test_search(arg, request, ext_recipe_1_s1):
c = request.getfixturevalue(arg[0])
assert c.get(reverse('view_search')).status_code == arg[1]
@pytest.mark.parametrize("arg", [
@@ -39,11 +46,6 @@ def test_plan(arg, request, ext_recipe_1_s1):
assert c.get(reverse('view_plan')).status_code == arg[1]
def test_plan_entry():
# TODO add appropriate test
pass
@pytest.mark.parametrize("arg", [
['a_u', 302],
['g1_s1', 302],
@@ -120,6 +122,7 @@ def test_api_info(arg, request, ext_recipe_1_s1):
c = request.getfixturevalue(arg[0])
assert c.get(reverse('docs_api')).status_code == arg[1]
@pytest.mark.parametrize("arg", [
['a_u', 302],
['g1_s1', 200],

View File

@@ -120,8 +120,6 @@ urlpatterns = [
path('api/get_recipe_file/<int:recipe_id>/', api.get_recipe_file, name='api_get_recipe_file'),
path('api/sync_all/', api.sync_all, name='api_sync'),
# path('api/plan-ical/<slug:from_date>/<slug:to_date>/', api.get_plan_ical, name='api_get_plan_ical'),
path('api/recipe-from-source/', api.RecipeUrlImportView.as_view(), name='api_recipe_from_source'),
path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'),
path('api/share-link/<int:pk>', api.share_link, name='api_share_link'),

View File

@@ -1764,30 +1764,6 @@ def share_link(request, pk):
return JsonResponse({'error': 'sharing_disabled'}, status=403)
# NOTE: I think this was replaced by icalMealPlanApi?
# @extend_schema(
# request=inline_serializer(name="PlanIcalSerializer", fields={'from_date': CharField(), 'to_date': CharField()}),
# responses=None,
# parameters=[
# OpenApiParameter(name='from_date', location=OpenApiParameter.PATH, description=_('Get meal plans from date (inclusive).'), type=str, examples=[DateExample]),
# OpenApiParameter(name='to_date', location=OpenApiParameter.PATH, description=_('Get meal plans to date (inclusive).'), type=str, examples=[DateExample]),
# ]
# )
# @api_view(['GET'])
# @permission_classes([CustomIsUser & CustomTokenHasReadWriteScope])
# def get_plan_ical(request, from_date=datetime.date.today(), to_date=None):
# queryset = MealPlan.objects.filter(Q(created_by=request.user)
# | Q(shared=request.user)).filter(space=request.user.userspace_set.filter(active=1).first().space).distinct().all()
# if from_date is not None:
# queryset = queryset.filter(from_date__gte=from_date)
# if to_date is not None:
# queryset = queryset.filter(to_date__lte=to_date)
# return meal_plans_to_ical(queryset, f'meal_plan_{from_date}-{to_date}.ics')
def meal_plans_to_ical(queryset, filename):
cal = Calendar()