test_period_yaml.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import pytz
  4. import ioex.datetimeex
  5. import datetime
  6. yaml = pytest.importorskip('yaml')
  7. @pytest.mark.parametrize(('period', 'yaml_string'), [
  8. [
  9. ioex.datetimeex.Period(
  10. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  11. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  12. ),
  13. '!period\nstart: 2016-07-24T12:21:00\nend: 2016-07-24T12:22:13',
  14. ],
  15. [
  16. ioex.datetimeex.Period(
  17. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  18. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  19. ),
  20. '!period\nstart: 2016-07-24 12:21:00\nend: 2016-07-24 12:22:13',
  21. ],
  22. [
  23. ioex.datetimeex.Period(
  24. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  25. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  26. ),
  27. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013',
  28. ],
  29. [
  30. ioex.datetimeex.Period(
  31. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  32. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13, tzinfo = pytz.utc),
  33. ),
  34. '!period\nstart: 2016-07-24T12:20:00.025500\nend: 2016-07-24T12:21:00.000013Z',
  35. ],
  36. ])
  37. def test_from_yaml(period, yaml_string):
  38. if period.start.tzinfo or period.end.tzinfo:
  39. pytest.xfail('pyyaml ignores timezones when loading timestamps')
  40. assert period == yaml.load(yaml_string)
  41. assert period == yaml.safe_load(yaml_string)
  42. @pytest.mark.parametrize(('period', 'yaml_string'), [
  43. [
  44. ioex.datetimeex.Period(
  45. start = datetime.datetime(2016, 7, 24, 12, 21, 0),
  46. end = datetime.datetime(2016, 7, 24, 12, 22, 13),
  47. ),
  48. '!period\nend: 2016-07-24 12:22:13\nstart: 2016-07-24 12:21:00\n',
  49. ],
  50. [
  51. ioex.datetimeex.Period(
  52. start = datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  53. end = datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13),
  54. ),
  55. '!period\nend: 2016-07-24 12:21:00.000013\nstart: 2016-07-24 12:20:00.025500\n',
  56. ],
  57. [
  58. ioex.datetimeex.Period(
  59. start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 20, 0)),
  60. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13)),
  61. ),
  62. '!period\nend: 2016-07-24 12:21:00.000013+00:00\nstart: 2016-07-24 12:20:00+01:00\n',
  63. ],
  64. ])
  65. def test_to_yaml(period, yaml_string):
  66. assert yaml.dump(period) == yaml_string
  67. assert yaml.safe_dump(period) == yaml_string