Debian Codegraph @

asdf_astropy/tests/test_integration.py

raw
1 import io
2 import sys
3 import warnings
4 from pathlib import Path
5
6 import asdf
7 import pytest
8 import yaml
9
10
11 def test_resources():
12 resources_root = Path(__file__).parent.parent / "resources"
13 resource_manager = asdf.get_config().resource_manager
14
15 for resource_path in resources_root.glob("**/*.yaml"):
16 with resource_path.open("rb") as f:
17 resource_content = f.read()
18 resource = yaml.safe_load(resource_content)
19 resource_uri = resource["id"]
20 assert resource_manager[resource_uri] == resource_content
21
22
23 def test_manifests():
24 manifests_root = Path(__file__).parent.parent / "resources" / "manifests"
25 resource_manager = asdf.get_config().resource_manager
26
27 for manifest_path in manifests_root.glob("*.yaml"):
28 with manifest_path.open("rb") as f:
29 manifest_content = f.read()
30 manifest = yaml.safe_load(manifest_content)
31
32 manifest_schema = asdf.schema.load_schema("asdf://asdf-format.org/core/schemas/extension_manifest-1.0.0")
33
34 # The manifest must be valid against its own schema:
35 asdf.schema.validate(manifest, schema=manifest_schema)
36
37 for tag_definition in manifest["tags"]:
38 # The tag's schema must be available:
39 assert tag_definition["schema_uri"] in resource_manager
40
41
42 def test_extensions():
43 package_and_uri_pairs = {(e.package_name, e.extension_uri) for e in asdf.get_config().extensions}
44
45 assert ("asdf-astropy", "asdf://astropy.org/astropy/extensions/astropy-1.0.0") in package_and_uri_pairs
46 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.0.0") in package_and_uri_pairs
47 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.1.0") in package_and_uri_pairs
48 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.2.0") in package_and_uri_pairs
49 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.3.0") in package_and_uri_pairs
50 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.4.0") in package_and_uri_pairs
51 assert ("asdf-astropy", "asdf://asdf-format.org/transform/extensions/transform-1.5.0") in package_and_uri_pairs
52
53
54 _ASTROPY_MODULES = [
55 "astropy.coordinates",
56 "astropy.io",
57 "astropy.modeling",
58 "astropy.table",
59 "astropy.time",
60 "astropy.units",
61 ]
62
63
64 @pytest.fixture
65 def _clean_astropy_imports():
66 """Temporally unload all astropy modules used by asdf-astropy"""
67
68 # If any astropy or asdf_astropy modules are already imported
69 # remove them from sys.modules so we can later in this test check
70 # if those modules were imported. We have to store these as
71 # many submodules will reference or use types defined in other
72 # submodules
73 previous_modules = {}
74 for name in sys.modules.copy():
75 if name.startswith("asdf_astropy") or any(name.startswith(m) for m in _ASTROPY_MODULES):
76 previous_modules[name] = sys.modules[name]
77 del sys.modules[name]
78
79 # Register a module finder that just raises an exception if
80 # one of the tracked astropy modules is imported
81 class _Finder:
82 def find_spec(self, modulename, path=None, target=None):
83 if any(modulename.startswith(m) for m in _ASTROPY_MODULES): # pragma: no cover
84 msg = f"attempt to import astropy submodule({modulename}) during integration"
85 raise Exception(msg) # noqa: TRY002
86
87 sys.meta_path.insert(0, _Finder())
88
89 # Setup complete return the test
90 yield
91
92 # Restore the previously imported modules. This is necessary as other code
93 # may have already used the modules we removed from the cache (astropy.constants
94 # defines some quantities).
95 for name, previous_module in previous_modules.items():
96 sys.modules[name] = previous_module
97 if isinstance(sys.meta_path[0], _Finder):
98 sys.meta_path.pop(0)
99
100
101 @pytest.mark.usefixtures("_clean_astropy_imports")
102 def test_no_astropy_import():
103 """
104 Confirm that none of the ASDF plugins import astropy modules
105 at import time.
106 """
107
108 from asdf_astropy import integration
109
110 integration.get_resource_mappings()
111 integration.get_extensions()
112
113 assert not any(k for k in sys.modules if any(k.startswith(m) for m in _ASTROPY_MODULES))
114
115
116 def test_no_core_extension_overwrite():
117 """
118 Check that this package (even though it implements converters for core types)
119 does not overwrite the core extension provided by asdf
120 """
121 import astropy.units as u
122
123 # define a tree with a unit that will be serialized by this package
124 # the tree itself will be serialized by asdf (which should result in
125 # including asdf as a used extension)
126 tree = {"meter": u.m}
127
128 bio = io.BytesIO()
129 asdf.AsdfFile(tree).write_to(bio)
130 bio.seek(0)
131
132 with asdf.open(bio) as af:
133 packages = [ext["software"]["name"] for ext in af.tree["history"]["extensions"]]
134 # check that both asdf and asdf-astropy are recorded as being used to
135 # generate the file
136 assert "asdf" in packages
137 assert "asdf-astropy" in packages
138
139
140 def test_no_warnings_for_astropy_manifest_files():
141 """
142 asdf-astropy 0.5.0 wrote files with astropy.org manifests
143 that were compounds of core and astropy types. These types are
144 now handled by different extensions. This test checks that files
145 generated with the no longer used astropy.org manifests open without
146 warnings.
147 """
148 file_contents = io.BytesIO(
149 b"""#ASDF 1.0.0
150 #ASDF_STANDARD 1.5.0
151 %YAML 1.1
152 %TAG ! tag:stsci.edu:asdf/
153 --- !core/asdf-1.1.0
154 asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
155 name: asdf, version: 3.3.0}
156 history:
157 extensions:
158 - !core/extension_metadata-1.0.0
159 extension_class: asdf.extension._manifest.ManifestExtension
160 extension_uri: asdf://asdf-format.org/core/extensions/core-1.5.0
161 manifest_software: !core/software-1.0.0 {name: asdf_standard, version: 1.1.1}
162 software: !core/software-1.0.0 {name: asdf, version: 3.3.0}
163 - !core/extension_metadata-1.0.0
164 extension_class: asdf_astropy._manifest.CompoundManifestExtension
165 extension_uri: asdf://astropy.org/core/extensions/core-1.5.0
166 software: !core/software-1.0.0 {name: asdf-astropy, version: 0.5.0}
167 u: !unit/unit-1.0.0 m
168 ...""",
169 )
170 with warnings.catch_warnings():
171 # make sure warnings are errors
172 warnings.simplefilter("error")
173 with asdf.open(file_contents) as af:
174 assert af["u"] # we don't care about the contents here