diff --git a/test/asynchronous/test_unified_format.py b/test/asynchronous/test_unified_format.py index e03ca1cc0e..6c652e5167 100644 --- a/test/asynchronous/test_unified_format.py +++ b/test/asynchronous/test_unified_format.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import annotations +import json import os import sys from pathlib import Path @@ -24,9 +25,11 @@ from test import UnitTest, unittest from test.asynchronous.unified_format import ( MatchEvaluatorUtil, + UnifiedSpecTestMixinV1, generate_test_classes, get_test_path, ) +from test.version import Version _IS_SYNC = False @@ -55,6 +58,41 @@ ) +class TestSchemaVersionCoverage(UnitTest): + def test_all_spec_files_are_supported(self): + # PYTHON-6293: a spec resync that bumps a file's schemaVersion past + # the runner's SCHEMA_VERSION otherwise only surfaces as a runtime + # failure in asyncSetUp. Every consumed spec file must declare a + # schema version the runner supports. + # test/unified-test-format/invalid/ is not consumed by the runner and + # valid-fail/ files (including schemaVersion-unsupported.json) are + # deliberately invalid, so both are skipped. + runner_version = UnifiedSpecTestMixinV1.SCHEMA_VERSION + root = get_test_path() + skipped = ( + os.path.join("unified-test-format", "invalid"), + os.path.join("unified-test-format", "valid-fail"), + ) + unsupported = [] + for dirpath, dirnames, filenames in os.walk(root): + rel = os.path.relpath(dirpath, root) + if any(rel == prefix or rel.startswith(prefix + os.sep) for prefix in skipped): + continue + for filename in sorted(filenames): + if not filename.endswith(".json"): + continue + with open(os.path.join(dirpath, filename)) as fpath: + spec = json.load(fpath) + version = spec.get("schemaVersion") if isinstance(spec, dict) else None + if not isinstance(version, str): + continue + parsed = Version.from_string(version) + if parsed[0] != runner_version[0] or parsed > runner_version: + unsupported.append((os.path.join(rel, filename), version)) + + self.assertEqual(unsupported, []) + + class TestMatchEvaluatorUtil(UnitTest): def setUp(self): self.match_evaluator = MatchEvaluatorUtil(self) @@ -88,6 +126,48 @@ def test_type(self): }, ) + def test_lte(self): + # Added in schema version 1.9. + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 5}) + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 3}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 6}) + # An empty field name is a valid field key, not whole-result matching. + self.match_evaluator.match_result({"": {"$$lte": 5}}, {"": 5}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"": {"$$lte": 5}}, {"": 6}) + + def test_lte_whole_result(self): + # A special operator can match an entire operation result or an + # array element, in which case there is no field key to compare. + self.match_evaluator.match_result({"$$lte": 5}, 5) + self.match_evaluator.match_result([{"$$lte": 5}], [5]) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"$$lte": 5}, 6) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result([{"$$lte": 5}], [6]) + + def test_gte(self): + # Added in schema version 1.29. + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 5}) + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 6}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 3}) + # An empty field name is a valid field key, not whole-result matching. + self.match_evaluator.match_result({"": {"$$gte": 5}}, {"": 5}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"": {"$$gte": 5}}, {"": 3}) + + def test_gte_whole_result(self): + # A special operator can match an entire operation result or an + # array element, in which case there is no field key to compare. + self.match_evaluator.match_result({"$$gte": 5}, 5) + self.match_evaluator.match_result([{"$$gte": 5}], [5]) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"$$gte": 5}, 3) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result([{"$$gte": 5}], [3]) + if __name__ == "__main__": unittest.main() diff --git a/test/asynchronous/unified_format.py b/test/asynchronous/unified_format.py index 9e5afb6d98..e1c43d3c32 100644 --- a/test/asynchronous/unified_format.py +++ b/test/asynchronous/unified_format.py @@ -491,7 +491,7 @@ class UnifiedSpecTestMixinV1(AsyncIntegrationTest): a class attribute ``TEST_SPEC``. """ - SCHEMA_VERSION = Version.from_string("1.28") + SCHEMA_VERSION = Version.from_string("1.30") RUN_ON_LOAD_BALANCER = True TEST_SPEC: Any TEST_PATH = "" # This gets filled in by generate_test_classes diff --git a/test/test_unified_format.py b/test/test_unified_format.py index a02df1c560..190746bddf 100644 --- a/test/test_unified_format.py +++ b/test/test_unified_format.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import annotations +import json import os import sys from pathlib import Path @@ -24,9 +25,11 @@ from test import UnitTest, unittest from test.unified_format import ( MatchEvaluatorUtil, + UnifiedSpecTestMixinV1, generate_test_classes, get_test_path, ) +from test.version import Version _IS_SYNC = True @@ -55,6 +58,41 @@ ) +class TestSchemaVersionCoverage(UnitTest): + def test_all_spec_files_are_supported(self): + # PYTHON-6293: a spec resync that bumps a file's schemaVersion past + # the runner's SCHEMA_VERSION otherwise only surfaces as a runtime + # failure in setUp. Every consumed spec file must declare a + # schema version the runner supports. + # test/unified-test-format/invalid/ is not consumed by the runner and + # valid-fail/ files (including schemaVersion-unsupported.json) are + # deliberately invalid, so both are skipped. + runner_version = UnifiedSpecTestMixinV1.SCHEMA_VERSION + root = get_test_path() + skipped = ( + os.path.join("unified-test-format", "invalid"), + os.path.join("unified-test-format", "valid-fail"), + ) + unsupported = [] + for dirpath, dirnames, filenames in os.walk(root): + rel = os.path.relpath(dirpath, root) + if any(rel == prefix or rel.startswith(prefix + os.sep) for prefix in skipped): + continue + for filename in sorted(filenames): + if not filename.endswith(".json"): + continue + with open(os.path.join(dirpath, filename)) as fpath: + spec = json.load(fpath) + version = spec.get("schemaVersion") if isinstance(spec, dict) else None + if not isinstance(version, str): + continue + parsed = Version.from_string(version) + if parsed[0] != runner_version[0] or parsed > runner_version: + unsupported.append((os.path.join(rel, filename), version)) + + self.assertEqual(unsupported, []) + + class TestMatchEvaluatorUtil(UnitTest): def setUp(self): self.match_evaluator = MatchEvaluatorUtil(self) @@ -88,6 +126,48 @@ def test_type(self): }, ) + def test_lte(self): + # Added in schema version 1.9. + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 5}) + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 3}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"x": {"$$lte": 5}}, {"x": 6}) + # An empty field name is a valid field key, not whole-result matching. + self.match_evaluator.match_result({"": {"$$lte": 5}}, {"": 5}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"": {"$$lte": 5}}, {"": 6}) + + def test_lte_whole_result(self): + # A special operator can match an entire operation result or an + # array element, in which case there is no field key to compare. + self.match_evaluator.match_result({"$$lte": 5}, 5) + self.match_evaluator.match_result([{"$$lte": 5}], [5]) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"$$lte": 5}, 6) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result([{"$$lte": 5}], [6]) + + def test_gte(self): + # Added in schema version 1.29. + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 5}) + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 6}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"x": {"$$gte": 5}}, {"x": 3}) + # An empty field name is a valid field key, not whole-result matching. + self.match_evaluator.match_result({"": {"$$gte": 5}}, {"": 5}) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"": {"$$gte": 5}}, {"": 3}) + + def test_gte_whole_result(self): + # A special operator can match an entire operation result or an + # array element, in which case there is no field key to compare. + self.match_evaluator.match_result({"$$gte": 5}, 5) + self.match_evaluator.match_result([{"$$gte": 5}], [5]) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result({"$$gte": 5}, 3) + with self.assertRaises(AssertionError): + self.match_evaluator.match_result([{"$$gte": 5}], [3]) + if __name__ == "__main__": unittest.main() diff --git a/test/unified_format.py b/test/unified_format.py index 244d58514c..c4542de403 100644 --- a/test/unified_format.py +++ b/test/unified_format.py @@ -489,7 +489,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest): a class attribute ``TEST_SPEC``. """ - SCHEMA_VERSION = Version.from_string("1.28") + SCHEMA_VERSION = Version.from_string("1.30") RUN_ON_LOAD_BALANCER = True TEST_SPEC: Any TEST_PATH = "" # This gets filled in by generate_test_classes diff --git a/test/unified_format_shared.py b/test/unified_format_shared.py index d860d345fc..e72611d6f0 100644 --- a/test/unified_format_shared.py +++ b/test/unified_format_shared.py @@ -449,9 +449,16 @@ def _operation_sessionLsid(self, spec, actual, key_to_compare): self.test.assertEqual(expected_lsid, actual[key_to_compare]) def _operation_lte(self, spec, actual, key_to_compare): - if key_to_compare not in actual: + if key_to_compare is not None and key_to_compare not in actual: + self.test.fail(f"Actual command is missing the {key_to_compare} field: {spec}") + value = actual[key_to_compare] if key_to_compare is not None else actual + self.test.assertLessEqual(value, spec) + + def _operation_gte(self, spec, actual, key_to_compare): + if key_to_compare is not None and key_to_compare not in actual: self.test.fail(f"Actual command is missing the {key_to_compare} field: {spec}") - self.test.assertLessEqual(actual[key_to_compare], spec) + value = actual[key_to_compare] if key_to_compare is not None else actual + self.test.assertGreaterEqual(value, spec) def _operation_matchAsDocument(self, spec, actual, key_to_compare): self._match_document(spec, json_util.loads(actual[key_to_compare]), False, test=True)