فهرست منبع

added tests for yaml import and export of datetime instances

Fabian Peter Hammerle 9 سال پیش
والد
کامیت
091f94ab1f
1فایلهای تغییر یافته به همراه33 افزوده شده و 0 حذف شده
  1. 33 0
      tests/test_yaml.py

+ 33 - 0
tests/test_yaml.py

@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+import pytz
+import datetime
+yaml = pytest.importorskip('yaml')
+
+@pytest.mark.parametrize('source_object,yaml_string', [
+    [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04\n...\n'],
+    [pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+02:00\n...\n'],
+    [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+00:00\n...\n'],
+    ])
+def test_to_yaml(source_object, yaml_string):
+    assert yaml.dump(source_object) == yaml_string
+    assert yaml.safe_dump(source_object) == yaml_string
+
+@pytest.mark.parametrize('expected_object,yaml_string', [
+    [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04'],
+    [pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+02:00'],
+    [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04+00:00'],
+    [pytz.utc.localize(datetime.datetime(2016, 7, 14, 13, 50, 4, 0)), '2016-07-14 13:50:04Z'],
+    ])
+def test_from_yaml(expected_object, yaml_string):
+    try:
+        assert expected_object == yaml.load(yaml_string)
+        assert expected_object == yaml.safe_load(yaml_string)
+    except TypeError, ex:
+        if (isinstance(expected_object, datetime.datetime) 
+                and not expected_object.tzinfo is None
+                and "can't compare offset-naive and offset-aware datetimes" in ex.message):
+            pytest.xfail('pyyaml\'s loaders do not set datetime.tzinfo')
+        else:
+            raise ex