def mock_perform_es_search_empty(): return response.Response( search.Search(), {"hits": { "total": 0, "max_score": 'null', "hits": [] }})
def test_attribute_error_in_hits_is_not_hidden(dummy_response): def f(hit): raise AttributeError() s = Search().doc_type(employee=f) r = response.Response(s, dummy_response) with raises(TypeError): r.hits
def test_response_is_pickleable(dummy_response): res = response.Response(Search(), dummy_response) res.hits r = pickle.loads(pickle.dumps(res)) assert r == res assert r._search == res._search assert r.hits == res.hits
def test_bucket_keys_get_deserialized(aggs_data, aggs_search): class Commit(DocType): info = Object(properties={'committed_date': Date()}) aggs_search._doc_type_map = {'commit': Commit} agg_response = response.Response(aggs_search, aggs_data) per_month = agg_response.aggregations.per_month for b in per_month: assert isinstance(b.key, date)
def test_bucket_keys_get_deserialized(aggs_data, aggs_search): class Commit(Document): info = Object(properties={'committed_date': Date()}) class Index: name = 'test-commit' aggs_search = aggs_search.doc_type(Commit) agg_response = response.Response(aggs_search, aggs_data) per_month = agg_response.aggregations.per_month for b in per_month: assert isinstance(b.key, date)
def test_hits_provide_dot_and_bracket_access_to_attrs(dummy_response): res = response.Response(Search(), dummy_response) h = res.hits[0] assert 'Elasticsearch' == h.name assert 'Elasticsearch' == h['name'] assert 'Honza' == res.hits[2].name.first with raises(KeyError): h['not_there'] with raises(AttributeError): h.not_there
def test_interactive_helpers(dummy_response): res = response.Response(Search(), dummy_response) hits = res.hits h = hits[0] rhits = "[<Hit(test-index/company/elasticsearch): {}>, <Hit(test-index/employee/42): {}...}}>, <Hit(test-index/employee/47): {}...}}>, <Hit(test-index/employee/53): {{}}>]".format( repr(dummy_response['hits']['hits'][0]['_source']), repr(dummy_response['hits']['hits'][1]['_source'])[:60], repr(dummy_response['hits']['hits'][2]['_source'])[:60], ) assert res assert '<Response: %s>' % rhits == repr(res) assert rhits == repr(hits) assert {'meta', 'city', 'name'} == set(dir(h)) assert "<Hit(test-index/company/elasticsearch): %r>" % dummy_response['hits']['hits'][0]['_source'] == repr(h)
def test_iterating_over_response_gives_you_hits(dummy_response): res = response.Response(Search(), dummy_response) hits = list(h for h in res) assert res.success() assert 123 == res.took assert 4 == len(hits) assert all(isinstance(h, response.Hit) for h in hits) h = hits[0] assert 'test-index' == h.meta.index assert 'company' == h.meta.doc_type assert 'elasticsearch' == h.meta.id assert 12 == h.meta.score assert hits[1].meta.parent == 'elasticsearch'
def mock_perform_es_search(): return response.Response( search.Search(), { "hits": { "hits": [ { "_index": "records-hep", "_type": "hep", "_id": "elasticsearch", "_score": 12.0, "_source": { "title": "Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "42", "_score": 11.123, "_source": { "title": "Another Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "47", "_score": 1, "_source": { "title": "And another Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "53", "_score": 16.0, }, ], "max_score": 12.0, "total": 10 }, "timed_out": False, "took": 123 })
def test_interactive_helpers(dummy_response): res = response.Response(Search(), dummy_response) hits = res.hits h = hits[0] rhits = ( "[<Hit(test-index/elasticsearch): {}>, <Hit(test-index/42): {}...}}>, " "<Hit(test-index/47): {}...}}>, <Hit(test-index/53): {{}}>]").format( repr(dummy_response["hits"]["hits"][0]["_source"]), repr(dummy_response["hits"]["hits"][1]["_source"])[:60], repr(dummy_response["hits"]["hits"][2]["_source"])[:60], ) assert res assert "<Response: %s>" % rhits == repr(res) assert rhits == repr(hits) assert {"meta", "city", "name"} == set(dir(h)) assert "<Hit(test-index/elasticsearch): %r>" % dummy_response["hits"][ "hits"][0]["_source"] == repr(h)
def mock_perform_es_search_onerecord(): return response.Response( search.Search(), { "hits": { "hits": [{ "_index": "records-hep", "_type": "hep", "_id": "42", "_score": 11.123, "_source": { "control_number": 1410174 } }], "max_score": 11.123, "total": 1 }, "timed_out": False, "took": 123 })
def agg_response(aggs_search, aggs_data): return response.Response(aggs_search, aggs_data)
def test_len_response(dummy_response): res = response.Response(Search(), dummy_response) assert len(res) == 4
def test_empty_response_is_false(dummy_response): dummy_response['hits']['hits'] = [] res = response.Response(Search(), dummy_response) assert not res
def test_response_stores_search(dummy_response): s = Search() r = response.Response(s, dummy_response) assert r._search is s
def test_hit_is_pickleable(dummy_response): res = response.Response(Search(), dummy_response) hits = pickle.loads(pickle.dumps(res.hits)) assert hits == res.hits
def test_slicing_on_response_slices_on_hits(dummy_response): res = response.Response(Search(), dummy_response) assert res[0] is res.hits[0] assert res[::-1] == res.hits[::-1]
def test_hits_get_wrapped_to_contain_additional_attrs(dummy_response): res = response.Response(Search(), dummy_response) hits = res.hits assert 123 == hits.total assert 12.0 == hits.max_score