|
1 | | -from typing import Dict, List, Optional, Union |
| 1 | +from typing import Any, Dict, List, Optional, Union |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from ..utils import ( |
6 | 6 | _ensure_newline, |
| 7 | + find_objs, |
7 | 8 | name2title, |
8 | 9 | sanitize_value, |
9 | 10 | strip_top_level_optional, |
@@ -88,3 +89,151 @@ def test_sanitize_value() -> None: |
88 | 89 | assert sanitize_value("A;B") == "A-B" |
89 | 90 | assert sanitize_value("A\\/B") == "A--B" |
90 | 91 | assert sanitize_value("A\"'B") == "A--B" |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + "instance, schema_key, expected", |
| 96 | + [ |
| 97 | + # Single matching object. |
| 98 | + pytest.param( |
| 99 | + {"schemaKey": "Test", "data": 123}, |
| 100 | + "Test", |
| 101 | + [{"schemaKey": "Test", "data": 123}], |
| 102 | + id="single-match", |
| 103 | + ), |
| 104 | + # No match. |
| 105 | + pytest.param( |
| 106 | + {"schemaKey": "NotMatch", "data": 123}, |
| 107 | + "Test", |
| 108 | + [], |
| 109 | + id="no-match", |
| 110 | + ), |
| 111 | + # Empty dictionary should return an empty list. |
| 112 | + pytest.param( |
| 113 | + {}, |
| 114 | + "Test", |
| 115 | + [], |
| 116 | + id="empty-dict", |
| 117 | + ), |
| 118 | + # Empty list should return an empty list. |
| 119 | + pytest.param( |
| 120 | + [], |
| 121 | + "Test", |
| 122 | + [], |
| 123 | + id="empty-list", |
| 124 | + ), |
| 125 | + # Nested dictionary: the matching object is nested within another dictionary. |
| 126 | + pytest.param( |
| 127 | + {"level1": {"schemaKey": "Test", "info": "nested"}}, |
| 128 | + "Test", |
| 129 | + [{"schemaKey": "Test", "info": "nested"}], |
| 130 | + id="nested-dict", |
| 131 | + ), |
| 132 | + # List of dictionaries: only those with matching schema key are returned. |
| 133 | + pytest.param( |
| 134 | + [ |
| 135 | + {"schemaKey": "Test", "data": 1}, |
| 136 | + {"schemaKey": "Test", "data": 2}, |
| 137 | + {"schemaKey": "NotTest", "data": 3}, |
| 138 | + ], |
| 139 | + "Test", |
| 140 | + [ |
| 141 | + {"schemaKey": "Test", "data": 1}, |
| 142 | + {"schemaKey": "Test", "data": 2}, |
| 143 | + ], |
| 144 | + id="list-of-dicts", |
| 145 | + ), |
| 146 | + # Mixed structure: nested dictionaries and lists. |
| 147 | + pytest.param( |
| 148 | + { |
| 149 | + "a": {"schemaKey": "Test", "value": 1}, |
| 150 | + "b": [ |
| 151 | + {"schemaKey": "NotTest", "value": 2}, |
| 152 | + {"schemaKey": "Test", "value": 3}, |
| 153 | + ], |
| 154 | + "c": "irrelevant", |
| 155 | + "d": [{"e": {"schemaKey": "Test", "value": 4}}], |
| 156 | + }, |
| 157 | + "Test", |
| 158 | + [ |
| 159 | + {"schemaKey": "Test", "value": 1}, |
| 160 | + {"schemaKey": "Test", "value": 3}, |
| 161 | + {"schemaKey": "Test", "value": 4}, |
| 162 | + ], |
| 163 | + id="mixed-structure", |
| 164 | + ), |
| 165 | + # Non-collection type: integer. |
| 166 | + pytest.param( |
| 167 | + 42, |
| 168 | + "Test", |
| 169 | + [], |
| 170 | + id="non-collection-int", |
| 171 | + ), |
| 172 | + # Non-collection type: string. |
| 173 | + pytest.param( |
| 174 | + "some string", |
| 175 | + "Test", |
| 176 | + [], |
| 177 | + id="non-collection-string", |
| 178 | + ), |
| 179 | + # Non-collection type: float. |
| 180 | + pytest.param( |
| 181 | + 3.14, |
| 182 | + "Test", |
| 183 | + [], |
| 184 | + id="non-collection-float", |
| 185 | + ), |
| 186 | + # Non-collection type: None. |
| 187 | + pytest.param( |
| 188 | + None, |
| 189 | + "Test", |
| 190 | + [], |
| 191 | + id="non-collection-None", |
| 192 | + ), |
| 193 | + # Nested child: an object with the schema key contains a nested child that also |
| 194 | + # has the schema key. |
| 195 | + pytest.param( |
| 196 | + {"schemaKey": "Test", "child": {"schemaKey": "Test", "data": "child"}}, |
| 197 | + "Test", |
| 198 | + [ |
| 199 | + {"schemaKey": "Test", "child": {"schemaKey": "Test", "data": "child"}}, |
| 200 | + {"schemaKey": "Test", "data": "child"}, |
| 201 | + ], |
| 202 | + id="nested-child", |
| 203 | + ), |
| 204 | + # List in field: |
| 205 | + # The object with the given schema key has a field whose value is a list |
| 206 | + # containing objects, some of which also have the given schema key. |
| 207 | + pytest.param( |
| 208 | + { |
| 209 | + "schemaKey": "Test", |
| 210 | + "items": [ |
| 211 | + {"schemaKey": "Test", "data": "item1"}, |
| 212 | + {"schemaKey": "Other", "data": "item2"}, |
| 213 | + {"schemaKey": "Test", "data": "item3"}, |
| 214 | + ], |
| 215 | + }, |
| 216 | + "Test", |
| 217 | + [ |
| 218 | + # The outer object is returned first... |
| 219 | + { |
| 220 | + "schemaKey": "Test", |
| 221 | + "items": [ |
| 222 | + {"schemaKey": "Test", "data": "item1"}, |
| 223 | + {"schemaKey": "Other", "data": "item2"}, |
| 224 | + {"schemaKey": "Test", "data": "item3"}, |
| 225 | + ], |
| 226 | + }, |
| 227 | + # ...followed by the matching objects within the list. |
| 228 | + {"schemaKey": "Test", "data": "item1"}, |
| 229 | + {"schemaKey": "Test", "data": "item3"}, |
| 230 | + ], |
| 231 | + id="list-in-field", |
| 232 | + ), |
| 233 | + ], |
| 234 | +) |
| 235 | +def test_find_objs_parametrized( |
| 236 | + instance: Any, schema_key: str, expected: list[dict] |
| 237 | +) -> None: |
| 238 | + result = find_objs(instance, schema_key) |
| 239 | + assert result == expected |
0 commit comments