test_period_yaml.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import datetime
  4. import ioex.datetimeex
  5. import pytz
  6. yaml = pytest.importorskip('yaml')
  7. @pytest.mark.parametrize(('loader'), [yaml.Loader, yaml.SafeLoader])
  8. @pytest.mark.parametrize(('expected_period', 'yaml_string'), [
  9. [
  10. ioex.datetimeex.Period(
  11. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  12. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  13. ),
  14. '!period\nstart: 2016-07-24T12:21:00\nend: 2016-07-24T12:22:13',
  15. ],
  16. [
  17. ioex.datetimeex.Period(
  18. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  19. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  20. ),
  21. '!period\nstart: 2016-07-24 12:21:00\nend: 2016-07-24 12:22:13',
  22. ],
  23. [
  24. ioex.datetimeex.Period(
  25. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  26. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  27. ),
  28. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013',
  29. ],
  30. [
  31. ioex.datetimeex.Period(
  32. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  33. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  34. ),
  35. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013Z',
  36. ],
  37. [
  38. ioex.datetimeex.Period(
  39. start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 20, 0, microsecond = 25500)),
  40. end = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  41. ),
  42. '!period\nstart: 2016-01-24T12:20:00.025500Z\nend: 2016-07-24T12:21:00.000013+01:00',
  43. ],
  44. ])
  45. def test_from_yaml(expected_period, yaml_string, loader):
  46. class TestLoader(loader):
  47. pass
  48. ioex.datetimeex.Period.register_yaml_constructor(TestLoader)
  49. loaded_period = yaml.load(yaml_string, Loader = TestLoader)
  50. assert expected_period == loaded_period
  51. assert expected_period.start.utcoffset() == loaded_period.start.utcoffset()
  52. assert expected_period.end.utcoffset() == loaded_period.end.utcoffset()
  53. @pytest.mark.parametrize(('dumper'), [yaml.Dumper, yaml.SafeDumper])
  54. @pytest.mark.parametrize(('period', 'yaml_string'), [
  55. [
  56. ioex.datetimeex.Period(
  57. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  58. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  59. ),
  60. '!period\nend: 2016-07-24 12:22:13\nstart: 2016-07-24 12:21:00\n',
  61. ],
  62. [
  63. ioex.datetimeex.Period(
  64. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  65. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  66. ),
  67. '!period\nend: 2016-07-24 12:21:00.000013\nstart: 2016-07-24 12:20:00.025500\n',
  68. ],
  69. [
  70. ioex.datetimeex.Period(
  71. start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 20, 0)),
  72. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  73. ),
  74. '!period\nend: 2016-07-24 12:21:00.000013+00:00\nstart: 2016-07-24 12:20:00+01:00\n',
  75. ],
  76. ])
  77. def test_to_yaml(period, yaml_string, dumper):
  78. class TestDumper(dumper):
  79. pass
  80. ioex.datetimeex.Period.register_yaml_representer(TestDumper)
  81. assert yaml.dump(period, Dumper = TestDumper) == yaml_string