|
| 1 | +"""Tests for whatsmyuri command argument handling. |
| 2 | +
|
| 3 | +Validates that whatsmyuri accepts any BSON type as its argument value |
| 4 | +and ignores unrecognized fields. |
| 5 | +""" |
| 6 | + |
| 7 | +from datetime import datetime, timezone |
| 8 | + |
| 9 | +import pytest |
| 10 | +from bson import Binary, Code, Decimal128, Int64, MaxKey, MinKey, ObjectId, Regex, Timestamp |
| 11 | + |
| 12 | +from documentdb_tests.compatibility.tests.system.diagnostic.utils.diagnostic_test_case import ( |
| 13 | + DiagnosticTestCase, |
| 14 | +) |
| 15 | +from documentdb_tests.framework.assertions import assertProperties |
| 16 | +from documentdb_tests.framework.executor import execute_admin_command |
| 17 | +from documentdb_tests.framework.parametrize import pytest_params |
| 18 | +from documentdb_tests.framework.property_checks import Eq |
| 19 | + |
| 20 | +pytestmark = pytest.mark.admin |
| 21 | + |
| 22 | + |
| 23 | +# Property [Type Acceptance]: whatsmyuri accepts all BSON types as the command field value. |
| 24 | +ARGUMENT_TYPE_TESTS: list[DiagnosticTestCase] = [ |
| 25 | + DiagnosticTestCase( |
| 26 | + "int_1", |
| 27 | + command={"whatsmyuri": 1}, |
| 28 | + checks={"ok": Eq(1.0)}, |
| 29 | + msg="whatsmyuri should accept int 1", |
| 30 | + ), |
| 31 | + DiagnosticTestCase( |
| 32 | + "int_0", |
| 33 | + command={"whatsmyuri": 0}, |
| 34 | + checks={"ok": Eq(1.0)}, |
| 35 | + msg="whatsmyuri should accept int 0", |
| 36 | + ), |
| 37 | + DiagnosticTestCase( |
| 38 | + "int_neg1", |
| 39 | + command={"whatsmyuri": -1}, |
| 40 | + checks={"ok": Eq(1.0)}, |
| 41 | + msg="whatsmyuri should accept int -1", |
| 42 | + ), |
| 43 | + DiagnosticTestCase( |
| 44 | + "bool_true", |
| 45 | + command={"whatsmyuri": True}, |
| 46 | + checks={"ok": Eq(1.0)}, |
| 47 | + msg="whatsmyuri should accept true", |
| 48 | + ), |
| 49 | + DiagnosticTestCase( |
| 50 | + "bool_false", |
| 51 | + command={"whatsmyuri": False}, |
| 52 | + checks={"ok": Eq(1.0)}, |
| 53 | + msg="whatsmyuri should accept false", |
| 54 | + ), |
| 55 | + DiagnosticTestCase( |
| 56 | + "string", |
| 57 | + command={"whatsmyuri": "hello"}, |
| 58 | + checks={"ok": Eq(1.0)}, |
| 59 | + msg="whatsmyuri should accept string", |
| 60 | + ), |
| 61 | + DiagnosticTestCase( |
| 62 | + "empty_string", |
| 63 | + command={"whatsmyuri": ""}, |
| 64 | + checks={"ok": Eq(1.0)}, |
| 65 | + msg="whatsmyuri should accept empty string", |
| 66 | + ), |
| 67 | + DiagnosticTestCase( |
| 68 | + "null", |
| 69 | + command={"whatsmyuri": None}, |
| 70 | + checks={"ok": Eq(1.0)}, |
| 71 | + msg="whatsmyuri should accept null", |
| 72 | + ), |
| 73 | + DiagnosticTestCase( |
| 74 | + "empty_object", |
| 75 | + command={"whatsmyuri": {}}, |
| 76 | + checks={"ok": Eq(1.0)}, |
| 77 | + msg="whatsmyuri should accept empty object", |
| 78 | + ), |
| 79 | + DiagnosticTestCase( |
| 80 | + "nested_object", |
| 81 | + command={"whatsmyuri": {"a": {"b": 1}}}, |
| 82 | + checks={"ok": Eq(1.0)}, |
| 83 | + msg="whatsmyuri should accept nested object", |
| 84 | + ), |
| 85 | + DiagnosticTestCase( |
| 86 | + "empty_array", |
| 87 | + command={"whatsmyuri": []}, |
| 88 | + checks={"ok": Eq(1.0)}, |
| 89 | + msg="whatsmyuri should accept empty array", |
| 90 | + ), |
| 91 | + DiagnosticTestCase( |
| 92 | + "array_with_elements", |
| 93 | + command={"whatsmyuri": [1, 2, 3]}, |
| 94 | + checks={"ok": Eq(1.0)}, |
| 95 | + msg="whatsmyuri should accept array with elements", |
| 96 | + ), |
| 97 | + DiagnosticTestCase( |
| 98 | + "double", |
| 99 | + command={"whatsmyuri": 1.5}, |
| 100 | + checks={"ok": Eq(1.0)}, |
| 101 | + msg="whatsmyuri should accept double", |
| 102 | + ), |
| 103 | + DiagnosticTestCase( |
| 104 | + "negative_double", |
| 105 | + command={"whatsmyuri": -1.5}, |
| 106 | + checks={"ok": Eq(1.0)}, |
| 107 | + msg="whatsmyuri should accept negative double", |
| 108 | + ), |
| 109 | + DiagnosticTestCase( |
| 110 | + "large_int", |
| 111 | + command={"whatsmyuri": 999_999_999}, |
| 112 | + checks={"ok": Eq(1.0)}, |
| 113 | + msg="whatsmyuri should accept large int", |
| 114 | + ), |
| 115 | + DiagnosticTestCase( |
| 116 | + "int64", |
| 117 | + command={"whatsmyuri": Int64(1)}, |
| 118 | + checks={"ok": Eq(1.0)}, |
| 119 | + msg="whatsmyuri should accept int64", |
| 120 | + ), |
| 121 | + DiagnosticTestCase( |
| 122 | + "decimal128", |
| 123 | + command={"whatsmyuri": Decimal128("1")}, |
| 124 | + checks={"ok": Eq(1.0)}, |
| 125 | + msg="whatsmyuri should accept decimal128", |
| 126 | + ), |
| 127 | + DiagnosticTestCase( |
| 128 | + "decimal128_nan", |
| 129 | + command={"whatsmyuri": Decimal128("NaN")}, |
| 130 | + checks={"ok": Eq(1.0)}, |
| 131 | + msg="whatsmyuri should accept decimal128 NaN", |
| 132 | + ), |
| 133 | + DiagnosticTestCase( |
| 134 | + "decimal128_infinity", |
| 135 | + command={"whatsmyuri": Decimal128("Infinity")}, |
| 136 | + checks={"ok": Eq(1.0)}, |
| 137 | + msg="whatsmyuri should accept decimal128 Infinity", |
| 138 | + ), |
| 139 | + DiagnosticTestCase( |
| 140 | + "decimal128_neg_zero", |
| 141 | + command={"whatsmyuri": Decimal128("-0")}, |
| 142 | + checks={"ok": Eq(1.0)}, |
| 143 | + msg="whatsmyuri should accept decimal128 negative zero", |
| 144 | + ), |
| 145 | + DiagnosticTestCase( |
| 146 | + "infinity", |
| 147 | + command={"whatsmyuri": float("inf")}, |
| 148 | + checks={"ok": Eq(1.0)}, |
| 149 | + msg="whatsmyuri should accept infinity", |
| 150 | + ), |
| 151 | + DiagnosticTestCase( |
| 152 | + "neg_infinity", |
| 153 | + command={"whatsmyuri": float("-inf")}, |
| 154 | + checks={"ok": Eq(1.0)}, |
| 155 | + msg="whatsmyuri should accept negative infinity", |
| 156 | + ), |
| 157 | + DiagnosticTestCase( |
| 158 | + "nan", |
| 159 | + command={"whatsmyuri": float("nan")}, |
| 160 | + checks={"ok": Eq(1.0)}, |
| 161 | + msg="whatsmyuri should accept NaN", |
| 162 | + ), |
| 163 | + DiagnosticTestCase( |
| 164 | + "date", |
| 165 | + command={"whatsmyuri": datetime(2024, 1, 1, tzinfo=timezone.utc)}, |
| 166 | + checks={"ok": Eq(1.0)}, |
| 167 | + msg="whatsmyuri should accept date", |
| 168 | + ), |
| 169 | + DiagnosticTestCase( |
| 170 | + "binData", |
| 171 | + command={"whatsmyuri": Binary(b"")}, |
| 172 | + checks={"ok": Eq(1.0)}, |
| 173 | + msg="whatsmyuri should accept binData", |
| 174 | + ), |
| 175 | + DiagnosticTestCase( |
| 176 | + "objectId", |
| 177 | + command={"whatsmyuri": ObjectId()}, |
| 178 | + checks={"ok": Eq(1.0)}, |
| 179 | + msg="whatsmyuri should accept objectId", |
| 180 | + ), |
| 181 | + DiagnosticTestCase( |
| 182 | + "regex", |
| 183 | + command={"whatsmyuri": Regex("test")}, |
| 184 | + checks={"ok": Eq(1.0)}, |
| 185 | + msg="whatsmyuri should accept regex", |
| 186 | + ), |
| 187 | + DiagnosticTestCase( |
| 188 | + "timestamp", |
| 189 | + command={"whatsmyuri": Timestamp(0, 0)}, |
| 190 | + checks={"ok": Eq(1.0)}, |
| 191 | + msg="whatsmyuri should accept timestamp", |
| 192 | + ), |
| 193 | + DiagnosticTestCase( |
| 194 | + "minKey", |
| 195 | + command={"whatsmyuri": MinKey()}, |
| 196 | + checks={"ok": Eq(1.0)}, |
| 197 | + msg="whatsmyuri should accept minKey", |
| 198 | + ), |
| 199 | + DiagnosticTestCase( |
| 200 | + "maxKey", |
| 201 | + command={"whatsmyuri": MaxKey()}, |
| 202 | + checks={"ok": Eq(1.0)}, |
| 203 | + msg="whatsmyuri should accept maxKey", |
| 204 | + ), |
| 205 | + DiagnosticTestCase( |
| 206 | + "code", |
| 207 | + command={"whatsmyuri": Code("function(){}")}, |
| 208 | + checks={"ok": Eq(1.0)}, |
| 209 | + msg="whatsmyuri should accept JavaScript code", |
| 210 | + ), |
| 211 | +] |
| 212 | + |
| 213 | +# Property [Extra Fields Ignored]: whatsmyuri ignores unrecognized fields. |
| 214 | +EXTRA_FIELD_TESTS: list[DiagnosticTestCase] = [ |
| 215 | + DiagnosticTestCase( |
| 216 | + "extra_field_ignored", |
| 217 | + command={"whatsmyuri": 1, "unknownField": 1}, |
| 218 | + checks={"ok": Eq(1.0)}, |
| 219 | + msg="whatsmyuri should succeed even with unrecognized fields", |
| 220 | + ), |
| 221 | +] |
| 222 | + |
| 223 | +ALL_TESTS = ARGUMENT_TYPE_TESTS + EXTRA_FIELD_TESTS |
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.parametrize("test", pytest_params(ALL_TESTS)) |
| 227 | +def test_whatsmyuri_argument_handling(collection, test): |
| 228 | + """Test whatsmyuri argument handling.""" |
| 229 | + result = execute_admin_command(collection, test.command) |
| 230 | + assertProperties(result, test.checks, msg=test.msg, raw_res=True) |
0 commit comments