59 lines
2.5 KiB
Python
59 lines
2.5 KiB
Python
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)
|