def test_wrappers(context): """ Search with wrapped match_all query """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) # And I do a search def wrapper_function(search_results): return map(lambda x: x["_source"]["bar"] + 1, search_results) t.wrappers(wrapper_function) t.order_by(Sort("bar", order="asc")) results = t[0:10] # Then I get a the expected results len(results).should.equal(3) results[0].should.equal(2) results[1].should.equal(3) results[2].should.equal(4)
def test_queryset_iteration_with_no_results(): """ Fetch results with QuerySet via __iter__ with no results """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have no records response = { "took": 12, "hits": { "total": 0, "max_score": 0, "hits": [] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = [] for result in t: results.append(result) len(results).should.equal(0) t.count().should.equal(0)
def test_queryset_iteration(): """ Fetch results with QuerySet via __iter__ """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have a record response = { "took": 12, "hits": { "total": 1, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = [] for result in t: results.append(result) len(results).should.equal(1) len(t).should.equal(1) t.count().should.equal(1)
def test_queryset_getitem_multiple(): """ Fetch from QuerySet with __getitem__ multiple times """ # When I create a query block t = QuerySet("localhost", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have a record response = { "took": 12, "hits": { "total": 1, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = t[0:1] len(results).should.equal(1) t.count().should.equal(1) results = t[0:1] len(results).should.equal(1) t.count().should.equal(1)
def test_wrappers(context): """ Search with wrapped match_all query """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) # And I do a search def wrapper_function(search_results): return list(map(lambda x: x['_source']['bar'] + 1, search_results)) t.wrappers(wrapper_function) t.order_by(Sort("bar", order="asc")) results = t[0:10] # Then I get a the expected results len(results).should.equal(3) results[0].should.equal(2) results[1].should.equal(3) results[2].should.equal(4)
def test_queryset_iteration_with_multiple_cache_fetches(): """ Fetch results with QuerySet via __iter__ with multiple cache fetches """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And we lower the per request to force multiple fetches t._per_request = 2 # And I have records first_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] }, { "_index": "bar", "_type": "baz", "_id": "2", "_score": 10, "_source": { "foo": "barbar" }, "sort": [ 1395687078000 ] } ] } } second_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "3", "_score": 10, "_source": { "foo": "barbarbar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", responses=[ httpretty.Response(body=json.dumps(first_response), content_type="application/json"), httpretty.Response(body=json.dumps(second_response), content_type="application/json"), ]) # Then I should eventually get all records results = [] for result in t: results.append(result) len(results).should.equal(3) t.count().should.equal(3) results[0].should.equal("1") results[1].should.equal("2") results[2].should.equal("3")