#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Tests for BaseComponent.
This module contains unit tests for the BaseComponent class and related functionality.
"""
import pytest
from pDESy.model.base_component import BaseComponent, BaseComponentState
from pDESy.model.base_task import BaseTask
from pDESy.model.base_workplace import BaseWorkplace
[docs]
def test_init():
"""Test initialization of BaseComponent."""
c1 = BaseComponent("c1")
assert c1.name == "c1"
assert len(c1.ID) > 0
assert c1.error_tolerance == 0.0
assert c1.error == 0.0
task = BaseTask("task")
c = BaseComponent(
"c",
ID="xx88xx",
child_component_id_set={c1.ID},
targeted_task_id_set={task.ID},
space_size=2.0,
state=BaseComponentState.FINISHED,
state_record_list=["aa"],
error_tolerance=0.1,
error=0.0,
placed_workplace_id=BaseWorkplace("t").ID,
placed_workplace_id_record_list=["fff"],
)
assert c.name == "c"
assert c.ID == "xx88xx"
assert c.child_component_id_set == {c1.ID}
assert c.targeted_task_id_set == {task.ID}
assert c.space_size == 2.0
assert c.placed_workplace_id_record_list == ["fff"]
assert c.error_tolerance == 0.1
assert c.error == 0.0
[docs]
def test_update_child_component_set():
"""Test updating the child component set."""
c = BaseComponent("c")
c1 = BaseComponent("c1")
c2 = BaseComponent("c2")
c.update_child_component_set({c1, c2})
assert c.child_component_id_set == {c1.ID, c2.ID}
[docs]
def test_add_child_component():
"""Test adding a child component."""
c = BaseComponent("c")
c1 = BaseComponent("c1")
c2 = BaseComponent("c2")
c.add_child_component(c1)
c1.add_child_component(c2)
assert c.child_component_id_set == {c1.ID}
assert c1.child_component_id_set == {c2.ID}
[docs]
def test_update_targeted_task_set():
"""Test updating the targeted task set."""
c = BaseComponent("c")
task1 = BaseTask("task1")
task2 = BaseTask("task2")
c.update_targeted_task_set({task1, task2})
assert c.targeted_task_id_set == {task1.ID, task2.ID}
assert task1.target_component_id == c.ID
assert task2.target_component_id == c.ID
[docs]
def test_update_error_value():
"""Test updating the error value."""
c = BaseComponent("c")
c.update_error_value(0.9, 1.0, seed=32) # seed==32 -> rand()=0.85
assert c.error == 0.0
c.update_error_value(0.4, 0.5, seed=32) # seed==32 -> rand()=0.85
assert c.error == 0.5
c.update_error_value(0.4, 0.5)
[docs]
def test_add_targeted_task():
"""Test adding a targeted task."""
c = BaseComponent("c")
task = BaseTask("task1")
assert task.target_component_id is None
c.add_targeted_task(task)
assert c.targeted_task_id_set == {task.ID}
assert task.target_component_id == c.ID
[docs]
def test_create_task():
"""Test creating a task from a component."""
c = BaseComponent("c")
task = c.create_task(name="task1")
assert isinstance(task, BaseTask)
assert task.name == "task1"
assert task.target_component_id == c.ID
assert c.targeted_task_id_set == {task.ID}
[docs]
def test_initialize():
"""Test initialization/reset of error value."""
c = BaseComponent("c", error_tolerance=0.1)
c.error += 1
assert c.error == 1
c.initialize()
assert c.error == 0
[docs]
def test_str():
"""Test string representation of BaseComponent."""
print(BaseComponent("c1"))
[docs]
def test_remove_insert_absence_time_list():
"""Test removing and inserting absence time list."""
w = BaseComponent("w1", "----")
w.placed_workplace_id_record_list = ["aa", "bb", "cc", "dd", "ee", "ff"]
w.state_record_list = [0, 1, 2, 3, 4, 5]
absence_time_list = [0, 1]
w.remove_absence_time_list(absence_time_list)
assert w.placed_workplace_id_record_list == ["cc", "dd", "ee", "ff"]
assert w.state_record_list == [2, 3, 4, 5]
w.insert_absence_time_list(absence_time_list)
assert w.placed_workplace_id_record_list == [
None,
None,
"cc",
"dd",
"ee",
"ff",
]
assert w.state_record_list == [
BaseComponentState.NONE,
BaseComponentState.READY,
2,
3,
4,
5,
]
[docs]
def test_get_time_list_for_gantt_chart():
"""Test getting time lists for Gantt chart visualization."""
w = BaseComponent("w1", "----")
w.state_record_list = [
BaseComponentState.NONE,
BaseComponentState.READY,
BaseComponentState.WORKING,
]
ready_time_list, working_time_list = w.get_time_list_for_gantt_chart()
assert ready_time_list == [(1, 1)]
assert working_time_list == [(2, 1)]
w.state_record_list = [
BaseComponentState.NONE,
BaseComponentState.READY,
BaseComponentState.READY,
]
ready_time_list, working_time_list = w.get_time_list_for_gantt_chart()
assert ready_time_list == [(1, 2)]
assert working_time_list == [(0, 0)]
w.state_record_list = [
BaseComponentState.NONE,
BaseComponentState.WORKING,
BaseComponentState.FINISHED,
]
ready_time_list, working_time_list = w.get_time_list_for_gantt_chart()
assert ready_time_list == [(0, 0)]
assert working_time_list == [(1, 1)]
# for backward
w.state_record_list = [
BaseComponentState.FINISHED,
BaseComponentState.WORKING,
BaseComponentState.READY,
BaseComponentState.READY,
BaseComponentState.FINISHED,
BaseComponentState.WORKING,
BaseComponentState.WORKING,
BaseComponentState.READY,
]
ready_time_list, working_time_list = w.get_time_list_for_gantt_chart()
assert ready_time_list == [(2, 2), (7, 1)]
assert working_time_list == [(1, 1), (5, 2)]
[docs]
@pytest.fixture(name="dummy_component")
def fixture_dummy_component():
"""Fixture for a dummy BaseComponent.
Returns:
BaseComponent: A dummy component instance.
"""
return BaseComponent("dummy_component")
[docs]
def test_print_mermaid_diagram(dummy_component):
"""Test the print_mermaid_diagram method.
Args:
dummy_component (BaseComponent): The dummy component fixture.
"""
dummy_component.print_mermaid_diagram(
subgraph=True,
)