from django.test import TestCase, Client from files.models import File from inventory.models import Event, Container, Item client = Client() class ItemTestCase(TestCase): def setUp(self): super().setUp() self.event = Event.objects.create(slug='EVENT', name='Event') self.box = Container.objects.create(name='BOX') def test_empty(self): response = client.get(f'/api/1/{self.event.slug}/item') self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'[]') def test_members(self): item = Item.objects.create(container=self.box, event=self.event, description='1') response = client.get(f'/api/1/{self.event.slug}/item') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}]) def test_members_with_file(self): import base64 item = Item.objects.create(container=self.box, event=self.event, description='1') file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')) response = client.get(f'/api/1/{self.event.slug}/item') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash}]) def test_multi_members(self): Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') Item.objects.create(container=self.box, event=self.event, description='3') response = client.get(f'/api/1/{self.event.slug}/item') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 3) def test_create_item(self): response = client.post(f'/api/1/{self.event.slug}/item', {'cid': self.box.cid, 'description': '1'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '1') self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) def test_create_item_with_file(self): import base64 response = client.post(f'/api/1/{self.event.slug}/item', {'cid': self.box.cid, 'description': '1', 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode( 'utf-8')}, content_type='application/json') self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['uid'], 1) self.assertEqual(response.json()['description'], '1') self.assertEqual(response.json()['box'], 'BOX') self.assertEqual(response.json()['cid'], self.box.cid) self.assertEqual(len(response.json()['file']), 64) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '1') self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(len(File.objects.all()), 1) def test_update_item(self): item = Item.objects.create(container=self.box, event=self.event, description='1') response = client.put(f'/api/1/{self.event.slug}/item/{item.uid}', {'description': '2'}, content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '2') self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) def test_update_item_with_file(self): import base64 item = Item.objects.create(container=self.box, event=self.event, description='1') response = client.put(f'/api/1/{self.event.slug}/item/{item.uid}', {'description': '2', 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')}, content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['uid'], 1) self.assertEqual(response.json()['description'], '2') self.assertEqual(response.json()['box'], 'BOX') self.assertEqual(response.json()['cid'], self.box.cid) self.assertEqual(len(response.json()['file']), 64) self.assertEqual(len(Item.objects.all()), 1) self.assertEqual(Item.objects.all()[0].uid, 1) self.assertEqual(Item.objects.all()[0].description, '2') self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(len(File.objects.all()), 1) def test_delete_item(self): item = Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) response = client.delete(f'/api/1/{self.event.slug}/item/{item.uid}') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) def test_delete_item2(self): Item.objects.create(container=self.box, event=self.event, description='1') item2 = Item.objects.create(container=self.box, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) response = client.delete(f'/api/1/{self.event.slug}/item/{item2.uid}') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) item3 = Item.objects.create(container=self.box, event=self.event, description='3') self.assertEqual(item3.uid, 3) self.assertEqual(len(Item.objects.all()), 2) def test_item_count(self): Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') response = client.get('/api/1/boxes') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['itemCount'], 2) def test_item_nonexistent(self): response = client.get(f'/api/1/NOEVENT/item') self.assertEqual(response.status_code, 404)