|
8 | 8 | from search_query.query import SearchField |
9 | 9 | from search_query.query_and import AndQuery |
10 | 10 | from search_query.query_near import NEARQuery |
| 11 | +from search_query.query_not import NotQuery |
11 | 12 | from search_query.query_or import OrQuery |
| 13 | +from search_query.query_range import RangeQuery |
12 | 14 | from search_query.utils import format_query_string_positions |
13 | 15 |
|
14 | 16 | # pylint: disable=line-too-long |
@@ -174,3 +176,105 @@ def test_value_setter() -> None: |
174 | 176 | ethics.operator = "non_operators" # type: ignore |
175 | 177 |
|
176 | 178 | ethics.value = "NEAR" |
| 179 | + |
| 180 | + |
| 181 | +def test_children_setter() -> None: |
| 182 | + """Test children setter.""" |
| 183 | + # Test for OrQuery --------------------------------------------- |
| 184 | + or_query = OrQuery( |
| 185 | + ["ethics", "morality"], |
| 186 | + search_field="abstract", |
| 187 | + ) |
| 188 | + assert or_query.children[0].value == "ethics" |
| 189 | + assert or_query.children[1].value == "morality" |
| 190 | + |
| 191 | + with pytest.raises(TypeError): |
| 192 | + or_query.children = "not_a_list" # type: ignore |
| 193 | + |
| 194 | + with pytest.raises(TypeError): |
| 195 | + or_query.children = ["valid", 123] # type: ignore |
| 196 | + |
| 197 | + with pytest.raises(ValueError): |
| 198 | + or_query.children = ["new_child"] # type: ignore |
| 199 | + |
| 200 | + or_query.children = ["new_child", "another_child", "third_child"] # type: ignore |
| 201 | + |
| 202 | + # Test for AndQuery --------------------------------------------- |
| 203 | + and_query = AndQuery( |
| 204 | + ["ethics", "morality"], |
| 205 | + search_field="abstract", |
| 206 | + ) |
| 207 | + assert and_query.children[0].value == "ethics" |
| 208 | + assert and_query.children[1].value == "morality" |
| 209 | + |
| 210 | + with pytest.raises(TypeError): |
| 211 | + and_query.children = "not_a_list" # type: ignore |
| 212 | + |
| 213 | + with pytest.raises(TypeError): |
| 214 | + and_query.children = ["valid", 123] # type: ignore |
| 215 | + |
| 216 | + with pytest.raises(ValueError): |
| 217 | + and_query.children = ["new_child"] # type: ignore |
| 218 | + |
| 219 | + and_query.children = ["new_child", "another_child", "third_child"] # type: ignore |
| 220 | + |
| 221 | + # Test for NotQuery --------------------------------------------- |
| 222 | + not_query = NotQuery( |
| 223 | + ["ethics"], |
| 224 | + search_field="abstract", |
| 225 | + ) |
| 226 | + assert not_query.children[0].value == "ethics" |
| 227 | + |
| 228 | + with pytest.raises(TypeError): |
| 229 | + not_query.children = "not_a_list" # type: ignore |
| 230 | + |
| 231 | + with pytest.raises(TypeError): |
| 232 | + not_query.children = ["valid", 123] # type: ignore |
| 233 | + |
| 234 | + with pytest.raises(ValueError): |
| 235 | + not_query.children = ["new_child", "another_child", "third_child"] # type: ignore |
| 236 | + |
| 237 | + with pytest.raises(ValueError): |
| 238 | + not_query.children = ["new_child"] # type: ignore |
| 239 | + |
| 240 | + # Test for NEARQuery --------------------------------------------- |
| 241 | + near_query = NEARQuery( |
| 242 | + "NEAR", |
| 243 | + distance=5, |
| 244 | + children=["ethics", "morality"], |
| 245 | + search_field="abstract", |
| 246 | + ) |
| 247 | + assert near_query.children[0].value == "ethics" |
| 248 | + assert near_query.children[1].value == "morality" |
| 249 | + |
| 250 | + with pytest.raises(TypeError): |
| 251 | + near_query.children = "not_a_list" # type: ignore |
| 252 | + |
| 253 | + with pytest.raises(TypeError): |
| 254 | + near_query.children = ["valid", 123] # type: ignore |
| 255 | + |
| 256 | + with pytest.raises(ValueError): |
| 257 | + near_query.children = ["new_child"] # type: ignore |
| 258 | + |
| 259 | + with pytest.raises(ValueError): |
| 260 | + near_query.children = ["new_child", "another_child", "third_child"] # type: ignore |
| 261 | + |
| 262 | + # Test for RangeQuery --------------------------------------------- |
| 263 | + range_query = RangeQuery( |
| 264 | + children=["2010", "2020"], |
| 265 | + search_field="year-publication", |
| 266 | + ) |
| 267 | + assert range_query.children[0].value == "2010" |
| 268 | + assert range_query.children[1].value == "2020" |
| 269 | + |
| 270 | + with pytest.raises(TypeError): |
| 271 | + range_query.children = "not_a_list" # type: ignore |
| 272 | + |
| 273 | + with pytest.raises(TypeError): |
| 274 | + range_query.children = ["valid", 123] # type: ignore |
| 275 | + |
| 276 | + with pytest.raises(ValueError): |
| 277 | + range_query.children = ["new_child"] # type: ignore |
| 278 | + |
| 279 | + with pytest.raises(ValueError): |
| 280 | + range_query.children = ["new_child", "another_child", "third_child"] # type: ignore |
0 commit comments