Skip to content

Commit

Permalink
test workign - postgress working localy
Browse files Browse the repository at this point in the history
  • Loading branch information
womullan committed Feb 26, 2024
1 parent 3a08bad commit 63ee8c8
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 393 deletions.
744 changes: 363 additions & 381 deletions requirements/main.txt

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/obsloctap/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ class Configuration(BaseSettings):
env="SAFIR_LOG_LEVEL",
)

database: str = Field(
"",
title="postgres database name",
env="database",
)

database_url: str = Field(
"",
title="URL for postgres database",
env="database_url",
)

database_user: str = Field(
"",
title="user for postgres database",
env="database_user",
)

database_password: str = Field(
"",
title="password for postgres database",
Expand Down
12 changes: 9 additions & 3 deletions src/obsloctap/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async def get_schedule(self, time: float = 0) -> list[Obsplan]:
obs.s_ra = 90.90909091666666
obs.s_dec = -74.60384434722222
obs.rubin_rot_sky_pos = 18.33895879413964
obs.rubin_nexpnexp = 3
obs.rubin_nexp = 3
observations.append(obs)
return observations

Expand All @@ -200,12 +200,18 @@ async def getHelper() -> DbHelp:
if dbHelper is None:
if "database_url" in os.environ:
config = Configuration()
full_url = (
f"postgresql+asyncpg://{config.database_user}:"
f"{config.database_password}@"
f"{config.database_url}/{config.database}"
)
logging.info(
f"Creating SQlAlchemy engine with "
f"{config.database_url[0:25]}......"
f"{config.database_user}@{config.database_url}"
f"/config.database"
f" and schema: {config.database_schema}."
)
engine = create_async_engine(config.database_url)
engine = create_async_engine(full_url)
dbHelper = DbHelp(engine=engine)
dbHelper.schema = config.database_schema
logging.info("Got engine")
Expand Down
6 changes: 4 additions & 2 deletions src/obsloctap/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
constructed when this module is loaded and is not deferred until a function is
called.
"""

import logging
from importlib.metadata import metadata, version

from fastapi import FastAPI
Expand All @@ -28,6 +28,8 @@
)
configure_uvicorn_logging(config.log_level)

log = logging.getLogger(__name__)

app = FastAPI(
title="obsloctap",
description=metadata("obsloctap")["Summary"],
Expand All @@ -42,12 +44,12 @@
app.include_router(internal_router)
app.include_router(external_router, prefix=config.path_prefix)

# Add middleware.
app.add_middleware(XForwardedMiddleware)


@app.on_event("startup")
async def startup_event() -> None:
logging.info("Starting up")
pass


Expand Down
5 changes: 3 additions & 2 deletions tests/DbTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ async def test_insert() -> None:
return

plan = Obsplan()
plan.t_planning = Timestamp.now().to_julian_date()
now: float = Timestamp.now().to_julian_date()
plan.t_planning = now
plan.s_ra = 90.90909091666666
plan.s_dec = -74.60384434722222
plan.rubin_nexp = 3
Expand Down Expand Up @@ -47,7 +48,7 @@ async def test_insert() -> None:
# now get it back
plans = await dbhelp.get_schedule(time=100)

await dbhelp.tidyup(plan.t_planning)
await dbhelp.tidyup(now)

assert len(plans) >= 1

Expand Down
8 changes: 4 additions & 4 deletions tests/handlers/external_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ async def test_get_exernal_index(client: AsyncClient) -> None:

@pytest.mark.asyncio
async def test_get_schedule(client: AsyncClient) -> None:
"""will use mock efd for local esting"""
"""will use mock for local esting"""
response = await client.get(f"{config.path_prefix}/schedule")
assert response.status_code == 200
data = response.json()
assert len(data) >= 1
obs = data[0]
assert "nexp" in obs
assert "mjd" in obs
assert data[0]["mjd"] == "60032.194918981484"
assert "rubin_nexp" in obs
assert "t_planning" in obs
assert data[0]["t_planning"] == 60032.194918981484
2 changes: 1 addition & 1 deletion tests/handlers/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ async def test_get_schedule(client: AsyncClient) -> None:
data = response.json()
assert len(data) >= 1
obs = data[0]
assert "nexp" in obs
assert "rubin_nexp" in obs

0 comments on commit 63ee8c8

Please sign in to comment.