Skip to content

Commit

Permalink
ignore irrelevant keys in same_query
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas committed Nov 2, 2023
1 parent a07f7cf commit 08d6750
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 15 additions & 1 deletion backend/api/save_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@ def recent_queries(user):
def has_aggregations(es_query):
return 'aggs' in es_query

IGNORE_KEYS = [
'size',
'scroll',
'from',
'aggs',
]

def _filter_relevant_keys(es_query):
return {
key: value
for (key, value) in es_query.items()
if key not in IGNORE_KEYS
}

def same_query(es_query_1, es_query_2):
return es_query_1 == es_query_2
return _filter_relevant_keys(es_query_1) == _filter_relevant_keys(es_query_2)
19 changes: 17 additions & 2 deletions backend/api/tests/test_save_query.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.utils import timezone
import pytest
from visualization.query import MATCH_ALL
from copy import deepcopy

from visualization.query import MATCH_ALL, set_query_text
from addcorpus.models import Corpus
from api.models import Query
from api.save_query import recent_queries
from api.save_query import recent_queries, same_query


@pytest.fixture()
Expand All @@ -28,3 +30,16 @@ def test_recent_queries(auth_user, saved_query):

assert saved_query not in recent_queries(auth_user)

def test_same_query():
assert same_query(MATCH_ALL, MATCH_ALL)

q1 = deepcopy(MATCH_ALL)
q1.update({
'size': 20,
'from': 21,
})

assert same_query(q1, MATCH_ALL)

q2 = set_query_text(MATCH_ALL, 'test')
assert not same_query(q2, MATCH_ALL)

0 comments on commit 08d6750

Please sign in to comment.