From 24d8edec193667f9037818b700e883a8d3be0413 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 1 Jul 2023 21:10:44 +0200 Subject: [PATCH] continue refactoring tests --- backend/toolshed/tests/fixtures.py | 48 ++++++++++++++++-------- backend/toolshed/tests/test_category.py | 46 +++++++++-------------- backend/toolshed/tests/test_inventory.py | 5 ++- backend/toolshed/tests/test_property.py | 38 +++++++++---------- backend/toolshed/tests/test_tag.py | 27 ++++--------- 5 files changed, 80 insertions(+), 84 deletions(-) diff --git a/backend/toolshed/tests/fixtures.py b/backend/toolshed/tests/fixtures.py index a59a36c..443bf6d 100644 --- a/backend/toolshed/tests/fixtures.py +++ b/backend/toolshed/tests/fixtures.py @@ -1,24 +1,42 @@ from toolshed.models import Category, Tag, Property, InventoryItem, ItemProperty -class InventoryTestMixin: - def prepare_inventory(self): - self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) +class CategoryTestMixin: + def prepare_categories(self): self.f['cat1'] = Category.objects.create(name='cat1') self.f['cat2'] = Category.objects.create(name='cat2') - self.f['tag1'] = Tag.objects.create(name='tag1', category=self.f['cat1']) - self.f['tag2'] = Tag.objects.create(name='tag2', category=self.f['cat1']) - self.f['tag3'] = Tag.objects.create(name='tag3') - self.f['prop1'] = Property.objects.create(name='prop1') - self.f['prop2'] = Property.objects.create(name='prop2') - self.f['prop3'] = Property.objects.create(name='prop3', category=self.f['cat1']) + self.f['cat3'] = Category.objects.create(name='cat3') + self.f['subcat1'] = Category.objects.create(name='subcat1', parent=self.f['cat1']) + self.f['subcat2'] = Category.objects.create(name='subcat2', parent=self.f['cat1']) + self.f['subcat3'] = Category.objects.create(name='subcat3', parent=self.f['subcat1']) - self.f['item1'] = InventoryItem.objects.create(owner=self.f['local_user1'], owned_quantity=1, name='test1', - description='test', - category=self.f['cat1'], availability_policy='friends') - self.f['item2'] = InventoryItem.objects.create(owner=self.f['local_user1'], owned_quantity=1, name='test2', - description='test2', - category=self.f['cat1'], availability_policy='friends') + +class TagTestMixin: + def prepare_tags(self): + self.f['tag1'] = Tag.objects.create(name='tag1', description='tag1 description', category=self.f['cat1']) + self.f['tag2'] = Tag.objects.create(name='tag2', description='tag2 description', category=self.f['cat1']) + self.f['tag3'] = Tag.objects.create(name='tag3') + + +class PropertyTestMixin: + def prepare_properties(self): + self.f['prop1'] = Property.objects.create(name='prop1') + self.f['prop2'] = Property.objects.create( + name='prop2', description='prop2 description', category=self.f['cat1']) + self.f['prop3'] = Property.objects.create( + name='prop3', description='prop3 description', category=self.f['cat1']) + + +class InventoryTestMixin(CategoryTestMixin, TagTestMixin, PropertyTestMixin): + def prepare_inventory(self): + self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) + + self.f['item1'] = InventoryItem.objects.create( + owner=self.f['local_user1'], owned_quantity=1, name='test1', description='test', category=self.f['cat1'], + availability_policy='friends') + self.f['item2'] = InventoryItem.objects.create( + owner=self.f['local_user1'], owned_quantity=1, name='test2', description='test2', category=self.f['cat1'], + availability_policy='friends') self.f['item2'].tags.add(self.f['tag1'], through_defaults={}) self.f['item2'].tags.add(self.f['tag2'], through_defaults={}) ItemProperty.objects.create(inventory_item=self.f['item2'], property=self.f['prop1'], value='value1').save() diff --git a/backend/toolshed/tests/test_category.py b/backend/toolshed/tests/test_category.py index 1953aff..254c525 100644 --- a/backend/toolshed/tests/test_category.py +++ b/backend/toolshed/tests/test_category.py @@ -1,55 +1,43 @@ from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase from toolshed.models import Category +from toolshed.tests import CategoryTestMixin client = SignatureAuthClient() -class CategoryTestCase(UserTestMixin, ToolshedTestCase): +class CategoryTestCase(CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['cat2'] = Category.objects.create(name='cat2') - self.f['cat2'].save() - self.f['cat3'] = Category.objects.create(name='cat3') - self.f['cat3'].save() + self.prepare_categories() def test_get_categories(self): - subcat1 = Category.objects.create(name='subcat1', parent=self.f['cat1']) - subcat1.save() - subcat2 = Category.objects.create(name='subcat2', parent=self.f['cat1']) - subcat2.save() - subcat3 = Category.objects.create(name='subcat3', parent=subcat1) - subcat3.save() self.assertEqual(self.f['cat1'].children.count(), 2) - self.assertEqual(self.f['cat1'].children.first(), subcat1) - self.assertEqual(self.f['cat1'].children.last(), subcat2) - self.assertEqual(subcat1.parent, self.f['cat1']) - self.assertEqual(subcat2.parent, self.f['cat1']) - self.assertEqual(subcat3.parent, subcat1) - self.assertEqual(str(subcat1), 'cat1/subcat1') - self.assertEqual(str(subcat2), 'cat1/subcat2') - self.assertEqual(str(subcat3), 'cat1/subcat1/subcat3') + self.assertEqual(self.f['cat1'].children.first(), self.f['subcat1']) + self.assertEqual(self.f['cat1'].children.last(), self.f['subcat2']) + self.assertEqual(self.f['subcat1'].parent, self.f['cat1']) + self.assertEqual(self.f['subcat2'].parent, self.f['cat1']) + self.assertEqual(self.f['subcat1'].children.count(), 1) + self.assertEqual(str(self.f['subcat1']), 'cat1/subcat1') + self.assertEqual(str(self.f['subcat2']), 'cat1/subcat2') + self.assertEqual(str(self.f['subcat3']), 'cat1/subcat1/subcat3') -class CategoryApiTestCase(UserTestMixin, ToolshedTestCase): +class CategoryApiTestCase(CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['cat2'] = Category.objects.create(name='cat2') - self.f['cat2'].save() - self.f['cat3'] = Category.objects.create(name='cat3') - self.f['cat3'].save() + self.prepare_categories() def test_get_categories(self): reply = client.get('/api/categories/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) - self.assertEqual(len(reply.json()), 3) + self.assertEqual(len(reply.json()), 6) self.assertEqual(reply.json()[0], 'cat1') self.assertEqual(reply.json()[1], 'cat2') self.assertEqual(reply.json()[2], 'cat3') + self.assertEqual(reply.json()[3], 'cat1/subcat1') + self.assertEqual(reply.json()[4], 'cat1/subcat2') + self.assertEqual(reply.json()[5], 'cat1/subcat1/subcat3') diff --git a/backend/toolshed/tests/test_inventory.py b/backend/toolshed/tests/test_inventory.py index e3c775a..5cf8014 100644 --- a/backend/toolshed/tests/test_inventory.py +++ b/backend/toolshed/tests/test_inventory.py @@ -1,6 +1,6 @@ from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase from toolshed.models import InventoryItem, Category -from toolshed.tests import InventoryTestMixin +from toolshed.tests import InventoryTestMixin, CategoryTestMixin, TagTestMixin, PropertyTestMixin client = SignatureAuthClient() @@ -10,6 +10,9 @@ class InventoryTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() + self.prepare_categories() + self.prepare_tags() + self.prepare_properties() self.prepare_inventory() def test_get_inventory(self): diff --git a/backend/toolshed/tests/test_property.py b/backend/toolshed/tests/test_property.py index 4e0d748..1090fc3 100644 --- a/backend/toolshed/tests/test_property.py +++ b/backend/toolshed/tests/test_property.py @@ -1,22 +1,17 @@ from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase -from toolshed.models import Property, Category +from toolshed.models import Property +from toolshed.tests import PropertyTestMixin, CategoryTestMixin client = SignatureAuthClient() -class PropertyTestCase(UserTestMixin, ToolshedTestCase): +class PropertyTestCase(PropertyTestMixin, CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['prop1'] = Property.objects.create(name='prop1') - self.f['prop1'].save() - self.f['prop2'] = Property.objects.create(name='prop2', description='prop2 description', category=self.f['cat1']) - self.f['prop2'].save() - self.f['prop3'] = Property.objects.create(name='prop3', description='prop3 description', category=self.f['cat1']) - self.f['prop3'].save() + self.prepare_categories() + self.prepare_properties() def test_properties(self): self.assertEqual(len(Property.objects.all()), 3) @@ -37,21 +32,24 @@ class PropertyTestCase(UserTestMixin, ToolshedTestCase): self.assertEqual(self.f['cat1'].properties.last(), self.f['prop3']) -class PropertyApiTestCase(UserTestMixin, ToolshedTestCase): +class PropertyApiTestCase(PropertyTestMixin, CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['cat2'] = Category.objects.create(name='cat2') - self.f['cat2'].save() - self.f['cat3'] = Category.objects.create(name='cat3') - self.f['cat3'].save() + self.prepare_categories() + self.prepare_properties() def test_get_properties(self): reply = client.get('/api/properties/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) - self.assertEqual(len(reply.json()), 0) - prop1 = Property.objects.create(name='prop1') - prop1.save() + self.assertEqual(len(reply.json()), 3) + self.assertEqual(reply.json()[0]['name'], 'prop1') + self.assertEqual(reply.json()[0]['description'], None) + self.assertEqual(reply.json()[0]['category'], None) + self.assertEqual(reply.json()[1]['name'], 'prop2') + self.assertEqual(reply.json()[1]['description'], 'prop2 description') + self.assertEqual(reply.json()[1]['category'], 'cat1') + self.assertEqual(reply.json()[2]['name'], 'prop3') + self.assertEqual(reply.json()[2]['description'], 'prop3 description') + self.assertEqual(reply.json()[2]['category'], 'cat1') diff --git a/backend/toolshed/tests/test_tag.py b/backend/toolshed/tests/test_tag.py index e0ccfeb..7a32d36 100644 --- a/backend/toolshed/tests/test_tag.py +++ b/backend/toolshed/tests/test_tag.py @@ -1,22 +1,17 @@ from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase -from toolshed.models import Tag, Category +from toolshed.models import Tag +from toolshed.tests import TagTestMixin, CategoryTestMixin client = SignatureAuthClient() -class TagTestCase(UserTestMixin, ToolshedTestCase): +class TagTestCase(TagTestMixin, CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['tag1'] = Tag.objects.create(name='tag1', description='tag1 description', category=self.f['cat1']) - self.f['tag1'].save() - self.f['tag2'] = Tag.objects.create(name='tag2', description='tag2 description', category=self.f['cat1']) - self.f['tag2'].save() - self.f['tag3'] = Tag.objects.create(name='tag3') - self.f['tag3'].save() + self.prepare_categories() + self.prepare_tags() def test_tags(self): self.assertEqual(len(Tag.objects.all()), 3) @@ -37,19 +32,13 @@ class TagTestCase(UserTestMixin, ToolshedTestCase): self.assertEqual(self.f['cat1'].tags.last(), self.f['tag2']) -class TagApiTestCase(UserTestMixin, ToolshedTestCase): +class TagApiTestCase(TagTestMixin, CategoryTestMixin, UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() self.prepare_users() - self.f['cat1'] = Category.objects.create(name='cat1') - self.f['cat1'].save() - self.f['tag1'] = Tag.objects.create(name='tag1', description='tag1 description', category=self.f['cat1']) - self.f['tag1'].save() - self.f['tag2'] = Tag.objects.create(name='tag2', description='tag2 description', category=self.f['cat1']) - self.f['tag2'].save() - self.f['tag3'] = Tag.objects.create(name='tag3') - self.f['tag3'].save() + self.prepare_categories() + self.prepare_tags() def test_get_tags(self): reply = client.get('/api/tags/', self.f['local_user1'])