Ejemplo n.º 1
0
    def test_get_not_found_with_decorator(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.get = MagicMock(side_effect=PersistenceError())

        user_service.get_user()
Ejemplo n.º 2
0
    def test_get_not_found_with_decorator(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.get = MagicMock(side_effect=PersistenceError())

        user_service.get_user()
Ejemplo n.º 3
0
    def test_get_unhandled_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.get = MagicMock(side_effect=ValueError())

        with self.assertRaises(ValueError):
            user_service.get_user()
Ejemplo n.º 4
0
    def test_update_user_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben", "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)

        response = user_service.update_user()

        self.assertEqual(202, response.status_int)
Ejemplo n.º 5
0
    def test_update_user_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben", "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)
        user_service._dao.update = Mock(side_effect=PersistenceError())

        with self.assertRaises(HTTPNotFound):
            user_service.update_user()
Ejemplo n.º 6
0
    def test_delete_user_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=Mock)
        user_service._dao.delete = Mock(side_effect=PersistenceError())

        with self.assertRaises(HTTPNotFound):
            user_service.delete_user()
Ejemplo n.º 7
0
    def test_delete_user_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=Mock)

        response = user_service.delete_user()

        self.assertEqual(204, response.status_int)
Ejemplo n.º 8
0
    def test_delete_user_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"
        user_service = UserServiceRest(None, request, dao=Mock)

        response = user_service.delete_user()

        self.assertEqual(204, response.status_int)
Ejemplo n.º 9
0
    def test_delete_user_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"
        user_service = UserServiceRest(None, request, dao=Mock)
        user_service._dao.delete = Mock(side_effect=PersistenceError())

        with self.assertRaises(HTTPNotFound):
            user_service.delete_user()
Ejemplo n.º 10
0
    def test_get_unhandled_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.get = MagicMock(side_effect=ValueError())

        with self.assertRaises(ValueError):
            user_service.get_user()
Ejemplo n.º 11
0
    def test_update_user_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben",
                             "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)
        user_service._dao.update = Mock(side_effect=PersistenceError())

        with self.assertRaises(HTTPNotFound):
            user_service.update_user()
Ejemplo n.º 12
0
    def test_update_user_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben",
                             "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)

        response = user_service.update_user()

        self.assertEqual(202, response.status_int)
Ejemplo n.º 13
0
    def test_add_user_db_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben",
                             "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.add = MagicMock(side_effect=sqlite3.DatabaseError())

        with self.assertRaisesRegex(HTTPInternalServerError, r'Could not add'):
            user_service.add_user()
        user_service._dao.add.assert_called_with(ANY, ANY)
Ejemplo n.º 14
0
    def test_get_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        person = Person(id=123)
        user_service._dao.get.return_value = person

        result = user_service.get_user()

        self.assertEqual(result, person)
Ejemplo n.º 15
0
    def test_get_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=MagicMock)
        person = Person(id=123)
        user_service._dao.get.return_value = person

        result = user_service.get_user()

        self.assertEqual(result, person)
Ejemplo n.º 16
0
    def test_add_user_db_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben",
                             "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=MagicMock)
        user_service._dao.add = MagicMock(side_effect=sqlite3.DatabaseError())

        with self.assertRaisesRegex(HTTPInternalServerError, r'Could not add'):
            user_service.add_user()
        user_service._dao.add.assert_called_with(ANY, ANY)
