1
0

test_period_yaml.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import copy
  4. import datetime
  5. import ioex.datetimeex
  6. import pytz
  7. yaml = pytest.importorskip('yaml')
  8. @pytest.mark.parametrize(('loader'), [yaml.Loader, yaml.SafeLoader])
  9. @pytest.mark.parametrize(('expected_period', 'yaml_string'), [
  10. [
  11. ioex.datetimeex.Period(
  12. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  13. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  14. ),
  15. '!period\nstart: 2016-07-24T12:21:00\nend: 2016-07-24T12:22:13',
  16. ],
  17. [
  18. ioex.datetimeex.Period(
  19. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  20. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  21. ),
  22. '!period\nstart: 2016-07-24 12:21:00\nend: 2016-07-24 12:22:13',
  23. ],
  24. [
  25. ioex.datetimeex.Period(
  26. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  27. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  28. ),
  29. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013',
  30. ],
  31. [
  32. ioex.datetimeex.Period(
  33. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  34. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  35. ),
  36. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013Z',
  37. ],
  38. [
  39. ioex.datetimeex.Period(
  40. start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 20, 0, microsecond = 25500)),
  41. end = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  42. ),
  43. '!period\nstart: 2016-01-24T12:20:00.025500Z\nend: 2016-07-24T12:21:00.000013+01:00',
  44. ],
  45. ])
  46. def test_from_yaml(expected_period, yaml_string, loader):
  47. loader_copy = copy.deepcopy(loader)
  48. ioex.datetimeex.Period.register_yaml_constructor(loader_copy)
  49. loaded_period = yaml.load(yaml_string, Loader = loader_copy)
  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. dumper_copy = copy.deepcopy(dumper)
  79. ioex.datetimeex.Period.register_yaml_representer(dumper_copy)
  80. assert yaml.dump(period, Dumper = dumper_copy) == yaml_string