Exemplo n.º 1
0
def test_empty_query(app):
    """Test building an empty query."""
    q = RecordsSearch()
    if ES_VERSION[0] >= 7:
        q.to_dict() == {}
    else:
        q.to_dict() == {'query': {'match_all': {}}}

    q = RecordsSearch.faceted_search('')
    if ES_VERSION[0] >= 7:
        q._s.to_dict() == {'highlight': {'fields': {'*': {}}}}
    else:
        q._s.to_dict() == {'query': {'match_all': {}}}

    q = RecordsSearch()[10]
    assert q.to_dict()['from'] == 10
    assert q.to_dict()['size'] == 1

    q = q[10:20]
    assert q.to_dict()['from'] == 10
    assert q.to_dict()['size'] == 10

    q = q.sort({'field1': {'order': 'asc'}})
    assert q.to_dict()['sort'][0] == {'field1': {'order': 'asc'}}

    q = q.sort()
    assert 'sort' not in q.to_dict()

    q = q.sort('-field1')
    assert q.to_dict()['sort'][0] == {'field1': {'order': 'desc'}}

    q = q.sort('field2', {'field3': {'order': 'asc'}})
    assert q.to_dict()['sort'][0] == 'field2'
    assert q.to_dict()['sort'][1] == {'field3': {'order': 'asc'}}
    q.sort()

    q = RecordsSearch()
    q = q.highlight('field1', index_options='offsets')
    assert len(q.to_dict()['highlight']['fields']) == 1
    assert q.to_dict()['highlight']['fields']['field1'] == {
        'index_options': 'offsets'
    }

    q = q.highlight('field2')
    assert len(q.to_dict()['highlight']['fields']) == 2
    assert q.to_dict()['highlight']['fields']['field1'] == {
        'index_options': 'offsets'
    }
    assert q.to_dict()['highlight']['fields']['field2'] == {}

    q = q.highlight()
    assert 'highligth' not in q.to_dict()
Exemplo n.º 2
0
def test_empty_query(app):
    """Test building an empty query."""
    with app.app_context():
        q = RecordsSearch()
        assert q.to_dict()['query'] == {'match_all': {}}

        q = RecordsSearch.faceted_search('')
        assert q._s.to_dict()['query'] == {'match_all': {}}

        q = RecordsSearch()[10]
        assert q.to_dict()['from'] == 10
        assert q.to_dict()['size'] == 1

        q = q[10:20]
        assert q.to_dict()['from'] == 10
        assert q.to_dict()['size'] == 10

        q = q.sort({'field1': {'order': 'asc'}})
        assert q.to_dict()['sort'][0] == {'field1': {'order': 'asc'}}

        q = q.sort()
        assert 'sort' not in q.to_dict()

        q = q.sort('-field1')
        assert q.to_dict()['sort'][0] == {'field1': {'order': 'desc'}}

        q = q.sort('field2', {'field3': {'order': 'asc'}})
        assert q.to_dict()['sort'][0] == 'field2'
        assert q.to_dict()['sort'][1] == {'field3': {'order': 'asc'}}
        q.sort()

        q = RecordsSearch()
        q = q.highlight('field1', index_options='offsets')
        assert len(q.to_dict()['highlight']['fields']) == 1
        assert q.to_dict()['highlight']['fields']['field1'] == {
            'index_options': 'offsets'
        }

        q = q.highlight('field2')
        assert len(q.to_dict()['highlight']['fields']) == 2
        assert q.to_dict()['highlight']['fields']['field1'] == {
            'index_options': 'offsets'
        }
        assert q.to_dict()['highlight']['fields']['field2'] == {}

        q = q.highlight()
        assert 'highligth' not in q.to_dict()
