spiff-arena/tests/SpiffWorkflow/bpmn/events/TimeDurationParseTest.py
Dan b8d3d5d84b Squashed 'SpiffWorkflow/' changes from 450ef3bcd..98c6294f1
98c6294f1 Merge pull request #287 from sartography/feature/workflow_data_exceptions
d40a1da59 Workflow Data Exceptions were broken in the previous error refactor.  This assures we are getting good messages from these errors.
a156378e1 Merge pull request #286 from sartography/feature/inclusive-gateway-support
7f6e398c2 bypass unnecessary checks in gateway joins
ade21a894 revert a few things
e1cf75202 Merge branch 'main' into feature/inclusive-gateway-support
15a0a4414 revert change to MultiChoice and handle no defaults in BPMN specs
e1469e6bb add support for diverging inclusive gateways
71fd86386 really prevent non-default flows without conditions
924759d9b clean up join specs
7378639d3 Merge pull request #284 from sartography/feature/improved-timer-events
dc8d139d2 remove useless method
530f23697 Merge branch 'main' into feature/improved-timer-events
307cca9c5 partially clean up existing gateways
0a344285e clean up task parsers
2cef997d1 add waiting_events method to bpmn workflow
48091c407 serializer migration script and miscellaneous fixes to serialization
61316854b store internal timer data as string/float
389c14c4c add some tests for parsing durations
582bc9482 convert timers to iso 8601
6dfd7ebe9 remove extraneous calls to update
6bd429529 clean up tests
d56e9912f remove useless method

git-subtree-dir: SpiffWorkflow
git-subtree-split: 98c6294f1240aee599cd98bcee58d121cb57b331
2023-01-26 18:17:35 -05:00

60 lines
3.6 KiB
Python

import unittest
from datetime import datetime
from SpiffWorkflow.bpmn.specs.events.event_definitions import TimerEventDefinition
class TimeDurationParseTest(unittest.TestCase):
"Non-exhaustive ISO durations, but hopefully covers basic support"
def test_parse_duration(self):
valid = [
("P1Y6M1DT1H1M1S", {'years': 1, 'months': 6, 'days': 1, 'hours': 1, 'minutes': 1, 'seconds': 1 }), # everything
("P1Y6M1DT1H1M1.5S", {'years': 1, 'months': 6, 'days': 1, 'hours': 1, 'minutes': 1, 'seconds': 1.5 }), # fractional seconds
("P1YT1H1M1S", {'years': 1, 'hours': 1, 'minutes': 1, 'seconds': 1 }), # minutes but no month
("P1MT1H", {'months': 1, 'hours':1}), # months but no minutes
("P4W", {'weeks': 4}), # weeks
("P1Y6M1D", {'years': 1, 'months': 6, 'days': 1}), # no time
("PT1H1M1S", {'hours': 1,'minutes': 1,'seconds': 1}), # time only
("PT1.5H", {'hours': 1.5}), # alt fractional
("T1,5H", {'hours': 1.5}), # fractional with comma
("PDT1H1M1S", {'hours': 1, 'minutes': 1, 'seconds': 1}), # empty spec
("PYMDT1H1M1S", {'hours': 1, 'minutes': 1, 'seconds': 1}), # multiple empty
]
for duration, parsed_duration in valid:
result = TimerEventDefinition.parse_iso_duration(duration)
self.assertDictEqual(result, parsed_duration)
invalid = [
"PT1.5H30S", # fractional duration with subsequent non-fractional
"PT1,5H30S", # with comma
"P1H1M1S", # missing 't'
"P1DT", # 't' without time spec
"P1W1D", # conflicting day specs
"PT1H1M1", # trailing values
]
for duration in invalid:
self.assertRaises(Exception, TimerEventDefinition.parse_iso_duration, duration)
def test_calculate_timedelta_from_start(self):
start, one_day = datetime.fromisoformat("2023-01-01"), 24 * 3600
# Leap years
self.assertEqual(TimerEventDefinition.get_timedelta_from_start({'years': 1}, start).total_seconds(), 365 * one_day)
self.assertEqual(TimerEventDefinition.get_timedelta_from_start({'years': 2}, start).total_seconds(), (365 + 366) * one_day)
# Increment by month does not change day
for month in range(1, 13):
dt = start + TimerEventDefinition.get_timedelta_from_start({'months': month}, start)
self.assertEqual(dt.day, 1)
def test_calculate_timedelta_from_end(self):
end, one_day = datetime.fromisoformat("2025-01-01"), 24 * 3600
# Leap years
self.assertEqual(TimerEventDefinition.get_timedelta_from_end({'years': 1}, end).total_seconds(), 366 * one_day)
self.assertEqual(TimerEventDefinition.get_timedelta_from_end({'years': 2}, end).total_seconds(), (365 + 366) * one_day)
dt = end - TimerEventDefinition.get_timedelta_from_end({'months': 11}, end)
# Decrement by month does not change day
for month in range(1, 13):
dt = end - TimerEventDefinition.get_timedelta_from_end({'months': month}, end)
self.assertEqual(dt.day, 1)