Skip to content

Commit

Permalink
Merge pull request #58 from observerly/feature/seed/messier
Browse files Browse the repository at this point in the history
feat: Added seeder for Messier objects.
  • Loading branch information
michealroberts authored Aug 29, 2022
2 parents e9d2932 + 29a418e commit 5385d91
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/init_db_seed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from app.db.session import SessionLocal
from app.seed.messier import seed_messier
from app.seed.stars import seed_stars

logging.basicConfig(level=logging.INFO)
Expand All @@ -10,6 +11,7 @@

def seed() -> None:
db = SessionLocal()
seed_messier(db, logger)
seed_stars(db, logger)


Expand Down
57 changes: 57 additions & 0 deletions app/seed/messier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
from logging import Logger

from sqlalchemy.orm import Session

from app import crud, schemas
from app.utils import ROOT_DIR


def seed_messier(db: Session, logger: Logger) -> None:
"""
This function call seeds the Body model with a known set of Messier
objects in data directory
"""
count = 0

logger.info("ROOT_DIR: {}".format(ROOT_DIR))

objects = []

data = []

# Open the majorStars.json file:
with open("{}/data/objects/messier.json".format(ROOT_DIR), "r") as f:
data += json.loads(f.read())

for body in data:
m = {
"name": body["name"],
"iau": body["iau"],
"ra": float(body["ra"]),
"dec": float(body["dec"]),
"constellation": body["constellation"],
"type": body["type"],
"messier": body["messier"],
"ngc": body["ngc"],
"ic": body["ic"],
"simbad": body["simbad"],
}

m["m"] = (lambda body: None if body["m"] is None else float(body["m"]))(body)

m["M"] = (lambda body: None if body["M"] is None else float(body["M"]))(body)

m["d"] = (lambda body: None if body["d"] is None else float(body["d"]))(body)

object_in = schemas.BodyCreate(**m)
objects.append(object_in)

for object_in in objects:
crud.body.create(db, body=object_in) # noqa: F841
count += 1
logger.info("Successfully Populated Object {}".format(object_in.name))

# Read the stars incrementally into the db:

logger.info("Populated Initial API w/{} Objects".format(count))
10 changes: 5 additions & 5 deletions app/tests/api/api_v1/test_bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def test_list_bodies_without_any_query_params(client: AsyncClient) -> None

body = response.json()

assert body["count"] == 3767
assert body["count"] == 3877
assert "/api/v1/bodies/2?limit=20" in body["next_page"]
assert body["previous_page"] is None
assert len(body["results"]) == 20
Expand Down Expand Up @@ -153,7 +153,7 @@ async def test_list_bodies_within_the_constellation_orion(client: AsyncClient) -

body = response.json()

assert body["count"] == 85
assert body["count"] == 88
assert (
"https://perseus.docker.localhost/api/v1/bodies/2?limit=20&constellation=orion"
in body["next_page"]
Expand Down Expand Up @@ -186,7 +186,7 @@ async def test_list_bodies_above_local_observers_horizon(client: AsyncClient) ->

body = response.json()

assert body["count"] == 1493
assert body["count"] == 1518
assert (
"/api/v1/bodies/2?limit=20&latitude=19.8968&longitude=155.8912&datetime=2021-05-14T00%3A00%3A00.000Z" # noqa: E501,
in body["next_page"]
Expand All @@ -210,7 +210,7 @@ async def test_list_bodies_above_local_observers_horizon_between_interval_for_20

body = response.json()

assert body["count"] == 2739
assert body["count"] == 2843
assert (
"/api/v1/bodies/2?limit=20&latitude=19.8968&longitude=-155.8912&start=2021-05-14T18%3A46%3A50.000-10%3A00&end=2021-05-15T05%3A49%3A30.000-10%3A00" # noqa: E501,
in body["next_page"]
Expand All @@ -234,7 +234,7 @@ async def test_list_bodies_above_local_observers_horizon_between_interval_for_20

body = response.json()

assert body["count"] == 2717
assert body["count"] == 2799
assert (
"/api/v1/bodies/2?limit=20&latitude=19.8968&longitude=-155.8912&start=2022-01-01T17%3A58%3A56.000-10%3A00&end=2022-01-02T06%3A52%3A13.000-10%3A00" # noqa: E501,
in body["next_page"]
Expand Down
1 change: 1 addition & 0 deletions data/objects/messier.json

Large diffs are not rendered by default.

0 comments on commit 5385d91

Please sign in to comment.