import unittest
import datetime

from dida import schemas, triggers
from marshmallow import ValidationError


class TestTriggerSchema(unittest.TestCase):
    def test_dump_trigger(self):
        result = schemas.TriggerSchema().dump(triggers.IntervalTrigger())
        print('IntervalTrigger dump:', result)

        result = schemas.TriggerSchema().dump(triggers.DateTrigger())
        print('DateTrigger dump:', result)

    def test_load_trigger(self):
        self.assertRaises(ValidationError, schemas.TriggerSchema().load, {"type": "unknown"})

        obj = schemas.TriggerSchema().load({'type': "interval"})
        self.assertIsInstance(obj, triggers.IntervalTrigger)

        obj = schemas.TriggerSchema().load({'type': 'date', "params": {'run_date': "2020-01-01 00:00:00"}})
        self.assertEqual(obj.run_date, datetime.datetime(2020, 1, 1).astimezone())

[((10, 46, 10, 72), 'dida.triggers.IntervalTrigger', 'triggers.IntervalTrigger', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((13, 46, 13, 68), 'dida.triggers.DateTrigger', 'triggers.DateTrigger', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((10, 17, 10, 40), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((13, 17, 13, 40), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((17, 43, 17, 66), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((19, 14, 19, 37), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((22, 14, 22, 37), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ({}, {}), '()', False, 'from dida import schemas, triggers\n'), ((23, 39, 23, 68), 'datetime.datetime', 'datetime.datetime', ({(23, 57, 23, 61): '(2020)', (23, 63, 23, 64): '(1)', (23, 66, 23, 67): '(1)'}, {}), '(2020, 1, 1)', False, 'import datetime\n')]