Beispiel #1
0
    def _delete_user(self, user_id, token, hostname, port):
        '''This method removes the requested user_id.'''

        db_conn = None
        conn_manager = None
        request_id = None

        person_facade = None
        person = None

        try:
            conn_manager, request_id, db_conn = self._get_db_conn()

            person_facade = ModelFacade(Person, db_conn)
            user = ModelFacade(User, db_conn).find_by_pk({User.user_id: user_id})
            person = person_facade.find_by_pk({Person.person_id: user.person_id})

            endpoint = "/api/latest/oauth-idp-profile/%s?token=%s" % (user_id, token)

            http_conn = http.client.HTTPConnection(hostname, port)

            http_conn.request("DELETE", endpoint)

            response = http_conn.getresponse()

            http_conn.close()

            self.assertIsNotNone(response)
            self.assertEqual(204, response.status)
        finally:
            if person:
                person_facade.delete(person)

            if db_conn:
                conn_manager.close_connection(request_id)
    def setup_once(cls):
        '''This method is invoked once before executing any test case. For model facade we create a series of messages.'''

        cls.settings_facade = SettingsFacade()
        mvc.CONN_MANAGER = mvc.init_dm_db_engine(
            cls.settings_facade.get("database_config"), echo=True)

        cls.model_facade = ModelFacade(
            ModelFacadeMessage,
            mvc.CONN_MANAGER.get_connection(ModelFacadeIntegration.request_id))
Beispiel #3
0
    def _delete_user(self, user_id, token, hostname, port):
        '''This method removes the requested user_id.'''

        db_conn = None
        conn_manager = None
        request_id = None

        person_facade = None
        person = None

        try:
            conn_manager, request_id, db_conn = self._get_db_conn()

            person_facade = ModelFacade(Person, db_conn)
            user = ModelFacade(User,
                               db_conn).find_by_pk({User.user_id: user_id})
            person = person_facade.find_by_pk(
                {Person.person_id: user.person_id})

            endpoint = "/api/latest/oauth-idp-profile/%s?token=%s" % (user_id,
                                                                      token)

            http_conn = http.client.HTTPConnection(hostname, port)

            http_conn.request("DELETE", endpoint)

            response = http_conn.getresponse()

            http_conn.close()

            self.assertIsNotNone(response)
            self.assertEqual(204, response.status)
        finally:
            if person:
                person_facade.delete(person)

            if db_conn:
                conn_manager.close_connection(request_id)
Beispiel #4
0
    def test_new_model_incompatible(self):
        '''This test case makes sure a model facade instance raises an exception if given model class does not
        extend **BASEMODEL**.'''

        with self.assertRaises(FantasticoIncompatibleClassError):
            ModelFacade(Mock(), Mock())
Beispiel #5
0
 def init(self):
     self._session = Mock()
     self._facade = ModelFacade(PersonModelTest, self._session)
     self._rollbacked = False
     self._model_filter = None
     self._model_sort = None
