示例#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()
示例#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()
示例#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()
示例#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)
示例#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()
示例#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()
示例#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)
示例#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)
示例#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()
示例#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()
示例#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()
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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()
示例#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()
示例#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()
示例#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()
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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
        ....
示例#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
        ....