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, 'dida.triggers.IntervalTrigger', 'triggers.IntervalTrigger', False, 'from dida import schemas, triggers\n'), (13, 'dida.triggers.DateTrigger', 'triggers.DateTrigger', False, 'from dida import schemas, triggers\n'), (10, 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', False, 'from dida import schemas, triggers\n'), (13, 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', False, 'from dida import schemas, triggers\n'), (17, 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', False, 'from dida import schemas, triggers\n'), (19, 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', False, 'from dida import schemas, triggers\n'), (22, 'dida.schemas.TriggerSchema', 'schemas.TriggerSchema', False, 'from dida import schemas, triggers\n'), (23, 'datetime.datetime', 'datetime.datetime', False, 'import datetime\n')]