Skip to content

Commit de34a5f

Browse files
Added more tests
1 parent f05c46b commit de34a5f

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

test_project/app/tests.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
from django.test import TestCase
44
from django.db.utils import IntegrityError
55

6+
import datetime
67
from decimal import Decimal
78

89
class JSONFieldTest(TestCase):
910

1011
def test_simple(self):
1112
t1 = Test.objects.create(json=123)
12-
self.assertEqual(123, t1.json)
13+
self.assertEqual(123, Test.objects.get(pk=t1.pk).json)
1314
t2 = Test.objects.create(json='123')
14-
self.assertEqual(123, t2.json)
15+
self.assertEqual(123, Test.objects.get(pk=t2.pk).json)
1516
t3 = Test.objects.create(json=[123])
16-
self.assertEqual([123], t3.json)
17+
self.assertEqual([123], Test.objects.get(pk=t3.pk).json)
1718
t4 = Test.objects.create(json='[123]')
18-
self.assertEqual([123], t4.json)
19+
self.assertEqual([123], Test.objects.get(pk=t4.pk).json)
1920
t5 = Test.objects.create(json={'test':[1,2,3]})
20-
self.assertEqual({'test':[1,2,3]}, t5.json)
21+
self.assertEqual({'test':[1,2,3]}, Test.objects.get(pk=t5.pk).json)
2122
t6 = Test.objects.create(json='{"test":[1,2,3]}')
22-
self.assertEqual({'test':[1,2,3]}, t6.json)
23+
self.assertEqual({'test':[1,2,3]}, Test.objects.get(pk=t6.pk).json)
2324

2425
def test_null(self):
2526
with self.assertRaises(IntegrityError):
@@ -31,15 +32,31 @@ def test_null(self):
3132

3233
def test_decimal(self):
3334
t1 = Test.objects.create(json=Decimal(1.24))
34-
self.assertEqual(Decimal(1.24), t1.json)
35+
self.assertEqual(Decimal(1.24), Test.objects.get(pk=t1.pk).json)
3536
t2 = Test.objects.create(json={'test':[{'test':Decimal(1.24)}]})
36-
self.assertEqual({'test':[{'test':Decimal(1.24)}]}, t2.json)
37+
self.assertEqual({'test':[{'test':Decimal(1.24)}]}, Test.objects.get(pk=t2.pk).json)
3738

3839
def test_time(self):
39-
pass
40+
now = datetime.datetime.now().time()
41+
t1 = Test.objects.create(json=now)
42+
# JSON does not have microsecond precision, round to millisecond
43+
now_rounded = now.replace(microsecond=(int(now.microsecond) / 1000) * 1000)
44+
self.assertEqual(now_rounded, Test.objects.get(pk=t1.pk).json)
45+
t2 = Test.objects.create(json={'time':[now]})
46+
self.assertEqual({'time':[now_rounded]}, Test.objects.get(pk=t2.pk).json)
4047

4148
def test_date(self):
42-
pass
49+
today = datetime.date.today()
50+
t1 = Test.objects.create(json=today)
51+
self.assertEqual(today, Test.objects.get(pk=t1.pk).json)
52+
t2 = Test.objects.create(json={'today':today})
53+
self.assertEqual({'today':today}, Test.objects.get(pk=t2.pk).json)
4354

4455
def test_datetime(self):
45-
pass
56+
now = datetime.datetime.now()
57+
t1 = Test.objects.create(json=now)
58+
# JSON does not have microsecond precision, round to millisecond
59+
now_rounded = now.replace(microsecond=(int(now.microsecond) / 1000) * 1000)
60+
self.assertEqual(now_rounded, Test.objects.get(pk=t1.pk).json)
61+
t2 = Test.objects.create(json={'test':[{'test':now}]})
62+
self.assertEqual({'test':[{'test':now_rounded}]}, Test.objects.get(pk=t2.pk).json)

0 commit comments

Comments
 (0)