From 4a64653644740f6d16c4f1f306b269b252818858 Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Mon, 8 Apr 2024 08:26:27 +1200 Subject: [PATCH 1/8] New test folder structure for run_task unit tests Signed-off-by: Nathan McDougall --- tests/labtech/test_lab.py | 57 +++++++++++++++++++++++++++++++++++++++ tests/test_lab.py | 34 ----------------------- 2 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 tests/labtech/test_lab.py delete mode 100644 tests/test_lab.py diff --git a/tests/labtech/test_lab.py b/tests/labtech/test_lab.py new file mode 100644 index 0000000..9065b3e --- /dev/null +++ b/tests/labtech/test_lab.py @@ -0,0 +1,57 @@ +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: + lab.run_tasks(tasks=[]) + + def test_simple_task(self, lab: Lab) -> None: + lab.run_tasks(tasks=[_SimpleTask(a=1)]) + + def test_noparam_task(self, lab: Lab) -> None: + lab.run_tasks(tasks=[_NoParamTask()]) + + 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 \ No newline at end of file diff --git a/tests/test_lab.py b/tests/test_lab.py deleted file mode 100644 index 7e76986..0000000 --- a/tests/test_lab.py +++ /dev/null @@ -1,34 +0,0 @@ -import labtech - - -def test_duplicated_task(): - """Tests both duplicate parent tasks and duplicated child tasks.""" - - @labtech.task(cache=None) - class ChildTask: - - def run(self): - return 1 - - @labtech.task(cache=None) - class ParentTask: - child: ChildTask - - def run(self): - return self.child.result - - tasks = [ - ParentTask(child=ChildTask()), - ParentTask(child=ChildTask()), - ] - - lab = labtech.Lab(storage=None, max_workers=1) - results = lab.run_tasks(tasks) - - assert results == { - ParentTask(child=ChildTask()): 1, - } - - for task in tasks: - assert task.result_meta is not None - assert task.child.result_meta is not None From e05dd52b14bd230551cbe8b80f377b1c575ab12a Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Mon, 8 Apr 2024 08:27:21 +1200 Subject: [PATCH 2/8] Add final newline Signed-off-by: Nathan McDougall --- tests/labtech/test_lab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/labtech/test_lab.py b/tests/labtech/test_lab.py index 9065b3e..357e120 100644 --- a/tests/labtech/test_lab.py +++ b/tests/labtech/test_lab.py @@ -54,4 +54,4 @@ class _ParentTask: child: _NoParamTask def run(self) -> int: - return self.child.result \ No newline at end of file + return self.child.result From dac9e4bd93bea682541a564f0ed79cfbdea0b0ca Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Mon, 8 Apr 2024 12:42:08 +1200 Subject: [PATCH 3/8] Further standardization of test suite Signed-off-by: Nathan McDougall --- .../test_init__.py} | 2 +- tests/labtech/test_utils.py | 47 +++++++++++++++++++ tests/test_utils.py | 32 ------------- 3 files changed, 48 insertions(+), 33 deletions(-) rename tests/{test_labtech.py => labtech/test_init__.py} (75%) create mode 100644 tests/labtech/test_utils.py delete mode 100644 tests/test_utils.py diff --git a/tests/test_labtech.py b/tests/labtech/test_init__.py similarity index 75% rename from tests/test_labtech.py rename to tests/labtech/test_init__.py index b43f757..d72dd34 100644 --- a/tests/test_labtech.py +++ b/tests/labtech/test_init__.py @@ -1,5 +1,5 @@ from labtech import __version__ -def test_version(): +def test_version__(): assert __version__ == '0.5.1' diff --git a/tests/labtech/test_utils.py b/tests/labtech/test_utils.py new file mode 100644 index 0000000..2ca7345 --- /dev/null +++ b/tests/labtech/test_utils.py @@ -0,0 +1,47 @@ +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') diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index 757affa..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,32 +0,0 @@ -import sys - -from labtech.utils import OrderedSet, LoggerFileProxy - - -def test_OrderedSet(): - items = OrderedSet(['a', 'c', 'b', 'c', 'd']) - - assert 'a' in items - items.add('e') - assert 'e' in items - items.remove('d') - assert 'd' not in items - - assert list(items) == ['a', 'c', 'b', 'e'] - assert str(items) == '{a, c, b, e}' - assert repr(items) == "{'a', 'c', 'b', 'e'}" - assert len(items) == 4 - - 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'] - - -def test_LoggerFileProxy(mocker): - 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') From 8dcbae3e78b5cf0bda499db233322027ffada969 Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Mon, 8 Apr 2024 12:43:48 +1200 Subject: [PATCH 4/8] Revert test function rename Signed-off-by: Nathan McDougall --- tests/labtech/test_init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/labtech/test_init__.py b/tests/labtech/test_init__.py index d72dd34..b43f757 100644 --- a/tests/labtech/test_init__.py +++ b/tests/labtech/test_init__.py @@ -1,5 +1,5 @@ from labtech import __version__ -def test_version__(): +def test_version(): assert __version__ == '0.5.1' From 044c6248df50b9f70abb65167d0a14c76eb0e684 Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Tue, 9 Apr 2024 11:38:16 +1200 Subject: [PATCH 5/8] Add missing __init__.py Signed-off-by: Nathan McDougall --- tests/labtech/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/labtech/__init__.py diff --git a/tests/labtech/__init__.py b/tests/labtech/__init__.py new file mode 100644 index 0000000..e69de29 From 4c52a9f3ece4046bee06f4bc065f4239ebbd5cbd Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Tue, 9 Apr 2024 21:53:08 +1200 Subject: [PATCH 6/8] Update tests/labtech/test_lab.py with asserts Co-authored-by: Dr Ben Denham --- tests/labtech/test_lab.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/labtech/test_lab.py b/tests/labtech/test_lab.py index 357e120..8bf31b7 100644 --- a/tests/labtech/test_lab.py +++ b/tests/labtech/test_lab.py @@ -13,13 +13,16 @@ def lab(tmp_path: Path) -> Lab: class TestLab: class TestRunTasks: def test_no_tasks(self, lab: Lab) -> None: - lab.run_tasks(tasks=[]) + results = lab.run_tasks(tasks=[]) + assert results == {} def test_simple_task(self, lab: Lab) -> None: - lab.run_tasks(tasks=[_SimpleTask(a=1)]) + results = lab.run_tasks(tasks=[_SimpleTask(a=1)]) + assert results == {_SimpleTask(a=1): 1} def test_noparam_task(self, lab: Lab) -> None: - lab.run_tasks(tasks=[_NoParamTask()]) + 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.""" From eef284ede5abd5767e057967abb6a9fcdae1712b Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Tue, 9 Apr 2024 21:58:58 +1200 Subject: [PATCH 7/8] Add missing blank lines to comply with flake8 E302 Signed-off-by: Nathan McDougall --- tests/labtech/test_lab.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/labtech/test_lab.py b/tests/labtech/test_lab.py index 8bf31b7..76aa0fa 100644 --- a/tests/labtech/test_lab.py +++ b/tests/labtech/test_lab.py @@ -10,6 +10,7 @@ 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: @@ -40,6 +41,7 @@ def test_duplicated_task(self, lab: Lab) -> None: assert task.result_meta is not None assert task.child.result_meta is not None + @labtech.task(cache=None) class _SimpleTask: a: int @@ -47,11 +49,13 @@ class _SimpleTask: 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 From a2b4748c591c40e756c8e11dcb89dc1a1643f97a Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Tue, 9 Apr 2024 22:04:03 +1200 Subject: [PATCH 8/8] Add missing blank line to comply with flake8 rule E302 Signed-off-by: Nathan McDougall --- tests/labtech/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/labtech/test_utils.py b/tests/labtech/test_utils.py index 2ca7345..a730f17 100644 --- a/tests/labtech/test_utils.py +++ b/tests/labtech/test_utils.py @@ -38,6 +38,7 @@ def test_concat(self) -> None: 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()