Skip to content

Commit cd338a9

Browse files
Clean up the conftest for latest fixtures
1 parent 85c36a9 commit cd338a9

2 files changed

Lines changed: 68 additions & 46 deletions

File tree

tests/conftest.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ def _find_latest(uris):
4949
# Look directly at the latest schemas storage directory to infer latest schemas
5050
_LATEST_DIR = Path(__file__).parent.parent.absolute() / "latest"
5151
_LATEST_PATHS = MappingProxyType(
52-
{latest_path: yaml.safe_load(latest_path.read_bytes()) for latest_path in _LATEST_DIR.glob("**/*.yaml")}
52+
{
53+
latest_path.relative_to(_LATEST_DIR): yaml.safe_load(latest_path.read_bytes())
54+
for latest_path in _LATEST_DIR.glob("**/*.yaml")
55+
}
5356
)
54-
_LATEST_TOP_LEVEL_PATHS = tuple(latest_path for latest_path in _LATEST_PATHS if latest_path.parent == _LATEST_DIR)
5557
_LATEST_URI_PATHS = MappingProxyType({schema["id"]: path for path, schema in _LATEST_PATHS.items()})
56-
_LATEST_URIS = tuple(_LATEST_URI_PATHS.keys())
57-
_LATEST_MANIFEST_URIS = tuple(uri for uri in _LATEST_URIS if "manifests" in uri)
58+
_LATEST_MANIFEST_URIS = MappingProxyType(
59+
{schema["id"]: schema for schema in _LATEST_PATHS.values() if "manifests" in schema["id"]}
60+
)
61+
_LATEST_TOP_LEVEL_PATHS = tuple(latest_path for latest_path in _LATEST_PATHS if latest_path.parent == Path("."))
62+
5863
_LATEST_MANIFEST_TAGS = MappingProxyType(
59-
{uri: tuple(entry["tag_uri"] for entry in _CURRENT_RESOURCES[uri]["tags"]) for uri in _LATEST_MANIFEST_URIS}
64+
{uri: tuple(entry["tag_uri"] for entry in schema["tags"]) for uri, schema in _LATEST_MANIFEST_URIS.items()}
6065
)
6166
_LATEST_DATAMODELS_URI = next(uri for uri in _LATEST_MANIFEST_URIS if "static" not in uri)
6267
_LATEST_STATIC_URI = next(uri for uri in _LATEST_MANIFEST_URIS if "static" in uri)
63-
_LATEST_DATAMODEL_URIS = tuple(uri["schema_uri"] for uri in _CURRENT_RESOURCES[_LATEST_DATAMODELS_URI]["tags"])
68+
_LATEST_DATAMODEL_URIS = tuple(uri["schema_uri"] for uri in _LATEST_MANIFEST_URIS[_LATEST_DATAMODELS_URI]["tags"])
6469
_LATEST_ARCHIVE_URIS = tuple(schema["id"] for schema in _LATEST_PATHS.values() if "archive_meta" in schema)
6570

6671

@@ -121,6 +126,22 @@ def latest_path(request):
121126
return request.param
122127

123128

