def test_search_calls_feature(logic, get_user): """It should call request.feature() once with the right args.""" request = mock.Mock() views.search(request) request.feature.assert_called_once_with('search_normalized')
def test_search_calls_get_user(get_user): """It should call get_user() once passing the request.""" request = mock.Mock() views.search(request) get_user.assert_called_once_with(request)
def test_search_calls_get_user(logic, get_user): """It should call get_user() once with the right args.""" request = mock.Mock() views.search(request) get_user.assert_called_once_with(request)
def test_search_passes_params_to_search(search_lib): """It should pass request.params to search_lib.search().""" request = mock.Mock() views.search(request) assert search_lib.search.call_args[1]['request_params'] == request.params
def test_search_calls_feature(): """It should call request.feature() once, passing 'search_normalized'.""" request = mock.Mock() views.search(request) request.feature.assert_called_once_with('search_normalized')
def test_search_searches(search_lib): request = testing.DummyRequest() views.search(request) search_lib.search.assert_called_once_with( request, request.params, separate_replies=False)
def test_search_passes_search_normalized_uris(search_lib): """It should pass search_normalized from request.feature() to search().""" request = mock.Mock() views.search(request) assert search_lib.search.call_args[1]['search_normalized_uris'] == ( request.feature.return_value)
def test_it_loads_annotations_from_database(self, mock_request, search_lib, storage): search_lib.search.return_value = {'total': 2, 'rows': [{'id': 'row-1'}, {'id': 'row-2'}]} views.search(mock_request) storage.fetch_ordered_annotations.assert_called_once_with( mock_request.db, ['row-1', 'row-2'], load_documents=True)
def test_search_searches(search_lib): request = testing.DummyRequest() views.search(request) search_lib.search.assert_called_once_with(request, request.params, separate_replies=False)
def test_search_calls_search_annotations(logic, get_user): """It should call search_annotations() once with the right args.""" request = mock.Mock() views.search(request) logic.search_annotations.assert_called_once_with( request.params, get_user.return_value, request.feature.return_value)
def test_it_loads_replies_from_database(self, mock_request, search_lib, storage): mock_request.params = {'_separate_replies': '1'} search_lib.search.return_value = {'total': 1, 'rows': [{'id': 'row-1'}], 'replies': [{'id': 'reply-1'}, {'id': 'reply-2'}]} views.search(mock_request) assert mock.call(mock_request.db, ['reply-1', 'reply-2'], load_documents=True) in storage.fetch_ordered_annotations.call_args_list
def test_it_renders_replies(self, mock_request, search_lib): ann = models.Annotation(userid='luke') mock_request.db.add(ann) mock_request.db.flush() reply1 = models.Annotation(userid='sarah', references=[ann.id]) reply2 = models.Annotation(userid='sarah', references=[ann.id]) mock_request.db.add_all([reply1, reply2]) mock_request.db.flush() search_lib.search.return_value = {'total': 1, 'rows': [{'id': ann.id}], 'replies': [{'id': reply1.id}, {'id': reply2.id}], } mock_request.params = {'_separate_replies': '1'} expected = { 'total': 1, 'rows': [presenters.AnnotationJSONPresenter(mock_request, ann).asdict()], 'replies': [ presenters.AnnotationJSONPresenter(mock_request, reply1).asdict(), presenters.AnnotationJSONPresenter(mock_request, reply2).asdict(), ] } assert views.search(mock_request) == expected
def test_search_returns_search_results(search_lib): request = testing.DummyRequest() search_lib.search.return_value = {'total': 0, 'rows': []} result = views.search(request) assert result == {'total': 0, 'rows': []}
def test_search_presents_replies(search_lib, AnnotationJSONPresenter): request = testing.DummyRequest(params={'_separate_replies': '1'}) search_lib.search.return_value = { 'total': 1, 'rows': [{ 'foo': 'bar' }], 'replies': [{ 'baz': 'bat' }, { 'baz': 'bat' }] } presenter = AnnotationJSONPresenter.return_value presenter.asdict.return_value = {'giraffe': True} result = views.search(request) assert result == { 'total': 1, 'rows': [{ 'giraffe': True }], 'replies': [{ 'giraffe': True }, { 'giraffe': True }] }
def test_it_returns_search_results(self, search_lib): request = testing.DummyRequest() search_lib.search.return_value = {'total': 0, 'rows': []} result = views.search(request) assert result == {'total': 0, 'rows': []}
def test_search_renders_replies(search_lib): search_lib.search.return_value = { 'total': 3, 'rows': ['parent_annotation'], 'replies': ['a', 'b', 'c'] } search_lib.render.side_effect = lambda x: x.upper() result = views.search(mock.Mock()) assert result['replies'] == ['A', 'B', 'C']
def test_search_returns_total(search_lib): """It should return the total from search_lib.search().""" search_lib.search.return_value = { 'total': 3, # In production these would be annotation dicts, not strings. 'rows': ['annotation_1', 'annotation_2', 'annotation_3'] } response_data = views.search(mock.Mock()) assert response_data['total'] == 3
def test_search_renders_results(search_lib): request = testing.DummyRequest() search_lib.search.return_value = {'total': 3, 'rows': ['a', 'b', 'c']} search_lib.render.side_effect = lambda x: x.upper() result = views.search(request) assert result == { 'total': 3, 'rows': ['A', 'B', 'C'], }
def test_search_presents_annotations(search_lib, AnnotationJSONPresenter): request = testing.DummyRequest() search_lib.search.return_value = {'total': 2, 'rows': [{'foo': 'bar'}, {'baz': 'bat'}]} presenter = AnnotationJSONPresenter.return_value presenter.asdict.return_value = {'giraffe': True} result = views.search(request) assert result == {'total': 2, 'rows': [{'giraffe': True}, {'giraffe': True}]}
def test_it_presents_annotations(self, pyramid_request, search_lib, AnnotationJSONPresenter): search_lib.search.return_value = {'total': 2, 'rows': [{'foo': 'bar'}, {'baz': 'bat'}]} presenter = AnnotationJSONPresenter.return_value presenter.asdict.return_value = {'giraffe': True} result = views.search(pyramid_request) assert result == {'total': 2, 'rows': [{'giraffe': True}, {'giraffe': True}]}
def test_search_renders_results(search_lib): request = testing.DummyRequest() search_lib.search.return_value = { 'total': 3, 'rows': ['a', 'b', 'c'], } search_lib.render.side_effect = lambda x: x.upper() result = views.search(request) assert result == { 'total': 3, 'rows': ['A', 'B', 'C'], }
def test_it_presents_replies(self, search_lib, AnnotationJSONPresenter): request = testing.DummyRequest(params={'_separate_replies': '1'}) search_lib.search.return_value = {'total': 1, 'rows': [{'foo': 'bar'}], 'replies': [{'baz': 'bat'}, {'baz': 'bat'}]} presenter = AnnotationJSONPresenter.return_value presenter.asdict.return_value = {'giraffe': True} result = views.search(request) assert result == {'total': 1, 'rows': [{'giraffe': True}], 'replies': [{'giraffe': True}, {'giraffe': True}]}
def test_it_renders_search_results(self, mock_request, search_lib): ann1 = models.Annotation(userid='luke') ann2 = models.Annotation(userid='sarah') mock_request.db.add_all([ann1, ann2]) mock_request.db.flush() search_lib.search.return_value = {'total': 2, 'rows': [{'id': ann1.id}, {'id': ann2.id}]} expected = { 'total': 2, 'rows': [ presenters.AnnotationJSONPresenter(mock_request, ann1).asdict(), presenters.AnnotationJSONPresenter(mock_request, ann2).asdict(), ] } assert views.search(mock_request) == expected
def test_search_returns_rendered_annotations(search_lib): """It should return the rendered annotations. It should pass the annotations from search_lib.search() through search_lib.render() and return the results. """ search_lib.search.return_value = { 'total': 3, # In production these would be annotation dicts, not strings. 'rows': ['annotation_1', 'annotation_2', 'annotation_3'] } # Our mock render function just appends '_rendered' onto our mock # annotation strings. search_lib.render.side_effect = lambda annotation: annotation + '_rendered' response_data = views.search(mock.Mock()) assert response_data['rows'] == [ 'annotation_1_rendered', 'annotation_2_rendered', 'annotation_3_rendered']
def test_search_returns_rendered_annotations(search_lib): """It should return the rendered annotations. It should pass the annotations from search_lib.search() through search_lib.render() and return the results. """ search_lib.search.return_value = { 'total': 3, # In production these would be annotation dicts, not strings. 'rows': ['annotation_1', 'annotation_2', 'annotation_3'] } # Our mock render function just appends '_rendered' onto our mock # annotation strings. search_lib.render.side_effect = lambda annotation: annotation + '_rendered' response_data = views.search(mock.Mock()) assert response_data['rows'] == [ 'annotation_1_rendered', 'annotation_2_rendered', 'annotation_3_rendered' ]
def test_it_presents_annotations(self, search_lib, AnnotationJSONPresenter): request = testing.DummyRequest() search_lib.search.return_value = { 'total': 2, 'rows': [{ 'foo': 'bar' }, { 'baz': 'bat' }] } presenter = AnnotationJSONPresenter.return_value presenter.asdict.return_value = {'giraffe': True} result = views.search(request) assert result == { 'total': 2, 'rows': [{ 'giraffe': True }, { 'giraffe': True }] }
def test_search_passes_user_to_search(get_user, search_lib): """It should pass the user from get_user() to search_lib.search().""" views.search(mock.Mock()) assert search_lib.search.call_args[1]['user'] == get_user.return_value
def test_search_calls_search(search_lib): """It should call search_lib.search() once.""" views.search(mock.Mock()) assert search_lib.search.call_count == 1
def test_it_searches(self, mock_request, search_lib): views.search(mock_request) search_lib.search.assert_called_once_with(mock_request, mock_request.params, separate_replies=False)
def test_search_returns_search_results(search_lib): request = testing.DummyRequest() result = views.search(request) assert result == search_lib.search.return_value
def test_it_returns_search_results(self, pyramid_request, search_lib): search_lib.search.return_value = {'total': 0, 'rows': []} result = views.search(pyramid_request) assert result == {'total': 0, 'rows': []}
def test_search_returns_search_annotations(logic, get_user): """It should return what search_annotations() returns.""" assert views.search(mock.Mock()) == logic.search_annotations.return_value