Skip to content

Commit

Permalink
Merge pull request #73 from observerly/feature/query/catalogueSearchF…
Browse files Browse the repository at this point in the history
…ilter

feat: Added ability to query { messier, ngc, ic } column of Body with a sub-string matching to the catalogue.
  • Loading branch information
michealroberts authored Jan 28, 2023
2 parents 2860f15 + 3ee28e3 commit 2675719
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/api/api_v1/params/bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ class BodyQueryParams(BaseModel):
title="The end date / datetime to perform body object search",
deprecated=True,
)

catalogue: Optional[str] = Query(
default=None,
title="The catalogue of the body object to search",
deprecated=True,
)
34 changes: 34 additions & 0 deletions app/crud/crud_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def get_filter_query(self, query: Query, query_params: QueryParams):

query = self.perform_constellation_search_filter(query, query_params)

query = self.perform_catalogue_search_filter(query, query_params)

query = self.perform_horizontal_altitude_search_filter(query, query_params)

return query
Expand Down Expand Up @@ -141,6 +143,38 @@ def perform_constellation_search_filter(

return query

def perform_catalogue_search_filter(
self, query: Query, query_params: QueryParams
) -> Query:
# Catalogue
catalogue = getattr(query_params, "catalogue", None)

# If no catalogue is specified, return all:
if not catalogue or catalogue == "all":
return query

# Otherwise, filter by the given catalogue:

# Messier:
if catalogue.strip().lower() == "messier":
query = query.filter(
self.model.messier.isnot(None),
)

# New General Catalogue:
if catalogue.strip().lower() == "ngc":
query = query.filter(
self.model.ngc.isnot(None),
)

# Index Catalogue:
if catalogue.strip().lower() == "ic":
query = query.filter(
self.model.ic.isnot(None),
)

return query

def perform_order_by_type(self, query: Query) -> Query:
whens = {
"Other": 4,
Expand Down
24 changes: 24 additions & 0 deletions app/tests/api/api_v1/test_bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ async def test_list_bodies_within_the_partial_constellation_ori(
assert body["results"][19]["name"] == "ο² Orionis"


@pytest.mark.asyncio
async def test_list_bodies_within_a_specific_catalogue(client: AsyncClient) -> None:
page = 1

response = await client.get(
f"{settings.API_V1_STR}/bodies/{page}?catalogue=Messier",
headers={"Host": "perseus.docker.localhost"},
)

assert response.status_code == 200

body = response.json()

assert body["count"] == 110

assert (
"https://perseus.docker.localhost/api/v1/bodies/2?limit=20&catalogue=Messier"
in body["next_page"]
)

assert body["previous_page"] is None
assert len(body["results"]) == 20


@pytest.mark.asyncio
async def test_list_bodies_above_local_observers_horizon(client: AsyncClient) -> None:
page = 1
Expand Down

0 comments on commit 2675719

Please sign in to comment.