Skip to content

Commit 42edec0

Browse files
committed
Refactor tests to use fixture files
1 parent dbe3bba commit 42edec0

File tree

14 files changed

+39
-14
lines changed

14 files changed

+39
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,6 @@ bruno/
232232
# Ruff
233233
# Ruff usually installs a .gitignore file in its directory.
234234
.ruff_cache/
235+
236+
# Local Allows
237+
!**/*.log

tests/cli/test_errors.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import pytest
24
from click.testing import CliRunner
35
from safir.testing.uvicorn import UvicornProcess
@@ -43,17 +45,18 @@ async def test_error_create_cli(uvicorn: UvicornProcess, api_version: str) -> No
4345
async def test_load_error_types_cli(uvicorn: UvicornProcess, api_version: str) -> None:
4446
"""Test `error_type` db table."""
4547

48+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
4649
client_config.service_url = f"{uvicorn.url}{config.asgi.prefix}/{api_version}"
4750
runner = CliRunner()
4851

49-
result = runner.invoke(client_top, "load error-types --yaml_file examples/error_types.yaml")
52+
result = runner.invoke(client_top, f"load error-types --yaml_file {fixtures}/error_types.yaml")
5053
assert result.exit_code == 0
5154

52-
result = runner.invoke(client_top, "load error-types --yaml_file examples/error_types.yaml")
55+
result = runner.invoke(client_top, f"load error-types --yaml_file {fixtures}/error_types.yaml")
5356
assert result.exit_code == 0
5457

5558
result = runner.invoke(
56-
client_top, "load error-types --allow_update --yaml_file examples/error_types.yaml"
59+
client_top, f"load error-types --allow_update --yaml_file {fixtures}/error_types.yaml"
5760
)
5861
assert result.exit_code == 0
5962

tests/cli/util_functions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import TypeAlias, TypeVar
23

34
import yaml
@@ -70,9 +71,10 @@ def create_tree(
7071
level: LevelEnum,
7172
uuid_int: int,
7273
) -> None:
74+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
7375
result = runner.invoke(
7476
client_top,
75-
"load specification --output yaml --yaml_file examples/empty_config.yaml",
77+
f"load specification --output yaml --yaml_file {fixtures}/empty_config.yaml",
7678
)
7779
# check_and_parse_result(result, models.Specification)
7880

tests/common/test_common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
async def test_common_bash() -> None:
2929
"""Test common.bash utilities"""
3030

31+
fixtures = Path(__file__).parent.parent / "fixtures" / "logs"
3132
the_script = await write_bash_script(
3233
"temp.sh",
3334
"ls",
@@ -47,10 +48,10 @@ async def test_common_bash() -> None:
4748
await Path("temp.stamp").unlink(missing_ok=True)
4849
await Path("temp.log").unlink(missing_ok=True)
4950

50-
bps_dict = await parse_bps_stdout("examples/bps_stdout.log")
51+
bps_dict = await parse_bps_stdout(f"{fixtures}/bps_stdout.log")
5152
assert bps_dict["run_id"].strip() == "334"
5253

53-
diag_message = await get_diagnostic_message("examples/bps_stdout.log")
54+
diag_message = await get_diagnostic_message(f"{fixtures}/bps_stdout.log")
5455
assert diag_message == "dummy: ada"
5556

5657

tests/db/test_campaign.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import uuid
3+
from pathlib import Path
34

45
import pytest
56
import structlog
@@ -26,6 +27,7 @@
2627
async def test_campaign_db(engine: AsyncEngine) -> None:
2728
"""Test `campaign` db table."""
2829

30+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
2931
db.handler.Handler.reset_cache()
3032

3133
# generate a uuid to avoid collisions
@@ -39,7 +41,7 @@ async def test_campaign_db(engine: AsyncEngine) -> None:
3941
await create_tree(session, LevelEnum.step, uuid_int)
4042

4143
# test the upsert mechanism
42-
await interface.load_specification(session, "examples/empty_config.yaml", allow_update=True)
44+
await interface.load_specification(session, f"{fixtures}/empty_config.yaml", allow_update=True)
4345

4446
with pytest.raises(IntegrityError):
4547
await db.Campaign.create_row(

tests/db/test_micro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async def test_micro_db(
2525
monkeypatch: MonkeyPatch,
2626
) -> None:
2727
"""Test fake end to end run using example/example_micro.yaml"""
28+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
2829
ScriptHandler = importlib.import_module("lsst.cmservice.handlers.script_handler").ScriptHandler
2930
interface = importlib.import_module("lsst.cmservice.handlers.interface")
3031
monkeypatch.setattr("lsst.cmservice.config.config.butler.mock", True)
@@ -36,7 +37,7 @@ async def test_micro_db(
3637
async with engine.begin():
3738
session = await create_async_session(engine, logger)
3839
os.environ["CM_CONFIGS"] = "examples"
39-
specification = await interface.load_specification(session, "examples/empty_config.yaml")
40+
specification = await interface.load_specification(session, f"{fixtures}/empty_config.yaml")
4041
check2 = await specification.get_block(session, "campaign")
4142
assert check2.name == "campaign"
4243

tests/db/test_pipetask_error_type.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import os
3+
from pathlib import Path
34

45
import pytest
56
import structlog
@@ -20,13 +21,14 @@ async def test_error_match_db(engine: AsyncEngine) -> None:
2021
fake error which is not in the database.
2122
"""
2223

24+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
2325
interface = importlib.import_module("lsst.cmservice.handlers.interface")
2426
functions = importlib.import_module("lsst.cmservice.handlers.functions")
2527
logger = structlog.get_logger(__name__)
2628
async with engine.begin():
2729
session = await create_async_session(engine, logger)
2830
os.environ["CM_CONFIGS"] = "examples"
29-
specification = await interface.load_specification(session, "examples/empty_config.yaml")
31+
specification = await interface.load_specification(session, f"{fixtures}/empty_config.yaml")
3032
check2 = await specification.get_block(session, "campaign")
3133
assert check2.name == "campaign"
3234

@@ -113,12 +115,13 @@ async def test_error_match_db(engine: AsyncEngine) -> None:
113115
async def test_error_type_db(engine: AsyncEngine) -> None:
114116
"""Test `error_type` db table."""
115117

118+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
116119
interface = importlib.import_module("lsst.cmservice.handlers.interface")
117120
logger = structlog.get_logger(__name__)
118121
async with engine.begin():
119122
session = await create_async_session(engine, logger)
120123
os.environ["CM_CONFIGS"] = "examples"
121-
specification = await interface.load_specification(session, "examples/empty_config.yaml")
124+
specification = await interface.load_specification(session, f"{fixtures}/empty_config.yaml")
122125
check2 = await specification.get_block(session, "campaign")
123126
assert check2.name == "campaign"
124127

tests/db/test_reports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib
22
import os
33
import uuid
4+
from pathlib import Path
45

56
import pytest
67
import structlog
@@ -19,6 +20,7 @@
1920
@pytest.mark.asyncio()
2021
async def test_reports_db(engine: AsyncEngine) -> None:
2122
"""Test `job` db table."""
23+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
2224
interface = importlib.import_module("lsst.cmservice.handlers.interface")
2325
functions = importlib.import_module("lsst.cmservice.handlers.functions")
2426

@@ -46,7 +48,7 @@ async def test_reports_db(engine: AsyncEngine) -> None:
4648

4749
await interface.load_error_types(
4850
session,
49-
"examples/error_types.yaml",
51+
f"{fixtures}/error_types.yaml",
5052
)
5153

5254
status_check = await functions.compute_job_status(

tests/db/util_functions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
from pathlib import Path
23
from typing import TypeAlias
34

45
import pytest
@@ -43,8 +44,9 @@ async def create_tree(
4344
level: LevelEnum,
4445
uuid_int: int,
4546
) -> None:
47+
fixtures = Path(__file__).parent.parent / "fixtures" / "seeds"
4648
interface = importlib.import_module("lsst.cmservice.handlers.interface")
47-
specification = await interface.load_specification(session, "examples/empty_config.yaml")
49+
specification = await interface.load_specification(session, f"{fixtures}/empty_config.yaml")
4850
_ = await specification.get_block(session, "campaign")
4951

5052
pname = f"prod0_{uuid_int}"

0 commit comments

Comments
 (0)