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())

[((233, 259), 'dida.triggers.IntervalTrigger', 'triggers.IntervalTrigger', ([], {}), '()\n', (257, 259), False, 'from dida import schemas, triggers\n'), ((355, 377), 'dida.triggers.DateTrigger', 'triggers.DateTrigger', ([], {}), '()\n', (375, 377), False, 'from dida import schemas, triggers\n'), ((204, 227), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ([], {}), '()\n', (225, 227), False, 'from dida import schemas, triggers\n'), ((326, 349), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ([], {}), '()\n', (347, 349), False, 'from dida import schemas, triggers\n'), ((499, 522), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ([], {}), '()\n', (520, 522), False, 'from dida import schemas, triggers\n'), ((565, 588), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ([], {}), '()\n', (586, 588), False, 'from dida import schemas, triggers\n'), ((692, 715), 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', ([], {}), '()\n', (713, 715), False, 'from dida import schemas, triggers\n'), ((825, 854), 'datetime.datetime', 'datetime.datetime', (['(2020)', '(1)', '(1)'], {}), '(2020, 1, 1)\n', (842, 854), False, 'import datetime\n')]