Skip to content

Commit

Permalink
Add config tests (#75)
Browse files Browse the repository at this point in the history
* added tomli, moved pyside6

* added tests for the availability of tomli and the content of the toml config files.
Also added tests for the availability and use of the ApplicationEntities.json and MachineMap.json files
  • Loading branch information
sjswerdloff authored Sep 22, 2024
1 parent 7e7206e commit 665165a
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tdwii_plus_examples/tdwii_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def load_machine_map(path_to_machine_map=None):
if path_to_machine_map is not None:
machine_to_ae_config_file = path_to_machine_map
else:
machine_to_ae_config_file = "MachineToAE.json"
machine_to_ae_config_file = "MachineMap.json"
with open(machine_to_ae_config_file, "r") as f:
machine_to_ae_list = json.load(f)
for machine in machine_to_ae_list:
Expand Down
75 changes: 75 additions & 0 deletions tdwii_plus_examples/tests/test_ppvs_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os

import pytest


def test_tomli_availability():
try:
import tomli # noqa: F401
except ImportError:
pytest.fail("tomli package is not installed")


def test_toml_file_exists():
assert os.path.exists("ppvs.toml"), "ppvs.toml file does not exist"


def test_toml_file_readable():
assert os.access("ppvs.toml", os.R_OK), "ppvs.toml file is not readable"


def test_load_toml_file():
import tomli

with open("ppvs.toml", "rb") as f:
try:
config = tomli.load(f)
except tomli.TOMLDecodeError as e:
pytest.fail(f"Failed to parse TOML file: {str(e)}")

assert isinstance(config, dict), "Loaded TOML should be a dictionary"


def test_default_section_exists():
import tomli

with open("ppvs.toml", "rb") as f:
config = tomli.load(f)

assert "DEFAULT" in config, "DEFAULT section not found in TOML file"


def test_default_section_values():
import tomli

with open("ppvs.toml", "rb") as f:
config = tomli.load(f)

default = config["DEFAULT"]
assert default["ups_ae_title"] == "UPSSCP"
assert default["qr_ae_title"] == "QRSCP"
assert default["ae_title"] == "PPVS_SCP"
assert default["import_staging_directory"] == "~/ppvs_import_staging"
assert default["machine"] == "ProNova SC360 GR"


def test_no_extra_sections():
import tomli

with open("ppvs.toml", "rb") as f:
config = tomli.load(f)

assert len(config) == 1, "There should only be one section (DEFAULT) in the TOML file"


def test_no_listen_port():
import tomli

with open("ppvs.toml", "rb") as f:
config = tomli.load(f)

assert "listen_port" not in config["DEFAULT"], "listen_port should not be present (it's commented out)"


if __name__ == "__main__":
pytest.main([__file__])
66 changes: 66 additions & 0 deletions tdwii_plus_examples/tests/test_rtbdi_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

import pytest


def test_tomli_availability():
try:
import tomli # noqa: F401
except ImportError:
pytest.fail("tomli package is not installed")


def test_toml_file_exists():
assert os.path.exists("rtbdi.toml"), "rtbdi.toml file does not exist"


def test_toml_file_readable():
assert os.access("rtbdi.toml", os.R_OK), "rtbdi.toml file is not readable"


def test_load_toml_file():
import tomli

with open("rtbdi.toml", "rb") as f:
try:
config = tomli.load(f)
except tomli.TOMLDecodeError as e:
pytest.fail(f"Failed to parse TOML file: {str(e)}")

assert isinstance(config, dict), "Loaded TOML should be a dictionary"


def test_default_section_exists():
import tomli

with open("rtbdi.toml", "rb") as f:
config = tomli.load(f)

assert "DEFAULT" in config, "DEFAULT section not found in TOML file"


def test_default_section_values():
import tomli

with open("rtbdi.toml", "rb") as f:
config = tomli.load(f)

default = config["DEFAULT"]
assert default["qr_ae_title"] == "OST"
assert default["ae_title"] == "TMS"
assert default["export_staging_directory"] == "~/BDIFolder"
assert default["plan_path"] == "~/SamplePlanFolder"
assert default["ups_scp_ae_title"] == "TMS"


def test_no_extra_sections():
import tomli

with open("rtbdi.toml", "rb") as f:
config = tomli.load(f)

assert len(config) == 1, "There should only be one section (DEFAULT) in the TOML file"


if __name__ == "__main__":
pytest.main([__file__])
76 changes: 76 additions & 0 deletions tdwii_plus_examples/tests/test_tdd_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os

import pytest


def test_tomli_availability():
try:
import tomli # noqa: F401
except ImportError:
pytest.fail("tomli package is not installed")


def test_toml_file_exists():
assert os.path.exists("tdd.toml"), "tdd.toml file does not exist"


def test_toml_file_readable():
assert os.access("tdd.toml", os.R_OK), "tdd.toml file is not readable"


def test_load_toml_file():
import tomli

with open("tdd.toml", "rb") as f:
try:
config = tomli.load(f)
except tomli.TOMLDecodeError as e:
pytest.fail(f"Failed to parse TOML file: {str(e)}")

assert isinstance(config, dict), "Loaded TOML should be a dictionary"


def test_default_section_exists():
import tomli

with open("tdd.toml", "rb") as f:
config = tomli.load(f)

assert "DEFAULT" in config, "DEFAULT section not found in TOML file"


def test_default_section_values():
import tomli

with open("tdd.toml", "rb") as f:
config = tomli.load(f)

default = config["DEFAULT"]
assert default["ups_ae_title"] == "UPSSCP"
assert default["qr_ae_title"] == "QRSCP"
assert default["ae_title"] == "TDD"
assert default["import_staging_directory"] == "~/tdd_import_staging"
assert default["machine"] == "ProNova SC360 GR"


def test_no_extra_sections():
import tomli

with open("tdd.toml", "rb") as f:
config = tomli.load(f)

assert len(config) == 1, "There should only be one section (DEFAULT) in the TOML file"


def test_comment_preservation():
with open("tdd.toml", "r") as f:
content = f.read()

assert "# Our AE Title" in content, "Comment for AE Title should be preserved"
assert (
"# This directory contains the retrieved Composit IOD Instances" in content
), "Comment for import_staging_directory should be preserved"


if __name__ == "__main__":
pytest.main([__file__])
99 changes: 99 additions & 0 deletions tdwii_plus_examples/tests/test_tdwii_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import json

import pytest

from tdwii_plus_examples.tdwii_config import (
known_ae_ipaddr,
known_ae_port,
load_ae_config,
load_machine_map,
machine_ae_map,
)


@pytest.fixture
def sample_ae_config():
return [
{"AETitle": "IMS_IHERO_TMS1", "IPAddr": "10.11.255.8", "Port": 10401},
{"AETitle": "IHERO_SCP", "IPAddr": "10.11.255.8", "Port": 10403},
{"AETitle": "TMS", "IPAddr": "127.0.0.1", "Port": 11114},
]


@pytest.fixture
def sample_machine_map():
return [{"machine": "FX1", "AETitle": "IHERO_SCP"}, {"machine": "ProNova SC360 GR", "AETitle": "TDWII_SCP"}]


@pytest.fixture
def setup_config_files(tmp_path, sample_ae_config, sample_machine_map):
ae_config_file = tmp_path / "ApplicationEntities.json"
machine_map_file = tmp_path / "MachineMap.json"

with open(ae_config_file, "w") as f:
json.dump(sample_ae_config, f)

with open(machine_map_file, "w") as f:
json.dump(sample_machine_map, f)

return ae_config_file, machine_map_file


def test_load_ae_config(setup_config_files):
ae_config_file, _ = setup_config_files
load_ae_config(str(ae_config_file))

assert len(known_ae_ipaddr) == 3
assert len(known_ae_port) == 3
assert known_ae_ipaddr["IMS_IHERO_TMS1"] == "10.11.255.8"
assert known_ae_port["IMS_IHERO_TMS1"] == 10401
assert known_ae_ipaddr["TMS"] == "127.0.0.1"
assert known_ae_port["TMS"] == 11114


def test_load_ae_config_default_path(setup_config_files, monkeypatch):
ae_config_file, _ = setup_config_files
monkeypatch.chdir(ae_config_file.parent)
load_ae_config()

assert len(known_ae_ipaddr) == 3
assert len(known_ae_port) == 3


def test_load_machine_map(setup_config_files):
_, machine_map_file = setup_config_files
load_machine_map(str(machine_map_file))

assert len(machine_ae_map) == 2
assert machine_ae_map["FX1"] == "IHERO_SCP"
assert machine_ae_map["ProNova SC360 GR"] == "TDWII_SCP"


def test_load_machine_map_default_path(setup_config_files, monkeypatch):
_, machine_map_file = setup_config_files
monkeypatch.chdir(machine_map_file.parent)
load_machine_map()

assert len(machine_ae_map) == 2


def test_integration(setup_config_files):
ae_config_file, machine_map_file = setup_config_files
load_ae_config(str(ae_config_file))
load_machine_map(str(machine_map_file))

assert machine_ae_map["FX1"] == "IHERO_SCP"
assert known_ae_ipaddr[machine_ae_map["FX1"]] == "10.11.255.8"
assert known_ae_port[machine_ae_map["FX1"]] == 10403


def test_nonexistent_file():
with pytest.raises(FileNotFoundError):
load_ae_config("nonexistent_file.json")

with pytest.raises(FileNotFoundError):
load_machine_map("nonexistent_file.json")


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 665165a

Please sign in to comment.