From 1d596eac2fea36228199bcb007600c9f0b267cf5 Mon Sep 17 00:00:00 2001 From: perillaroc Date: Tue, 13 Sep 2022 17:17:51 +0800 Subject: [PATCH] :sparkles: add free_dependencies function in Node. #44 :white_check_mark: add test for time attr. #43 --- takler/core/calendar.py | 1 + takler/core/node.py | 38 +++++++ tests/core/attribute/test_time_attr.py | 136 +++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 tests/core/attribute/test_time_attr.py diff --git a/takler/core/calendar.py b/takler/core/calendar.py index 1668d50..a076424 100644 --- a/takler/core/calendar.py +++ b/takler/core/calendar.py @@ -106,6 +106,7 @@ def day_of_year(self) -> int: def begin(self, time: datetime.datetime): """ Start to run calendar, set initial_time and clear duration. + Get current time and set it to initial_real_time and last_real_time. Parameters ---------- diff --git a/takler/core/node.py b/takler/core/node.py index a8918c2..625cdf8 100644 --- a/takler/core/node.py +++ b/takler/core/node.py @@ -15,6 +15,8 @@ from .repeat import Repeat, RepeatBase from .time_attr import TimeAttribute +from ._logger import logger + if TYPE_CHECKING: from .bunch import Bunch from .calendar import Calendar @@ -881,3 +883,39 @@ def resume(self): Resume the node. """ self.state.suspended = False + + def free_dependencies(self, dep_type: Optional[str] = None): + """ + Ignore some type of dependencies in Node. + + Parameters + ---------- + dep_type + dependency type: + + * all + * time + * trigger + """ + if dep_type is None: + dep_type = "all" + + if dep_type not in ("all", "time", "trigger"): + raise ValueError(f"dependency type {dep_type} is not supported.") + + free_time = False + free_trigger = False + + if dep_type == "all" or dep_type == "time": + free_time = True + if dep_type == "all" or dep_type == "trigger": + free_trigger = True + + if free_time: + for time_attr in self.times: + time_attr.set_free() + + if free_trigger: + logger.error("Free trigger is not implemented yet.") + + return diff --git a/tests/core/attribute/test_time_attr.py b/tests/core/attribute/test_time_attr.py new file mode 100644 index 0000000..e6c672a --- /dev/null +++ b/tests/core/attribute/test_time_attr.py @@ -0,0 +1,136 @@ +import datetime + +import pytest + +from takler.core import Flow, Task + + +class ObjectContainer: + pass + + +@pytest.fixture +def one_task_time_flow() -> ObjectContainer: + """ + |- flow1 + |- task1 + time 12:00 + """ + oc = ObjectContainer() + with Flow("flow1") as flow: + oc.flow = flow + with flow.add_task("task1") as task1: + task1.add_time(datetime.time(12, 0)) + oc.task1 = task1 + + return oc + + +TEST_TIME = datetime.datetime(2022, 9, 12, 10, 0, 0) + + +@pytest.fixture +def patch_datetime_now(monkeypatch): + """ + set ``datetime.datetime.now`` to a fixed time, 2022-09-12 10:00:01 + """ + class TestDateTime(datetime.datetime): + @classmethod + def now(cls, tz=None): + return TEST_TIME + + monkeypatch.setattr(datetime, "datetime", TestDateTime) + + +def test_time_attr_catch_time_point(one_task_time_flow, patch_datetime_now): + flow: Flow = one_task_time_flow.flow + task1: Task = one_task_time_flow.task1 + + start_time = datetime.datetime(2022, 9, 12, 10, 0, 0) + flow.calendar.begin(start_time) + # flow.calendar.flow_time = start_time + # flow.calendar.last_real_time = start_time + + flow.update_calendar(datetime.datetime(2022, 9, 12, 11, 0, 0)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 12, 0, 0)) + assert task1.times[0].free + assert task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 12, 1, 10)) + assert task1.times[0].free + + assert task1.resolve_time_dependencies() + + +def test_time_attr_miss_time_point(one_task_time_flow, patch_datetime_now): + flow: Flow = one_task_time_flow.flow + task1: Task = one_task_time_flow.task1 + + start_time = datetime.datetime(2022, 9, 12, 10, 0, 0) + flow.calendar.begin(start_time) + # flow.calendar.flow_time = start_time + # flow.calendar.last_real_time = start_time + + flow.update_calendar(datetime.datetime(2022, 9, 12, 11, 59, 0)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 12, 1, 0)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 13, 0, 10)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + +def test_time_attr_requeue(one_task_time_flow, patch_datetime_now): + flow: Flow = one_task_time_flow.flow + task1: Task = one_task_time_flow.task1 + + start_time = datetime.datetime(2022, 9, 12, 10, 0, 0) + flow.calendar.begin(start_time) + # flow.calendar.flow_time = start_time + # flow.calendar.last_real_time = start_time + + flow.update_calendar(datetime.datetime(2022, 9, 12, 11, 59, 0)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 12, 0, 0)) + assert task1.times[0].free + assert task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 13, 0, 10)) + assert task1.times[0].free + assert task1.resolve_time_dependencies() + + # SECTION: requeue + flow.requeue() + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + flow.update_calendar(datetime.datetime(2022, 9, 12, 12, 0, 0)) + assert task1.times[0].free + assert task1.resolve_time_dependencies() + + +def test_time_attr_free_dependencies(one_task_time_flow, patch_datetime_now): + flow: Flow = one_task_time_flow.flow + task1: Task = one_task_time_flow.task1 + + start_time = datetime.datetime(2022, 9, 12, 10, 0, 0) + flow.calendar.begin(start_time) + # flow.calendar.flow_time = start_time + # flow.calendar.last_real_time = start_time + + flow.update_calendar(datetime.datetime(2022, 9, 12, 11, 50, 0)) + assert not task1.times[0].free + assert not task1.resolve_time_dependencies() + + task1.free_dependencies(dep_type="time") + assert task1.times[0].free + assert task1.resolve_time_dependencies()