def test_with_an_ordering(self, monkeypatch, db_request, page, order, expected): params = MultiDict({"q": "foo bar"}) if page is not None: params["page"] = page if order is not None: params["o"] = order db_request.params = params sort = pretend.stub() suggest = pretend.stub( sort=pretend.call_recorder(lambda *a, **kw: sort), ) es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: suggest), ) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query) ) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) assert search(db_request) == { "page": page_obj, "term": params.get("q", ''), "order": params.get("o", ''), "applied_filters": [], "available_filters": [], } assert page_cls.calls == [ pretend.call( sort if order is not None else suggest, url_maker=url_maker, page=page or 1, ), ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call( views.gather_es_queries(params["q"]) ) ] assert es_query.suggest.calls == [ pretend.call( "name_suggestion", params["q"], term={"field": "name"}, ), ] assert suggest.sort.calls == [pretend.call(i) for i in expected]
def test_with_an_ordering(self, monkeypatch, db_request, metrics, page, order, expected): params = MultiDict({"q": "foo bar"}) if page is not None: params["page"] = page if order is not None: params["o"] = order db_request.params = params sort = pretend.stub() suggest = pretend.stub( sort=pretend.call_recorder(lambda *a, **kw: sort)) es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: suggest)) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query)) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) assert search(db_request) == { "page": page_obj, "term": params.get("q", ""), "order": params.get("o", ""), "applied_filters": [], "available_filters": [], } assert page_cls.calls == [ pretend.call( sort if order is not None else suggest, url_maker=url_maker, page=page or 1, ) ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call(views.gather_es_queries(params["q"])) ] assert es_query.suggest.calls == [ pretend.call("name_suggestion", params["q"], term={"field": "name"}) ] assert suggest.sort.calls == [pretend.call(i) for i in expected] assert metrics.histogram.calls == [ pretend.call("warehouse.views.search.results", 1000) ]
def test_with_a_single_char_query(self, monkeypatch, db_request, page): params = MultiDict({"q": "a"}) if page is not None: params["page"] = page db_request.params = params sort = pretend.stub() suggest = pretend.stub( sort=pretend.call_recorder(lambda *a, **kw: sort), ) es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: suggest), ) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query) ) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) assert search(db_request) == { "page": page_obj, "term": params.get("q", ''), "order": params.get("o", ''), "applied_filters": [], "available_filters": [], } assert page_cls.calls == [ pretend.call(suggest, url_maker=url_maker, page=page or 1), ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call( views.gather_es_queries(params["q"]) ) ] assert es_query.suggest.calls == [ pretend.call( "name_suggestion", params["q"], term={"field": "name"}, ), ] assert db_request.registry.datadog.histogram.calls == [ pretend.call('warehouse.views.search.results', 1000) ]
def test_with_a_single_char_query(self, monkeypatch, db_request, page): params = MultiDict({"q": "a"}) if page is not None: params["page"] = page db_request.params = params sort = pretend.stub() suggest = pretend.stub( sort=pretend.call_recorder(lambda *a, **kw: sort), ) es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: suggest), ) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query)) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) assert search(db_request) == { "page": page_obj, "term": params.get("q", ''), "order": params.get("o", ''), "applied_filters": [], "available_filters": [], } assert page_cls.calls == [ pretend.call(suggest, url_maker=url_maker, page=page or 1), ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call(views.gather_es_queries(params["q"])) ] assert es_query.suggest.calls == [ pretend.call( "name_suggestion", params["q"], term={"field": "name"}, ), ] assert db_request.registry.datadog.histogram.calls == [ pretend.call('warehouse.views.search.results', 1000) ]
def test_with_classifiers(self, monkeypatch, db_request, page): params = MultiDict([ ("q", "foo bar"), ("c", "foo :: bar"), ("c", "fiz :: buz"), ]) if page is not None: params["page"] = page db_request.params = params es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: es_query), filter=pretend.call_recorder(lambda *a, **kw: es_query), sort=pretend.call_recorder(lambda *a, **kw: es_query), ) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query)) classifier1 = ClassifierFactory.create(classifier="foo :: bar") classifier2 = ClassifierFactory.create(classifier="foo :: baz") classifier3 = ClassifierFactory.create(classifier="fiz :: buz") project = ProjectFactory.create() release1 = ReleaseFactory.create(project=project) release1.created = datetime.date(2011, 1, 1) release1._classifiers.append(classifier1) release1._classifiers.append(classifier2) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) search_view = search(db_request) assert search_view == { "page": page_obj, "term": params.get("q", ''), "order": params.get("o", ''), "applied_filters": params.getall("c"), "available_filters": [('foo', [ classifier1.classifier, classifier2.classifier, ])], } assert (("fiz", [classifier3.classifier]) not in search_view["available_filters"]) assert page_cls.calls == [ pretend.call(es_query, url_maker=url_maker, page=page or 1), ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call(views.gather_es_queries(params["q"])) ] assert es_query.suggest.calls == [ pretend.call( "name_suggestion", params["q"], term={"field": "name"}, ), ] assert es_query.filter.calls == [ pretend.call('terms', classifiers=['foo :: bar']), pretend.call('terms', classifiers=['fiz :: buz']) ]
def test_with_classifiers(self, monkeypatch, db_request, page): params = MultiDict([ ("q", "foo bar"), ("c", "foo :: bar"), ("c", "fiz :: buz"), ]) if page is not None: params["page"] = page db_request.params = params es_query = pretend.stub( suggest=pretend.call_recorder(lambda *a, **kw: es_query), filter=pretend.call_recorder(lambda *a, **kw: es_query), sort=pretend.call_recorder(lambda *a, **kw: es_query), ) db_request.es = pretend.stub( query=pretend.call_recorder(lambda *a, **kw: es_query) ) classifier1 = ClassifierFactory.create(classifier="foo :: bar") classifier2 = ClassifierFactory.create(classifier="foo :: baz") classifier3 = ClassifierFactory.create(classifier="fiz :: buz") project = ProjectFactory.create() release1 = ReleaseFactory.create(project=project) release1.created = datetime.date(2011, 1, 1) release1._classifiers.append(classifier1) release1._classifiers.append(classifier2) page_obj = pretend.stub(page_count=(page or 1) + 10, item_count=1000) page_cls = pretend.call_recorder(lambda *a, **kw: page_obj) monkeypatch.setattr(views, "ElasticsearchPage", page_cls) url_maker = pretend.stub() url_maker_factory = pretend.call_recorder(lambda request: url_maker) monkeypatch.setattr(views, "paginate_url_factory", url_maker_factory) search_view = search(db_request) assert search_view == { "page": page_obj, "term": params.get("q", ''), "order": params.get("o", ''), "applied_filters": params.getall("c"), "available_filters": [ ('foo', [ classifier1.classifier, classifier2.classifier, ]) ], } assert ( ("fiz", [ classifier3.classifier ]) not in search_view["available_filters"] ) assert page_cls.calls == [ pretend.call(es_query, url_maker=url_maker, page=page or 1), ] assert url_maker_factory.calls == [pretend.call(db_request)] assert db_request.es.query.calls == [ pretend.call( views.gather_es_queries(params["q"]) ) ] assert es_query.suggest.calls == [ pretend.call( "name_suggestion", params["q"], term={"field": "name"}, ), ] assert es_query.filter.calls == [ pretend.call('terms', classifiers=['foo :: bar']), pretend.call('terms', classifiers=['fiz :: buz']) ]