Skip to content

Commit

Permalink
wip: Tests, fixing linting issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
KamenDimitrov97 committed Aug 7, 2024
1 parent 8cf7ee8 commit 6b23817
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/dewret/renderers/snakemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def from_step(cls, step: BaseStep) -> "OutputDefinition":
output_file = step.arguments["output_file"]
if isinstance(output_file, Raw):
args = to_snakemake_type(output_file)
else:
else:
args = ""

return cls(output_file=args)
Expand Down
24 changes: 8 additions & 16 deletions tests/unit/cwl/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
from dewret.renderers.cwl import set_configuration, configuration


def test_default_configuration():
"""Test the default configuration."""
# Set default configuration
set_configuration({})

# Retrieve default settings

assert configuration("allow_complex_types") is False
assert configuration("factories_as_params") is False


def test_custom_configuration():
"""Test setting and retrieving custom configuration."""
# Set custom configuration
custom_config = {
"allow_complex_types": True,
"factories_as_params": True
}
custom_config = {"allow_complex_types": True, "factories_as_params": True}
set_configuration(custom_config)

# Retrieve custom settings

assert configuration("allow_complex_types") is True
assert configuration("factories_as_params") is True


def test_unknown_configuration_key():
"""Test retrieving an unknown configuration key."""
# Set some configuration
set_configuration({"allow_complex_types": True})

# Retrieve known key

assert configuration("allow_complex_types") is True

# Retrieve unknown key

try:
configuration("unknown_key")
except KeyError as e:
assert str(e) == "'Unknown configuration settings.'"
else:
raise AssertionError("Expected KeyError not raised.")

7 changes: 0 additions & 7 deletions tests/unit/cwl/test_input_output.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
from dewret.renderers.cwl import (
raw_to_command_input_schema,
to_output_schema,
_raw_to_command_input_schema_internal,
set_configuration
)

# def test_raw_to_command_input_schema_dict():
# set_configuration({})
# result = raw_to_command_input_schema("test_dict", {"a": 1, "b": [2, 3]})
Expand Down
23 changes: 14 additions & 9 deletions tests/unit/cwl/test_reference_definition.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
from dewret.renderers.cwl import ReferenceDefinition


class MockReference:
"""Mock class to simulate a `Reference` object."""

def __init__(self, name: str):
self.name = name


def test_reference_definition_from_reference():
"""Test the `from_reference` class method."""
ref = MockReference(name="test_step")

# Create a ReferenceDefinition from a Reference
ref_def = ReferenceDefinition.from_reference(ref)

# Assert the source is correctly set
assert ref_def.source == "test_step"


def test_reference_definition_render():
"""Test the `render` method."""
# Create a ReferenceDefinition instance
ref_def = ReferenceDefinition(source="test_step")

# Render the instance to a dict
rendered = ref_def.render()

# Expected rendered dict
expected = {"source": "test_step"}

# Assert the rendered dict is as expected
assert rendered == expected


def test_reference_definition_render_empty_source():
"""Test the `render` method with an empty source."""
# Create a ReferenceDefinition instance with an empty source
ref_def = ReferenceDefinition(source="")

# Render the instance to a dict
rendered = ref_def.render()

# Expected rendered dict
expected = {"source": ""}

# Assert the rendered dict is as expected
assert rendered == expected
assert rendered == expected
23 changes: 10 additions & 13 deletions tests/unit/cwl/test_step_definition.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from dewret.renderers.cwl import StepDefinition, Reference, ReferenceDefinition
from dewret.workflow import Raw


# Mock Task
class MockTask:
def __init__(self, name="mock_task"):
self.name = name


# Mock a Reference
class MockReference(Reference):
def __init__(self, name):
Expand All @@ -15,6 +17,7 @@ def __init__(self, name):
def name(self) -> str:
return self._name


# Mock a baseStep
class MockBaseStep:
def __init__(self, name="mock_step", task=None, return_type=str, arguments=None):
Expand All @@ -23,6 +26,7 @@ def __init__(self, name="mock_step", task=None, return_type=str, arguments=None)
self.return_type = return_type
self.arguments = arguments or {}


def test_step_definition_from_step_basic():
"""Test creating a StepDefinition from a simple BaseStep."""
step = MockBaseStep()
Expand All @@ -34,13 +38,11 @@ def test_step_definition_from_step_basic():
assert step_def.out == ["out"]
assert step_def.in_ == {}


def test_step_definition_from_step_with_inputs():
"""Test creating a StepDefinition with inputs."""
step = MockBaseStep(
arguments={
"in1": MockReference("input_ref"),
"in2": Raw("raw_value")
}
arguments={"in1": MockReference("input_ref"), "in2": Raw("raw_value")}
)

step_def = StepDefinition.from_step(step)
Expand All @@ -52,17 +54,15 @@ def test_step_definition_from_step_with_inputs():
assert isinstance(step_def.in_["in1"], ReferenceDefinition)
assert isinstance(step_def.in_["in2"], Raw)


def test_step_definition_render():
"""Test rendering a StepDefinition to a dict structure."""
# Create a mock StepDefinition
step_def = StepDefinition(
name="mock_step",
run="mock_task",
out=["out"],
in_={
"in1": ReferenceDefinition("input_ref"),
"in2": Raw("raw_value")
}
in_={"in1": ReferenceDefinition("input_ref"), "in2": Raw("raw_value")},
)

