Skip to content

Commit a23287e

Browse files
author
Isaac Milarsky
committed
NDH-800 fix location filters and add tests
Signed-off-by: Isaac Milarsky <isaac.milarsky@hhs.cms.gov>
1 parent 869f1a9 commit a23287e

File tree

2 files changed

+139
-12
lines changed

2 files changed

+139
-12
lines changed

backend/npdfhir/filters/organization_affiliation_filter_set.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ def filter_organization_type(self, queryset, name, value):
8585
).filter(
8686
taxonomy_search=query
8787
).distinct()
88-
88+
8989
def filter_location(self, queryset, name, value):
9090
return queryset.annotate(
9191
location_search=SearchVector(
92-
"location_set__name",
93-
"location_set__address__address_us__delivery_line_1",
94-
"location_set__address__address_us__delivery_line_2",
95-
"location_set__address__address_us__city_name",
96-
"location_set__address__address_us__state_code__abbreviation",
97-
"location_set__address__address_us__zipcode",
92+
"location__name",
93+
"location__address__address_us__delivery_line_1",
94+
"location__address__address_us__delivery_line_2",
95+
"location__address__address_us__city_name",
96+
"location__address__address_us__state_code__abbreviation",
97+
"location__address__address_us__zipcode",
9898
)
9999
).filter(
100100
location_search=SearchQuery(value, search_type="websearch")
@@ -103,18 +103,18 @@ def filter_location(self, queryset, name, value):
103103
def filter_address_city(self, queryset, name, value):
104104
return queryset.annotate(
105105
search=SearchVector(
106-
"location_set__address__address_us__city_name"
106+
"location__address__address_us__city_name"
107107
)
108108
).filter(search=value)
109109

110110
def filter_address_state(self, queryset, name, value):
111111
return queryset.annotate(
112112
search=SearchVector(
113-
"location_set__address__address_us__state_code__abbreviation"
113+
"location__address__address_us__state_code__abbreviation"
114114
)
115115
).filter(search=value)
116116

117117
def filter_address_postalcode(self, queryset, name, value):
118118
return queryset.filter(
119-
location_set__address__address_us__zipcode=value
119+
location__address__address_us__zipcode=value
120120
)

backend/npdfhir/tests/test_organization_affiliation.py

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ..models import (
77
EhrVendor,
88
LocationToEndpointInstance,
9+
Location,
910
Nucc,
1011
Organization,
1112
OtherIdType,
@@ -73,6 +74,10 @@ def setUpTestData(cls):
7374
loc_good_1 = create_location(
7475
organization=cls.org_good_1,
7576
name="Good Location 1",
77+
city="Albuquerque",
78+
state="NM",
79+
zipcode="87101",
80+
addr_line_1="807 Dusty Ln"
7681
)
7782

7883
endpoint_instance = create_endpoint_instance(
@@ -96,8 +101,15 @@ def setUpTestData(cls):
96101

97102
cls.orgs.append(cls.org_good_2)
98103

99-
loc_good_2a = create_location(organization=cls.org_good_2, name="Location A")
100-
loc_good_2b = create_location(organization=cls.org_good_2, name="Location B")
104+
loc_good_2a = create_location(
105+
organization=cls.org_good_2,
106+
name="Location A",
107+
city="Springfield",
108+
state="MO",
109+
zipcode="65203",
110+
addr_line_1="403 Spring Ln"
111+
)
112+
loc_good_2b = create_location(organization=cls.org_good_2, name="Location B", zipcode="01234")
101113

102114
endpoint_good_2a = create_endpoint_instance(
103115
organization=cls.org_good_2,
@@ -332,6 +344,121 @@ def test_retrieve_single_organization_affil(self):
332344
self.assertIn("organization", org_affiliation_entry)
333345
self.assertIn("participatingOrganization", org_affiliation_entry)
334346
self.assertIn("endpoint", org_affiliation_entry)
347+
348+
def test_list_filter_by_address(self):
349+
address_search = "807 Dusty Ln"
350+
url = reverse("fhir-organizationaffiliation-list")
351+
response = self.client.get(url, {"address": address_search})
352+
self.assertEqual(response.status_code, status.HTTP_200_OK)
353+
assert_has_results(self, response)
354+
355+
bundle = response.data["results"]
356+
357+
for entry in bundle["entry"]:
358+
self.assertIn("resource", entry)
359+
location_entry = entry["resource"]
360+
361+
address_lines = []
362+
for location in location_entry['location']:
363+
loc_id = location['reference'].split('/')[-1]
364+
loc_obj = (
365+
Location.objects.filter(pk=loc_id).first()
366+
)
367+
address_lines.append(loc_obj.address.address_us.delivery_line_1)
368+
369+
self.assertIn(address_search, address_lines)
370+
371+
def test_list_filter_by_address_city(self):
372+
address_search = "Springfield"
373+
url = reverse("fhir-organizationaffiliation-list")
374+
response = self.client.get(url, {"address_city": address_search})
375+
self.assertEqual(response.status_code, status.HTTP_200_OK)
376+
assert_has_results(self, response)
377+
378+
bundle = response.data["results"]
379+
380+
for entry in bundle["entry"]:
381+
self.assertIn("resource", entry)
382+
location_entry = entry["resource"]
383+
384+
address_lines = []
385+
for location in location_entry['location']:
386+
loc_id = location['reference'].split('/')[-1]
387+
loc_obj = (
388+
Location.objects.filter(pk=loc_id).first()
389+
)
390+
address_lines.append(loc_obj.address.address_us.city_name)
391+
392+
self.assertIn(address_search, address_lines)
393+
394+
def test_list_filter_by_address_state(self):
395+
address_search = "NY"
396+
url = reverse("fhir-organizationaffiliation-list")
397+
response = self.client.get(url, {"address_state": address_search})
398+
self.assertEqual(response.status_code, status.HTTP_200_OK)
399+
assert_has_results(self, response)
400+
401+
bundle = response.data["results"]
402+
403+
for entry in bundle["entry"]:
404+
self.assertIn("resource", entry)
405+
location_entry = entry["resource"]
406+
407+
address_lines = []
408+
for location in location_entry['location']:
409+
loc_id = location['reference'].split('/')[-1]
410+
loc_obj = (
411+
Location.objects.filter(pk=loc_id).first()
412+
)
413+
address_lines.append(loc_obj.address.address_us.state_code.abbreviation)
414+
415+
self.assertIn(address_search, address_lines)
416+
417+
def test_list_filter_by_address_zipcode(self):
418+
address_search = "87101"
419+
url = reverse("fhir-organizationaffiliation-list")
420+
response = self.client.get(url, {"address_postalcode": address_search})
421+
self.assertEqual(response.status_code, status.HTTP_200_OK)
422+
assert_has_results(self, response)
423+
424+
bundle = response.data["results"]
425+
426+
for entry in bundle["entry"]:
427+
self.assertIn("resource", entry)
428+
location_entry = entry["resource"]
429+
430+
address_lines = []
431+
for location in location_entry['location']:
432+
loc_id = location['reference'].split('/')[-1]
433+
loc_obj = (
434+
Location.objects.filter(pk=loc_id).first()
435+
)
436+
address_lines.append(loc_obj.address.address_us.zipcode)
437+
438+
self.assertIn(address_search, address_lines)
439+
440+
def test_list_filter_by_address_zipcode_leading_zero(self):
441+
address_search = "01234"
442+
url = reverse("fhir-organizationaffiliation-list")
443+
response = self.client.get(url, {"address_postalcode": address_search})
444+
self.assertEqual(response.status_code, status.HTTP_200_OK)
445+
assert_has_results(self, response)
446+
447+
bundle = response.data["results"]
448+
449+
for entry in bundle["entry"]:
450+
self.assertIn("resource", entry)
451+
location_entry = entry["resource"]
452+
453+
address_lines = []
454+
for location in location_entry['location']:
455+
loc_id = location['reference'].split('/')[-1]
456+
loc_obj = (
457+
Location.objects.filter(pk=loc_id).first()
458+
)
459+
address_lines.append(loc_obj.address.address_us.zipcode)
460+
461+
self.assertIn(address_search, address_lines)
335462

336463
def test_retrieve_non_existant_organization_affil(self):
337464
url = reverse(

0 commit comments

Comments
 (0)