-
Notifications
You must be signed in to change notification settings - Fork 7
Added tests for $anyElementTrue and $allElementTrue #122
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
Merged
Merged
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
Empty file.
Empty file.
304 changes: 304 additions & 0 deletions
304
...tibility/tests/core/operator/expressions/set/allElementsTrue/test_allElementsTrue_core.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,304 @@ | ||
| """ | ||
| Tests for $allElementsTrue core behavior. | ||
|
|
||
| Covers mixed arrays, nested array behavior (does not descend), | ||
| element order independence, falsy position testing, scale testing, | ||
| large mixed-type arrays, falsy position testing, and scale testing. | ||
| """ | ||
|
|
||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from bson import Binary, Decimal128, Int64, ObjectId, Regex, Timestamp | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.expressions.set.allElementsTrue.utils.allElementsTrue_utils import ( # noqa: E501 | ||
| AllElementsTrueTest, | ||
| build_expr, | ||
| ) | ||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( | ||
| assert_expression_result, | ||
| execute_expression, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| _LARGE_TRUTHY = [ | ||
| True, | ||
| 1, | ||
| Int64(1), | ||
| 1.5, | ||
| Decimal128("1"), | ||
| "str", | ||
| {"a": 1}, | ||
| [1], | ||
| ObjectId("507f1f77bcf86cd799439011"), | ||
| datetime(2017, 1, 1), | ||
| Timestamp(1, 1), | ||
| Binary(b"", 0), | ||
| Regex("re"), | ||
| ] | ||
|
|
||
| ALL_ELEMENTS_TRUE_CORE_TESTS: list[AllElementsTrueTest] = [ | ||
| # --- Mixed arrays — all truthy --- | ||
| AllElementsTrueTest( | ||
| "true_1_string", | ||
| array=[True, 1, "someString"], | ||
| expected=True, | ||
| msg="Should return true for mixed truthy types", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "true_seven", | ||
| array=[True, "seven"], | ||
| expected=True, | ||
| msg="Should return true for bool and string", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "all_numeric_nonzero", | ||
| array=[1, Int64(2), 3.5, Decimal128("4")], | ||
| expected=True, | ||
| msg="Should return true for all non-zero numerics", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "mixed_bson_truthy", | ||
| array=[ | ||
| True, | ||
| 1, | ||
| Int64(1), | ||
| 1.5, | ||
| Decimal128("1"), | ||
| "str", | ||
| {"a": 1}, | ||
| [1], | ||
| ObjectId("507f1f77bcf86cd799439011"), | ||
| datetime(2017, 1, 1), | ||
| Timestamp(1, 1), | ||
| Binary(b"", 0), | ||
| Regex("re"), | ||
| ], | ||
| expected=True, | ||
| msg="Should return true for all BSON truthy types", | ||
| ), | ||
| # --- Mixed truthy and falsy — returns false --- | ||
| AllElementsTrueTest( | ||
| "true_false", | ||
| array=[True, False], | ||
| expected=False, | ||
| msg="Should return false for true and false", | ||
| ), | ||
| AllElementsTrueTest("1_0", array=[1, 0], expected=False, msg="Should return false for 1 and 0"), | ||
| AllElementsTrueTest( | ||
| "true_null", array=[True, None], expected=False, msg="Should return false for true and null" | ||
| ), | ||
| AllElementsTrueTest( | ||
| "hello_0_true", | ||
| array=["hello", 0, True], | ||
| expected=False, | ||
| msg="Should return false for string with zero", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "null_false_0", | ||
| array=[None, False, 0], | ||
| expected=False, | ||
| msg="Should return false when all elements are falsy", | ||
| ), | ||
| # --- Single falsy among many truthy --- | ||
| AllElementsTrueTest( | ||
| "false_at_end", | ||
| array=[True, 1, "a", {}, [], False], | ||
| expected=False, | ||
| msg="Should return false when false is at end", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "null_at_end", | ||
| array=[True, 1, "a", {}, [], None], | ||
| expected=False, | ||
| msg="Should return false when null is at end", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "zero_at_end", | ||
| array=[True, 1, "a", {}, [], 0], | ||
| expected=False, | ||
| msg="Should return false when zero is at end", | ||
| ), | ||
| # --- Large mixed-type array --- | ||
| AllElementsTrueTest( | ||
| "all_truthy_large", | ||
| array=_LARGE_TRUTHY, | ||
| expected=True, | ||
| msg="Should return true for large all-truthy array", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "plus_false", | ||
| array=_LARGE_TRUTHY + [False], | ||
| expected=False, | ||
| msg="Should return false for large array with false appended", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "plus_null", | ||
| array=_LARGE_TRUTHY + [None], | ||
| expected=False, | ||
| msg="Should return false for large array with null appended", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "plus_zero", | ||
| array=_LARGE_TRUTHY + [0], | ||
| expected=False, | ||
| msg="Should return false for large array with zero appended", | ||
| ), | ||
| # --- Nested array behavior — does NOT descend --- | ||
| AllElementsTrueTest( | ||
| "nested_false", | ||
| array=[[False]], | ||
| expected=True, | ||
| msg="Should return true for nested false element", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "nested_zero", array=[[0]], expected=True, msg="Should return true for nested zero element" | ||
| ), | ||
| AllElementsTrueTest( | ||
| "nested_null", | ||
| array=[[None]], | ||
| expected=True, | ||
| msg="Should return true for nested null element", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "nested_empty", | ||
| array=[[]], | ||
| expected=True, | ||
| msg="Should return true for nested empty array element", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "nested_all_falsy", | ||
| array=[[False, 0, None]], | ||
| expected=True, | ||
| msg="Should return true for nested array with all falsy", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "two_nested_arrays", | ||
| array=[[True], [False]], | ||
| expected=True, | ||
| msg="Should return true for two nested arrays", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "two_empty_nested", | ||
| array=[[], []], | ||
| expected=True, | ||
| msg="Should return true for two empty nested arrays", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "deeply_nested", | ||
| array=[[[[False]]]], | ||
| expected=True, | ||
| msg="Should return true for deeply nested false", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "deeply_nested_zero", | ||
| array=[[[[[[0]]]]]], | ||
| expected=True, | ||
| msg="Should return true for deeply nested 0", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "mixed_nesting_depths", | ||
| array=[[False], [[None]], [[[0]]]], | ||
| expected=True, | ||
| msg="Should return true for mixed nesting depths", | ||
| ), | ||
| # --- Element order independence --- | ||
| AllElementsTrueTest( | ||
| "true_1_string_order1", | ||
| array=[True, 1, "string"], | ||
| expected=True, | ||
| msg="Should return true for order 1", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "true_1_string_order2", | ||
| array=[1, "string", True], | ||
| expected=True, | ||
| msg="Should return true for order 2", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "false_1_string_order1", | ||
| array=[False, 1, "string"], | ||
| expected=False, | ||
| msg="Should return false for order 1 with false", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "false_1_string_order2", | ||
| array=[1, "string", False], | ||
| expected=False, | ||
| msg="Should return false for order 2 with false", | ||
| ), | ||
| # --- Comparison with $anyElementTrue — allElementsTrue side --- | ||
| AllElementsTrueTest( | ||
| "cmp_empty", array=[], expected=True, msg="Should return true for empty array" | ||
| ), | ||
| AllElementsTrueTest( | ||
| "cmp_true_only", | ||
| array=[True], | ||
| expected=True, | ||
| msg="Should return true for single true element", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "cmp_false_only", | ||
| array=[False], | ||
| expected=False, | ||
| msg="Should return false for single false element", | ||
| ), | ||
| AllElementsTrueTest( | ||
| "cmp_true_false", | ||
| array=[True, False], | ||
| expected=False, | ||
| msg="Should return false for true and false elements", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(ALL_ELEMENTS_TRUE_CORE_TESTS)) | ||
| def test_allElementsTrue_core(collection, test): | ||
| """Test $allElementsTrue core behavior with literal arrays.""" | ||
| result = execute_expression(collection, build_expr(test)) | ||
| assert_expression_result(result, expected=test.expected, msg=test.msg) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Nested arrays via field reference (requires insert) | ||
| # --------------------------------------------------------------------------- | ||
| def test_allElementsTrue_field_array_of_arrays_all_truthy(collection): | ||
| """Test $allElementsTrue with field containing array of arrays — all truthy as elements.""" | ||
| result = execute_expression_with_insert( | ||
| collection, {"$allElementsTrue": ["$arr"]}, {"arr": [[True], [False], [None]]} | ||
| ) | ||
| assert_expression_result( | ||
| result, expected=True, msg="Should return true when each element is an array" | ||
| ) | ||
|
|
||
|
|
||
| def test_allElementsTrue_field_array_with_bare_false(collection): | ||
| """Test $allElementsTrue with field containing array with bare false element.""" | ||
| result = execute_expression_with_insert( | ||
| collection, {"$allElementsTrue": ["$arr"]}, {"arr": [[True], False]} | ||
| ) | ||
| assert_expression_result( | ||
| result, expected=False, msg="Should return false when array contains bare false element" | ||
| ) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Boolean result type verification | ||
| # --------------------------------------------------------------------------- | ||
| def test_allElementsTrue_returns_boolean_true(collection): | ||
| """Test $allElementsTrue returns exactly boolean true, not 1.""" | ||
| result = execute_expression(collection, {"$allElementsTrue": [[True]]}) | ||
| assert_expression_result(result, expected=True, msg="Should return boolean true") | ||
|
|
||
|
|
||
| def test_allElementsTrue_returns_boolean_false(collection): | ||
| """Test $allElementsTrue returns exactly boolean false, not 0.""" | ||
| result = execute_expression(collection, {"$allElementsTrue": [[False]]}) | ||
| assert_expression_result(result, expected=False, msg="Should return boolean false") | ||
|
|
||
|
|
||
| def test_allElementsTrue_empty_returns_boolean_true(collection): | ||
| """Test $allElementsTrue on empty array returns exactly boolean true.""" | ||
| result = execute_expression(collection, {"$allElementsTrue": [[]]}) | ||
| assert_expression_result(result, expected=True, msg="Empty array should return boolean true") | ||
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.