Skip to content

Commit

Permalink
Merge pull request #9 from nathanjmcdougall/feature/test_run_task_ref…
Browse files Browse the repository at this point in the history
…actor

New test folder structure for run_task unit tests
  • Loading branch information
ben-denham authored Apr 9, 2024
2 parents a8573ad + a2b4748 commit dbb5e96
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 66 deletions.
Empty file added tests/labtech/__init__.py
Empty file.
File renamed without changes.
64 changes: 64 additions & 0 deletions tests/labtech/test_lab.py
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
48 changes: 48 additions & 0 deletions tests/labtech/test_utils.py
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')
34 changes: 0 additions & 34 deletions tests/test_lab.py

This file was deleted.

32 changes: 0 additions & 32 deletions tests/test_utils.py

This file was deleted.

0 comments on commit dbb5e96

Please sign in to comment.