test_period_yaml.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. loaded_period = yaml.load(yaml_string, Loader = loader_copy)
  49. assert expected_period == loaded_period
  50. assert expected_period.start.utcoffset() == loaded_period.start.utcoffset()
  51. assert expected_period.end.utcoffset() == loaded_period.end.utcoffset()
  52. @pytest.mark.parametrize(('period', 'yaml_string'), [
  53. [
  54. ioex.datetimeex.Period(
  55. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  56. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  57. ),
  58. '!period\nend: 2016-07-24 12:22:13\nstart: 2016-07-24 12:21:00\n',
  59. ],
  60. [
  61. ioex.datetimeex.Period(
  62. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  63. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  64. ),
  65. '!period\nend: 2016-07-24 12:21:00.000013\nstart: 2016-07-24 12:20:00.025500\n',
  66. ],
  67. [
  68. ioex.datetimeex.Period(
  69. start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 20, 0)),
  70. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  71. ),
  72. '!period\nend: 2016-07-24 12:21:00.000013+00:00\nstart: 2016-07-24 12:20:00+01:00\n',
  73. ],
  74. ])
  75. def test_to_yaml(period, yaml_string):
  76. assert yaml.dump(period) == yaml_string
  77. assert yaml.safe_dump(period) == yaml_string