11"""Test examples."""
22import datetime
33import os
4- from typing import cast
4+ from io import StringIO
5+ from typing import cast , Any , Dict
56
67from pytest import CaptureFixture
78from ruamel .yaml .comments import CommentedMap , CommentedSeq
8- from ruamel .yaml .main import YAML
99
1010import schema_salad .main
1111import schema_salad .schema
1212from schema_salad .jsonld_context import makerdf
1313from schema_salad .ref_resolver import Loader , file_uri , uri_file_path
1414from schema_salad .sourceline import SourceLine , cmap
15- from schema_salad .utils import ContextType , stdout
15+ from schema_salad .utils import ContextType , stdout , yaml_no_ts
1616
1717from .util import get_data
1818
@@ -278,7 +278,7 @@ def test_examples() -> None:
278278 ldr , _ , _ , _ = schema_salad .schema .load_schema (path )
279279 path2 = get_data (f"metaschema/{ a } _src.yml" )
280280 assert path2
281- yaml = YAML ()
281+ yaml = yaml_no_ts ()
282282 with open (path2 ) as src_fp :
283283 src = ldr .resolve_all (yaml .load (src_fp ), "" , checklinks = False )[0 ]
284284 path3 = get_data (f"metaschema/{ a } _proc.yml" )
@@ -289,7 +289,7 @@ def test_examples() -> None:
289289
290290
291291def test_yaml_float_test () -> None :
292- assert YAML ().load ("float-test: 2e-10" )["float-test" ] == 2e-10
292+ assert yaml_no_ts ().load ("float-test: 2e-10" )["float-test" ] == 2e-10
293293
294294
295295def test_typedsl_ref () -> None :
@@ -425,6 +425,21 @@ def test_rdf_datetime() -> None:
425425 g2 .serialize (destination = stdout (), format = "n3" )
426426
427427
428+ def test_yaml_datetime () -> None :
429+ """Affirm that yaml_no_ts prevents the creation of datetime objects."""
430+ example : Dict [str , Any ] = {
431+ "id" : "foo" ,
432+ "bar" : {"id" : "baz" },
433+ }
434+ example ["s:dateCreated" ] = datetime .datetime (2020 , 10 , 8 )
435+ yaml = yaml_no_ts ()
436+ stream = StringIO ()
437+ yaml .dump (example , stream )
438+ stream2 = StringIO (stream .getvalue ())
439+ example2 = yaml .load (stream2 )
440+ assert isinstance (example2 ["s:dateCreated" ], str )
441+
442+
428443def test_subscoped_id () -> None :
429444 ldr = Loader ({})
430445 ctx = {
0 commit comments