Beispiel #6
0
class ModelFacadeTests(FantasticoUnitTestsCase):
    '''This class provides test suite for generating a model facade for BaseModel classes.'''
    def init(self):
        self._session = Mock()
        self._facade = ModelFacade(PersonModelTest, self._session)
        self._rollbacked = False
        self._model_filter = None
        self._model_sort = None

    def test_new_model_ok(self):
        '''This test case ensures a model facade can obtain an instance of a given class.'''

        for model in [
                self._facade.new_model("John", **{"last_name": "Doe"}),
                self._facade.new_model("John", last_name="Doe")
        ]:
            self.assertEqual(PersonModelTest, self._facade.model_cls)
            self.assertIsInstance(model, BASEMODEL)
            self.assertEqual("John", model.first_name)
            self.assertEqual("Doe", model.last_name)

    def test_new_model_incompatible(self):
        '''This test case makes sure a model facade instance raises an exception if given model class does not
        extend **BASEMODEL**.'''

        with self.assertRaises(FantasticoIncompatibleClassError):
            ModelFacade(Mock(), Mock())

    def test_create_ok(self):
        '''This test case makes sure a model can be correctly saved using a facade.'''

        model = PersonModelTest(first_name="John", last_name="Doe")

        def add(model):
            model.id = 1

            return self._session

        self._session.add = add

        model_id = self._facade.create(model)

        self.assertFalse(self._rollbacked)
        self.assertEqual(1, len(model_id))
        self.assertEqual(1, model_id[0])
        self.assertEqual(1, model.id)

    def test_create_exception_unhandled(self):
        '''This test case makes sure a model creation exception is wrapped correctly into a concrete fantastico exception.'''

        model = PersonModelTest(first_name="John", last_name="Doe")

        self._session.add = Mock(side_effect=Exception("Unhandled exception"))

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        self.assertRaises(FantasticoDbError, self._facade.create, *[model])
        self.assertTrue(self._rollbacked)

    def test_update_ok(self):
        '''This test case ensures a model can be updated correctly using model facade.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        def merge(model):
            model.first_name = "John Changed"

        self._session.merge = merge

        self._facade.update(model)

        self.assertFalse(self._rollbacked)
        self.assertEqual("John Changed", model.first_name)

    def test_update_exception_unhandled(self):
        '''This test case ensures a model update gracefully handles all unhandled exceptions.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        self._session.merge = Mock(
            side_effect=Exception("Unhandled exception"))

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        self.assertRaises(FantasticoDbError, self._facade.update, *[model])
        self.assertTrue(self._rollbacked)

    def test_update_exception_notfound(self):
        '''This test case ensures a model update exception is raised when the given model does not exist.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[])

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        self.assertRaises(FantasticoDbNotFoundError, self._facade.update,
                          *[model])
        self.assertTrue(self._rollbacked)

    def test_find_by_pk_ok(self):
        '''This test case ensures find_by_pk method retrieves a model instance when a record is found by id.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        response = self._facade.find_by_pk({PersonModelTest.id: 1})

        self.assertFalse(self._rollbacked)
        self.assertEqual(model, response)

    def test_find_by_pk_notfound(self):
        '''This test case ensures find_by_pk method raises an exception when a matching record is not found.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[])

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        with self.assertRaises(FantasticoDbNotFoundError):
            self._facade.find_by_pk({PersonModelTest.id: 1})

        self.assertTrue(self._rollbacked)

    def test_delete_ok(self):
        '''This test case ensures a model can be deleted successfully if no exception occurs.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._facade.delete(model)

        self.assertFalse(self._rollbacked)

    def test_delete_exception_unhandled(self):
        '''This test case ensures unhandled exceptions are gracefully handled by delete method.'''

        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1

        self._session.delete = Mock(
            side_effect=Exception("Unhandled exception"))

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        with self.assertRaises(FantasticoDbError):
            self._facade.delete(model)

        self.assertTrue(self._rollbacked)

    def test_get_paged_records_ok(self):
        '''This test case ensures records can be retrieved correctly from a facade.'''

        expected_model = Mock()

        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_sort = ModelSort(PersonModelTest.first_name)

        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)

            return self._session

        def filter_mock(expr):
            self.assertEqual(self._model_filter.get_expression(), expr)

            return self._session

        def sort_mock(expr):
            self.assertEqual(self._model_sort.get_expression(), expr)

            return self._session

        def offset_mock(offset):
            self.assertEqual(0, offset)

            return self._session

        def limit_mock(limit_count):
            self.assertEqual(4, limit_count)

            return self._session

        self._session.query = query_mock
        self._session._primary_entity = self._session
        self._session.selectable = PersonModelTest.id.table
        self._session.filter = filter_mock
        self._session.order_by = sort_mock
        self._session.offset = offset_mock
        self._session.limit = limit_mock
        self._session.all = Mock(
            return_value=[expected_model, expected_model, expected_model])

        records = self._facade.get_records_paged(
            start_record=0,
            end_record=4,
            sort_expr=self._model_sort,
            filter_expr=self._model_filter)

        self.assertIsNotNone(records)
        self.assertEqual(3, len(records))

        for model in records:
            self.assertEqual(expected_model, model)

    def test_get_records_paged_default(self):
        '''This test case ensure records are retrieved when no filter / sort_expr are specified.'''
        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)

            return self._session

        def offset_mock(offset):
            self.assertEqual(0, offset)

            return self._session

        def limit_mock(limit_count):
            self.assertEqual(4, limit_count)

            return self._session

        self._session.query = query_mock
        self._session.offset = offset_mock
        self._session.limit = limit_mock
        self._session.all = Mock(return_value=[])

        records = self._facade.get_records_paged(start_record=0, end_record=4)

        self.assertIsNotNone(records)
        self.assertEqual(0, len(records))

    def test_get_records_paged_unhandled_exception(self):
        '''This test case ensures that any unhandled exception from filters / sql alchemy is gracefully handled.'''

        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_filter.build = Mock(
            side_effect=Exception("Unhandled exception"))

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        with self.assertRaises(FantasticoDbError):
            self._facade.get_records_paged(start_record=0,
                                           end_record=4,
                                           filter_expr=self._model_filter)

        self.assertTrue(self._rollbacked)

    def test_count_records_default_ok(self):
        '''This test case ensures count method works correctly.'''
        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)

            return self._session

        self._session.query = query_mock
        self._session.count = Mock(return_value=20)

        records_count = self._facade.count_records()

        self.assertEqual(20, records_count)

    def test_count_records_filtered_ok(self):
        '''This test case ensures count method works correctly when a filter is given.'''

        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)

        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)

            return self._session

        def filter_mock(expr):
            self.assertEqual(self._model_filter.get_expression(), expr)

            return self._session

        self._session.query = query_mock
        self._session._primary_entity = self._session
        self._session.selectable = PersonModelTest.id.table
        self._session.filter = filter_mock
        self._session.count = Mock(return_value=20)

        records_count = self._facade.count_records(self._model_filter)

        self.assertEqual(20, records_count)

    def test_count_records_unhandled_exception(self):
        '''This test case ensures count method gracefully handles unexpected exceptions.'''

        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_filter.build = Mock(
            side_effect=Exception("Unhandled exception"))

        def rollback():
            self._rollbacked = True

        self._session.rollback = rollback

        with self.assertRaises(FantasticoDbError):
            self._facade.count_records(self._model_filter)

        self.assertTrue(self._rollbacked)
 def init(self):
     self._session = Mock()
     self._facade = ModelFacade(PersonModelTest, self._session)
     self._rollbacked = False
     self._model_filter = None
     self._model_sort = None
