def test_queryset_wrap(): connections = { 'default': {'URL': 'http://localhost:9200'}} conf = Mock(connections=connections, indexes=connections.keys()) queryset = QuerySet(conf=conf) queryset.wrap('a wrapper') queryset.wrap('another wrapper') queryset.wrappers.should.be.length_of(2) queryset.wrappers.should.equal(['a wrapper', 'another wrapper'])
def test_get_items_after_wrap_calls_wrap(): # Given that we have a well configured queryset object connections = { 'default': {'URL': 'http://localhost:9200'}} conf = Mock(connections=connections, indexes=connections.keys()) queryset = QuerySet(conf=conf) queryset.raw_results = {'hits': {'hits': ['hit1', 'hit2']}} wrapper = Mock() wrapper.wrap.return_value = [] queryset.wrap(wrapper).items() wrapper.wrap.assert_called_once_with(['hit1', 'hit2'])
def test_get_items_ordered_results(): # Given that we have a well configured queryset object connections = { 'default': {'URL': 'http://localhost:9200'}} conf = Mock(connections=connections, indexes=connections.keys()) queryset = QuerySet(conf=conf) hit1 = {'_type': 'type1', '_id': 'some_id', 'value': 'hit1'} hit2 = {'_type': 'type2', '_id': 'another_id', 'value': 'hit2'} hit3 = {'_type': 'type1', '_id': 'last_id', 'value': 'hit3'} queryset.raw_results = {'hits': {'hits': [hit1, hit2, hit3]}} wrapper = Wrapper() wrapper.wrap = lambda objs: objs wrapper.match = lambda obj: obj['_type'] == 'type1' wrapper2 = Wrapper() wrapper2.wrap = lambda objs: objs wrapper2.match = lambda obj: obj['_type'] == 'type2' results = queryset.wrap(wrapper).wrap(wrapper2).items() results.should.equal([hit1, hit2, hit3])
def test_get_items_after_wrap_calls_match(): # Given that we have a well configured queryset object connections = { 'default': {'URL': 'http://localhost:9200'}} conf = Mock(connections=connections, indexes=connections.keys()) queryset = QuerySet(conf=conf) queryset.raw_results = {'hits': {'hits': ['hit1', 'hit2']}} wrapper = Mock() wrapper.wrap.return_value = [] queryset.wrap(wrapper).items() wrapper.match.call_count.should.equal(2) wrapper.match.assert_any_call('hit1') wrapper.match.assert_any_call('hit2')
def test_get_items_false_clean(): # Given that we have a well configured queryset object connections = { 'default': {'URL': 'http://localhost:9200'}} conf = Mock(connections=connections, indexes=connections.keys()) queryset = QuerySet(conf=conf) hit1 = {'_type': 'type1', '_id': 'some_id', 'value': 'hit1'} hit2 = {'_type': 'type1', '_id': 'last_id', 'value': 'hit2'} queryset.raw_results = {'hits': {'hits': [hit1, hit2]}} wrapper = Wrapper() wrapper.wrap = lambda objs: [objs[0]] wrapper.match = lambda obj: obj['_type'] == 'type1' results = queryset.wrap(wrapper).items(clean=False) results.should.equal([hit1, None])