Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test suite #96

Merged
merged 14 commits into from
Jan 16, 2025
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
Loading