1
0

test_yaml_timestamp_constructor.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. yaml = pytest.importorskip('yaml')
  4. import datetime
  5. import dateutil.tz.tz
  6. import ioex.datetimeex
  7. import pytz
  8. @pytest.mark.parametrize(('loader'), [yaml.Loader, yaml.SafeLoader])
  9. @pytest.mark.parametrize(('yaml_string', 'expected_timestamp'), [
  10. ['2016-07-14 13:50:04', datetime.datetime(2016, 7, 14, 13, 50, 4, 0)],
  11. ['2016-07-14 13:50:04Z', pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0))],
  12. ['2016-07-14 13:50:04Z', datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = dateutil.tz.tz.tzutc())],
  13. ['2016-01-14 13:50:04+01:00', pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 1, 14, 13, 50, 4, 0))],
  14. ['2016-07-14 13:50:04+02:00', pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0))],
  15. ['2016-07-14 13:50:04+02:00', datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = dateutil.tz.tz.tzoffset('Vienna', 2 * 60 * 60))],
  16. ['2016-07-14 13:50:04-07:00', pytz.timezone('US/Pacific').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0))],
  17. ])
  18. def test_from_yaml(yaml_string, expected_timestamp, loader):
  19. # create subclass so call to class method does not interfere with other tests
  20. # see yaml.BaseConstructor.add_constructor()
  21. class TestLoader(loader):
  22. pass
  23. ioex.datetimeex.register_yaml_timestamp_constructor(TestLoader)
  24. loaded_timestamp = yaml.load(yaml_string, Loader = TestLoader)
  25. assert loaded_timestamp == expected_timestamp
  26. assert loaded_timestamp.utcoffset() == expected_timestamp.utcoffset()
  27. @pytest.mark.parametrize(('yaml_string', 'tag', 'expected_timestamp'), [
  28. ['!without_timezone 2016-07-14 13:50:04', '!without_timezone', datetime.datetime(2016, 7, 14, 13, 50, 4, 0)],
  29. ['!datetime 2016-07-14 13:50:04Z', '!datetime', pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0))],
  30. ])
  31. def test_from_yaml_tag(yaml_string, tag, expected_timestamp):
  32. # create subclass so call to class method does not interfere with other tests
  33. # see yaml.BaseConstructor.add_constructor()
  34. class TestLoader(yaml.SafeLoader):
  35. pass
  36. ioex.datetimeex.register_yaml_timestamp_constructor(TestLoader, tag = tag)
  37. assert yaml.load(yaml_string, Loader = TestLoader) == expected_timestamp
  38. @pytest.mark.parametrize(('yaml_string', 'expected_timestamp'), [
  39. ['2016-07-14 13:50:04', datetime.datetime(2016, 7, 14, 13, 50, 4, 0)],
  40. ['2016-07-14 13:50:04Z', pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0))],
  41. ])
  42. def test_from_yaml_repeat(yaml_string, expected_timestamp):
  43. # create subclass so call to class method does not interfere with other tests
  44. # see yaml.BaseRepresenter.add_representer()
  45. class TestLoader(yaml.SafeLoader):
  46. pass
  47. ioex.datetimeex.register_yaml_timestamp_constructor(TestLoader)
  48. assert yaml.load(yaml_string, Loader = TestLoader) == expected_timestamp
  49. ioex.datetimeex.register_yaml_timestamp_constructor(TestLoader)
  50. assert yaml.load(yaml_string, Loader = TestLoader) == expected_timestamp