| 1 |
Datetimes (Datetime): |
| 2 |
docs: scalar/datetime |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
Datetime validator parses using the python-dateutil library and |
| 6 |
returns a python datetime object. |
| 7 |
given: |
| 8 |
setup: | |
| 9 |
from strictyaml import Map, Datetime, YAMLValidationError, load, as_document |
| 10 |
from collections import OrderedDict |
| 11 |
from dateutil.tz.tz import tzutc |
| 12 |
from datetime import datetime |
| 13 |
from ensure import Ensure |
| 14 |
|
| 15 |
schema = Map({ |
| 16 |
"date": Datetime(), |
| 17 |
"datetime1": Datetime(), |
| 18 |
"datetime2": Datetime(), |
| 19 |
"datetime3": Datetime(), |
| 20 |
}) |
| 21 |
|
| 22 |
equivalent_data = OrderedDict([ |
| 23 |
("date", datetime(2016, 10, 22, 0, 0)), |
| 24 |
("datetime1", datetime(2016, 10, 22, 14, 23, 12, tzinfo=tzutc())), |
| 25 |
("datetime2", datetime(2016, 10, 22, 14, 23, 12, tzinfo=tzutc())), |
| 26 |
("datetime3", datetime(2016, 10, 22, 14, 23, 12, tzinfo=tzutc())), |
| 27 |
]) |
| 28 |
yaml_snippet: | |
| 29 |
date: 2016-10-22 |
| 30 |
datetime1: 2016-10-22T14:23:12+00:00 |
| 31 |
datetime2: 2016-10-22T14:23:12Z |
| 32 |
datetime3: 20161022T142312Z |
| 33 |
variations: |
| 34 |
Each of the four datetimes are valid and parsed: |
| 35 |
steps: |
| 36 |
- Run: |
| 37 |
code: | |
| 38 |
Ensure(load(yaml_snippet, schema)).equals(equivalent_data) |
| 39 |
|
| 40 |
.text still returns the original text: |
| 41 |
steps: |
| 42 |
- Run: |
| 43 |
code: | |
| 44 |
Ensure(load(yaml_snippet, schema)["date"].text).equals("2016-10-22") |
| 45 |
|
| 46 |
Non datetimes raise an exception: |
| 47 |
given: |
| 48 |
yaml_snippet: | |
| 49 |
date: 1 |
| 50 |
datetime1: รข |
| 51 |
datetime2: b |
| 52 |
datetime3: c |
| 53 |
steps: |
| 54 |
- Run: |
| 55 |
code: load(yaml_snippet, schema) |
| 56 |
raises: |
| 57 |
type: strictyaml.exceptions.YAMLValidationError |
| 58 |
message: |- |
| 59 |
when expecting a datetime |
| 60 |
found arbitrary text |
| 61 |
in "<unicode string>", line 2, column 1: |
| 62 |
datetime1: "\xE2" |
| 63 |
^ (line: 2) |
| 64 |
|
| 65 |
Datetime objects serialize directly to ISO-8601 format YAML strings: |
| 66 |
steps: |
| 67 |
- Run: |
| 68 |
code: | |
| 69 |
print(as_document(equivalent_data, schema).as_yaml()) |
| 70 |
will output: |- |
| 71 |
date: 2016-10-22T00:00:00 |
| 72 |
datetime1: 2016-10-22T14:23:12+00:00 |
| 73 |
datetime2: 2016-10-22T14:23:12+00:00 |
| 74 |
datetime3: 2016-10-22T14:23:12+00:00 |
| 75 |
|
| 76 |
Valid datetime strings serialize to YAML: |
| 77 |
steps: |
| 78 |
- Run: |
| 79 |
code: | |
| 80 |
print(as_document({"a": "2016-10-22"}, Map({"a": Datetime()})).as_yaml()) |
| 81 |
will output: 'a: 2016-10-22' |
| 82 |
|
| 83 |
Serializing invalid datetime string raises exception: |
| 84 |
steps: |
| 85 |
- Run: |
| 86 |
code: | |
| 87 |
as_document({"a": "x"}, Map({"a": Datetime()})) |
| 88 |
raises: |
| 89 |
type: strictyaml.exceptions.YAMLSerializationError |
| 90 |
message: expected a datetime, got 'x' |
| 91 |
|
| 92 |
Serializing non-string and non-datetime object raises exception: |
| 93 |
steps: |
| 94 |
- Run: |
| 95 |
code: | |
| 96 |
as_document({"a": 55}, Map({"a": Datetime()})) |
| 97 |
raises: |
| 98 |
type: strictyaml.exceptions.YAMLSerializationError |
| 99 |
message: expected a datetime, got '55' of type 'int' |