Skip to content
Snippets Groups Projects
Commit a99f9b76 authored by lukas leufen's avatar lukas leufen
Browse files

shift tests for RunEnvironment, PyTestRegex class is now part of the helpers functions

parent 2a101b80
Branches
Tags
2 merge requests!17update to v0.4.0,!15new feat split subsets
import re
__author__ = 'Lukas Leufen' __author__ = 'Lukas Leufen'
__date__ = '2019-10-21' __date__ = '2019-10-21'
...@@ -180,3 +182,16 @@ def set_experiment_name(experiment_date=None, experiment_path=None): ...@@ -180,3 +182,16 @@ def set_experiment_name(experiment_date=None, experiment_path=None):
else: else:
experiment_path = os.path.abspath(experiment_path) experiment_path = os.path.abspath(experiment_path)
return experiment_name, 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
import logging import logging
from src.modules import run, PreProcessing from src.modules.pre_processing import PreProcessing
from src.helpers import TimeTracking from src.helpers import TimeTracking, PyTestRegex
from src.modules.experiment_setup import ExperimentSetup from src.modules.experiment_setup import ExperimentSetup
from src.data_generator import DataGenerator from src.data_generator import DataGenerator
import re
import mock import mock
import numpy as np 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: class TestPreProcessing:
def test_init(self, caplog): def test_init(self, caplog):
...@@ -49,7 +16,7 @@ class TestPreProcessing: ...@@ -49,7 +16,7 @@ class TestPreProcessing:
pre = PreProcessing(setup) pre = PreProcessing(setup)
assert caplog.record_tuples[0] == ('root', 20, 'PreProcessing started') 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, '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): def test_run(self):
pre_processing = object.__new__(PreProcessing) pre_processing = object.__new__(PreProcessing)
...@@ -73,7 +40,7 @@ class TestPreProcessing: ...@@ -73,7 +40,7 @@ class TestPreProcessing:
valids = pre.check_valid_stations(pre.setup.__dict__, kwargs, pre.setup.stations) valids = pre.check_valid_stations(pre.setup.__dict__, kwargs, pre.setup.stations)
assert valids == pre.setup.stations assert valids == pre.setup.stations
assert caplog.record_tuples[0] == ('root', 20, 'check valid stations started') 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): def test_update_kwargs(self):
args = {"testName": {"testAttribute": "TestValue", "optional": "2019-11-21"}} args = {"testName": {"testAttribute": "TestValue", "optional": "2019-11-21"}}
......
import pytest
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"))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment