Skip to content

Commit

Permalink
Merge pull request #96 from genomic-medicine-sweden/dev-update-tests
Browse files Browse the repository at this point in the history
Update test suite
  • Loading branch information
erik-brink authored Jan 16, 2025
2 parents 443496b + a6a2a63 commit fe06490
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 153 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ inputs = ["NGPIris"]
pythonpath = [
"."
]
testpaths = [
"tests"
]
addopts = "--strict-markers" # Adds command-line options
filterwarnings = "ignore::urllib3.connectionpool.InsecureRequestWarning"

[project.scripts]
Expand Down
67 changes: 67 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

from pytest import Config, fixture, UsageError
from configparser import ConfigParser
from typing import Any, Generator
from shutil import rmtree

from NGPIris.hcp import HCPHandler

class CustomConfig:
"""A typed wrapper around pytest.Config for dynamic attributes."""
def __init__(self, pytest_config : Config):
self._config = pytest_config

@property
def hcp_h(self) -> HCPHandler:
"""Access the HCPHandler instance."""
return getattr(self._config, "hcp_h")

def __getattr__(self, name : str) -> Any:
"""Dynamically get attributes set during pytest configuration."""
return getattr(self._config, name)

def set_section(config : Config, parser : ConfigParser, section : str) -> None:
parse_dict = dict(parser.items(section))
for k, v in parse_dict.items():
setattr(config, k, v) # Adds attributes dynamically to pytest.Config

def pytest_addoption(parser) -> None:
parser.addoption(
"--config",
action="store",
default=None,
help="Path to the configuration file (e.g., path/to/config.ini)",
)

def pytest_configure(config : Config) -> None:
config_path = config.getoption("--config")
if not config_path:
raise UsageError("--config argument is required.")
else:
parser = ConfigParser()
parser.read(str(config_path))

# Add the INI parser to config
setattr(config, "parser", parser)

# Dynamically add an HCPHandler instance to config
setattr(config, "hcp_h", HCPHandler(parser.get("General", "credentials_path")))

# Dynamically add all key-value pairs from "HCP_tests" section
set_section(config, parser, "HCP_tests")

@fixture(scope = "session")
def hcp_result_path(pytestconfig : Config) -> str:
return pytestconfig.parser.get("HCP_tests", "result_path") # type: ignore

@fixture(scope = "session", autouse = True)
def clean_up_after_tests(hcp_result_path : str) -> Generator[None, Any, None]:
# Setup code can go here if needed
yield
# Teardown code
rmtree(hcp_result_path)

@fixture
def custom_config(pytestconfig : Config) -> CustomConfig:
"""Provide the typed wrapper for pytest.Config."""
return CustomConfig(pytestconfig)
10 changes: 7 additions & 3 deletions tests/test_conf_template.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[hcp_tests]
bucket =
data_test_file = 80MB_test_file
[General]
credentials_path =

[HCP_tests]
test_bucket =
test_file_path = tests/data/80MB_test_file
result_path = tests/data/results/
6 changes: 5 additions & 1 deletion tests/test_hci.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

from configparser import ConfigParser
from NGPIris.hci import HCIHandler
from random import randint
from json import dump
from os import remove

hci_h = HCIHandler("credentials/testCredentials.json")
ini_config = ConfigParser()
ini_config.read("tests/test_conf.ini")

hci_h = HCIHandler(ini_config.get("General", "credentials_path"))
hci_h.request_token()

def test_list_index_names_type() -> None:
Expand Down
Loading

0 comments on commit fe06490

Please sign in to comment.