Esempio n. 1
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)
Esempio n. 2
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')
Esempio n. 3
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')
Esempio n. 4
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')
Esempio n. 5
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')