Exemple #1
0
def test_search_api(pyelasticsearch):
    # Given that I want to query for something on elasticsearch, I need
    # to add some configuration to my settings file.
    connections = {
        'default': {'URL': 'http://localhost:9200'}
    }

    conf = Mock(connections=connections, indexes=connections.keys())
    queryset = QuerySet(conf=conf)

    pyelasticsearch.ElasticSearch.return_value.search.return_value = \
        'a lot of awesome stuff'

    # When I use the queryset.search() method
    results = queryset.search('something')

    # Then I see that behinde the scenes it uses the proper connection
    # declared in the django settings and also, the query was performed
    # against the right index
    pyelasticsearch.ElasticSearch.assert_called_once_with(
        'http://localhost:9200')
    pyelasticsearch.ElasticSearch.return_value.search.assert_called_once_with(
        'something', index='default')
    results.should.be.a('elasticfun.queryset.QuerySet')
    results.raw_results.should.equal('a lot of awesome stuff')
Exemple #2
0
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])
Exemple #3
0
def test_queryset_max_score_without_searching_before():
    connections = {
        'default': {'URL': 'http://localhost:9200'}}
    conf = Mock(connections=connections, indexes=connections.keys())
    queryset = QuerySet(conf=conf)

    # when the queryset.max_score() method is called
    # without first calling search should return 0
    queryset.max_score().should.equal(0)
Exemple #4
0
def test_get_items_without_wrap_before(pyelasticsearch):
    # 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']}}

    # it should return a list of hits
    queryset.items().should.equal(['hit1', 'hit2'])
Exemple #5
0
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'])
Exemple #6
0
def test_queryset_max_score(pyelasticsearch):
    connections = {
        'default': {'URL': 'http://localhost:9200'}}
    conf = Mock(connections=connections, indexes=connections.keys())

    pyelasticsearch.ElasticSearch.return_value.search.return_value = {'hits': {'max_score': 18.31415}}
    queryset = QuerySet(conf=conf)
    queryset.search('something')

    # when the queryset.max_score() method is called
    # without first calling search should return 0
    queryset.max_score().should.equal(18.31415)
Exemple #7
0
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'])
Exemple #8
0
def test_searching_with_dicts(pyelasticsearch):
    connections = {
        'default': {'URL': 'http://localhost:9200'}}
    conf = Mock(connections=connections, indexes=connections.keys())
    queryset = QuerySet(conf=conf)

    # Given that I have a query dict
    query = {'query': {'filtered': {'query': {'query_string': 'stuff'}}}}

    # When I pass this query to the search function
    queryset.search(query)

    # Then I see that the pyelasticsearch function was called with a
    # string containing the evaluated query
    pyelasticsearch.ElasticSearch.return_value.search.assert_called_once_with(
        query, index='default')
Exemple #9
0
def test_searching_with_query_objects(pyelasticsearch):

    connections = {
        'default': {'URL': 'http://localhost:9200'}}
    conf = Mock(connections=connections, indexes=connections.keys())
    queryset = QuerySet(conf=conf)

    # Given that I have a query object
    query = Query('ice') & Query('cream')

    # When I pass this query to the search function
    queryset.search(query)

    # Then I see that the pyelasticsearch function was called with a
    # string containing the evaluated query
    pyelasticsearch.ElasticSearch.return_value.search.assert_called_once_with(
        '("ice" AND "cream")', index='default')
Exemple #10
0
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')
Exemple #11
0
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])
Exemple #12
0
def test_connection_with_index_name(pyelasticsearch):
    # Given that we might want to name a connection with a different
    # string then the one used to identify my indexes
    connections = {
        'default': {'URL': 'http://localhost:9200'},
        'stuff': {'URL': 'http://localhost:9201'}
    }
    # When I query the available connections and indexes
    conf = Mock(connections=connections, indexes=connections.keys())
    queryset = QuerySet(conf=conf)

    # So, when I query for something using the API, I need to pass
    # the connection name or the index name, depending on how I declared
    # them in my settings.
    queryset.search('something', index='stuff')

    # Then I see that the pyelasticsearch API made the right calls
    pyelasticsearch.ElasticSearch.assert_called_once_with(
        'http://localhost:9201')
    pyelasticsearch.ElasticSearch.return_value.search.assert_called_once_with(
        'something', index='stuff')