From a260f3853aeb5cd3c86c1db73fcd702743fe20cf Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:25:01 -0400 Subject: [PATCH 1/9] unit tests for sync using symbolic context names --- changes/1091.testing.rst | 1 + conftest.py | 6 ++++++ crds/core/cmdline.py | 8 ++++---- test/core/test_cmdline.py | 20 ++++++++++++++++++++ test/core/test_heavy_client.py | 14 +++++++++++--- test/test_sync.py | 20 +++++++++++++++++++- 6 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 changes/1091.testing.rst diff --git a/changes/1091.testing.rst b/changes/1091.testing.rst new file mode 100644 index 000000000..d087410e6 --- /dev/null +++ b/changes/1091.testing.rst @@ -0,0 +1 @@ +tests for sync using symbolic context names diff --git a/conftest.py b/conftest.py index ecbb628ba..37bce7674 100644 --- a/conftest.py +++ b/conftest.py @@ -7,6 +7,7 @@ import pstats import mock import yaml +import re # ============================================================================== @@ -379,3 +380,8 @@ def run_and_profile(name, case, globs={}, locs={}): def combined_spec(scope='session'): return json.load( open(Path(__file__).parent.parent / "crds" / "roman" / "specs" / "combined_specs.json", 'r')) + + +@fixture(scope='function') +def jwst_pmap_pattern(): + return re.compile("jwst_[0-9]{4}.pmap") diff --git a/crds/core/cmdline.py b/crds/core/cmdline.py index bffb04c35..36324c027 100644 --- a/crds/core/cmdline.py +++ b/crds/core/cmdline.py @@ -741,7 +741,7 @@ def determine_contexts(self): elif config.get_crds_env_context(): contexts = [self.resolve_context(config.get_crds_env_context())] else: - contexts = [self.resolve_context(self.observatory + "-operational")] + contexts = [self.resolve_context(self.observatory + "-latest")] log.verbose("Determined contexts: ", contexts, verbosity=55) return sorted(contexts) @@ -855,11 +855,11 @@ def get_context_references(self): def expand_all_instruments(observatory, context): """Expand symbolic context specifiers for rmaps with "all" for instrument - into the list of rmaps for every instrument in the related context (e.g. edit or operational). + into the list of rmaps for every instrument in the related context (e.g. edit or latest). - e.g. jwst-all-photom-operational --> [jwst-miri-photom-operational, jwst-nircam-photom-operational, ...] + e.g. jwst-all-photom-operational --> [jwst-miri-photom-latest, jwst-nircam-photom-latest, ...] - Expansion of "all" is determined by instruments in e.g. jwst-operational + Expansion of "all" is determined by instruments in e.g. jwst-latest """ mtch = config.CONTEXT_RE.match(context) if mtch and mtch.group("instrument") == "all": diff --git a/test/core/test_cmdline.py b/test/core/test_cmdline.py index 2522f72c2..7fa2e5e36 100644 --- a/test/core/test_cmdline.py +++ b/test/core/test_cmdline.py @@ -1,5 +1,6 @@ from pytest import mark import os +import re import logging from crds.core import cmdline, utils, log from crds.core import config as crds_config @@ -320,3 +321,22 @@ def test_determine_contexts_direct(default_test_cache_state): assert contexts[0] == "hst.pmap" mappings = sorted(list(set(s.get_context_mappings()))) assert len(mappings) >= 116 + +@mark.jwst +@mark.core +@mark.cmdline +def test_determine_contexts_no_args(jwst_default_cache_state, jwst_pmap_pattern): + s = ContextsScript("cmdline.ContextsScript --jwst") + s.contexts = contexts = s.determine_contexts() + assert len(contexts) == 1 + matches = re.match(jwst_pmap_pattern, contexts[0]) + assert matches.group() is not None + + +def test_determine_jwst_build_context(jwst_default_cache_state, jwst_pmap_pattern): + s = ContextsScript("cmdline.ContextsScript --contexts jwst-build") + s.contexts = contexts = s.determine_contexts() + matches = re.match(jwst_pmap_pattern, contexts[0]) + assert matches.group() is not None + mapping_closure = sorted(list(set(s.get_context_mappings()))) + assert len(mapping_closure) >= 199 diff --git a/test/core/test_heavy_client.py b/test/core/test_heavy_client.py index c0e6ec688..ccc6fa10b 100644 --- a/test/core/test_heavy_client.py +++ b/test/core/test_heavy_client.py @@ -104,6 +104,15 @@ def test_getreferences_ignore_cache(jwst_shared_cache_state): assert refs == {'flat': f'{cache_path}/references/jwst/jwst_miri_flat_0001.fits'} +@mark.jwst +@mark.core +@mark.heavy_client +def test_get_symbolic_build_context_name(jwst_shared_cache_state, jwst_pmap_pattern): + build_context = heavy_client.get_context_name("jwst", "jwst-build") + matches = re.match(jwst_pmap_pattern, build_context) + assert matches.group() is not None + + @mark.jwst @mark.core @mark.heavy_client @@ -159,11 +168,10 @@ def test_get_context_name_symbolic(jwst_serverless_state): jwst_serverless_state.mode = 'local' jwst_serverless_state.config_setup() pattern = re.compile("jwst_[0-9]{4}.pmap") - ops_context = heavy_client.get_context_name("jwst", "jwst-operational") + latest_context = heavy_client.get_context_name("jwst", "jwst-latest") edit_context = heavy_client.get_context_name("jwst", "jwst-edit") ver_context = heavy_client.get_context_name("jwst", "jwst-versions") - - for context in [ops_context, edit_context, ver_context]: + for context in [latest_context, edit_context, ver_context]: matches = re.match(pattern, context) assert matches.group() is not None diff --git a/test/test_sync.py b/test/test_sync.py index 0d09d0283..05c6b20d4 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -1,4 +1,4 @@ -from pytest import mark, fixture +from pytest import mark, fixture, warns import os import crds from crds.core import config, rmap @@ -131,3 +131,21 @@ def test_sync_fetch_sqlite3_db(self): def test_sync_dataset_ids(self): self.run_script("crds.sync --contexts hst.pmap --dataset-ids LA9K03CBQ:LA9K03CBQ --fetch-references") + + +@mark.jwst +@mark.sync +def test_sync_jwst_latest(jwst_default_cache_state): + with warns() as record: + errors = SyncScript("crds.sync --contexts jwst-latest")() + assert errors is 0 + assert len(record) is 0, record.pop().message + + +@mark.jwst +@mark.sync +def test_sync_jwst_latest(jwst_default_cache_state): + with warns() as record: + errors = SyncScript("crds.sync --contexts jwst-build")() + assert errors is 0 + assert len(record) is 0, record.pop().message From dd0191fb8a6acf82cdc83c55092aa5c85338ca2d Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:26:10 -0400 Subject: [PATCH 2/9] add markers --- test/core/test_cmdline.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/core/test_cmdline.py b/test/core/test_cmdline.py index 7fa2e5e36..d6d8af0fd 100644 --- a/test/core/test_cmdline.py +++ b/test/core/test_cmdline.py @@ -333,6 +333,9 @@ def test_determine_contexts_no_args(jwst_default_cache_state, jwst_pmap_pattern) assert matches.group() is not None +@mark.jwst +@mark.core +@mark.cmdline def test_determine_jwst_build_context(jwst_default_cache_state, jwst_pmap_pattern): s = ContextsScript("cmdline.ContextsScript --contexts jwst-build") s.contexts = contexts = s.determine_contexts() From 006f575f6fde9ccaa3e06c33a4248e00530d62a8 Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:29:12 -0400 Subject: [PATCH 3/9] equals not is --- test/test_sync.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/test_sync.py b/test/test_sync.py index 05c6b20d4..d08269e22 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -138,8 +138,9 @@ def test_sync_dataset_ids(self): def test_sync_jwst_latest(jwst_default_cache_state): with warns() as record: errors = SyncScript("crds.sync --contexts jwst-latest")() - assert errors is 0 - assert len(record) is 0, record.pop().message + assert errors == 0 + assert len(record) == 0 + record.pop().message @mark.jwst @@ -147,5 +148,6 @@ def test_sync_jwst_latest(jwst_default_cache_state): def test_sync_jwst_latest(jwst_default_cache_state): with warns() as record: errors = SyncScript("crds.sync --contexts jwst-build")() - assert errors is 0 - assert len(record) is 0, record.pop().message + assert errors == 0 + assert len(record) == 0 + record.pop().message From c71eefc0d3c7a3881cbc64d60aaf75ae60810654 Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:25:01 -0400 Subject: [PATCH 4/9] unit tests for sync using symbolic context names --- changes/1091.testing.rst | 1 + conftest.py | 6 ++++++ crds/core/cmdline.py | 8 ++++---- test/core/test_cmdline.py | 20 ++++++++++++++++++++ test/core/test_heavy_client.py | 14 +++++++++++--- test/test_sync.py | 20 +++++++++++++++++++- 6 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 changes/1091.testing.rst diff --git a/changes/1091.testing.rst b/changes/1091.testing.rst new file mode 100644 index 000000000..d087410e6 --- /dev/null +++ b/changes/1091.testing.rst @@ -0,0 +1 @@ +tests for sync using symbolic context names diff --git a/conftest.py b/conftest.py index ecbb628ba..37bce7674 100644 --- a/conftest.py +++ b/conftest.py @@ -7,6 +7,7 @@ import pstats import mock import yaml +import re # ============================================================================== @@ -379,3 +380,8 @@ def run_and_profile(name, case, globs={}, locs={}): def combined_spec(scope='session'): return json.load( open(Path(__file__).parent.parent / "crds" / "roman" / "specs" / "combined_specs.json", 'r')) + + +@fixture(scope='function') +def jwst_pmap_pattern(): + return re.compile("jwst_[0-9]{4}.pmap") diff --git a/crds/core/cmdline.py b/crds/core/cmdline.py index bffb04c35..36324c027 100644 --- a/crds/core/cmdline.py +++ b/crds/core/cmdline.py @@ -741,7 +741,7 @@ def determine_contexts(self): elif config.get_crds_env_context(): contexts = [self.resolve_context(config.get_crds_env_context())] else: - contexts = [self.resolve_context(self.observatory + "-operational")] + contexts = [self.resolve_context(self.observatory + "-latest")] log.verbose("Determined contexts: ", contexts, verbosity=55) return sorted(contexts) @@ -855,11 +855,11 @@ def get_context_references(self): def expand_all_instruments(observatory, context): """Expand symbolic context specifiers for rmaps with "all" for instrument - into the list of rmaps for every instrument in the related context (e.g. edit or operational). + into the list of rmaps for every instrument in the related context (e.g. edit or latest). - e.g. jwst-all-photom-operational --> [jwst-miri-photom-operational, jwst-nircam-photom-operational, ...] + e.g. jwst-all-photom-operational --> [jwst-miri-photom-latest, jwst-nircam-photom-latest, ...] - Expansion of "all" is determined by instruments in e.g. jwst-operational + Expansion of "all" is determined by instruments in e.g. jwst-latest """ mtch = config.CONTEXT_RE.match(context) if mtch and mtch.group("instrument") == "all": diff --git a/test/core/test_cmdline.py b/test/core/test_cmdline.py index 2522f72c2..7fa2e5e36 100644 --- a/test/core/test_cmdline.py +++ b/test/core/test_cmdline.py @@ -1,5 +1,6 @@ from pytest import mark import os +import re import logging from crds.core import cmdline, utils, log from crds.core import config as crds_config @@ -320,3 +321,22 @@ def test_determine_contexts_direct(default_test_cache_state): assert contexts[0] == "hst.pmap" mappings = sorted(list(set(s.get_context_mappings()))) assert len(mappings) >= 116 + +@mark.jwst +@mark.core +@mark.cmdline +def test_determine_contexts_no_args(jwst_default_cache_state, jwst_pmap_pattern): + s = ContextsScript("cmdline.ContextsScript --jwst") + s.contexts = contexts = s.determine_contexts() + assert len(contexts) == 1 + matches = re.match(jwst_pmap_pattern, contexts[0]) + assert matches.group() is not None + + +def test_determine_jwst_build_context(jwst_default_cache_state, jwst_pmap_pattern): + s = ContextsScript("cmdline.ContextsScript --contexts jwst-build") + s.contexts = contexts = s.determine_contexts() + matches = re.match(jwst_pmap_pattern, contexts[0]) + assert matches.group() is not None + mapping_closure = sorted(list(set(s.get_context_mappings()))) + assert len(mapping_closure) >= 199 diff --git a/test/core/test_heavy_client.py b/test/core/test_heavy_client.py index c0e6ec688..ccc6fa10b 100644 --- a/test/core/test_heavy_client.py +++ b/test/core/test_heavy_client.py @@ -104,6 +104,15 @@ def test_getreferences_ignore_cache(jwst_shared_cache_state): assert refs == {'flat': f'{cache_path}/references/jwst/jwst_miri_flat_0001.fits'} +@mark.jwst +@mark.core +@mark.heavy_client +def test_get_symbolic_build_context_name(jwst_shared_cache_state, jwst_pmap_pattern): + build_context = heavy_client.get_context_name("jwst", "jwst-build") + matches = re.match(jwst_pmap_pattern, build_context) + assert matches.group() is not None + + @mark.jwst @mark.core @mark.heavy_client @@ -159,11 +168,10 @@ def test_get_context_name_symbolic(jwst_serverless_state): jwst_serverless_state.mode = 'local' jwst_serverless_state.config_setup() pattern = re.compile("jwst_[0-9]{4}.pmap") - ops_context = heavy_client.get_context_name("jwst", "jwst-operational") + latest_context = heavy_client.get_context_name("jwst", "jwst-latest") edit_context = heavy_client.get_context_name("jwst", "jwst-edit") ver_context = heavy_client.get_context_name("jwst", "jwst-versions") - - for context in [ops_context, edit_context, ver_context]: + for context in [latest_context, edit_context, ver_context]: matches = re.match(pattern, context) assert matches.group() is not None diff --git a/test/test_sync.py b/test/test_sync.py index 0d09d0283..05c6b20d4 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -1,4 +1,4 @@ -from pytest import mark, fixture +from pytest import mark, fixture, warns import os import crds from crds.core import config, rmap @@ -131,3 +131,21 @@ def test_sync_fetch_sqlite3_db(self): def test_sync_dataset_ids(self): self.run_script("crds.sync --contexts hst.pmap --dataset-ids LA9K03CBQ:LA9K03CBQ --fetch-references") + + +@mark.jwst +@mark.sync +def test_sync_jwst_latest(jwst_default_cache_state): + with warns() as record: + errors = SyncScript("crds.sync --contexts jwst-latest")() + assert errors is 0 + assert len(record) is 0, record.pop().message + + +@mark.jwst +@mark.sync +def test_sync_jwst_latest(jwst_default_cache_state): + with warns() as record: + errors = SyncScript("crds.sync --contexts jwst-build")() + assert errors is 0 + assert len(record) is 0, record.pop().message From 3f227cec5506d3e62618de23cec573b5b18c6c2e Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:26:10 -0400 Subject: [PATCH 5/9] add markers --- test/core/test_cmdline.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/core/test_cmdline.py b/test/core/test_cmdline.py index 7fa2e5e36..d6d8af0fd 100644 --- a/test/core/test_cmdline.py +++ b/test/core/test_cmdline.py @@ -333,6 +333,9 @@ def test_determine_contexts_no_args(jwst_default_cache_state, jwst_pmap_pattern) assert matches.group() is not None +@mark.jwst +@mark.core +@mark.cmdline def test_determine_jwst_build_context(jwst_default_cache_state, jwst_pmap_pattern): s = ContextsScript("cmdline.ContextsScript --contexts jwst-build") s.contexts = contexts = s.determine_contexts() From dc53fa85dee7d24a64649b48898c4aef74336ec0 Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 13:29:12 -0400 Subject: [PATCH 6/9] equals not is --- test/test_sync.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/test_sync.py b/test/test_sync.py index 05c6b20d4..d08269e22 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -138,8 +138,9 @@ def test_sync_dataset_ids(self): def test_sync_jwst_latest(jwst_default_cache_state): with warns() as record: errors = SyncScript("crds.sync --contexts jwst-latest")() - assert errors is 0 - assert len(record) is 0, record.pop().message + assert errors == 0 + assert len(record) == 0 + record.pop().message @mark.jwst @@ -147,5 +148,6 @@ def test_sync_jwst_latest(jwst_default_cache_state): def test_sync_jwst_latest(jwst_default_cache_state): with warns() as record: errors = SyncScript("crds.sync --contexts jwst-build")() - assert errors is 0 - assert len(record) is 0, record.pop().message + assert errors == 0 + assert len(record) == 0 + record.pop().message From 82926b7bb96da2194aee1381f5ac788f9277950f Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Tue, 15 Oct 2024 14:17:17 -0400 Subject: [PATCH 7/9] use caplog to verify --- test/test_sync.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/test/test_sync.py b/test/test_sync.py index d08269e22..03b6c2629 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -3,7 +3,10 @@ import crds from crds.core import config, rmap from crds.sync import SyncScript - +from crds import log +import logging +log.THE_LOGGER.logger.propagate=True +log.set_verbose(50) EXPECTED_MAPPINGS = [ 'hst_cos.imap', @@ -135,19 +138,29 @@ def test_sync_dataset_ids(self): @mark.jwst @mark.sync -def test_sync_jwst_latest(jwst_default_cache_state): - with warns() as record: +def test_sync_jwst_latest(jwst_default_cache_state, caplog): + with caplog.at_level(logging.INFO, logger="CRDS"): errors = SyncScript("crds.sync --contexts jwst-latest")() + out = caplog.text assert errors == 0 - assert len(record) == 0 - record.pop().message + assert "Symbolic context 'jwst-latest' resolves to" in out @mark.jwst @mark.sync -def test_sync_jwst_latest(jwst_default_cache_state): - with warns() as record: +def test_sync_jwst_build(jwst_default_cache_state, caplog): + with caplog.at_level(logging.INFO, logger="CRDS"): errors = SyncScript("crds.sync --contexts jwst-build")() + out = caplog.text + assert errors == 0 + assert "Symbolic context 'jwst-build' resolves to" in out + + +@mark.jwst +@mark.sync +def test_sync_latest(jwst_default_cache_state, caplog): + with caplog.at_level(logging.INFO, logger="CRDS"): + errors = SyncScript("crds.sync --contexts latest")() + out = caplog.text assert errors == 0 - assert len(record) == 0 - record.pop().message + assert "Symbolic context 'latest' resolves to" in out From e8cf85b95929d31081bfb7039939253f4d3f4069 Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Wed, 16 Oct 2024 11:52:11 -0400 Subject: [PATCH 8/9] sync one reference file derived from latest --- crds/sync.py | 4 ++-- test/test_sync.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crds/sync.py b/crds/sync.py index e32b86923..ae4fe8733 100644 --- a/crds/sync.py +++ b/crds/sync.py @@ -452,9 +452,9 @@ def verify_context_change(self, old_context): except AttributeError: new_context = heavy_client.load_server_info(self.observatory).operational_context if old_context == new_context: - log.error("Expected operational context switch but starting and post-sync contexts are both", repr(old_context)) + log.error("Expected latest context switch but starting and post-sync contexts are both", repr(old_context)) else: - log.info("Operational context updated from", repr(old_context), "to", repr(new_context)) + log.info("Latest context updated from", repr(old_context), "to", repr(new_context)) def push_context(self): """Push the final context recorded in the local cache to the CRDS server so it can be displayed diff --git a/test/test_sync.py b/test/test_sync.py index 03b6c2629..566e67d63 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -1,4 +1,4 @@ -from pytest import mark, fixture, warns +from pytest import mark, fixture import os import crds from crds.core import config, rmap @@ -164,3 +164,15 @@ def test_sync_latest(jwst_default_cache_state, caplog): out = caplog.text assert errors == 0 assert "Symbolic context 'latest' resolves to" in out + + +@mark.jwst +@mark.sync +def test_sync_latest_miri_ipc_reference(jwst_default_cache_state): + latest = crds.get_default_context(observatory='jwst', state='latest') + context = rmap.load_mapping(latest) + miri_imap = context.get_imap('miri') + ipc_rmap = miri_imap.get_rmap('ipc') + rmap_name = ipc_rmap.name + errors = SyncScript(f"crds.sync --contexts {rmap_name} --fetch-references --ignore-cache")() + assert errors == 0 From 94fddbf0a46b9b18e41f21f1d02bfd4817d93f1d Mon Sep 17 00:00:00 2001 From: alphasentaurii Date: Wed, 16 Oct 2024 12:49:24 -0400 Subject: [PATCH 9/9] revert to previous commit --- test/test_sync.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/test_sync.py b/test/test_sync.py index 566e67d63..398dcc1c1 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -164,15 +164,3 @@ def test_sync_latest(jwst_default_cache_state, caplog): out = caplog.text assert errors == 0 assert "Symbolic context 'latest' resolves to" in out - - -@mark.jwst -@mark.sync -def test_sync_latest_miri_ipc_reference(jwst_default_cache_state): - latest = crds.get_default_context(observatory='jwst', state='latest') - context = rmap.load_mapping(latest) - miri_imap = context.get_imap('miri') - ipc_rmap = miri_imap.get_rmap('ipc') - rmap_name = ipc_rmap.name - errors = SyncScript(f"crds.sync --contexts {rmap_name} --fetch-references --ignore-cache")() - assert errors == 0