Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions test/asynchronous/test_unified_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
from __future__ import annotations

import json
import os
import sys
from pathlib import Path
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion test/asynchronous/unified_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions test/test_unified_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
from __future__ import annotations

import json
import os
import sys
from pathlib import Path
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion test/unified_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions test/unified_format_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading