示例#1
0
    def test_get_valid_id(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database
        """
        # use return value
        self.mock_db_instance.get_book.return_value = {
            "book_id": "1",
            "author_name": "test_mock",
            "name": "name_mock"
        }
        self.mock_db_instance.get_author.return_value = {
            "name": "name_mock",
            "age": -1,
            "best_sellers": -5
        }

        # call the method
        get_book = GetBookAuthor()
        data = get_book.get_info(25)

        # asserts
        self.mock_db.assert_called_once_with(None, None)  # constructor
        self.mock_db_instance.get_book.assert_called_once_with(25)
        self.mock_db_instance.get_author.assert_called_once_with(ANY)
        self.assertEquals("name_mock", data['title'])
示例#2
0
    def test_get_valid_id_with_cm(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database.
        In this case we patch again using patcher as context manager to override setup.
        """
        # use return value
        with patch('modules.exercises.mod_11_testing.process.MyConnection'
                   ) as mock_db_class:
            # return value
            mock_db = MagicMock(name='mock_db_instance')
            mock_db_class.return_value = mock_db
            mock_db.get_book.return_value = {
                "book_id": "10",
                "author_name": "test__another_mock",
                "name": "name_another_mock"
            }
            mock_db.get_author.return_value = {
                "name": "name_another_mock",
                "age": -10,
                "best_sellers": -50
            }

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info(10)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals("name_another_mock", data['title'])
            self.assertEquals(0, self.mock_db.call_count)
    def test_get_valid_id_with_cm(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database.
        In this case we patch again using patcher as context manager to override setup.
        """
        # use return value
        with patch("modules.exercises.mod_11_testing.process.MyConnection") as mock_db_class:
            # return value
            mock_db = MagicMock(name="mock_db_instance")
            mock_db_class.return_value = mock_db
            mock_db.get_book.return_value = {
                "book_id": "10",
                "author_name": "test__another_mock",
                "name": "name_another_mock",
            }
            mock_db.get_author.return_value = {"name": "name_another_mock", "age": -10, "best_sellers": -50}

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info(10)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals("name_another_mock", data["title"])
            self.assertEquals(0, self.mock_db.call_count)
示例#4
0
    def test_get_valid_id_with_patch_object(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database.
        We do not patch imports in this test. We patch the already created class db property.
        """
        # call the method
        get_book = GetBookAuthor()
        # use return value
        with patch.object(get_book, 'db') as mock_db:
            # mock_db is already our mock, no import mock this time
            mock_db.get_book.return_value = {
                "book_id": "10",
                "author_name": "test__another_mock",
                "name": "name_object_mock"
            }
            mock_db.get_author.return_value = {
                "name": "name_another_mock",
                "age": -10,
                "best_sellers": -50
            }

            data = get_book.get_info(10)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals("name_object_mock", data['title'])
示例#5
0
    def test_get_all_books_failing_db(self):
        """
        TEst that database error is handled in our process module and we dont raise any exception up
        """
        with patch('modules.exercises.mod_11_testing.process.MyConnection') as con_class,\
            patch('modules.exercises.mod_11_testing.process.logger') as logger_mock:
            mock_db = MagicMock(name='db_mock')
            con_class.return_value = mock_db
            mock_db.get_book.side_effect = [
                                    {"book_id": "10", "author_name": "test__another_1", "name": "name_1"},
                                    {"book_id": "11", "author_name": "test__another_2", "name": "name_2"},
                            ]
            # Two books with the same author, we suppose that
            mock_db.get_author.side_effect = ConnectionError("authors collection not created")

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info_list(10, 11)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals(0, len(data))

            # logger is not called with constructor, so we access mock method directly
            logger_mock.error.assert_called_once_with("Conection with database lost")
示例#6
0
    def test_get_valid_id_not_mocking_should_not_sleep(self, sleep_mock):
        """
        In this test we dont mock library and call directly.
        We only mock internal sleep of library to run fast.
        """
        self.patcher.stop()

        # call the method
        get_book = GetBookAuthor()
        data = get_book.get_info(34)

        self.assertEquals("El hobbit", data['title'])
        sleep_mock.assert_called_once_with(ANY)
    def test_get_valid_id_not_mocking_should_not_sleep(self, sleep_mock):
        """
        In this test we dont mock library and call directly.
        We only mock internal sleep of library to run fast.
        """
        self.patcher.stop()

        # call the method
        get_book = GetBookAuthor()
        data = get_book.get_info(34)

        self.assertEquals("El hobbit", data["title"])
        sleep_mock.assert_called_once_with(ANY)
    def test_get_valid_id(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database
        """
        # use return value
        self.mock_db_instance.get_book.return_value = {"book_id": "1", "author_name": "test_mock", "name": "name_mock"}
        self.mock_db_instance.get_author.return_value = {"name": "name_mock", "age": -1, "best_sellers": -5}

        # call the method
        get_book = GetBookAuthor()
        data = get_book.get_info(25)

        # asserts
        self.mock_db.assert_called_once_with(None, None)  # constructor
        self.mock_db_instance.get_book.assert_called_once_with(25)
        self.mock_db_instance.get_author.assert_called_once_with(ANY)
        self.assertEquals("name_mock", data["title"])
    def test_get_valid_id_with_patch_object(self):
        """
        when database is responding property we should return a valid object combining the
        book and author from database.
        We do not patch imports in this test. We patch the already created class db property.
        """
        # call the method
        get_book = GetBookAuthor()
        # use return value
        with patch.object(get_book, 'db') as mock_db:
            # mock_db is already our mock, no import mock this time
            mock_db.get_book.return_value = {"book_id": "10", "author_name": "test__another_mock", "name": "name_object_mock"}
            mock_db.get_author.return_value = {"name": "name_another_mock", "age": -10, "best_sellers": -50}

            data = get_book.get_info(10)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals("name_object_mock", data['title'])
示例#10
0
    def test_get_all_books_work(self):
        """
        When database module is working we check that our process return a valid list
        """
        with patch('modules.exercises.mod_11_testing.process.MyConnection'
                   ) as con_class:
            mock_db = MagicMock(name='db_mock')
            con_class.return_value = mock_db
            mock_db.get_book.side_effect = [
                {
                    "book_id": "10",
                    "author_name": "test__another_1",
                    "name": "name_1"
                },
                {
                    "book_id": "11",
                    "author_name": "test__another_2",
                    "name": "name_2"
                },
            ]
            # Two books with the same author, we suppose that
            mock_db.get_author.return_value = {
                "name": "name_another_mock",
                "age": -10,
                "best_sellers": -50
            }

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info_list(10, 11)

            # asserts
            mock_db.get_book.assert_has_calls([call(10), call(11)])
            mock_db.get_author.assert_has_calls([call(ANY), call(ANY)])
            self.assertEquals(2, len(data))
            self.assertEquals("name_1", data[0]['title'])
            self.assertEquals("name_2", data[1]['title'])
示例#11
0
    def test_get_invalid_id_should_raise_exception(self):
        """
        when database raise a Connection exception (invalid id) we shold handle
        properly in our method and convert exception to generic Exception.
        """
        self.mock_db_instance.get_book.side_effect = ConnectionError("test")
        self.mock_db_instance.get_author.return_value = {
            "name": "name_mock",
            "age": -1,
            "best_sellers": -5
        }

        get_book = GetBookAuthor()

        self.assertRaises(Exception, get_book.get_info, -1)
示例#12
0
    def test_get_all_books_failing_db(self):
        """
        TEst that database error is handled in our process module and we dont raise any exception up
        """
        with patch('modules.exercises.mod_11_testing.process.MyConnection') as con_class,\
            patch('modules.exercises.mod_11_testing.process.logger') as logger_mock:
            mock_db = MagicMock(name='db_mock')
            con_class.return_value = mock_db
            mock_db.get_book.side_effect = [
                {
                    "book_id": "10",
                    "author_name": "test__another_1",
                    "name": "name_1"
                },
                {
                    "book_id": "11",
                    "author_name": "test__another_2",
                    "name": "name_2"
                },
            ]
            # Two books with the same author, we suppose that
            mock_db.get_author.side_effect = ConnectionError(
                "authors collection not created")

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info_list(10, 11)

            # asserts
            mock_db.get_book.assert_called_once_with(10)
            mock_db.get_author.assert_called_once_with(ANY)
            self.assertEquals(0, len(data))

            # logger is not called with constructor, so we access mock method directly
            logger_mock.error.assert_called_once_with(
                "Conection with database lost")
示例#13
0
    def test_get_all_books_work(self):
        """
        When database module is working we check that our process return a valid list
        """
        with patch('modules.exercises.mod_11_testing.process.MyConnection') as con_class:
            mock_db = MagicMock(name='db_mock')
            con_class.return_value = mock_db
            mock_db.get_book.side_effect = [
                                    {"book_id": "10", "author_name": "test__another_1", "name": "name_1"},
                                    {"book_id": "11", "author_name": "test__another_2", "name": "name_2"},
                            ]
            # Two books with the same author, we suppose that
            mock_db.get_author.return_value = {"name": "name_another_mock", "age": -10, "best_sellers": -50}

            # call the method
            get_book = GetBookAuthor()
            data = get_book.get_info_list(10, 11)

            # asserts
            mock_db.get_book.assert_has_calls([call(10), call(11)])
            mock_db.get_author.assert_has_calls([call(ANY), call(ANY)])
            self.assertEquals(2, len(data))
            self.assertEquals("name_1", data[0]['title'])
            self.assertEquals("name_2", data[1]['title'])