Skip to content

Commit

Permalink
Support for examples speciflying asdf-standard version
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed May 27, 2022
1 parent ed50931 commit 00d1d3c
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions pytest_asdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import pytest
import yaml

from dataclasses import dataclass

# Avoid all imports of asdf at this level in order to avoid circular imports


Expand Down Expand Up @@ -122,7 +124,7 @@ def find_examples_in_schema(self):

for node in treeutil.iter_tree(schema_tree):
if isinstance(node, dict) and "examples" in node and isinstance(node["examples"], list):
for desc, example in node["examples"]:
for example in node["examples"]:
yield example


Expand Down Expand Up @@ -153,6 +155,44 @@ def reportinfo(self):
return self.fspath, 0, ""


@dataclass
class SchemaExample:
description: str
example: str
_version: str = None
other: any = None

@classmethod
def from_schema(cls, example: list):
if len(example) == 1:
_description = ""
_example = example[0]
elif len(example) == 2:
_description = example[0]
_example = example[1]
_version = None
_other = None
elif len(example) > 2:
_description = example[0]
_example = example[2]
_version = example[1]
_other = example[3:] if len(example) > 3 else None
else:
raise RuntimeError("Invalid example")

return cls(_description, _example, _version, _other)

@property
def version(self):
import asdf.versioning as versioning

if self._version is None:
return versioning.default_version

version = self._version.lower().split("asdf-standard-")[1]
return versioning.AsdfVersion(version)


class AsdfSchemaExampleItem(pytest.Item):
@classmethod
def from_parent(
Expand All @@ -172,7 +212,7 @@ def from_parent(
result = AsdfSchemaExampleItem(name, parent, **kwargs)

result.filename = str(schema_path)
result.example = example
result.example = SchemaExample.from_schema(example)
result.ignore_unrecognized_tag = ignore_unrecognized_tag
result.ignore_version_mismatch = ignore_version_mismatch
return result
Expand All @@ -183,7 +223,7 @@ def runtest(self):

# Make sure that the examples in the schema files (and thus the
# ASDF standard document) are valid.
buff = helpers.yaml_to_asdf("example: " + self.example.strip())
buff = helpers.yaml_to_asdf("example: " + self.example.example.strip(), standard_version=self.example.version)

ff = AsdfFile(
uri=util.filepath_to_url(os.path.abspath(self.filename)),
Expand Down Expand Up @@ -212,7 +252,7 @@ def runtest(self):

ff._open_impl(ff, buff, mode="rw")
except Exception:
print("From file:", self.filename)
print(f"Example: {self.example.description}\n From file: {self.filename}")
raise

# Just test we can write it out. A roundtrip test
Expand Down

0 comments on commit 00d1d3c

Please sign in to comment.