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)