-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from nathanjmcdougall/feature/test_run_task_ref…
…actor New test folder structure for run_task unit tests
- Loading branch information
Showing
6 changed files
with
112 additions
and
66 deletions.
There are no files selected for viewing
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from pathlib import Path | ||
from typing import Literal | ||
|
||
import labtech | ||
import pytest | ||
from labtech.lab import Lab | ||
|
||
|
||
@pytest.fixture() | ||
def lab(tmp_path: Path) -> Lab: | ||
return Lab(storage=tmp_path, max_workers=1) | ||
|
||
|
||
class TestLab: | ||
class TestRunTasks: | ||
def test_no_tasks(self, lab: Lab) -> None: | ||
results = lab.run_tasks(tasks=[]) | ||
assert results == {} | ||
|
||
def test_simple_task(self, lab: Lab) -> None: | ||
results = lab.run_tasks(tasks=[_SimpleTask(a=1)]) | ||
assert results == {_SimpleTask(a=1): 1} | ||
|
||
def test_noparam_task(self, lab: Lab) -> None: | ||
results = lab.run_tasks(tasks=[_NoParamTask()]) | ||
assert results == {_NoParamTask(): 1} | ||
|
||
def test_duplicated_task(self, lab: Lab) -> None: | ||
"""Tests both duplicate parent tasks and duplicated child tasks.""" | ||
tasks = [ | ||
_ParentTask(child=_NoParamTask()), | ||
_ParentTask(child=_NoParamTask()), | ||
] | ||
results = lab.run_tasks(tasks) | ||
|
||
assert results == { | ||
_ParentTask(child=_NoParamTask()): 1, | ||
} | ||
|
||
for task in tasks: | ||
assert task.result_meta is not None | ||
assert task.child.result_meta is not None | ||
|
||
|
||
@labtech.task(cache=None) | ||
class _SimpleTask: | ||
a: int | ||
|
||
def run(self) -> int: | ||
return self.a | ||
|
||
|
||
@labtech.task(cache=None) | ||
class _NoParamTask: | ||
def run(self) -> Literal[1]: | ||
return 1 | ||
|
||
|
||
@labtech.task(cache=None) | ||
class _ParentTask: | ||
child: _NoParamTask | ||
|
||
def run(self) -> int: | ||
return self.child.result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sys | ||
|
||
from labtech.utils import LoggerFileProxy, OrderedSet | ||
from pytest_mock import MockerFixture | ||
|
||
|
||
class TestOrderedSet: | ||
def test_in(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'c', 'd']) | ||
assert 'a' in items | ||
|
||
def test_add(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'c', 'd']) | ||
items.add('e') | ||
assert 'e' in items | ||
assert list(items) == ['a', 'c', 'b', 'd', 'e'] | ||
|
||
def test_remove(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'd', 'e']) | ||
items.remove('d') | ||
assert 'd' not in items | ||
assert len(items) == 4 | ||
assert list(items) == ['a', 'c', 'b', 'e'] | ||
|
||
def test_str(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'e']) | ||
assert str(items) == '{a, c, b, e}' | ||
|
||
def test_repr(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'e']) | ||
assert repr(items) == "{'a', 'c', 'b', 'e'}" | ||
|
||
def test_concat(self) -> None: | ||
items = OrderedSet(['a', 'c', 'b', 'e']) | ||
extra_items = OrderedSet(['a', 'f']) | ||
combined_items = items + extra_items | ||
assert combined_items is not items | ||
assert combined_items is not extra_items | ||
assert list(combined_items) == ['a', 'c', 'b', 'e', 'f'] | ||
|
||
|
||
class TestLoggerFileProxy: | ||
def test_prefix_added(self, mocker: MockerFixture) -> None: | ||
logger_func = mocker.Mock() | ||
mocker.patch('sys.stdout', LoggerFileProxy(logger_func, 'some_prefix:')) | ||
print('some_message') | ||
sys.stdout.flush() | ||
logger_func.assert_called_with('some_prefix:some_message') |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.