Skip to content

Commit eb28011

Browse files
committed
flesh out pharmacy search with the fields the backend supports
1 parent 9924c43 commit eb28011

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

canvas_sdk/tests/utils/test_http.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_search_pharmacies_default_search_term(mock_get: MagicMock) -> None:
164164
pharmacy_http.search_pharmacies()
165165

166166
mock_get.assert_called_once_with(
167-
"https://pharmacy-2017071.canvasmedical.com/surescripts/pharmacy/?search=+",
167+
"https://pharmacy-2017071.canvasmedical.com/surescripts/pharmacy/",
168168
headers={},
169169
timeout=30,
170170
)
@@ -176,3 +176,61 @@ def test_search_pharmacies_default_search_term(mock_get: MagicMock) -> None:
176176
headers={},
177177
timeout=30,
178178
)
179+
180+
181+
@patch("requests.Session.get")
182+
def test_search_pharmacies_with_filter_fields(mock_get: MagicMock) -> None:
183+
"""Test that search_pharmacies passes filter fields as query parameters."""
184+
mock_get.return_value = FakeResponse()
185+
186+
pharmacy_http.search_pharmacies(
187+
search_term=None,
188+
ncpdp_id="1234567",
189+
organization_name="CVS",
190+
state="CA",
191+
zip_code_prefix="902",
192+
specialty_type="retail",
193+
)
194+
195+
call_url = mock_get.call_args[0][0]
196+
assert "ncpdp_id=1234567" in call_url
197+
assert "organization_name__icontains=CVS" in call_url
198+
assert "state__iexact=CA" in call_url
199+
assert "zip_code_prefix=902" in call_url
200+
assert "specialty_type__icontains=retail" in call_url
201+
assert "search=" not in call_url
202+
203+
204+
@patch("requests.Session.get")
205+
def test_search_pharmacies_with_id(mock_get: MagicMock) -> None:
206+
"""Test that search_pharmacies passes an exact id filter."""
207+
mock_get.return_value = FakeResponse()
208+
209+
pharmacy_http.search_pharmacies(search_term=None, id=42)
210+
211+
call_url = mock_get.call_args[0][0]
212+
assert "id=42" in call_url
213+
214+
215+
@patch("requests.Session.get")
216+
def test_search_pharmacies_with_zip_code_prefix(mock_get: MagicMock) -> None:
217+
"""Test that search_pharmacies passes zip_code_prefix."""
218+
mock_get.return_value = FakeResponse()
219+
220+
pharmacy_http.search_pharmacies(search_term=None, zip_code_prefix="902,100,945")
221+
222+
call_url = mock_get.call_args[0][0]
223+
assert "zip_code_prefix=902%2C100%2C945" in call_url
224+
225+
226+
@patch("requests.Session.get")
227+
def test_search_pharmacies_with_location(mock_get: MagicMock) -> None:
228+
"""Test that latitude and longitude are passed when provided."""
229+
mock_get.return_value = FakeResponse()
230+
231+
pharmacy_http.search_pharmacies("walgreens", latitude="34.05", longitude="-118.24")
232+
233+
call_url = mock_get.call_args[0][0]
234+
assert "search=walgreens" in call_url
235+
assert "latitude=34.05" in call_url
236+
assert "longitude=-118.24" in call_url

canvas_sdk/utils/http.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,47 @@ def search_pharmacies(
333333
search_term: str | None = " ",
334334
latitude: str | None = None,
335335
longitude: str | None = None,
336+
id: int | None = None,
337+
ncpdp_id: str | None = None,
338+
organization_name: str | None = None,
339+
specialty_type: str | None = None,
340+
state: str | None = None,
341+
zip_code_prefix: str | None = None,
336342
) -> list[dict]:
337-
"""Search for pharmacies by term and optional location."""
338-
params = {"search": search_term}
343+
"""Search for pharmacies by term and optional location.
344+
345+
Args:
346+
search_term: Full-text search across multiple fields.
347+
latitude: Latitude for location-based ordering.
348+
longitude: Longitude for location-based ordering.
349+
id: Exact pharmacy ID match.
350+
ncpdp_id: Exact NCPDP ID match.
351+
organization_name: Case-insensitive contains match on organization name.
352+
specialty_type: Case-insensitive contains match on specialty type.
353+
state: Case-insensitive exact match on state.
354+
zip_code_prefix: One or more comma/space-separated zip code prefixes.
355+
"""
356+
params: dict[str, str | int] = {}
357+
358+
if search_term and search_term.strip():
359+
params["search"] = search_term
360+
339361
if latitude and longitude:
340-
params.update(
341-
{
342-
"latitude": latitude,
343-
"longitude": longitude,
344-
}
345-
)
362+
params["latitude"] = latitude
363+
params["longitude"] = longitude
364+
365+
if id is not None:
366+
params["id"] = id
367+
if ncpdp_id is not None:
368+
params["ncpdp_id"] = ncpdp_id
369+
if organization_name is not None:
370+
params["organization_name__icontains"] = organization_name
371+
if specialty_type is not None:
372+
params["specialty_type__icontains"] = specialty_type
373+
if state is not None:
374+
params["state__iexact"] = state
375+
if zip_code_prefix is not None:
376+
params["zip_code_prefix"] = zip_code_prefix
346377

347378
query_string = urllib.parse.urlencode(params)
348379
response = self._http_client.get_json(f"/surescripts/pharmacy/?{query_string}")

0 commit comments

Comments
 (0)