-
Notifications
You must be signed in to change notification settings - Fork 11
Added geospatial tests for $minDistance #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vic-tsang
wants to merge
2
commits into
documentdb:main
Choose a base branch
from
vic-tsang:geospatial/minDistance/tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
documentdb_tests/compatibility/tests/core/operator/query/geospatial/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def geo_2dsphere(collection): | ||
| """Create a 2dsphere index on loc.""" | ||
| collection.create_index([("loc", "2dsphere")]) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def geo_2d(collection): | ||
| """Create a 2d index on loc.""" | ||
| collection.create_index([("loc", "2d")]) |
Empty file.
129 changes: 129 additions & 0 deletions
129
.../core/operator/query/geospatial/specifiers/minDistance/test_minDistance_bson_validator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """Tests for $minDistance BSON type validation — GeoJSON and legacy syntax. | ||
|
|
||
| The legacy code path returns a different error code (16893) than GeoJSON (BAD_VALUE_ERROR = 2). | ||
| """ | ||
|
|
||
| import pytest | ||
| from bson import Decimal128, Int64 | ||
|
|
||
| from documentdb_tests.framework.assertions import assertFailureCode, assertSuccess | ||
| from documentdb_tests.framework.bson_type_validator import ( | ||
| BsonType, | ||
| BsonTypeTestCase, | ||
| generate_bson_acceptance_test_cases, | ||
| generate_bson_rejection_test_cases, | ||
| ) | ||
| from documentdb_tests.framework.error_codes import ( | ||
| BAD_VALUE_ERROR, | ||
| LEGACY_MIN_DISTANCE_NOT_NUMBER_ERROR, | ||
| ) | ||
| from documentdb_tests.framework.executor import execute_command | ||
|
|
||
| ORIGIN = {"type": "Point", "coordinates": [0, 0]} | ||
|
|
||
| # --- GeoJSON (2dsphere index) --- | ||
|
|
||
| GEOJSON_BSON_PARAMS = [ | ||
| BsonTypeTestCase( | ||
| id="minDistance", | ||
| msg="$minDistance should reject non-numeric types", | ||
| keyword="$minDistance", | ||
| valid_types=[BsonType.DOUBLE, BsonType.INT, BsonType.LONG, BsonType.DECIMAL], | ||
| valid_inputs={ | ||
| BsonType.DOUBLE: 0.0, | ||
| BsonType.INT: 0, | ||
| BsonType.LONG: Int64(0), | ||
| BsonType.DECIMAL: Decimal128("0"), | ||
| }, | ||
| default_error_code=BAD_VALUE_ERROR, | ||
| ), | ||
| ] | ||
|
|
||
| GEOJSON_REJECTION = generate_bson_rejection_test_cases(GEOJSON_BSON_PARAMS) | ||
| GEOJSON_ACCEPTANCE = generate_bson_acceptance_test_cases(GEOJSON_BSON_PARAMS) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2dsphere") | ||
| @pytest.mark.parametrize("operator", ["$near", "$nearSphere"]) | ||
| @pytest.mark.parametrize("bson_type,sample_value,spec", GEOJSON_REJECTION) | ||
| def test_minDistance_geojson_bson_rejected(collection, operator, bson_type, sample_value, spec): | ||
| """Verifies $minDistance rejects invalid BSON types (GeoJSON).""" | ||
| filt = { | ||
| "loc": { | ||
| operator: { | ||
| "$geometry": ORIGIN, | ||
| spec.keyword: sample_value, | ||
| } | ||
| } | ||
| } | ||
| result = execute_command(collection, {"find": collection.name, "filter": filt}) | ||
| assertFailureCode(result, spec.expected_code(bson_type), msg=spec.msg) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2dsphere") | ||
| @pytest.mark.parametrize("operator", ["$near", "$nearSphere"]) | ||
| @pytest.mark.parametrize("bson_type,sample_value,spec", GEOJSON_ACCEPTANCE) | ||
| def test_minDistance_geojson_bson_accepted(collection, operator, bson_type, sample_value, spec): | ||
| """Verifies $minDistance accepts valid BSON types (GeoJSON).""" | ||
| collection.insert_one({"_id": 1, "loc": ORIGIN}) | ||
| filt = { | ||
| "loc": { | ||
| operator: { | ||
| "$geometry": ORIGIN, | ||
| spec.keyword: sample_value, | ||
| } | ||
| } | ||
| } | ||
| result = execute_command(collection, {"find": collection.name, "filter": filt}) | ||
| assertSuccess( | ||
| result, | ||
| [{"_id": 1, "loc": ORIGIN}], | ||
| msg=f"{spec.keyword} should accept {bson_type.value}", | ||
| ) | ||
|
|
||
|
|
||
| # --- Legacy (2d index) --- | ||
|
|
||
| LEGACY_BSON_PARAMS = [ | ||
| BsonTypeTestCase( | ||
| id="minDistance_legacy", | ||
| msg="$minDistance should reject non-numeric types (legacy)", | ||
| keyword="$minDistance", | ||
| valid_types=[BsonType.DOUBLE, BsonType.INT, BsonType.LONG, BsonType.DECIMAL], | ||
| valid_inputs={ | ||
| BsonType.DOUBLE: 0.0, | ||
| BsonType.INT: 0, | ||
| BsonType.LONG: Int64(0), | ||
| BsonType.DECIMAL: Decimal128("0"), | ||
| }, | ||
| default_error_code=LEGACY_MIN_DISTANCE_NOT_NUMBER_ERROR, | ||
| ), | ||
| ] | ||
|
|
||
| LEGACY_REJECTION = generate_bson_rejection_test_cases(LEGACY_BSON_PARAMS) | ||
| LEGACY_ACCEPTANCE = generate_bson_acceptance_test_cases(LEGACY_BSON_PARAMS) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2d") | ||
| @pytest.mark.parametrize("operator", ["$near", "$nearSphere"]) | ||
| @pytest.mark.parametrize("bson_type,sample_value,spec", LEGACY_REJECTION) | ||
| def test_minDistance_legacy_bson_rejected(collection, operator, bson_type, sample_value, spec): | ||
| """Verifies $minDistance rejects invalid BSON types (legacy).""" | ||
| filt = {"loc": {operator: [0, 0], spec.keyword: sample_value}} | ||
| result = execute_command(collection, {"find": collection.name, "filter": filt}) | ||
| assertFailureCode(result, spec.expected_code(bson_type), msg=spec.msg) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2d") | ||
| @pytest.mark.parametrize("operator", ["$near", "$nearSphere"]) | ||
| @pytest.mark.parametrize("bson_type,sample_value,spec", LEGACY_ACCEPTANCE) | ||
| def test_minDistance_legacy_bson_accepted(collection, operator, bson_type, sample_value, spec): | ||
| """Verifies $minDistance accepts valid BSON types (legacy).""" | ||
| collection.insert_one({"_id": 1, "loc": [0, 0]}) | ||
| filt = {"loc": {operator: [0, 0], spec.keyword: sample_value}} | ||
| result = execute_command(collection, {"find": collection.name, "filter": filt}) | ||
| assertSuccess( | ||
| result, | ||
| [{"_id": 1, "loc": [0, 0]}], | ||
| msg=f"{spec.keyword} should accept {bson_type.value} (legacy)", | ||
| ) |
237 changes: 237 additions & 0 deletions
237
...ty/tests/core/operator/query/geospatial/specifiers/minDistance/test_minDistance_errors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| """Tests for $minDistance error cases — invalid context, values, types, and missing index.""" | ||
|
|
||
| import pytest | ||
| from bson import Decimal128, Int64 | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.query.utils.query_test_case import ( | ||
| QueryTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertFailureCode | ||
| from documentdb_tests.framework.error_codes import ( | ||
| BAD_VALUE_ERROR, | ||
| LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| NO_QUERY_EXECUTION_PLANS_ERROR, | ||
| ) | ||
| from documentdb_tests.framework.executor import execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
| from documentdb_tests.framework.test_constants import ( | ||
| DECIMAL128_INFINITY, | ||
| DECIMAL128_NAN, | ||
| DECIMAL128_NEGATIVE_INFINITY, | ||
| DECIMAL128_NEGATIVE_NAN, | ||
| FLOAT_INFINITY, | ||
| FLOAT_NAN, | ||
| FLOAT_NEGATIVE_INFINITY, | ||
| FLOAT_NEGATIVE_NAN, | ||
| ) | ||
|
|
||
| ORIGIN = {"type": "Point", "coordinates": [0, 0]} | ||
|
|
||
|
|
||
| ERROR_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="without_near_context", | ||
| filter={"loc": {"$minDistance": 1000}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject $minDistance without $near or $nearSphere context", | ||
| ), | ||
| QueryTestCase( | ||
| id="minDistance_with_geoWithin", | ||
| filter={ | ||
| "loc": { | ||
| "$geoWithin": { | ||
| "$geometry": { | ||
| "type": "Polygon", | ||
| "coordinates": [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]], | ||
| }, | ||
| "$minDistance": 1000, | ||
| } | ||
| } | ||
| }, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject $minDistance with $geoWithin (invalid context)", | ||
| ), | ||
| QueryTestCase( | ||
| id="nearSphere_minDistance_without_geometry", | ||
| filter={ | ||
| "loc": { | ||
| "$nearSphere": {"$minDistance": 1000, "$maxDistance": 5000}, | ||
| } | ||
| }, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject $nearSphere with $minDistance but no $geometry or coordinates", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| INVALID_VALUE_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="negative_int", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": -1}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject negative int $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_double", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": -1.5}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject negative double $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_int64", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": Int64(-1)}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject negative Int64 $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_decimal128", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": Decimal128("-1")}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject negative Decimal128 $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="nan_double", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": FLOAT_NAN}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject NaN $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_nan_double", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": FLOAT_NEGATIVE_NAN}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject -NaN $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="infinity", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": FLOAT_INFINITY}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject Infinity $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_infinity", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": FLOAT_NEGATIVE_INFINITY}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject -Infinity $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="nan_decimal128", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": DECIMAL128_NAN}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject Decimal128 NaN $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_nan_decimal128", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": DECIMAL128_NEGATIVE_NAN}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject Decimal128 -NaN $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="infinity_decimal128", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": DECIMAL128_INFINITY}}}, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject Decimal128 Infinity $minDistance", | ||
| ), | ||
| QueryTestCase( | ||
| id="negative_infinity_decimal128", | ||
| filter={ | ||
| "loc": {"$near": {"$geometry": ORIGIN, "$minDistance": DECIMAL128_NEGATIVE_INFINITY}} | ||
| }, | ||
| error_code=BAD_VALUE_ERROR, | ||
| msg="Should reject Decimal128 -Infinity $minDistance", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2dsphere") | ||
| @pytest.mark.parametrize("test", pytest_params(ERROR_TESTS + INVALID_VALUE_TESTS)) | ||
| def test_minDistance_errors(collection, test): | ||
| """Verifies $minDistance rejects invalid contexts and values.""" | ||
| result = execute_command(collection, {"find": collection.name, "filter": test.filter}) | ||
| assertFailureCode(result, test.error_code, msg=test.msg) | ||
|
|
||
|
|
||
| # --- Legacy (2d) path invalid values --- | ||
|
|
||
| LEGACY_INVALID_VALUE_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="legacy_negative_int", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": -1}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject negative int $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_negative_double", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": -1.5}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject negative double $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_nan_double", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": FLOAT_NAN}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject NaN $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_negative_nan_double", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": FLOAT_NEGATIVE_NAN}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject -NaN $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_negative_infinity", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": FLOAT_NEGATIVE_INFINITY}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject -Infinity $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_nan_decimal128", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": DECIMAL128_NAN}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject Decimal128 NaN $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_negative_nan_decimal128", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": DECIMAL128_NEGATIVE_NAN}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject Decimal128 -NaN $minDistance with legacy 2d", | ||
| ), | ||
| QueryTestCase( | ||
| id="legacy_negative_infinity_decimal128", | ||
| filter={"loc": {"$near": [0, 0], "$minDistance": DECIMAL128_NEGATIVE_INFINITY}}, | ||
| error_code=LEGACY_MIN_DISTANCE_NON_NEGATIVE_ERROR, | ||
| msg="Should reject Decimal128 -Infinity $minDistance with legacy 2d", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("geo_2d") | ||
| @pytest.mark.parametrize("test", pytest_params(LEGACY_INVALID_VALUE_TESTS)) | ||
| def test_minDistance_legacy_errors(collection, test): | ||
| """Verifies $minDistance rejects invalid values with legacy 2d index.""" | ||
| result = execute_command(collection, {"find": collection.name, "filter": test.filter}) | ||
| assertFailureCode(result, test.error_code, msg=test.msg) | ||
|
|
||
|
|
||
| # No geo_2dsphere fixture applied — these tests intentionally run without an index. | ||
| NO_INDEX_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="near_without_index", | ||
| filter={"loc": {"$near": {"$geometry": ORIGIN, "$minDistance": 1000}}}, | ||
| error_code=NO_QUERY_EXECUTION_PLANS_ERROR, | ||
| msg="Should error when no geospatial index exists", | ||
| ), | ||
| QueryTestCase( | ||
| id="nearSphere_without_index", | ||
| filter={"loc": {"$nearSphere": {"$geometry": ORIGIN, "$minDistance": 1000}}}, | ||
| error_code=NO_QUERY_EXECUTION_PLANS_ERROR, | ||
| msg="Should error when no geospatial index for $nearSphere", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(NO_INDEX_TESTS)) | ||
| def test_minDistance_no_index_errors(collection, test): | ||
| """Verifies $minDistance fails without geospatial index.""" | ||
| collection.insert_one({"_id": 1, "loc": {"type": "Point", "coordinates": [0, 0]}}) | ||
| result = execute_command(collection, {"find": collection.name, "filter": test.filter}) | ||
| assertFailureCode(result, test.error_code, msg=test.msg) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.