Skip to content

Commit

Permalink
✨ add free_dependencies function in Node. #44
Browse files Browse the repository at this point in the history
✅ add test for time attr. #43
  • Loading branch information
perillaroc committed Sep 13, 2022
1 parent 68fd157 commit 1d596ea
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions takler/core/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down
38 changes: 38 additions & 0 deletions takler/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
136 changes: 136 additions & 0 deletions tests/core/attribute/test_time_attr.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1d596ea

Please sign in to comment.