class ModelFacadeTests(FantasticoUnitTestsCase):
    '''This class provides test suite for generating a model facade for BaseModel classes.'''
    
    def init(self):
        self._session = Mock()
        self._facade = ModelFacade(PersonModelTest, self._session)
        self._rollbacked = False
        self._model_filter = None
        self._model_sort = None
        
    def test_new_model_ok(self):
        '''This test case ensures a model facade can obtain an instance of a given class.'''
        
        for model in [self._facade.new_model("John", **{"last_name": "Doe"}),
                      self._facade.new_model("John", last_name="Doe")]:
            self.assertEqual(PersonModelTest, self._facade.model_cls)
            self.assertIsInstance(model, BASEMODEL)
            self.assertEqual("John", model.first_name)
            self.assertEqual("Doe", model.last_name)
            
    def test_new_model_incompatible(self):
        '''This test case makes sure a model facade instance raises an exception if given model class does not
        extend **BASEMODEL**.'''

        with self.assertRaises(FantasticoIncompatibleClassError):
            ModelFacade(Mock(), Mock())
        
    def test_create_ok(self):
        '''This test case makes sure a model can be correctly saved using a facade.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        
        def add(model):
            model.id = 1
            
            return self._session
        
        self._session.add = add
        
        model_id = self._facade.create(model)
        
        self.assertFalse(self._rollbacked)
        self.assertEqual(1, len(model_id))
        self.assertEqual(1, model_id[0])
        self.assertEqual(1, model.id)
        
    def test_create_exception_unhandled(self):
        '''This test case makes sure a model creation exception is wrapped correctly into a concrete fantastico exception.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe") 
        
        self._session.add = Mock(side_effect=Exception("Unhandled exception"))
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback
        
        self.assertRaises(FantasticoDbError, self._facade.create, *[model])
        self.assertTrue(self._rollbacked)
        
    def test_update_ok(self):
        '''This test case ensures a model can be updated correctly using model facade.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        
        def merge(model):
            model.first_name = "John Changed"
        
        self._session.merge = merge
        
        self._facade.update(model)
        
        self.assertFalse(self._rollbacked)
        self.assertEqual("John Changed", model.first_name)
        
    def test_update_exception_unhandled(self):
        '''This test case ensures a model update gracefully handles all unhandled exceptions.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        self._session.merge = Mock(side_effect=Exception("Unhandled exception"))
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback        
        
        self.assertRaises(FantasticoDbError, self._facade.update, *[model])
        self.assertTrue(self._rollbacked)
        
    def test_update_exception_notfound(self):
        '''This test case ensures a model update exception is raised when the given model does not exist.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[])
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback
        
        self.assertRaises(FantasticoDbNotFoundError, self._facade.update, *[model])
        self.assertTrue(self._rollbacked)
    
    def test_find_by_pk_ok(self):
        '''This test case ensures find_by_pk method retrieves a model instance when a record is found by id.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[model])

        response = self._facade.find_by_pk({PersonModelTest.id: 1})
        
        self.assertFalse(self._rollbacked)
        self.assertEqual(model, response)
        
    def test_find_by_pk_notfound(self):
        '''This test case ensures find_by_pk method raises an exception when a matching record is not found.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.query = Mock(return_value=self._session)
        self._session.filter = Mock(return_value=self._session)
        self._session.all = Mock(return_value=[])

        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback

        with self.assertRaises(FantasticoDbNotFoundError):
            self._facade.find_by_pk({PersonModelTest.id: 1})

        self.assertTrue(self._rollbacked)
                    
    def test_delete_ok(self):
        '''This test case ensures a model can be deleted successfully if no exception occurs.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._facade.delete(model)
        
        self.assertFalse(self._rollbacked)

    def test_delete_exception_unhandled(self):
        '''This test case ensures unhandled exceptions are gracefully handled by delete method.'''
        
        model = PersonModelTest(first_name="John", last_name="Doe")
        model.id = 1
        
        self._session.delete = Mock(side_effect=Exception("Unhandled exception"))
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback

        
        with self.assertRaises(FantasticoDbError):
            self._facade.delete(model)
        
        self.assertTrue(self._rollbacked)
        
    def test_get_paged_records_ok(self):
        '''This test case ensures records can be retrieved correctly from a facade.'''
        
        expected_model = Mock()
        
        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_sort = ModelSort(PersonModelTest.first_name)
        
        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)
            
            return self._session
        
        def filter_mock(expr):
            self.assertEqual(self._model_filter.get_expression(), expr)
            
            return self._session
        
        def sort_mock(expr):
            self.assertEqual(self._model_sort.get_expression(), expr)
            
            return self._session
        
        def offset_mock(offset):
            self.assertEqual(0, offset)
            
            return self._session
        
        def limit_mock(limit_count):
            self.assertEqual(4, limit_count)
            
            return self._session
        
        self._session.query = query_mock
        self._session._primary_entity = self._session
        self._session.selectable = PersonModelTest.id.table
        self._session.filter = filter_mock
        self._session.order_by = sort_mock
        self._session.offset = offset_mock
        self._session.limit = limit_mock
        self._session.all = Mock(return_value=[expected_model, expected_model, expected_model])
                
        records = self._facade.get_records_paged(start_record=0, end_record=4, 
                                                 sort_expr=self._model_sort,
                                                 filter_expr=self._model_filter)
        
        self.assertIsNotNone(records)
        self.assertEqual(3, len(records))
        
        for model in records:
            self.assertEqual(expected_model, model)
    
    def test_get_records_paged_default(self):
        '''This test case ensure records are retrieved when no filter / sort_expr are specified.'''

        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)
            
            return self._session
        
        def offset_mock(offset):
            self.assertEqual(0, offset)
            
            return self._session
        
        def limit_mock(limit_count):
            self.assertEqual(4, limit_count)
            
            return self._session
        
        self._session.query = query_mock
        self._session.offset = offset_mock
        self._session.limit = limit_mock
        self._session.all = Mock(return_value=[])
        
        records = self._facade.get_records_paged(start_record=0, end_record=4)
        
        self.assertIsNotNone(records)
        self.assertEqual(0, len(records))
    
    def test_get_records_paged_unhandled_exception(self):
        '''This test case ensures that any unhandled exception from filters / sql alchemy is gracefully handled.'''
        
        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_filter.build = Mock(side_effect=Exception("Unhandled exception"))
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback
        
        with self.assertRaises(FantasticoDbError):
            self._facade.get_records_paged(start_record=0, end_record=4, filter_expr=self._model_filter)

        self.assertTrue(self._rollbacked)
        
    def test_count_records_default_ok(self):
        '''This test case ensures count method works correctly.'''

        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)
            
            return self._session
        
        self._session.query = query_mock
        self._session.count = Mock(return_value=20)
        
        records_count = self._facade.count_records()
        
        self.assertEqual(20, records_count)
    
    def test_count_records_filtered_ok(self):
        '''This test case ensures count method works correctly when a filter is given.'''
        
        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        
        def query_mock(cls_obj):
            self.assertEqual(PersonModelTest, cls_obj)
            
            return self._session
        
        def filter_mock(expr):
            self.assertEqual(self._model_filter.get_expression(), expr)
            
            return self._session
        
        self._session.query = query_mock
        self._session._primary_entity = self._session
        self._session.selectable = PersonModelTest.id.table
        self._session.filter = filter_mock
        self._session.count = Mock(return_value=20)
        
        records_count = self._facade.count_records(self._model_filter)
        
        self.assertEqual(20, records_count)

    def test_count_records_unhandled_exception(self):
        '''This test case ensures count method gracefully handles unexpected exceptions.'''
        
        self._model_filter = ModelFilter(PersonModelTest.id, 1, ModelFilter.GT)
        self._model_filter.build = Mock(side_effect=Exception("Unhandled exception"))
        
        def rollback():
            self._rollbacked = True
            
        self._session.rollback = rollback
        
        with self.assertRaises(FantasticoDbError):
            self._facade.count_records(self._model_filter)

        self.assertTrue(self._rollbacked)