Skip to content

Commit

Permalink
add tests for proximity filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpayton committed Dec 19, 2024
1 parent 0a14d87 commit 9636b8e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
28 changes: 28 additions & 0 deletions tests/integration/test_ao_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ def test_q_filters(self):
self.assertEqual(len(response), 1)
self.assertEqual(response[0]["ao_no"], "2014-19")

def test_q_proximity_filters(self):
search_phrase = "Random document third ao"
proximity_filter = "before"
proximity_filter_term = "document"
max_gaps = 3

response = self._results_ao(api.url_for(UniversalSearch,
q_proximity=search_phrase,
proximity_filter=proximity_filter,
proximity_filter_term=proximity_filter_term,
max_gaps=max_gaps))

self.assertEqual(len(response), 1)
self.assertEqual(response[0]["ao_no"], "2024-12")

multiple_phrases = ["fourth ao", "proximity document"]
max_gaps = 3

response = self._results_ao(api.url_for(UniversalSearch,
q_proximity=multiple_phrases,
proximity_filter=proximity_filter,
proximity_filter_term=proximity_filter_term,
max_gaps=max_gaps))
self.assertEqual(len(response), 1)
self.assertEqual(response[0]["ao_no"], "2014-19")

self.check_incorrect_values({"q_proximity": search_phrase, "max_gaps": 1}, False)

def test_citation_filters(self):
statutory_title = 52
statutory_section = "30101"
Expand Down
47 changes: 38 additions & 9 deletions tests/integration/test_cases_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ def check_filters(self, params, field_name, doc_type):
self.assertNotEqual(response["total_" + doc_type], 0)
assert all(x[field_name] == list(params.values())[0] for x in response[doc_type])

def check_incorrect_values(self, params, doc_type, raiseError):
def check_incorrect_values(self, params, doc_types, raiseError):
response = self.app.get(api.url_for(UniversalSearch, **params))
# logging.info(response.json)

if raiseError:
assert response.status_code == 422
else:
assert response.status_code == 200
assert response.json[doc_type] == 0
if isinstance(doc_types, list):
for typ in doc_types:
assert response.json[typ] == 0
else:
assert response.json[doc_types] == 0

def check_sort_asc(self, doc_dict):
for i in range(len(doc_dict) - 1):
Expand Down Expand Up @@ -143,9 +147,7 @@ def test_penalty_filter(self):
"case_min_penalty_amount": 999999
}

self.check_incorrect_values(params, "total_murs", False)
self.check_incorrect_values(params, "total_admin_fines", False)
self.check_incorrect_values(params, "total_adrs", False)
self.check_incorrect_values(params, ["total_murs", "total_admin_fines", "total_adrs"], False)

def test_case_doc_cat_id_filter(self):
# for archived and current murs, adrs, and afs
Expand Down Expand Up @@ -244,6 +246,35 @@ def test_q_filters(self):
self.assertEqual(response["total_admin_fines"], 0)
self.assertEqual(response["total_all"], 10)

def test_q_proximity_filters(self):
# for archived and current murs, advisory_opinions, adrs, and afs
search_phrase = "first document archived mur"
proximity_filter = "after"
proximity_filter_term = "sample text"
max_gaps = 2

response = self._results_case(api.url_for(UniversalSearch,
q_proximity=search_phrase,
proximity_filter=proximity_filter,
proximity_filter_term=proximity_filter_term,
max_gaps=max_gaps))

self.assertEqual(response["total_murs"], 1)
self.assertEqual(response["total_admin_fines"], 0)
self.assertEqual(response["total_adrs"], 0)

multiple_phrases = ["second adr", "sample text"]
max_gaps = 6
response = self._results_case(api.url_for(UniversalSearch, q_proximity=multiple_phrases, max_gaps=max_gaps))

self.assertEqual(response["total_murs"], 0)
self.assertEqual(response["total_admin_fines"], 0)
self.assertEqual(response["total_adrs"], 1)

self.check_incorrect_values({"q_proximity": search_phrase, "max_gaps": 1},
["total_murs", "total_admin_fines", "total_adrs"],
False)

def test_sort(self):
sort_value = "case_no"
ignore_type = ["advisory_opinions", "statutes"]
Expand Down Expand Up @@ -392,8 +423,7 @@ def test_case_respondents(self):
for rsp in adr["respondents"]
) for adr in response["adrs"])

self.check_incorrect_values({"case_respondents": "Bad value"}, "total_murs", False)
self.check_incorrect_values({"case_respondents": "Bad value"}, "total_adrs", False)
self.check_incorrect_values({"case_respondents": "Bad value"}, ["total_murs", "total_adrs"], False)

def test_citation_filters(self):
# filter for current murs and adrs
Expand Down Expand Up @@ -447,8 +477,7 @@ def test_citation_filters(self):
["case_regulatory_citation", "1111 CFR §112.4111"]
]
for filter in filters:
self.check_incorrect_values({filter[0]: filter[1]}, "total_murs", False)
self.check_incorrect_values({filter[0]: filter[1]}, "total_adrs", False)
self.check_incorrect_values({filter[0]: filter[1]}, ["total_murs", "total_adrs"], False)

# ---------------------- End MUR and ADR filters ------------------------------------------------
# ---------------------- Start MUR filters ------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/test_legal_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@
"date": "2015-01-16T00:00:00",
"description": "2014-19",
"document_id": 80838,
"text": "Document text for the fourth ao document",
"text": "Proximity document text for the fourth ao document",
"url": "/files/legal/aos/2014-19/AO_2014-19_(ActBlue)_Final_(1.15.15).pdf"
}
],
Expand Down
5 changes: 3 additions & 2 deletions webservices/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,15 +2362,16 @@ def add_ytd(var):
'''

MAX_GAPS = '''
The maximum number of positions allowed between matching terms specified in q_proximity
The maximum number of positions allowed between matching terms specified in `q_proximity`
'''

PROXIMITY_FILTER = '''
Adds additional filters to the proximity search that provides options to specify positional constraints
'''

PROXIMITY_FILTER_TERM = '''
q_proximity phrase
Specifies the term to which the `proximity_filter` option applies to and defines what must appear in relation to the \
`q_proximity` phrase
'''

# ======== legal end =========
Expand Down

0 comments on commit 9636b8e

Please sign in to comment.