class ServiceClassService(BaseService): """ Service to handle ServiceClass objects with mongodb and provide all business logic needed to views layer """ def __init__(self): self.class_dao = ServiceClassDao() self.instance_dao = ServiceInstanceDao() def get_all_service_classes(self): return list(self.class_dao.find_all()) def create(self, obj): try: return self.class_dao.create(obj) except DuplicateKeyError: raise UnsupportedParameterValueException(obj['_id'], 'non-existing-class') @return_obj_or_raise_not_found def get(self, class_name): return self.class_dao.find(class_name) def update(self, obj): # raise not found if not existing class if self.class_dao.update( obj): # When OperationFailure, Exception mapper will translate return obj else: raise GenericServiceError('Class update failed') def delete(self, class_name): class_obj = self.class_dao.find(class_name) if class_obj: try: self.instance_dao.delete_by_class_name(class_name) except OperationFailure: raise GenericServiceError('The class delete process failed.') if self.class_dao.delete(class_name): return else: raise GenericServiceError('The class delete process failed.') raise NotFoundException(class_name)
class InstancesDaoTests(TestCase): def setUp(self): self.class_ = { '_id': 'test', 'description': 'Descripcion test', 'default_version': "1.0" } self.instance = { 'uri': 'http://test', 'version': '1.0', 'class_name': 'test' } self.instance_new = { 'uri': 'http://testNew', 'version': '1.0', 'class_name': 'test' } self.dao = ServiceInstanceDao() self.classes_dao = ServiceClassDao() self.classes_dao.create(self.class_) self.dao.create(self.instance) super(InstancesDaoTests, self).setUp() def tearDown(self): self.dao.delete(self.instance['_id']) self.classes_dao.delete(self.class_['_id']) def test_add_new_instance_should_work(self): self.dao.create(self.instance_new) created_instance = self.dao.find(self.instance_new['_id']) self.assertEquals(created_instance, self.instance_new) # Removing created instance self.dao.delete(created_instance['_id']) def test_find_existing_instance_should_work(self): # _id is an objectId but we convert to str representation existing_instance = self.dao.find(str(self.instance['_id'])) self.assertEquals(existing_instance, self.instance) def test_find_instance_by_class_name_and_id_invalid_id_should_return_none( self): # _id is an objectId but we convert to str representation res = self.dao.find_by_class_name_and_id('test', 'invalid_id') self.assertEquals(None, res) def test_find_all_existing_instance_should_work(self): # _id is an objectId but we convert to str representation instances_list = self.dao.find_all('test') self.assertEquals(1, len(list(instances_list))) def test_add_existing_instance_should_raiseException(self): """ We check than uri, version and class_name are unique for instances collection """ # We don't use self.instance as _id will be populated by setUp method # We want ton ensure unique index is being managed self.assertRaises(DuplicateKeyError, self.dao.create, { 'uri': 'http://test', 'version': '1.0', 'class_name': 'test' }) def test_modify_existing_instance_should_return_true(self): self.instance['ob'] = 'oba' res = self.dao.update(self.instance) self.assertEquals(True, res, 'ob param not added') def test_modify_invalid_instance_id_should_return_false(self): self.instance_new['_id'] = '8989_nonExisting' res = self.dao.update(self.instance_new) self.assertEquals( False, res, 'Non existing instance was modified and it shouldnt') def test_modify_non_existing_instance_should_return_false(self): self.instance_new['_id'] = '5555b1d2f26ba3088459ffff' res = self.dao.update(self.instance_new) self.assertEquals( False, res, 'Non existing instance was modified and it shouldnt') def test_modify_existing_instance_matching_another_should_return_DuplicateKeyException( self): instance_new = self.dao.create(self.instance_new) instance_new['uri'] = self.instance['uri'] instance_new['version'] = self.instance['version'] self.assertRaises(DuplicateKeyError, self.dao.update, instance_new) def test_delete_invalid_instance_id_should_return_false(self): res = self.dao.delete('non_exitings_id') self.assertEquals(res, False, 'Delete not existing instance didnt return false') def test_delete_unexisting_instance_id_should_return_false(self): res = self.dao.delete('5555b1d2f26ba3088459ffff') self.assertEquals(res, False, 'Delete not existing instance didnt return false')