Exemplo n.º 3
0
def test_empty_query(app):
    """Test building an empty query."""
    def _assert_base_search(q):
        """Assert base search queries."""
        if ES_VERSION[0] >= 7:
            q.to_dict() == {}
        else:
            q.to_dict() == {'query': {'match_all': {}}}

    def _assert_base_faceted_search(q):
        """Assert base faceted search queries."""
        if ES_VERSION[0] >= 7:
            q._s.to_dict() == {'highlight': {'fields': {'*': {}}}}
        else:
            q._s.to_dict() == {'query': {'match_all': {}}}

    def _assert_pagination_sorting(q):
        """Assert pagination and sorting cases."""
        assert q.to_dict()['from'] == 10
        assert q.to_dict()['size'] == 1

        q = q[10:20]
        assert q.to_dict()['from'] == 10
        assert q.to_dict()['size'] == 10

        q = q.sort({'field1': {'order': 'asc'}})
        assert q.to_dict()['sort'][0] == {'field1': {'order': 'asc'}}

        q = q.sort()
        assert 'sort' not in q.to_dict()

        q = q.sort('-field1')
        assert q.to_dict()['sort'][0] == {'field1': {'order': 'desc'}}

        q = q.sort('field2', {'field3': {'order': 'asc'}})
        assert q.to_dict()['sort'][0] == 'field2'
        assert q.to_dict()['sort'][1] == {'field3': {'order': 'asc'}}
        q.sort()

    def _assert_highlighting(q):
        """Assert query highlighting."""
        q = q.highlight('field1', index_options='offsets')
        assert len(q.to_dict()['highlight']['fields']) == 1
        assert q.to_dict()['highlight']['fields']['field1'] == {
            'index_options': 'offsets'
        }

        q = q.highlight('field2')
        assert len(q.to_dict()['highlight']['fields']) == 2
        assert q.to_dict()['highlight']['fields']['field1'] == {
            'index_options': 'offsets'
        }
        assert q.to_dict()['highlight']['fields']['field2'] == {}

        q = q.highlight()
        assert 'highligth' not in q.to_dict()

    # V1
    q = RecordsSearch()
    _assert_base_search(q)

    q = RecordsSearch.faceted_search('')
    _assert_base_faceted_search(q)

    q = RecordsSearch()[10]
    _assert_pagination_sorting(q)

    q = RecordsSearch()
    _assert_highlighting(q)

    # V2 (Faceted search removed)
    q = RecordsSearchV2()
    _assert_base_search(q)

    q = RecordsSearchV2()[10]
    _assert_pagination_sorting(q)

    q = RecordsSearchV2()
    _assert_highlighting(q)
Exemplo n.º 4
0
def test_empty_query(app):
    """Test building an empty query."""
    def _assert_base_search(q):
        """Assert base search queries."""
        if ES_VERSION[0] >= 7:
            q.to_dict() == {}
        else:
            q.to_dict() == {"query": {"match_all": {}}}

    def _assert_base_faceted_search(q):
        """Assert base faceted search queries."""
        if ES_VERSION[0] >= 7:
            q._s.to_dict() == {"highlight": {"fields": {"*": {}}}}
        else:
            q._s.to_dict() == {"query": {"match_all": {}}}

    def _assert_pagination_sorting(q):
        """Assert pagination and sorting cases."""
        assert q.to_dict()["from"] == 10
        assert q.to_dict()["size"] == 1

        q = q[10:20]
        assert q.to_dict()["from"] == 10
        assert q.to_dict()["size"] == 10

        q = q.sort({"field1": {"order": "asc"}})
        assert q.to_dict()["sort"][0] == {"field1": {"order": "asc"}}

        q = q.sort()
        assert "sort" not in q.to_dict()

        q = q.sort("-field1")
        assert q.to_dict()["sort"][0] == {"field1": {"order": "desc"}}

        q = q.sort("field2", {"field3": {"order": "asc"}})
        assert q.to_dict()["sort"][0] == "field2"
        assert q.to_dict()["sort"][1] == {"field3": {"order": "asc"}}
        q.sort()

    def _assert_highlighting(q):
        """Assert query highlighting."""
        q = q.highlight("field1", index_options="offsets")
        assert len(q.to_dict()["highlight"]["fields"]) == 1
        assert q.to_dict()["highlight"]["fields"]["field1"] == {
            "index_options": "offsets"
        }

        q = q.highlight("field2")
        assert len(q.to_dict()["highlight"]["fields"]) == 2
        assert q.to_dict()["highlight"]["fields"]["field1"] == {
            "index_options": "offsets"
        }
        assert q.to_dict()["highlight"]["fields"]["field2"] == {}

        q = q.highlight()
        assert "highligth" not in q.to_dict()

    # V1
    q = RecordsSearch()
    _assert_base_search(q)

    q = RecordsSearch.faceted_search("")
    _assert_base_faceted_search(q)

    q = RecordsSearch()[10]
    _assert_pagination_sorting(q)

    q = RecordsSearch()
    _assert_highlighting(q)

    # V2 (Faceted search removed)
    q = RecordsSearchV2()
    _assert_base_search(q)

    q = RecordsSearchV2()[10]
    _assert_pagination_sorting(q)

    q = RecordsSearchV2()
    _assert_highlighting(q)