def test_attribute_error_in_hits_is_not_hidden(dummy_response): def f(hit): raise AttributeError() r = result.Response(dummy_response, callbacks={'employee': f}) with raises(TypeError): r.hits
def mock_perform_es_search_tworecord(): return result.Response({ "hits": { "hits": [{ "_index": "records-hep", "_type": "hep", "_id": "42", "_score": 11.123, "_source": { "control_number": 1407068, 'dois': [{ 'source': 'Elsevier', 'value': u'10.1016/j.ppnp.2015.10.002' }] } }, { "_index": "records-hep", "_type": "hep", "_id": "43", "_score": 11.123, "_source": { "control_number": 1407079 } }], "max_score": 11.123, "total": 2 } })
def mock_perform_es_search_empty(): return result.Response( {"hits": { "total": 0, "max_score": 'null', "hits": [] }})
def test_hits_get_wrapped_to_contain_additional_attrs(dummy_response): res = result.Response(dummy_response) hits = res.hits assert isinstance(hits, result.Hits) assert 123 == hits.total assert 12.0 == hits.max_score
def test_hits_provide_dot_and_bracket_access_to_attrs(dummy_response): res = result.Response(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 mock_perform_es_search(): return result.Response( { "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_iterating_over_response_gives_you_hits(dummy_response): res = result.Response(dummy_response) hits = list(h for h in res) assert res.success() assert 123 == res.took assert 4 == len(hits) assert all(isinstance(h, result.Result) 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 test_interactive_helpers(dummy_response): res = result.Response(dummy_response) hits = res.hits h = hits[0] rhits = "[<Result(test-index/company/elasticsearch): %s>, <Result(test-index/employee/42): %s...}>, <Result(test-index/employee/47): %s...}>, <Result(test-index/employee/53): {}>]" % ( repr(dummy_response['hits']['hits'][0]['_source']), repr(dummy_response['hits']['hits'][1]['_source'])[:60], repr(dummy_response['hits']['hits'][2]['_source'])[:60], ) assert '<Response: %s>' % rhits == repr(res) assert rhits == repr(hits) assert ['_meta', 'city', 'name'] == dir(h) assert "<Result(test-index/company/elasticsearch): %r>" % dummy_response[ 'hits']['hits'][0]['_source'] == repr(h)
def mock_perform_es_search_onerecord(): return result.Response({ "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 test_slicing_on_response_slices_on_hits(dummy_response): res = result.Response(dummy_response) assert res[0] is res.hits[0] assert res[::-1] == res.hits[::-1]
def test_enpty_response_is_false(dummy_response): dummy_response['hits']['hits'] = [] res = result.Response(dummy_response) assert not res
def no_searches(mocker, dummy_response): mocker.patch('elasticsearch_dsl.Search.execute', return_value=result.Response(dummy_response))
def _resp_maker(hits=[], aggregations={}): dummy_response['hits']['hits'] = hits dummy_response['aggregations'] = aggregations return ESResponse(result.Response(dummy_response))
def test_len_response(dummy_response): res = result.Response(dummy_response) assert len(dummy_response) == 4