# Render the StepDefinition
Expand All @@ -71,9 +71,6 @@ def test_step_definition_render():
# Assert the rendered output is correct
assert rendered == {
"run": "mock_task",
"in": {
"in1": {"source": "input_ref"},
"in2": {"default": "raw_value"}
},
"out": ["out"]
"in": {"in1": {"source": "input_ref"}, "in2": {"default": "raw_value"}},
"out": ["out"],
}
34 changes: 19 additions & 15 deletions tests/unit/cwl/test_to_cwl_type.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
from unittest.mock import patch, MagicMock
from dewret.renderers.cwl import cwl_type_from_value, to_cwl_type, set_configuration, configuration
from dewret.tasks import factory
from dewret.workflow import Unset
from dewret.renderers.cwl import cwl_type_from_value, to_cwl_type, set_configuration
from queue import Queue
from collections import OrderedDict
from types import UnionType
from collections.abc import Iterable
from typing import Tuple, Union, List
from typing import Tuple, List

############################################
#
# Tests for `to_cwl_type` method in dewret.renderers.cwl
#
############################################


def return_union_type(a) -> UnionType:
return a


def test_cwl_basic_types():
integer = type(12)
boolean = type(False)
dictionary = type({})
fl = type(12.2)
string = type("12")
byt = type(bytes([104, 101, 108, 108, 111]))

assert "int" == to_cwl_type(integer)
assert "boolean" == to_cwl_type(boolean)
assert "record" == to_cwl_type(dictionary)
assert "float" == to_cwl_type(fl)
assert "string" == to_cwl_type(string)
assert "bytes" == to_cwl_type(byt)


def test_allow_complex_types():
custom_config = {
"allow_complex_types": True,
"factories_as_params": False
}
custom_config = {"allow_complex_types": True, "factories_as_params": False}
set_configuration(custom_config)

queue = Queue(1)
queue.put(4)

od = OrderedDict()
od['one'] = 1
od["one"] = 1
assert "Queue" == to_cwl_type(type(queue))
assert "OrderedDict" == to_cwl_type(type(od))

def test_union_type():

def test_union_type():
uti = return_union_type(4)
uts = return_union_type("4")

assert "int" == to_cwl_type(type(uti))
assert "string" == to_cwl_type(type(uts))


def test_list_with_multiple_basic_types():
# Set default configurations
set_configuration({})
typ = list[int]
result = to_cwl_type(typ)
assert result == {"type": "array", "items": "int"}


def test_list_with_single_basic_type():
# Set default configurations
set_configuration({})
result = to_cwl_type(List[int])
assert result == {"type": "array", "items": "int"}


def test_tuple_with_multiple_basic_types():
# Set default configurations
set_configuration({})
Expand All @@ -78,6 +77,7 @@ def test_tuple_with_multiple_basic_types():
}
assert result == expected, f"Expected {expected}, but got {result}"


def test_tuple_with_single_basic_type():
# Set default configurations
set_configuration({})
Expand All @@ -99,24 +99,28 @@ def test_cwl_type_from_int():
expected = "int"
assert result == expected


def test_cwl_type_from_float():
val = 3.14
result = cwl_type_from_value(val)
expected = "float"
assert result == expected


def test_cwl_type_from_str():
val = "hello"
result = cwl_type_from_value(val)
expected = "string"
assert result == expected


def test_cwl_type_from_bytes():
val = b"binary data"
result = cwl_type_from_value(val)
expected = "bytes"
assert result == expected


def test_cwl_type_from_dict():
val = {"key": "value"}
result = cwl_type_from_value(val)
Expand All @@ -126,7 +130,7 @@ def test_cwl_type_from_dict():

# Question: Why doesn't it work with complex types

# def test_cwl_type_from_list_of_ints():
# def test_cwl_type_from_list_of_ints():
# # Set default configurations
# set_configuration({})
# val = [1,2,3]
Expand All @@ -144,7 +148,7 @@ def test_cwl_type_from_dict():
# assert result == expected

# def test_cwl_type_from_unset():
# val = Unset
# val = Unset
# result = cwl_type_from_value(val)
# expected = "unset"
# print("**********************",result)
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/snakemake/test_helper_methods.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from dewret.renderers.snakemake import get_method_args, get_method_rel_path


def test_get_method_args():
def sample_func(a: int, b: str) -> None:
pass

args = get_method_args(sample_func)
assert list(args.parameters.keys()) == ['a', 'b']
assert args.parameters['a'].annotation == int
assert args.parameters['b'].annotation == str
assert list(args.parameters.keys()) == ["a", "b"]
assert args.parameters["a"].annotation == int
assert args.parameters["b"].annotation == str


def test_get_method_rel_path():
def sample_func():
Expand All @@ -16,4 +18,3 @@ def sample_func():
relative_path = get_method_rel_path(sample_func)
assert isinstance(relative_path, str)
assert relative_path.endswith("test_helper_methods")

Loading

0 comments on commit 6b23817

Please sign in to comment.