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(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'])
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_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'])