3
3
from django .test import TestCase
4
4
from django .db .utils import IntegrityError
5
5
6
+ import datetime
6
7
from decimal import Decimal
7
8
8
9
class JSONFieldTest (TestCase ):
9
10
10
11
def test_simple (self ):
11
12
t1 = Test .objects .create (json = 123 )
12
- self .assertEqual (123 , t1 .json )
13
+ self .assertEqual (123 , Test . objects . get ( pk = t1 . pk ) .json )
13
14
t2 = Test .objects .create (json = '123' )
14
- self .assertEqual (123 , t2 .json )
15
+ self .assertEqual (123 , Test . objects . get ( pk = t2 . pk ) .json )
15
16
t3 = Test .objects .create (json = [123 ])
16
- self .assertEqual ([123 ], t3 .json )
17
+ self .assertEqual ([123 ], Test . objects . get ( pk = t3 . pk ) .json )
17
18
t4 = Test .objects .create (json = '[123]' )
18
- self .assertEqual ([123 ], t4 .json )
19
+ self .assertEqual ([123 ], Test . objects . get ( pk = t4 . pk ) .json )
19
20
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 )
21
22
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 )
23
24
24
25
def test_null (self ):
25
26
with self .assertRaises (IntegrityError ):
@@ -31,15 +32,31 @@ def test_null(self):
31
32
32
33
def test_decimal (self ):
33
34
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 )
35
36
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 )
37
38
38
39
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 )
40
47
41
48
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 )
43
54
44
55
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