finish refactoring test_api_food to use factoryboy

This commit is contained in:
smilerz 2021-11-03 21:39:17 -05:00
parent a035e02288
commit 6c0e979909

View File

@ -3,12 +3,12 @@ import json
import pytest import pytest
from django.contrib import auth from django.contrib import auth
from django.urls import reverse from django.urls import reverse
from django_scopes import scopes_disabled from django_scopes import scope, scopes_disabled
from pytest_factoryboy import LazyFixture, register from pytest_factoryboy import LazyFixture, register
from cookbook.models import Food, Ingredient, ShoppingList, ShoppingListEntry from cookbook.models import Food, Ingredient, ShoppingList, ShoppingListEntry
from cookbook.tests.conftest import get_random_json_recipe from cookbook.tests.conftest import get_random_json_recipe
from cookbook.tests.factories import FoodFactory from cookbook.tests.factories import FoodFactory, IngredientFactory, ShoppingListEntryFactory
# ------------------ IMPORTANT ------------------- # ------------------ IMPORTANT -------------------
# #
@ -34,81 +34,16 @@ register(FoodFactory, 'obj_2', space=LazyFixture('space_1'))
register(FoodFactory, 'obj_3', space=LazyFixture('space_2')) register(FoodFactory, 'obj_3', space=LazyFixture('space_2'))
# @pytest.fixture()
# def obj_1(food_factory, space_1):
# return food_factory(space=space_1)
@pytest.fixture() @pytest.fixture()
def obj_1_1(obj_1, space_1): def obj_tree_1(space_1):
return obj_1.add_child(name='test_1_1', space=space_1) objs = []
objs.extend(FoodFactory.create_batch(3, space=space_1))
objs[0].move(objs[1], node_location)
objs[1].move(objs[2], node_location)
return Food.objects.get(id=objs[1].id) # whenever you move/merge a tree it's safest to re-get the object
@pytest.fixture() @ pytest.mark.parametrize("arg", [
def obj_1_1_1(obj_1_1, space_1):
return obj_1_1.add_child(name='test_1_1_1', space=space_1)
# @pytest.fixture()
# def obj_2(food_factory, space_1):
# return food_factory(space=space_1)
# @pytest.fixture()
# def obj_3(food_factory, space_2):
# return food_factory(space=space_2)
@pytest.fixture()
def ing_1_s1(obj_1, space_1):
return Ingredient.objects.create(food=obj_1, space=space_1)
@pytest.fixture()
def ing_2_s1(obj_2, space_1):
return Ingredient.objects.create(food=obj_2, space=space_1)
@pytest.fixture()
def ing_3_s2(obj_3, space_2):
return Ingredient.objects.create(food=obj_3, space=space_2)
@pytest.fixture()
def ing_1_1_s1(obj_1_1, space_1):
return Ingredient.objects.create(food=obj_1_1, space=space_1)
@pytest.fixture()
def sle_1_s1(obj_1, u1_s1, space_1):
e = ShoppingListEntry.objects.create(food=obj_1, created_by=auth.get_user(u1_s1), space=space_1,)
s = ShoppingList.objects.create(created_by=auth.get_user(u1_s1), space=space_1, )
s.entries.add(e)
return e
@pytest.fixture()
def sle_2_s1(obj_2, u1_s1, space_1):
return ShoppingListEntry.objects.create(food=obj_2, created_by=auth.get_user(u1_s1), space=space_1,)
@pytest.fixture()
def sle_3_s2(obj_3, u1_s2, space_2):
e = ShoppingListEntry.objects.create(food=obj_3, created_by=auth.get_user(u1_s2), space=space_2)
s = ShoppingList.objects.create(created_by=auth.get_user(u1_s2), space=space_2, )
s.entries.add(e)
return e
@pytest.fixture()
def sle_1_1_s1(obj_1_1, u1_s1, space_1):
e = ShoppingListEntry.objects.create(food=obj_1_1, created_by=auth.get_user(u1_s1), space=space_1,)
s = ShoppingList.objects.create(created_by=auth.get_user(u1_s1), space=space_1, )
s.entries.add(e)
return e
@pytest.mark.parametrize("arg", [
['a_u', 403], ['a_u', 403],
['g1_s1', 403], ['g1_s1', 403],
['u1_s1', 200], ['u1_s1', 200],
@ -156,7 +91,7 @@ def test_list_filter(obj_1, obj_2, u1_s1):
assert response['count'] == 1 assert response['count'] == 1
@pytest.mark.parametrize("arg", [ @ pytest.mark.parametrize("arg", [
['a_u', 403], ['a_u', 403],
['g1_s1', 403], ['g1_s1', 403],
['u1_s1', 200], ['u1_s1', 200],
@ -181,7 +116,7 @@ def test_update(arg, request, obj_1):
assert response['name'] == 'new' assert response['name'] == 'new'
@pytest.mark.parametrize("arg", [ @ pytest.mark.parametrize("arg", [
['a_u', 403], ['a_u', 403],
['g1_s1', 403], ['g1_s1', 403],
['u1_s1', 201], ['u1_s1', 201],
@ -229,9 +164,9 @@ def test_add_duplicate(u1_s1, u1_s2, obj_1, obj_3):
assert json.loads(u1_s2.get(reverse(LIST_URL)).content)['count'] == 2 assert json.loads(u1_s2.get(reverse(LIST_URL)).content)['count'] == 2
def test_delete(u1_s1, u1_s2, obj_1, obj_1_1, obj_1_1_1): def test_delete(u1_s1, u1_s2, obj_1, obj_tree_1):
with scopes_disabled(): with scopes_disabled():
assert Food.objects.count() == 3 assert Food.objects.count() == 4
r = u1_s2.delete( r = u1_s2.delete(
reverse( reverse(
@ -241,18 +176,19 @@ def test_delete(u1_s1, u1_s2, obj_1, obj_1_1, obj_1_1_1):
) )
assert r.status_code == 404 assert r.status_code == 404
with scopes_disabled(): with scopes_disabled():
assert Food.objects.count() == 3 assert Food.objects.count() == 4
# should delete self and child, leaving parent
r = u1_s1.delete( r = u1_s1.delete(
reverse( reverse(
DETAIL_URL, DETAIL_URL,
args={obj_1_1.id} args={obj_tree_1.id}
) )
) )
assert r.status_code == 204 assert r.status_code == 204
with scopes_disabled(): with scopes_disabled():
assert Food.objects.count() == 1 assert Food.objects.count() == 2
assert Food.find_problems() == ([], [], [], [], []) assert Food.find_problems() == ([], [], [], [], [])
@ -292,13 +228,16 @@ def test_integrity(u1_s1, recipe_1_s1):
assert Ingredient.objects.count() == 9 assert Ingredient.objects.count() == 9
def test_move(u1_s1, obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, space_1): def test_move(u1_s1, obj_tree_1, obj_2, obj_3, space_1):
url = reverse(MOVE_URL, args=[obj_1_1.id, obj_2.id]) with scope(space=space_1):
with scopes_disabled(): parent = obj_tree_1.get_parent()
assert obj_1.get_num_children() == 1 child = obj_tree_1.get_descendants()[0]
assert obj_1.get_descendant_count() == 2 assert parent.get_num_children() == 1
assert parent.get_descendant_count() == 2
assert Food.get_root_nodes().filter(space=space_1).count() == 2 assert Food.get_root_nodes().filter(space=space_1).count() == 2
url = reverse(MOVE_URL, args=[obj_tree_1.id, obj_2.id])
# move child to new parent, only HTTP put method should work # move child to new parent, only HTTP put method should work
r = u1_s1.get(url) r = u1_s1.get(url)
assert r.status_code == 405 assert r.status_code == 405
@ -310,61 +249,107 @@ def test_move(u1_s1, obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, space_1):
assert r.status_code == 200 assert r.status_code == 200
with scopes_disabled(): with scopes_disabled():
# django-treebeard bypasses django ORM so object needs retrieved again # django-treebeard bypasses django ORM so object needs retrieved again
obj_1 = Food.objects.get(pk=obj_1.id) parent = Food.objects.get(pk=parent.id)
obj_2 = Food.objects.get(pk=obj_2.id) obj_2 = Food.objects.get(pk=obj_2.id)
assert obj_1.get_num_children() == 0 assert parent.get_num_children() == 0
assert obj_1.get_descendant_count() == 0 assert parent.get_descendant_count() == 0
assert obj_2.get_num_children() == 1 assert obj_2.get_num_children() == 1
assert obj_2.get_descendant_count() == 2 assert obj_2.get_descendant_count() == 2
# move child to root
r = u1_s1.put(reverse(MOVE_URL, args=[obj_1_1.id, 0]))
assert r.status_code == 200
with scopes_disabled():
assert Food.get_root_nodes().filter(space=space_1).count() == 3
# attempt to move to non-existent parent
r = u1_s1.put(
reverse(MOVE_URL, args=[obj_1.id, 9999])
)
assert r.status_code == 404
# attempt to move to wrong space
r = u1_s1.put(
reverse(MOVE_URL, args=[obj_1_1.id, obj_3.id])
)
assert r.status_code == 404
# run diagnostic to find problems - none should be found # run diagnostic to find problems - none should be found
with scopes_disabled(): with scopes_disabled():
assert Food.find_problems() == ([], [], [], [], []) assert Food.find_problems() == ([], [], [], [], [])
def test_merge( def test_move_errors(u1_s1, obj_tree_1, obj_3, space_1):
u1_s1, with scope(space=space_1):
obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, parent = obj_tree_1.get_parent()
ing_1_s1, ing_2_s1, ing_3_s2, ing_1_1_s1, child = obj_tree_1.get_descendants()[0]
sle_1_s1, sle_2_s1, sle_3_s2, sle_1_1_s1, # move child to root
space_1 r = u1_s1.put(reverse(MOVE_URL, args=[obj_tree_1.id, 0]))
): assert r.status_code == 200
with scopes_disabled(): with scopes_disabled():
assert obj_1.get_num_children() == 1
assert obj_1.get_descendant_count() == 2
assert Food.get_root_nodes().filter(space=space_1).count() == 2 assert Food.get_root_nodes().filter(space=space_1).count() == 2
assert Food.objects.filter(space=space_1).count() == 4
assert obj_1.ingredient_set.count() == 1
assert obj_2.ingredient_set.count() == 1
assert obj_3.ingredient_set.count() == 1
assert obj_1_1.ingredient_set.count() == 1
assert obj_1_1_1.ingredient_set.count() == 0
assert obj_1.shoppinglistentry_set.count() == 1
assert obj_2.shoppinglistentry_set.count() == 1
assert obj_3.shoppinglistentry_set.count() == 1
assert obj_1_1.shoppinglistentry_set.count() == 1
assert obj_1_1_1.shoppinglistentry_set.count() == 0
# merge food with no children and no ingredient/shopping list entry with another food, only HTTP put method should work # attempt to move to non-existent parent
url = reverse(MERGE_URL, args=[obj_1_1_1.id, obj_2.id]) r = u1_s1.put(
reverse(MOVE_URL, args=[parent.id, 9999])
)
assert r.status_code == 404
# attempt to move non-existent mode to parent
r = u1_s1.put(
reverse(MOVE_URL, args=[9999, parent.id])
)
assert r.status_code == 404
# attempt to move to wrong space
r = u1_s1.put(
reverse(MOVE_URL, args=[obj_tree_1.id, obj_3.id])
)
assert r.status_code == 404
# TODO: figure out how to generalize this to be all related objects
def test_merge_ingredients(obj_tree_1, u1_s1, space_1):
with scope(space=space_1):
parent = obj_tree_1.get_parent()
child = obj_tree_1.get_descendants()[0]
IngredientFactory.create(food=parent, space=space_1)
IngredientFactory.create(food=child, space=space_1)
assert parent.get_num_children() == 1
assert parent.get_descendant_count() == 2
assert Ingredient.objects.count() == 2
assert parent.ingredient_set.count() == 1
assert obj_tree_1.ingredient_set.count() == 0
assert child.ingredient_set.count() == 1
# merge food (with connected ingredient) with children to another food
r = u1_s1.put(reverse(MERGE_URL, args=[child.id, obj_tree_1.id]))
assert r.status_code == 200
with scope(space=space_1):
# django-treebeard bypasses django ORM so object needs retrieved again
with pytest.raises(Food.DoesNotExist):
Food.objects.get(pk=child.id)
obj_tree_1 = Food.objects.get(pk=obj_tree_1.id)
assert obj_tree_1.ingredient_set.count() == 1 # now has child's ingredient
def test_merge_shopping_entries(obj_tree_1, u1_s1, space_1):
with scope(space=space_1):
parent = obj_tree_1.get_parent()
child = obj_tree_1.get_descendants()[0]
ShoppingListEntryFactory.create(food=parent, space=space_1)
ShoppingListEntryFactory.create(food=child, space=space_1)
assert parent.get_num_children() == 1
assert parent.get_descendant_count() == 2
assert ShoppingListEntry.objects.count() == 2
assert parent.shopping_entries.count() == 1
assert obj_tree_1.shopping_entries.count() == 0
assert child.shopping_entries.count() == 1
# merge food (with connected shoppinglistentry) with children to another food
r = u1_s1.put(reverse(MERGE_URL, args=[child.id, obj_tree_1.id]))
assert r.status_code == 200
with scope(space=space_1):
# django-treebeard bypasses django ORM so object needs retrieved again
with pytest.raises(Food.DoesNotExist):
Food.objects.get(pk=child.id)
obj_tree_1 = Food.objects.get(pk=obj_tree_1.id)
assert obj_tree_1.shopping_entries.count() == 1 # now has child's ingredient
def test_merge(u1_s1, obj_tree_1, obj_1, obj_3, space_1):
with scope(space=space_1):
parent = obj_tree_1.get_parent()
child = obj_tree_1.get_descendants()[0]
assert parent.get_num_children() == 1
assert parent.get_descendant_count() == 2
assert Food.get_root_nodes().filter(space=space_1).count() == 2
assert Food.objects.count() == 4
# merge food with no children with another food, only HTTP put method should work
url = reverse(MERGE_URL, args=[child.id, obj_tree_1.id])
r = u1_s1.get(url) r = u1_s1.get(url)
assert r.status_code == 405 assert r.status_code == 405
r = u1_s1.post(url) r = u1_s1.post(url)
@ -373,95 +358,100 @@ def test_merge(
assert r.status_code == 405 assert r.status_code == 405
r = u1_s1.put(url) r = u1_s1.put(url)
assert r.status_code == 200 assert r.status_code == 200
with scopes_disabled(): with scope(space=space_1):
# django-treebeard bypasses django ORM so object needs retrieved again # django-treebeard bypasses django ORM so object needs retrieved again
with pytest.raises(Food.DoesNotExist):
Food.objects.get(pk=child.id)
obj_tree_1 = Food.objects.get(pk=obj_tree_1.id)
assert parent.get_num_children() == 1
assert parent.get_descendant_count() == 1
# merge food with children with another food
r = u1_s1.put(reverse(MERGE_URL, args=[parent.id, obj_1.id]))
assert r.status_code == 200
with scope(space=space_1):
# django-treebeard bypasses django ORM so object needs retrieved again
with pytest.raises(Food.DoesNotExist):
Food.objects.get(pk=parent.id)
obj_1 = Food.objects.get(pk=obj_1.id) obj_1 = Food.objects.get(pk=obj_1.id)
obj_2 = Food.objects.get(pk=obj_2.id)
assert Food.objects.filter(pk=obj_1_1_1.id).count() == 0
assert obj_1.get_num_children() == 1 assert obj_1.get_num_children() == 1
assert obj_1.get_descendant_count() == 1 assert obj_1.get_descendant_count() == 1
assert obj_2.get_num_children() == 0
assert obj_2.get_descendant_count() == 0
assert obj_1.ingredient_set.count() == 1
assert obj_2.ingredient_set.count() == 1
assert obj_3.ingredient_set.count() == 1
assert obj_1_1.ingredient_set.count() == 1
assert obj_1.shoppinglistentry_set.count() == 1
assert obj_2.shoppinglistentry_set.count() == 1
assert obj_3.shoppinglistentry_set.count() == 1
assert obj_1_1.shoppinglistentry_set.count() == 1
# merge food (with connected ingredient/shoppinglistentry) with children to another food
r = u1_s1.put(reverse(MERGE_URL, args=[obj_1.id, obj_2.id]))
assert r.status_code == 200
with scopes_disabled():
# django-treebeard bypasses django ORM so object needs retrieved again
obj_2 = Food.objects.get(pk=obj_2.id)
assert Food.objects.filter(pk=obj_1.id).count() == 0
assert obj_2.get_num_children() == 1
assert obj_2.get_descendant_count() == 1
assert obj_2.ingredient_set.count() == 2
assert obj_3.ingredient_set.count() == 1
assert obj_1_1.ingredient_set.count() == 1
assert obj_2.shoppinglistentry_set.count() == 2
assert obj_3.shoppinglistentry_set.count() == 1
assert obj_1_1.shoppinglistentry_set.count() == 1
# attempt to merge with non-existent parent
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_1_1.id, 9999])
)
assert r.status_code == 404
# attempt to move to wrong space
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_2.id, obj_3.id])
)
assert r.status_code == 404
# attempt to merge with child
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_2.id, obj_1_1.id])
)
assert r.status_code == 403
# attempt to merge with self
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_2.id, obj_2.id])
)
assert r.status_code == 403
# run diagnostic to find problems - none should be found # run diagnostic to find problems - none should be found
with scopes_disabled(): with scopes_disabled():
assert Food.find_problems() == ([], [], [], [], []) assert Food.find_problems() == ([], [], [], [], [])
def test_root_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1): def test_merge_errors(u1_s1, obj_tree_1, obj_3, space_1):
with scope(space=space_1):
parent = obj_tree_1.get_parent()
child = obj_tree_1.get_descendants()[0]
# attempt to merge with non-existent parent
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_tree_1.id, 9999])
)
assert r.status_code == 404
# attempt to merge non-existent node to parent
r = u1_s1.put(
reverse(MERGE_URL, args=[9999, obj_tree_1.id])
)
assert r.status_code == 404
# attempt to move to wrong space
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_tree_1.id, obj_3.id])
)
assert r.status_code == 404
# attempt to merge with child
r = u1_s1.put(
reverse(MERGE_URL, args=[parent.id, obj_tree_1.id])
)
assert r.status_code == 403
# attempt to merge with self
r = u1_s1.put(
reverse(MERGE_URL, args=[obj_tree_1.id, obj_tree_1.id])
)
assert r.status_code == 403
def test_root_filter(obj_tree_1, obj_2, obj_3, u1_s1):
with scope(space=obj_tree_1.space):
parent = obj_tree_1.get_parent()
child = obj_tree_1.get_descendants()[0]
# should return root objects in the space (obj_1, obj_2), ignoring query filters # should return root objects in the space (obj_1, obj_2), ignoring query filters
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root=0').content) response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root=0').content)
assert len(response['results']) == 2 assert len(response['results']) == 2
with scopes_disabled(): with scopes_disabled():
obj_2.move(obj_1, node_location) obj_2.move(parent, node_location)
# should return direct children of obj_1 (obj_1_1, obj_2), ignoring query filters # should return direct children of parent (obj_tree_1, obj_2), ignoring query filters
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}').content) response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={parent.id}').content)
assert response['count'] == 2 assert response['count'] == 2
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}&query={obj_2.name[4:]}').content) response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={parent.id}&query={obj_2.name[4:]}').content)
assert response['count'] == 2 assert response['count'] == 2
def test_tree_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1): def test_tree_filter(obj_tree_1, obj_2, obj_3, u1_s1):
with scopes_disabled(): with scope(space=obj_tree_1.space):
obj_2.move(obj_1, node_location) parent = obj_tree_1.get_parent()
# should return full tree starting at obj_1 (obj_1_1_1, obj_2), ignoring query filters child = obj_tree_1.get_descendants()[0]
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}').content) obj_2.move(parent, node_location)
# should return full tree starting at parent (obj_tree_1, obj_2), ignoring query filters
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={parent.id}').content)
assert response['count'] == 4 assert response['count'] == 4
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}&query={obj_2.name[4:]}').content) response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={parent.id}&query={obj_2.name[4:]}').content)
assert response['count'] == 4 assert response['count'] == 4
def test_inherit(obj_tree_1, u1_s1):
pass
# TODO test inherit creating, moving for each field type # TODO test inherit creating, moving for each field type
# TODO test ignore inherit for each field type # TODO test ignore inherit for each field type
# TODO test with grand-children # TODO test with grand-children
# - flow from parent through child and grand-child # - flow from parent through child and grand-child
# - flow from parent stop when child is ignore inherit # - flow from parent stop when child is ignore inherit
# TODO test reset_inheritance