def test_parent_is_NotFound(t): base = Device(id='base_id') base.getObjByPath = Mock(side_effect=NotFound()) object_map = ObjectMap({'id': 'os_id', 'compname': 'os'}) idm = IncrementalDataMap(base, object_map) t.assertEqual(idm.parent, base)
def test_applyDataMap_relmap(self): compname = "module/component" relname = "relationship" datamap = RelationshipMap() device = Device(id='test_device') device.dmd = self.dmd self.adm.applyDataMap(device, datamap, datamap.relname, datamap.compname)
def test_parent_is_from_path(t): base = Device(id='base_id') getObjByPath = create_autospec(base.getObjByPath) base.getObjByPath = getObjByPath object_map = ObjectMap({'id': 'os_id', 'compname': 'os'}) idm = IncrementalDataMap(base, object_map) t.assertEqual(idm.path, 'os') t.assertEqual(idm.parent, base.getObjByPath.return_value) getObjByPath.assert_called_with(idm.path)
def test_applyDataMap_RelationshipMap_contains_IncrementalDataMap(t): '''A relationshipMap may contain IncrementalDataMaps in addition to ObjectMaps ''' base = Device(id='owner') device = Device(id='related_device') device.dmd = Mock(name='dmd') relmap = RelationshipMap() relmap.append(IncrementalDataMap(device, ObjectMap({'comments': 'ok'}))) t.adm.applyDataMap(base, relmap) t.assertEqual(device.comments, 'ok')
def test_target_is_base(t): '''target is the parent if component and relname are undefined ''' base = Device(id='parent_id') object_map = ObjectMap({'id': base.id}) idm = IncrementalDataMap(base, object_map) t.assertEqual(idm.target, base)
def test_build_incrementalmap_from_dict(t): device = Device(id=sentinel.deviceid) datamap = {'id': sentinel.deviceid} ret = _validate_datamap(device, datamap, None, sentinel.compname, sentinel.modname, 'parentId') t.assertIsInstance(ret, IncrementalDataMap) t.assertEqual(ret.id, sentinel.deviceid) t.assertEqual(ret.modname, sentinel.modname) t.assertEqual(ret.path, sentinel.compname)
def test__updateRelationship(t): '''legacy method, exists so zenpack monkey patches won't fail some zenpacks will call this directly ''' t.adm.applyDataMap = create_autospec(t.adm.applyDataMap) device = Device(id='test_device') relmap = RelationshipMap() t.adm._updateRelationship(device, relmap) t.adm.applyDataMap.assert_called_with(device, relmap)
def test_target_is_component_without_id(t): '''the target may be a component on the parent ''' parent = Device(id='parent_id') object_map = ObjectMap({'compname': 'os'}) idm = IncrementalDataMap(parent, object_map) t.assertEqual(idm.target, parent.os)
def test_target_is_parent(t): '''The target may be the parent device ''' parent = Device(id='parent_id') object_map = ObjectMap({'id': parent.id}) idm = IncrementalDataMap(parent, object_map) t.assertEqual(idm.target, parent)
def test_target_is_relation(t): '''the target may be in a relationship on the parent ''' target = Mock(name='target', spec_set=['id'], id='target_obj') relname = 'relationship_name' parent = Device(id='parent_id') relationship = Mock(name='relationship', spec_set=[relname, '_getOb']) setattr(parent, relname, relationship) relationship._getOb.return_value = target object_map = ObjectMap({'id': target.id, 'relname': relname}) idm = IncrementalDataMap(parent, object_map) t.assertEqual(idm.target, target) relationship._getOb.assert_called_with(target.id)
def test_applyDataMap_ObjectMap_set_lists(t): device = Mock(Device(id='test_applyDataMap_ObjectMap_set_lists')) device.dmd = Mock(name='dmd') device.isLockedFromUpdates.return_value = False device.zCollectorDecoding = 'utf-8' device.getFoo = Mock(return_value=[3, 2, 1]) device.setFoo = Mock() # Set it to the same exact list- setter should not be called. t.adm.applyDataMap(device, ObjectMap({ 'id': device.id, 'setFoo': [3, 2, 1] })) device.setFoo.assert_not_called() # Set it to the same list contents, but in a different order. # Setter should not be called. device.setFoo.reset_mock() t.adm.applyDataMap(device, ObjectMap({ 'id': device.id, 'setFoo': [1, 2, 3] })) device.setFoo.assert_not_called() # Now change the contents. This should of course call the setter. device.setFoo.reset_mock() t.adm.applyDataMap(device, ObjectMap({'id': device.id, 'setFoo': [1]})) device.setFoo.assert_called_once_with([1]) # One more variation- change form a list to a string. # Now change the contents. This should of course call the setter. device.setFoo.reset_mock() t.adm.applyDataMap(device, ObjectMap({ 'id': device.id, 'setFoo': "foo" })) device.setFoo.assert_called_once_with("foo")
def test_applyDataMap_relmapException(self): '''test_applyDataMap_exception is mostly the same as test_applyDataMap_relmap - difference #1: compname is commented out - difference #2: with self.assertRaises(AttributeError) is added ''' dmd = self.dmd class datamap(list): #compname = "a/b" relname = "c" class Device(object): def deviceClass(self): return dmd.Devices class dmd: "Used for faking sync()" class _p_jar: @staticmethod def sync(): pass def getObjByPath(self, path): return reduce(getattr, path.split("/"), self) class a: class b: class c: "The relationship to populate" @staticmethod def objectIdsAll(): "returns the list of object ids in this relationship" return [] with self.assertRaises(AttributeError) as theException: self.adm.applyDataMap(Device(), datamap(), datamap.relname, datamap.compname) self.assertEqual(theException.exception.message, "type object 'datamap' has no attribute 'compname'")
def __init__(self, *args, **kw): Device.__init__(self, *args, **kw) self.buildRelations()
def setUp(t): t.obj = Device('oid') t.modname = 'Products.ZenModel.Device' t.classname = 'Device'