diff --git a/src/helpers.py b/src/helpers.py index 83c5dcf44ad6462b9469eaf218426d43f7b2e91e..1b635a2e9cd00bfbb3650a5a7a8768378cfcdb6b 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -1,3 +1,5 @@ +import re + __author__ = 'Lukas Leufen' __date__ = '2019-10-21' @@ -180,3 +182,16 @@ def set_experiment_name(experiment_date=None, experiment_path=None): else: experiment_path = os.path.abspath(experiment_path) return experiment_name, experiment_path + + +class PyTestRegex: + """Assert that a given string meets some expectations.""" + + def __init__(self, pattern: str, flags: int = 0): + self._regex = re.compile(pattern, flags) + + def __eq__(self, actual: str) -> bool: + return bool(self._regex.match(actual)) + + def __repr__(self) -> str: + return self._regex.pattern diff --git a/test/test_modules.py b/test/test_modules.py index 8437c9129737ff91d914af42653b0467d635123b..b2add462a973e01d90e4bccb3252e2d42233791a 100644 --- a/test/test_modules.py +++ b/test/test_modules.py @@ -1,45 +1,12 @@ import logging -from src.modules import run, PreProcessing -from src.helpers import TimeTracking +from src.modules.pre_processing import PreProcessing +from src.helpers import TimeTracking, PyTestRegex from src.modules.experiment_setup import ExperimentSetup from src.data_generator import DataGenerator -import re import mock import numpy as np -class pytest_regex: - """Assert that a given string meets some expectations.""" - - def __init__(self, pattern, flags=0): - self._regex = re.compile(pattern, flags) - - def __eq__(self, actual): - return bool(self._regex.match(actual)) - - def __repr__(self): - return self._regex.pattern - - -class TestRun: - - def test_enter_exit(self, caplog): - caplog.set_level(logging.INFO) - with run() as r: - assert caplog.record_tuples[-1] == ('root', 20, 'run started') - assert isinstance(r.time, TimeTracking) - r.do_stuff(0.1) - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r"run finished after \d+\.\d+s")) - - def test_init_del(self, caplog): - caplog.set_level(logging.INFO) - r = run() - assert caplog.record_tuples[-1] == ('root', 20, 'run started') - r.do_stuff(0.2) - del r - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r"run finished after \d+\.\d+s")) - - class TestPreProcessing: def test_init(self, caplog): @@ -49,7 +16,7 @@ class TestPreProcessing: pre = PreProcessing(setup) assert caplog.record_tuples[0] == ('root', 20, 'PreProcessing started') assert caplog.record_tuples[1] == ('root', 20, 'check valid stations started') - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r'run for \d+\.\d+s to check 5 station\(s\)')) + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r'run for \d+\.\d+s to check 5 station\(s\)')) def test_run(self): pre_processing = object.__new__(PreProcessing) @@ -73,7 +40,7 @@ class TestPreProcessing: valids = pre.check_valid_stations(pre.setup.__dict__, kwargs, pre.setup.stations) assert valids == pre.setup.stations assert caplog.record_tuples[0] == ('root', 20, 'check valid stations started') - assert caplog.record_tuples[1] == ('root', 20, pytest_regex(r'run for \d+\.\d+s to check 5 station\(s\)')) + assert caplog.record_tuples[1] == ('root', 20, PyTestRegex(r'run for \d+\.\d+s to check 5 station\(s\)')) def test_update_kwargs(self): args = {"testName": {"testAttribute": "TestValue", "optional": "2019-11-21"}} diff --git a/test/test_modules/__init__.py b/test/test_modules/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/test_modules/test_experiment_setup.py b/test/test_modules/test_experiment_setup.py new file mode 100644 index 0000000000000000000000000000000000000000..65ec052b98b85fae0c0468904fc8fe72390891fd --- /dev/null +++ b/test/test_modules/test_experiment_setup.py @@ -0,0 +1,4 @@ + +import pytest + + diff --git a/test/test_modules/test_run_environment.py b/test/test_modules/test_run_environment.py new file mode 100644 index 0000000000000000000000000000000000000000..ce5f995e54df1ffc93c80c191376347c5f0b3741 --- /dev/null +++ b/test/test_modules/test_run_environment.py @@ -0,0 +1,31 @@ +import logging + +from src.helpers import TimeTracking, PyTestRegex +from src.modules.run_environment import RunEnvironment + + +class TestRunEnvironment: + + def test_enter(self, caplog): + caplog.set_level(logging.INFO) + with RunEnvironment() as r: + assert caplog.record_tuples[-1] == ('root', 20, 'RunEnvironment started') + assert isinstance(r.time, TimeTracking) + + def test_exit(self, caplog): + caplog.set_level(logging.INFO) + with RunEnvironment() as r: + r.do_stuff(0.1) + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r"RunEnvironment finished after \d+\.\d+s")) + + def test_init(self, caplog): + caplog.set_level(logging.INFO) + r = RunEnvironment() + assert caplog.record_tuples[-1] == ('root', 20, 'RunEnvironment started') + + def test_del(self, caplog): + caplog.set_level(logging.INFO) + r = RunEnvironment() + r.do_stuff(0.2) + del r + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r"RunEnvironment finished after \d+\.\d+s"))