create inventory API v2 und update tests
This commit is contained in:
parent
77828295f8
commit
fc05c7c1d8
12 changed files with 486 additions and 0 deletions
0
core/inventory/tests/v1/__init__.py
Normal file
0
core/inventory/tests/v1/__init__.py
Normal file
34
core/inventory/tests/v1/test_api.py
Normal file
34
core/inventory/tests/v1/test_api.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
from django.test import TestCase, Client
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class ApiTest(TestCase):
|
||||
|
||||
def test_root(self):
|
||||
from core.settings import SYSTEM3_VERSION
|
||||
response = client.get('/api/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()["framework_version"], SYSTEM3_VERSION)
|
||||
|
||||
def test_events(self):
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_containers(self):
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_files(self):
|
||||
response = client.get('/api/1/files')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_items(self):
|
||||
from inventory.models import Event
|
||||
Event.objects.create(slug='TEST1', name='Event')
|
||||
response = client.get('/api/1/TEST1/items')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
59
core/inventory/tests/v1/test_containers.py
Normal file
59
core/inventory/tests/v1/test_containers.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
from django.test import TestCase, Client
|
||||
from inventory.models import Container
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class ContainerTestCase(TestCase):
|
||||
|
||||
def test_empty(self):
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_members(self):
|
||||
Container.objects.create(name='BOX')
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['cid'], 1)
|
||||
self.assertEqual(response.json()[0]['name'], 'BOX')
|
||||
self.assertEqual(response.json()[0]['itemCount'], 0)
|
||||
|
||||
def test_multi_members(self):
|
||||
Container.objects.create(name='BOX 1')
|
||||
Container.objects.create(name='BOX 2')
|
||||
Container.objects.create(name='BOX 3')
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_container(self):
|
||||
response = client.post('/api/1/box', {'name': 'BOX'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX')
|
||||
|
||||
def test_update_container(self):
|
||||
from rest_framework.test import APIClient
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
response = APIClient().put(f'/api/1/box/{box.cid}', {'name': 'BOX 2'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX 2')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX 2')
|
||||
|
||||
def test_delete_container(self):
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
Container.objects.create(name='BOX 2')
|
||||
self.assertEqual(len(Container.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/box/{box.cid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
56
core/inventory/tests/v1/test_events.py
Normal file
56
core/inventory/tests/v1/test_events.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
from django.test import TestCase, Client
|
||||
from inventory.models import Event
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class EventTestCase(TestCase):
|
||||
|
||||
def test_empty(self):
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_members(self):
|
||||
Event.objects.create(slug='EVENT', name='Event')
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['slug'], 'EVENT')
|
||||
self.assertEqual(response.json()[0]['name'], 'Event')
|
||||
|
||||
def test_multi_members(self):
|
||||
Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||
Event.objects.create(slug='EVENT3', name='Event 3')
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_event(self):
|
||||
response = client.post('/api/1/events', {'slug': 'EVENT', 'name': 'Event'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['slug'], 'EVENT')
|
||||
self.assertEqual(response.json()['name'], 'Event')
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT')
|
||||
self.assertEqual(Event.objects.all()[0].name, 'Event')
|
||||
|
||||
def test_update_event(self):
|
||||
from rest_framework.test import APIClient
|
||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
response = APIClient().put(f'/api/1/events/{event.eid}', {'slug': 'EVENT2', 'name': 'Event 2 new'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['slug'], 'EVENT2')
|
||||
self.assertEqual(response.json()['name'], 'Event 2 new')
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT2')
|
||||
self.assertEqual(Event.objects.all()[0].name, 'Event 2 new')
|
||||
|
||||
def test_remove_event(self):
|
||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||
self.assertEqual(len(Event.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/events/{event.eid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
133
core/inventory/tests/v1/test_items.py
Normal file
133
core/inventory/tests/v1/test_items.py
Normal file
|
@ -0,0 +1,133 @@
|
|||
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)
|
Loading…
Add table
Add a link
Reference in a new issue