129+
@pytest.fixture(scope="session")
130+
def latest_schema(latest_path):
131+
"""
132+
Get the latest schema from a latest path.
133+
"""
134+
return _LATEST_PATHS[latest_path]
135+
136+
137+
@pytest.fixture(scope="session")
138+
def latest_uri(latest_schema):
139+
"""
140+
Get a latest resource URI
141+
"""
142+
return latest_schema["id"]
143+
144+
124145
@pytest.fixture(scope="session", params=_LATEST_ARCHIVE_URIS)
125146
def latest_archive_uri(request):
126147
"""
@@ -138,19 +159,27 @@ def latest_dir():
138159

139160

140161
@pytest.fixture(scope="session")
141-
def latest_reference_files_dir(latest_dir):
162+
def latest_datamodels_dir():
163+
"""
164+
Get the path to the latest datamodels directory.
165+
"""
166+
return Path(".")
167+
168+
169+
@pytest.fixture(scope="session")
170+
def latest_reference_files_dir(latest_datamodels_dir):
142171
"""
143172
Get the path to the latest reference files directory.
144173
"""
145-
return latest_dir / "reference_files"
174+
return latest_datamodels_dir / "reference_files"
146175

147176

148177
@pytest.fixture(scope="session")
149-
def latest_ccsp_dir(latest_dir):
178+
def latest_ccsp_dir(latest_datamodels_dir):
150179
"""
151180
Get the path to the latest CCSP schemas directory.
152181
"""
153-
return latest_dir / "CCSP"
182+
return latest_datamodels_dir / "CCSP"
154183

155184

156185
@pytest.fixture(scope="session", params=_LATEST_TOP_LEVEL_PATHS)
@@ -161,28 +190,12 @@ def latest_top_level_path(request):
161190
return request.param
162191

163192

164-
@pytest.fixture(scope="session")
165-
def latest_uri_paths():
166-
"""
167-
Get the mapping of latest URIs to their paths.
168-
"""
169-
return _LATEST_URI_PATHS
170-
171-
172193
@pytest.fixture(scope="session")
173194
def latest_uris():
174195
"""
175196
Get the URIs of the latest schemas.
176197
"""
177-
return _LATEST_URIS
178-
179-
180-
@pytest.fixture(scope="session", params=_LATEST_URIS)
181-
def latest_uri(request):
182-
"""
183-
Get a latest resource URI
184-
"""
185-
return request.param
198+
return _LATEST_URI_PATHS
186199

187200

188201
@pytest.fixture(scope="session")
@@ -235,11 +248,14 @@ def latest_static_tag_uri(request):
235248

236249

237250
@pytest.fixture(scope="session")
238-
def latest_schemas(latest_paths, latest_uris):
251+
def latest_schemas(latest_dir, latest_paths, latest_uris):
239252
"""
240253
Get the text of the latest schemas.
241254
"""
242-
return {latest_uri: latest_path.read_text() for latest_uri, latest_path in zip(latest_uris, latest_paths, strict=True)}
255+
return {
256+
latest_uri: (latest_dir / latest_path).read_text()
257+
for latest_uri, latest_path in zip(latest_uris, latest_paths, strict=True)
258+
}
243259

244260

245261
@pytest.fixture(scope="session", params=tuple(entry["tag_uri"] for entry in _CURRENT_RESOURCES[_PREVIOUS_DATAMODELS_URI]["tags"]))
@@ -353,7 +369,7 @@ def schema_uris():
353369
return _SCHEMA_URIS
354370

355371

356-
@pytest.fixture(scope="session", params=tuple(uri for uri in _LATEST_URIS if uri in _SCHEMA_URIS))
372+
@pytest.fixture(scope="session", params=tuple(uri for uri in _LATEST_URI_PATHS if uri in _SCHEMA_URIS))
357373
def schema_uri(request):
358374
"""
359375
Get a URI for a RAD schema from the ASDF resource manager.
@@ -366,15 +382,18 @@ def schema_uri(request):
366382
return request.param
367383

368384

369-
@pytest.fixture(scope="session", params=tuple(uri for uri in _LATEST_URIS if "/reference_files" in uri and uri in _SCHEMA_URIS))
385+
@pytest.fixture(
386+
scope="session", params=tuple(uri for uri in _LATEST_URI_PATHS if "/reference_files" in uri and uri in _SCHEMA_URIS)
387+
)
370388
def ref_file_uri(request):
371389
"""
372-
Get a URI related to the RAD reference files from the ASDF resource manager.
390+
Get a URI related to the RAD reference files from the ASDF r
391+
esource manager.
373392
"""
374393
return request.param
375394

376395

377-
@pytest.fixture(scope="session", params=tuple(uri for uri in _LATEST_URIS if "/CCSP" in uri and uri in _SCHEMA_URIS))
396+
@pytest.fixture(scope="session", params=tuple(uri for uri in _LATEST_URI_PATHS if "/CCSP" in uri and uri in _SCHEMA_URIS))
378397
def ccsp_uri(request):
379398
"""
380399
Get a URI related to the RAD CCSP schemas.

tests/test_latest.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import asdf.treeutil
1212
import pytest
13-
import yaml
1413

1514
from rad import resources
1615
from rad._parser import archive_schema, super_schema
@@ -28,16 +27,15 @@ def test_smoke_latest_paths(self, latest_paths):
2827
"""
2928
assert latest_paths
3029

31-
def test_latest_filename(self, latest_path):
30+
def test_latest_filename(self, latest_path, latest_uri):
3231
"""
3332
Check that the file name of the schema matches the schema ID WITHOUT the version number suffix.
3433
"""
3534
# Check that the file name does not contain a version number
36-
assert len(str(latest_path).split("/latest/")[-1].split("-")) == 1
35+
assert len(str(latest_path).split("-")) == 1
3736

