Package cm_api_tests :: Module test_baseapiobject
[hide private]
[frames] | no frames]

Source Code for Module cm_api_tests.test_baseapiobject

 1  # Licensed to Cloudera, Inc. under one 
 2  # or more contributor license agreements.  See the NOTICE file 
 3  # distributed with this work for additional information 
 4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
 5  # to you under the Apache License, Version 2.0 (the 
 6  # "License"); you may not use this file except in compliance 
 7  # with the License.  You may obtain a copy of the License at 
 8  # 
 9  #     http://www.apache.org/licenses/LICENSE-2.0 
10  # 
11  # Unless required by applicable law or agreed to in writing, software 
12  # distributed under the License is distributed on an "AS IS" BASIS, 
13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  # See the License for the specific language governing permissions and 
15  # limitations under the License. 
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):
23 - def _get_attributes(self):
24 return { 'value' : None }
25
26 -class Parent(BaseApiObject):
27 - def _get_attributes(self):
28 return { 29 'child' : Attr(Child), 30 'children' : Attr(Child), 31 'date' : Attr(datetime.datetime), 32 'readOnly' : ROAttr(), 33 }
34
35 -class TestBaseApiObject(unittest.TestCase):
36
37 - def test_props(self):
38 obj = Parent(None) 39 obj.child = Child(None) 40 obj.children = [ ] 41 obj.date = datetime.datetime.now() 42 43 # Setting read-only attribute. 44 with self.assertRaises(AttributeError): 45 obj.readOnly = False 46 47 # Setting unknown attribute. 48 with self.assertRaises(AttributeError): 49 obj.unknown = 'foo'
50
51 - def test_serde(self):
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
88 - def test_init(self):
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