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_search_with_iterator(context): """ Search using an iterator """ # When create a queryset t = QuerySet("localhost", index="foo") # And set iterator fetching to a small size t._per_request = 2 # And there are records add_document("foo", {"bar": 0}) add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) add_document("foo", {"bar": 4}) # And I do a filter on my new object # And a different filter on my old object t.order_by(Sort("bar", order="asc")) # Then I get the expected results for (counter, result) in enumerate(t): result['_source'].should.equal({"bar": counter}) len(t).should.equal(5) t.count().should.equal(5)
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_search_with_iterator(context): """ Search using an iterator """ # When create a queryset t = QuerySet("localhost", index="foo") # And set iterator fetching to a small size t._per_request = 2 # And there are records add_document("foo", {"bar": 0}) add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) add_document("foo", {"bar": 4}) # And I do a filter on my new object # And a different filter on my old object t.order_by(Sort("bar", order="asc")) # Then I get the expected results for (counter, result) in enumerate(t): result['_source'].should.equal({"bar": counter}) len(t).should.equal(5) t.count().should.equal(5) t.max_score().should_not.be(None)
def test_queryset_count(): """ Get QuerySet Count """ # When I create a query block t = QuerySet("foobar") # Then I get an appropriate Count t.count().should.equal(None)
def test_queryset_count(): """ Get QuerySet Count """ # When I create a query block t = QuerySet("foobar") # Then I get an appropriate Count t.count().should.equal(None)
def test_queryset_getitem_with_post_query_action(): """ Fetch from QuerySet with __getitem__ and post query action """ # When I create a query block t = QuerySet("localhost", index="bar") # And I have a post query action global my_global_var my_global_var = 1 def action(self, results, start, stop): global my_global_var my_global_var += 1 t.post_query_actions(action) # And I have records response = { "took": 12, "timed_out": False, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "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) # Then I see the correct results results[0]['_id'].should.equal('1') my_global_var.should.equal(2)
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_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_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_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")
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")