def test_populate_dictionary_with_basic_link_from_DB(self): # Create resources and Link src_id = "124816" tar_id = "112358" src_res = core_model.Resource(src_id, occi_sla.AGREEMENT, None) tar_res = core_model.Resource(tar_id, occi_sla.AGREEMENT, None) lnk_id = "/agreement/load-link-entity-from-DB" lnk = core_model.Link(lnk_id, None, None, src_res, tar_res) links = EntityDictionary(self.api.registry) links._persist_resource(src_id, src_res) links._persist_resource(tar_id, tar_res) links._persist_link(lnk_id, lnk) links.populate_from_db() self.assertEqual(len(links), 3) self.assertTrue(isinstance(links[lnk_id], core_model.Link)) self.assertEqual(links[lnk_id].identifier, lnk_id) # Removing assistive field templates for comparison temp_src = links[lnk_id].source.__dict__ temp_trgt = links[lnk_id].target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, src_res.__dict__) self.assertEqual(temp_trgt, tar_res.__dict__)
def test_populate_dictionary_with_basic_link_from_DB(self): # Create resources and Link src_id = "124816" tar_id = "112358" src_res = core_model.Resource(src_id, occi_sla.AGREEMENT, None) tar_res = core_model.Resource(tar_id, occi_sla.AGREEMENT, None) lnk_id = "/agreement/load-link-entity-from-DB" lnk = core_model.Link(lnk_id, None, None, src_res, tar_res) links = EntityDictionary(self.api.registry) links._persist_resource(src_id, src_res) links._persist_resource(tar_id, tar_res) links._persist_link(lnk_id, lnk) links.populate_from_db() self.assertEqual(len(links), 3) self.assertTrue(isinstance(links[lnk_id], core_model.Link)) self.assertEqual(links[lnk_id].identifier, lnk_id) # Removing assistive field templates for comparison temp_src = links[lnk_id].source.__dict__ temp_trgt = links[lnk_id].target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, src_res.__dict__) self.assertEqual(temp_trgt, tar_res.__dict__)
def test_populate_dictionary_with_resource_containing_attributes(self): res_id = "/agreement/load_resource_w_attributes" res = core_model.Resource(res_id, None, None) res_attrs = {"the.test.attr": "1", "attr-2": "2", "attr_3": "3", "attr||four": "4"} res.attributes = res_attrs resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, res) resources.populate_from_db() self.assertEqual(resources[res_id].attributes, res_attrs)
def test_load_resource_with_mixin_type_from_DB(self): """ retrieve a resource from the db with the same mixins """ res_id = "/agreement/load-resource-with-mixin-from-db" test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, [test_data.epc_mixin]) resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, test_resource) resources.populate_from_db() res_mixins = resources[res_id].mixins self.assertEqual(len(res_mixins), 1) self.assertTrue(isinstance(res_mixins[0], core_model.Mixin))
def test_populate_dictionary_with_resource_containing_attributes(self): res_id = "/agreement/load_resource_w_attributes" res = core_model.Resource(res_id, None, None) res_attrs = { "the.test.attr": "1", "attr-2": "2", "attr_3": "3", "attr||four": "4" } res.attributes = res_attrs resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, res) resources.populate_from_db() self.assertEqual(resources[res_id].attributes, res_attrs)
def test_load_resource_with_mixin_type_from_DB(self): """ retrieve a resource from the db with the same mixins """ res_id = "/agreement/load-resource-with-mixin-from-db" test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, [test_data.epc_mixin]) resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, test_resource) resources.populate_from_db() res_mixins = resources[res_id].mixins self.assertEqual(len(res_mixins), 1) self.assertTrue(isinstance(res_mixins[0], core_model.Mixin))
def test_load_multiple_resource_with_mixins_from_DB(self): """ retrieve a resource from the dictionary with the same mixins """ res_id = "/agreement/load-mult-mixins-from-db" test_resource = core_model.Resource(res_id, None, [test_data.epc_mixin, test_data.ran_mixin]) resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, test_resource) resources.populate_from_db() test_mxn_loc = [mix.location for mix in test_resource.mixins] rtrnd_mxn_loc = [mixin.location for mixin in resources[res_id].mixins] self.assertEqual(test_mxn_loc.sort(), rtrnd_mxn_loc.sort())
def test_load_multiple_resource_with_mixins_from_DB(self): """ retrieve a resource from the dictionary with the same mixins """ res_id = "/agreement/load-mult-mixins-from-db" test_resource = core_model.Resource( res_id, None, [test_data.epc_mixin, test_data.ran_mixin]) resources = EntityDictionary(self.api.registry) resources._persist_resource(res_id, test_resource) resources.populate_from_db() test_mxn_loc = [mix.location for mix in test_resource.mixins] rtrnd_mxn_loc = [mixin.location for mixin in resources[res_id].mixins] self.assertEqual(test_mxn_loc.sort(), rtrnd_mxn_loc.sort())
def test_populate_dictionary_with_basic_resource_from_DB(self): """ This test retrieves a resource from the resource dictionary """ # Create resource and save to DB res_id = "/agreement/load-basic-from-db" test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, None) resources = EntityDictionary(self.api.registry) # bypass normal resource add so that only the db record exists and not # the in memeory dictionary resources._persist_resource(res_id, test_resource) resources.populate_from_db() print test_resource.__dict__ print resources[res_id].__dict__ self.assertEqual(len(resources), 1) self.assertTrue(isinstance(resources[res_id], core_model.Resource))
def test_populate_dictionary_with_basic_resource_from_DB(self): """ This test retrieves a resource from the resource dictionary """ # Create resource and save to DB res_id = "/agreement/load-basic-from-db" test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, None) resources = EntityDictionary(self.api.registry) # bypass normal resource add so that only the db record exists and not # the in memeory dictionary resources._persist_resource(res_id, test_resource) resources.populate_from_db() print test_resource.__dict__ print resources[res_id].__dict__ self.assertEqual(len(resources), 1) self.assertTrue(isinstance(resources[res_id], core_model.Resource))
def test_populate_dictionary_with_resource_containing_links(self): # Test entities - creating a resource with two links res_tar_1_id = "/an-id/res_tar_1" res_src_1_id = "/an-id/res_src_1" lnk_1_id = "/an-id/lnk_1_id" res_tar_2_id = "/an-id/res_tar_2" res_src_2_id = "/an-id/res_src_2" lnk_2_id = "/an-id/lnk_2_id" res_m_id = "/an-id/res_m" res_src_1 = core_model.Resource(res_src_1_id, occi_sla.AGREEMENT, None) res_tar_1 = core_model.Resource(res_tar_1_id, occi_sla.AGREEMENT, None) lnk_1 = core_model.Link(lnk_1_id, occi_sla.AGREEMENT_LINK, None, res_src_1, res_tar_1) res_src_2 = core_model.Resource(res_src_2_id, occi_sla.AGREEMENT, None) res_tar_2 = core_model.Resource(res_tar_2_id, occi_sla.AGREEMENT, None) lnk_2 = core_model.Link(lnk_2_id, occi_sla.AGREEMENT_LINK, None, res_src_2, res_tar_2) res_m = core_model.Resource(res_m_id, occi_sla.AGREEMENT, None, [lnk_1, lnk_2]) # Persist all to DB but not dict in memory entities = EntityDictionary(self.api.registry) entities._persist_link(lnk_1_id, lnk_1) entities._persist_link(lnk_2_id, lnk_2) entities._persist_resource(res_src_1_id, res_src_1) entities._persist_resource(res_tar_1_id, res_tar_1) entities._persist_resource(res_src_2_id, res_src_2) entities._persist_resource(res_tar_2_id, res_tar_2) entities._persist_resource(res_m_id, res_m) entities.populate_from_db() self.assertEqual(len(entities), 7) self.assertEqual(len(entities[res_m_id].links), 2) for link in entities[res_m_id].links: if link.identifier == lnk_1_id: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_1.__dict__) self.assertEqual(temp_trgt, res_tar_1.__dict__) else: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_2.__dict__) self.assertEqual(temp_trgt, res_tar_2.__dict__)
def test_populate_dictionary_with_resource_containing_links(self): # Test entities - creating a resource with two links res_tar_1_id = "/an-id/res_tar_1" res_src_1_id = "/an-id/res_src_1" lnk_1_id = "/an-id/lnk_1_id" res_tar_2_id = "/an-id/res_tar_2" res_src_2_id = "/an-id/res_src_2" lnk_2_id = "/an-id/lnk_2_id" res_m_id = "/an-id/res_m" res_src_1 = core_model.Resource(res_src_1_id, occi_sla.AGREEMENT, None) res_tar_1 = core_model.Resource(res_tar_1_id, occi_sla.AGREEMENT, None) lnk_1 = core_model.Link(lnk_1_id, occi_sla.AGREEMENT_LINK, None, res_src_1, res_tar_1) res_src_2 = core_model.Resource(res_src_2_id, occi_sla.AGREEMENT, None) res_tar_2 = core_model.Resource(res_tar_2_id, occi_sla.AGREEMENT, None) lnk_2 = core_model.Link(lnk_2_id, occi_sla.AGREEMENT_LINK, None, res_src_2, res_tar_2) res_m = core_model.Resource(res_m_id, occi_sla.AGREEMENT, None, [lnk_1, lnk_2]) # Persist all to DB but not dict in memory entities = EntityDictionary(self.api.registry) entities._persist_link(lnk_1_id, lnk_1) entities._persist_link(lnk_2_id, lnk_2) entities._persist_resource(res_src_1_id, res_src_1) entities._persist_resource(res_tar_1_id, res_tar_1) entities._persist_resource(res_src_2_id, res_src_2) entities._persist_resource(res_tar_2_id, res_tar_2) entities._persist_resource(res_m_id, res_m) entities.populate_from_db() self.assertEqual(len(entities), 7) self.assertEqual(len(entities[res_m_id].links), 2) for link in entities[res_m_id].links: if link.identifier == lnk_1_id: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_1.__dict__) self.assertEqual(temp_trgt, res_tar_1.__dict__) else: # Removing assistive field templates for comparison temp_src = link.source.__dict__ temp_trgt = link.target.__dict__ temp_src.pop('templates') temp_trgt.pop('templates') self.assertEqual(temp_src, res_src_2.__dict__) self.assertEqual(temp_trgt, res_tar_2.__dict__)