3837
# Check that the file name is consistent with the schema ID
39-
uri = yaml.safe_load(latest_path.read_bytes())["id"]
40-
uri = uri.split("/schemas/")[-1] if "/schemas/" in uri else uri.split("/manifests/")[-1]
38+
uri = latest_uri.split("/schemas/")[-1] if "/schemas/" in latest_uri else latest_uri.split("/manifests/")[-1]
4139

4240
# Find the filename without the version number suffix
4341
filename = str(latest_path.parent / latest_path.stem).split("/latest/")[-1].split("-")[0]
@@ -46,12 +44,12 @@ def test_latest_filename(self, latest_path):
4644
assert uri.split("-")[0] == filename.split("manifests/")[-1] if "manifests/" in filename else filename
4745

4846
@pytest.mark.skipif(not IS_EDITABLE, reason="Symbolic links are resolved in non-editable installs")
49-
def test_latest_symlink(self, latest_path):
47+
def test_latest_symlink(self, latest_uri, latest_path, latest_dir):
5048
"""
5149
Check that each "latest" file has a simlink in the `schemas` or `manifests` directory
5250
that points to that file which includes its version number (e.g. matches its ID).
5351
"""
54-
path_suffix = yaml.safe_load(latest_path.read_bytes())["id"].split("/roman/")[-1]
52+
path_suffix = latest_uri.split("/roman/")[-1]
5553
path_prefix = importlib_resources.files(resources)
5654
symlink_path = path_prefix / f"{path_suffix}.yaml"
5755

@@ -64,7 +62,9 @@ def test_latest_symlink(self, latest_path):
6462
assert not symlink_path.readlink().is_absolute(), f"Expected {symlink_path} to be a relative symlink, but it is not."
6563

6664
# Check that the symlink points to the correct file
67-
assert symlink_path.resolve() == latest_path, f"Expected {symlink_path} to point to {latest_path}, but it does not."
65+
assert symlink_path.resolve() == latest_dir / latest_path, (
66+
f"Expected {symlink_path} to point to {latest_dir / latest_path}, but it does not."
67+
)
6868

6969
def test_latest_datamodels_not_static(self, latest_datamodels_tag_uri, latest_static_tags):
7070
"""
@@ -97,12 +97,12 @@ def test_latest_datamodels_only_reference_latest(self, latest_datamodels_tag_uri
9797
f"{tag_schema_map[latest_datamodels_tag_uri]} is not in latest uris."
9898
)
9999

100-
def test_latest_schemas(self, latest_schema_tags, latest_schemas, latest_uri):
100+
def test_latest_schemas(self, latest_schema_tags, latest_schemas, latest_schema, latest_uri):
101101
"""
102102
Check that the latest schemas are using the latest versions of the schema and tag uris.
103103
"""
104104
# Sanity check that the latest_uri matches the id in the latest_schemas
105-
assert yaml.safe_load(latest_schemas[latest_uri])["id"] == latest_uri
105+
assert latest_schema["id"] == latest_uri
106106

107107
# Substitute all matching patterns with the latest_uri and check equality
108108
# If any substitution changes the schema, then we have a URI mismatch
@@ -139,12 +139,12 @@ def test_no_internal_tags(self, latest_tagged_schema_uri, current_resources):
139139
)
140140

141141
def test_tagged_schema_path(
142-
self, latest_tagged_schema_uri, latest_dir, latest_reference_files_dir, latest_ccsp_dir, latest_uri_paths
142+
self, latest_tagged_schema_uri, latest_datamodels_dir, latest_reference_files_dir, latest_ccsp_dir, latest_uris
143143
):
144144
"""
145145
Check that all datamodels are in the top-level directory and all reference files are in the reference_files directory.
146146
"""
147-
path = latest_uri_paths[latest_tagged_schema_uri]
147+
path = latest_uris[latest_tagged_schema_uri]
148148
if "reference_file" in latest_tagged_schema_uri:
149149
assert path.parent == latest_reference_files_dir, (
150150
f"{latest_tagged_schema_uri} is a reference file that is not in the reference_files directory."
@@ -159,7 +159,10 @@ def test_tagged_schema_path(
159159
f"{latest_tagged_schema_uri} is not prefixed with '{ccsp_id_dir.lower()}_'"
160160
)
161161
else:
162-
assert path.parent == latest_dir, f"{latest_tagged_schema_uri} is a datamodel that is not in the top-level directory."
162+
print(path.parent)
163+
assert path.parent == latest_datamodels_dir, (
164+
f"{latest_tagged_schema_uri} is a datamodel that is not in the top-level directory."
165+
)
163166

164167
def test_top_level_schema(self, latest_top_level_path, metaschema_uri, latest_paths):
165168
"""

0 commit comments

Comments
 (0)