2023-11-18 11:57:50 +00:00
|
|
|
from django.test import TestCase, Client
|
|
|
|
from inventory.models import Event
|
|
|
|
|
|
|
|
client = Client()
|
|
|
|
|
|
|
|
|
|
|
|
class EventTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_empty(self):
|
2023-11-20 14:56:32 +00:00
|
|
|
response = client.get('/api/1/events')
|
2023-11-18 11:57:50 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.json(), [])
|
|
|
|
|
|
|
|
def test_members(self):
|
|
|
|
Event.objects.create(slug='EVENT', name='Event')
|
2023-11-20 14:56:32 +00:00
|
|
|
response = client.get('/api/1/events')
|
2023-11-18 11:57:50 +00:00
|
|
|
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')
|
2023-11-20 14:56:32 +00:00
|
|
|
response = client.get('/api/1/events')
|
2023-11-18 11:57:50 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(len(response.json()), 3)
|
|
|
|
|
|
|
|
def test_create_event(self):
|
2023-11-20 14:56:32 +00:00
|
|
|
response = client.post('/api/1/events', {'slug': 'EVENT', 'name': 'Event'})
|
2023-11-18 11:57:50 +00:00
|
|
|
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')
|
2023-11-20 14:56:32 +00:00
|
|
|
response = APIClient().put(f'/api/1/events/{event.eid}', {'slug': 'EVENT2', 'name': 'Event 2 new'})
|
2023-11-18 11:57:50 +00:00
|
|
|
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)
|
2023-11-20 14:56:32 +00:00
|
|
|
response = client.delete(f'/api/1/events/{event.eid}')
|
2023-11-18 11:57:50 +00:00
|
|
|
self.assertEqual(response.status_code, 204)
|
|
|
|
self.assertEqual(len(Event.objects.all()), 1)
|