-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ukaea/james/test_db
James/test db
- Loading branch information
Showing
10 changed files
with
105 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
import numpy as np | ||
import xarray as xr | ||
|
||
|
||
@pytest.fixture | ||
def fake_dataset(): | ||
return xr.Dataset( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
pyuda_import = pytest.importorskip("pyuda") | ||
import subprocess | ||
import zarr | ||
import xarray as xr | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
pyuda_import = pytest.importorskip("pyuda") | ||
import zarr | ||
import xarray as xr | ||
import numpy as np | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy.orm import sessionmaker | ||
from src.api.main import app | ||
from src.api.database import get_db | ||
import os | ||
from sqlmodel import SQLModel, create_engine | ||
from pathlib import Path | ||
from sqlalchemy_utils.functions import ( | ||
drop_database, | ||
database_exists, | ||
create_database, | ||
) | ||
from src.api.create import DBCreationClient | ||
from os.path import exists | ||
import sys | ||
|
||
# Fixture to get data path from command line | ||
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--data-path", | ||
action="store", | ||
default="~/data/metadata/mini", | ||
help="Path to mini data directory", | ||
) | ||
|
||
@pytest.fixture(scope="session") | ||
def data_path(request): | ||
return request.config.getoption("--data-path") | ||
|
||
# Set up the database URL | ||
host = os.environ.get("DATABASE_HOST", "localhost") | ||
SQLALCHEMY_DATABASE_TEST_URL = f"postgresql://root:root@{host}:5432/test_db" | ||
|
||
# Fixture to create and drop the database | ||
@pytest.fixture(scope="session") | ||
def test_db(data_path): | ||
data_path = Path(data_path) | ||
client = DBCreationClient(SQLALCHEMY_DATABASE_TEST_URL) | ||
engine = client.create_database() | ||
client.create_cpf_summary(data_path) | ||
client.create_scenarios(data_path) | ||
client.create_shots(data_path) | ||
client.create_signals(data_path) | ||
client.create_sources(data_path) | ||
client.create_shot_source_links(data_path) | ||
|
||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
yield TestingSessionLocal() | ||
|
||
drop_database(SQLALCHEMY_DATABASE_TEST_URL) | ||
|
||
# Fixture to override the database dependency | ||
@pytest.fixture | ||
def override_get_db(test_db): | ||
def override(): | ||
try: | ||
db = test_db | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
app.dependency_overrides[get_db] = override | ||
|
||
# Fixture to create a client for testing | ||
@pytest.fixture(scope="module") | ||
def client(): | ||
with TestClient(app) as client: | ||
yield client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters