1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import datetime
18 import unittest
19 from cm_api.endpoints.types import *
20 from cm_api_tests import utils
21
22 -class Child(BaseApiObject):
24 return { 'value' : None }
25
34
36
38 obj = Parent(None)
39 obj.child = Child(None)
40 obj.children = [ ]
41 obj.date = datetime.datetime.now()
42
43
44 with self.assertRaises(AttributeError):
45 obj.readOnly = False
46
47
48 with self.assertRaises(AttributeError):
49 obj.unknown = 'foo'
50
52 JSON = '''
53 {
54 "child" : { "value" : "string1" },
55 "children" : [
56 { "value" : 1 },
57 { "value" : "2" }
58 ],
59 "date" : "2013-02-12T12:17:15.831765Z",
60 "readOnly" : true
61 }
62 '''
63 obj = utils.deserialize(JSON, Parent)
64 self.assertIsInstance(obj.child, Child)
65 self.assertEqual('string1', obj.child.value)
66 self.assertIsInstance(obj.children, list)
67 self.assertEqual(2, len(obj.children))
68 self.assertEqual(1, obj.children[0].value)
69 self.assertEqual('2', obj.children[1].value)
70 self.assertIsInstance(obj.date, datetime.datetime)
71 self.assertEqual(2013, obj.date.year)
72 self.assertEqual(2, obj.date.month)
73 self.assertEqual(12, obj.date.day)
74 self.assertEqual(12, obj.date.hour)
75 self.assertEqual(17, obj.date.minute)
76 self.assertEqual(15, obj.date.second)
77 self.assertEqual(831765, obj.date.microsecond)
78 self.assertTrue(obj.readOnly)
79
80 JSON = '''
81 {
82 "children" : [ ]
83 }
84 '''
85 obj = utils.deserialize(JSON, Parent)
86 self.assertEquals([], obj.children)
87
89 obj = Parent(None)
90 self.assertTrue(hasattr(obj, 'child'))
91 self.assertTrue(hasattr(obj, 'readOnly'))
92
93 obj = Parent(None, date=datetime.datetime.now())
94 self.assertIsInstance(obj.date, datetime.datetime)
95
96 self.assertRaises(AttributeError, Parent, None, readOnly=True)
97