Ejemplo n.º 17
0
    def test_get_not_found_with_decorator(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: note that we program the mock DAO's get() method to raise
        # a PersistenceError
        # (no code change required)
        user_service._dao.get = Mock(side_effect=PersistenceError())

        # TODO: call the user_service get_user() method.
        user_service.get_user()
Ejemplo n.º 18
0
    def test_get_not_found_with_decorator(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: note that we program the mock DAO's get() method to raise
        # a PersistenceError
        # (no code change required)
        user_service._dao.get = Mock(side_effect=PersistenceError())

        # TODO: call the user_service get_user() method.
        user_service.get_user()
Ejemplo n.º 19
0
    def test_get_unhandled_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"

        # TODO: pass Mock as the DAO class to the UserServiceRest
        # constructor.
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: program the mock DAO's get() method to have a side effect of
        # raising a ValueError.
        user_service._dao.get = Mock(side_effect=ValueError())

        # TODO: assert that a ValueError is raised when you call
        # the user_service's get_user() method.
        with self.assertRaises(ValueError):
            user_service.get_user()
Ejemplo n.º 20
0
    def test_get_unhandled_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'

        # TODO: pass Mock as the DAO class to the UserServiceRest
        # constructor.
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: program the mock DAO's get() method to have a side effect of
        # raising a ValueError.
        user_service._dao.get = Mock(side_effect=ValueError())

        # TODO: assert that a ValueError is raised when you call
        # the user_service's get_user() method.
        with self.assertRaises(ValueError):
            user_service.get_user()
Ejemplo n.º 21
0
    def test_add_user_db_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben", "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: note the mock DAO's add() method will raise a DatabaseError.
        # (no code change required)
        user_service._dao.add = Mock(side_effect=sqlite3.DatabaseError())

        # TODO: assert that an HTTPInternalServerError is raised when you call
        # the user_service's add_user() method, and that the exception's
        # message includes the string 'Could not add'
        with self.assertRaisesRegex(HTTPInternalServerError, r"Could not add"):
            user_service.add_user()

        # Assert that the mock DAO's add() method was called once, with
        # any two arguments.
        user_service._dao.add.assert_called_once(ANY, ANY)
Ejemplo n.º 22
0
    def test_get_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"

        # TODO: note that we pass Mock as the DAO class to the
        # UserServiceRest constructor.
        # (no code change required)
        user_service = UserServiceRest(None, request, dao=Mock)

        person = Person(id=123)

        # TODO: note how we access the DAO from the UserServiceRest instance
        # after the Mock DAO has been set. Here, we program the Mock so that
        # its get() method returns a reference to the Person created above.
        # (no code change required)
        user_service._dao.get.return_value = person

        result = user_service.get_user()

        self.assertEqual(result, person)
Ejemplo n.º 23
0
    def test_get_success(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'

        # TODO: note that we pass Mock as the DAO class to the
        # UserServiceRest constructor.
        # (no code change required)
        user_service = UserServiceRest(None, request, dao=Mock)

        person = Person(id=123)

        # TODO: note how we access the DAO from the UserServiceRest instance
        # after the Mock DAO has been set. Here, we program the Mock so that
        # its get() method returns a reference to the Person created above.
        # (no code change required)
        user_service._dao.get.return_value = person

        result = user_service.get_user()

        self.assertEqual(result, person)
Ejemplo n.º 24
0
    def test_get_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict["email"] = "*****@*****.**"

        # TODO: pass Mock as the DAO class to the UserServiceRest
        # constructor.
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: configure the mock so that a call to its get() method has
        # the side effect of raising a PersistenceError
        user_service._dao.get.side_effect = PersistenceError()

        # TODO: assert that an HTTPNotFound exception is raised when you call
        # the user_service's get_user() method.
        with self.assertRaises(HTTPNotFound):
            user_service.get_user()

        # assert_called_with() verifies that the mock DAO's add() method was
        # called once. ANY can be used as a placeholder so the mock doesn't
        # test the argument values.
        user_service._dao.add.assert_called_once(ANY, ANY)
Ejemplo n.º 25
0
    def test_get_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'

        # TODO: pass Mock as the DAO class to the UserServiceRest
        # constructor.
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: configure the mock so that a call to its get() method has
        # the side effect of raising a PersistenceError
        user_service._dao.get.side_effect = PersistenceError()

        # TODO: assert that an HTTPNotFound exception is raised when you call
        # the user_service's get_user() method.
        with self.assertRaises(HTTPNotFound):
            user_service.get_user()

        # assert_called_with() verifies that the mock DAO's add() method was
        # called once. ANY can be used as a placeholder so the mock doesn't
        # test the argument values.
        user_service._dao.add.assert_called_once(ANY, ANY)
Ejemplo n.º 26
0
    def test_add_user_db_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {
            "id": "1",
            "first_name": "Ben",
            "last_name": "Franklin",
            "email": "*****@*****.**"
        }
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: note the mock DAO's add() method will raise a DatabaseError.
        # (no code change required)
        user_service._dao.add = Mock(side_effect=sqlite3.DatabaseError())

        # TODO: assert that an HTTPInternalServerError is raised when you call
        # the user_service's add_user() method, and that the exception's
        # message includes the string 'Could not add'
        with self.assertRaisesRegex(HTTPInternalServerError, r'Could not add'):
            user_service.add_user()

        # Assert that the mock DAO's add() method was called once, with
        # any two arguments.
        user_service._dao.add.assert_called_once(ANY, ANY)
Ejemplo n.º 27
0
    def test_add_user_db_exception(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.json_body = {"id": "1", "first_name": "Ben",
                             "last_name": "Franklin", "email": "*****@*****.**"}
        user_service = UserServiceRest(None, request, dao=Mock)

        # TODO: note the mock DAO's add() method will raise a DatabaseError.
        # (no code change required)
        user_service._dao.add = Mock(side_effect=sqlite3.DatabaseError())

        # TODO: assert that an HTTPInternalServerError is raised when you call
        # the user_service's add_user() method, and that the exception's
        # message includes the string 'Could not add'
        # HINT: see slide 3-39
        ....
Ejemplo n.º 28
0
    def test_get_not_found(self):
        request = pyramid_testing.DummyRequest()
        request.db_session = None
        request.matchdict['email'] = '*****@*****.**'

        # TODO: pass Mock as the DAO class to the UserServiceRest
        # constructor.
        user_service = UserServiceRest(None, request, dao=....)

        # TODO: configure the mock so that a call to its get() method has
        # the side effect of raising a PersistenceError
        # HINT: see slide 3-39
        ....

        # TODO: assert that an HTTPNotFound exception is raised when you call
        # the user_service's get_user() method.
        # HINT: see slide 3-38
        ....