def test_book_search_title(self, mock_search_form_init, mock_render): """The book search view should render with title search results when searching and no search_in is provided.""" self.load_csv() mock_request = mock.MagicMock(name='request', spec=HttpRequest) mock_request.GET = {'search': 'keras'} # this makes it easier to use assert_calls_with since we know what the 'form' item is mock_search_form_init.return_value = SearchForm(mock_request.GET) resp = book_search(mock_request) self.assertEquals(resp, mock_render.return_value) mock_search_form_init.assert_called_with(mock_request.GET) self.assertEquals(mock_render.call_count, 1) self.assertEquals(mock_render.call_args[0][0], mock_request) self.assertEquals(mock_render.call_args[0][1], 'reviews/search-results.html') self.assertEquals(mock_render.call_args[0][2]['form'], mock_search_form_init.return_value) self.assertEquals(mock_render.call_args[0][2]['search_text'], 'keras') self.assertEquals(len(mock_render.call_args[0][2]['books']), 1) for book in mock_render.call_args[0][2]['books']: self.assertEquals(book.title, 'Advanced Deep Learning with Keras')
def test_book_search_contributor(self, mock_search_form_init, mock_render): """The book search view should render with contributor name search results when search_in is contributor.""" self.load_csv() mock_request = mock.MagicMock(name='request', spec=HttpRequest) mock_request.GET = {'search': 'king', 'search_in': 'contributor'} # this makes it easier to use assert_calls_with since we know what the 'form' item is mock_search_form_init.return_value = SearchForm(mock_request.GET) resp = book_search(mock_request) self.assertEquals(resp, mock_render.return_value) mock_search_form_init.assert_called_with(mock_request.GET) self.assertEquals(mock_render.call_count, 1) self.assertEquals(mock_render.call_args[0][0], mock_request) self.assertEquals(mock_render.call_args[0][1], 'reviews/search-results.html') self.assertEquals(mock_render.call_args[0][2]['form'], mock_search_form_init.return_value) self.assertEquals(mock_render.call_args[0][2]['search_text'], 'king') self.assertEquals(len(mock_render.call_args[0][2]['books']), 1) for book in mock_render.call_args[0][2]['books']: self.assertEquals(book.title, 'The Talisman')
def test_book_search_without_input(self, mock_search_form_init, mock_render): """The book search view should render with an empty set if no input is provided.""" mock_request = mock.MagicMock(name='request', spec=HttpRequest) mock_search_form_init.return_value.is_valid.return_value = False mock_request.GET = {} resp = book_search(mock_request) self.assertEquals(resp, mock_render.return_value) mock_search_form_init.assert_called_with(mock_request.GET) mock_render.assert_called_with( mock_request, 'reviews/search-results.html', { 'form': mock_search_form_init.return_value, 'search_text': '', 'books': set() })