add virtual item field 'returned' in item
This commit is contained in:
parent
83565d4b2a
commit
2e29b8b046
3 changed files with 62 additions and 15 deletions
|
@ -1,3 +1,5 @@
|
|||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase, Client
|
||||
from django.contrib.auth.models import Permission
|
||||
from knox.models import AuthToken
|
||||
|
@ -28,7 +30,8 @@ class ItemTestCase(TestCase):
|
|||
response = self.client.get(f'/api/2/{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}])
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False}])
|
||||
|
||||
def test_members_with_file(self):
|
||||
import base64
|
||||
|
@ -37,7 +40,8 @@ class ItemTestCase(TestCase):
|
|||
response = self.client.get(f'/api/2/{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}])
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash,
|
||||
'returned': False}])
|
||||
|
||||
def test_multi_members(self):
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
|
@ -51,7 +55,8 @@ class ItemTestCase(TestCase):
|
|||
response = self.client.post(f'/api/2/{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})
|
||||
{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '1')
|
||||
|
@ -60,9 +65,9 @@ class ItemTestCase(TestCase):
|
|||
def test_create_item_with_file(self):
|
||||
import base64
|
||||
response = self.client.post(f'/api/2/{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')
|
||||
{'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')
|
||||
|
@ -78,10 +83,11 @@ class ItemTestCase(TestCase):
|
|||
def test_update_item(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', {'description': '2'},
|
||||
content_type='application/json')
|
||||
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})
|
||||
{'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '2')
|
||||
|
@ -91,9 +97,9 @@ class ItemTestCase(TestCase):
|
|||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/',
|
||||
{'description': '2',
|
||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
{'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')
|
||||
|
@ -136,3 +142,31 @@ class ItemTestCase(TestCase):
|
|||
def test_item_nonexistent(self):
|
||||
response = self.client.get(f'/api/2/NOEVENT/item/')
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_item_return(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
self.assertEqual(item.returned_at, None)
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.uid}/', {'returned': True},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
item.refresh_from_db()
|
||||
self.assertNotEqual(item.returned_at, None)
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 0)
|
||||
|
||||
def test_item_show_not_returned(self):
|
||||
item1 = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item2 = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 2)
|
||||
item2.returned_at = datetime.now()
|
||||
item2.save()
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['uid'], item1.uid)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue