Skip to content

Commit

Permalink
Merge pull request #1143 from WilliamJamieson/plugin-update
Browse files Browse the repository at this point in the history
Support for examples speciflying asdf-standard version
  • Loading branch information
WilliamJamieson authored Jun 3, 2022
2 parents 991c1ae + 43f239a commit c6e721d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
2.11.2
------
2.11.2 (unreleased)
-------------------

- Added ability to display title as a comment in using the
``info()`` functionality. [#1138]
- Add ability to set asdf-standard version for schema example items. [#1143]

2.11.1 (2022-04-15)
-------------------
Expand Down
47 changes: 43 additions & 4 deletions pytest_asdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pathlib
import warnings
from dataclasses import dataclass

import numpy as np
import pytest
Expand Down Expand Up @@ -122,7 +123,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 +154,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 +211,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 +222,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 +251,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 c6e721d

Please sign in to comment.