test_yaml.py 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. yaml = pytest.importorskip('yaml')
  4. import datetime
  5. import pytz
  6. @pytest.mark.parametrize('source_object,yaml_string', [
  7. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04\n...\n'],
  8. [pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+02:00\n...\n'],
  9. [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+00:00\n...\n'],
  10. ])
  11. def test_to_yaml(source_object, yaml_string):
  12. assert yaml.dump(source_object) == yaml_string
  13. assert yaml.safe_dump(source_object) == yaml_string
  14. @pytest.mark.parametrize('expected_object,yaml_string', [
  15. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04'],
  16. [pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+02:00'],
  17. [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+00:00'],
  18. [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04Z'],
  19. ])
  20. def test_from_yaml(expected_object, yaml_string):
  21. try:
  22. assert expected_object == yaml.load(yaml_string)
  23. assert expected_object == yaml.safe_load(yaml_string)
  24. except TypeError, ex:
  25. if (isinstance(expected_object, datetime.datetime)
  26. and not expected_object.tzinfo is None
  27. and "can't compare offset-naive and offset-aware datetimes" in ex.message):
  28. pytest.xfail('pyyaml\'s loaders do not set datetime.tzinfo')
  29. else:
  30. raise ex