|
17 | 17 |
|
18 | 18 | import copy |
19 | 19 | import pickle |
| 20 | +import re |
20 | 21 |
|
21 | 22 | import pytest |
22 | 23 |
|
|
26 | 27 | BSONMaxKey, |
27 | 28 | BSONMinKey, |
28 | 29 | BSONObjectId, |
| 30 | + BSONRegex, |
29 | 31 | BSONTimestamp, |
30 | 32 | _BSONType, |
31 | 33 | ) |
@@ -411,3 +413,72 @@ def test_bson_timestamp_copy(): |
411 | 413 | def test_bson_timestamp_pickle(): |
412 | 414 | ts = BSONTimestamp(100, 1) |
413 | 415 | assert pickle.loads(pickle.dumps(ts)) == ts |
| 416 | + |
| 417 | + |
| 418 | +def test_bson_regex_valid(): |
| 419 | + rx = BSONRegex("^hello.*$", options="i") |
| 420 | + assert rx.pattern == "^hello.*$" |
| 421 | + assert rx.options == "i" |
| 422 | + assert rx._to_map_value() == { |
| 423 | + "__regex__": { |
| 424 | + "pattern": "^hello.*$", |
| 425 | + "options": "i", |
| 426 | + } |
| 427 | + } |
| 428 | + assert repr(rx) == "BSONRegex('^hello.*$', options='i')" |
| 429 | + |
| 430 | + |
| 431 | +def test_bson_regex_options_sorting_and_deduplication(): |
| 432 | + rx1 = BSONRegex("foo", options="msi") |
| 433 | + assert rx1.options == "ims" |
| 434 | + |
| 435 | + rx2 = BSONRegex("foo", options="mmiis") |
| 436 | + assert rx2.options == "ims" |
| 437 | + |
| 438 | + |
| 439 | +def test_bson_regex_options_from_re_flags(): |
| 440 | + rx = BSONRegex("foo", options=re.IGNORECASE | re.MULTILINE) |
| 441 | + assert rx.options == "im" |
| 442 | + |
| 443 | + |
| 444 | +@pytest.mark.parametrize( |
| 445 | + "pattern_input, options_input, exc_type, match_msg", |
| 446 | + [ |
| 447 | + (123, "i", TypeError, "pattern must be a str"), |
| 448 | + (None, "i", TypeError, "pattern must be a str"), |
| 449 | + ("foo", True, TypeError, "options must be a str or re flag integer"), |
| 450 | + ("foo", [1, 2], TypeError, "options must be a str or re flag integer"), |
| 451 | + ], |
| 452 | +) |
| 453 | +def test_bson_regex_invalid_inputs(pattern_input, options_input, exc_type, match_msg): |
| 454 | + with pytest.raises(exc_type, match=match_msg): |
| 455 | + BSONRegex(pattern_input, options_input) |
| 456 | + |
| 457 | + |
| 458 | +def test_bson_regex_equality(): |
| 459 | + rx1 = BSONRegex("^abc", options="i") |
| 460 | + rx2 = BSONRegex("^abc", options="i") |
| 461 | + rx3 = BSONRegex("^abc", options="m") |
| 462 | + rx4 = BSONRegex("^xyz", options="i") |
| 463 | + assert rx1 == rx2 |
| 464 | + assert rx1 != rx3 |
| 465 | + assert rx1 != rx4 |
| 466 | + assert rx1 != "^abc" |
| 467 | + |
| 468 | + |
| 469 | +def test_bson_regex_hash_and_dict_key(): |
| 470 | + rx1 = BSONRegex("^abc", options="i") |
| 471 | + rx2 = BSONRegex("^abc", options="i") |
| 472 | + assert hash(rx1) == hash(rx2) |
| 473 | + assert len({rx1, rx2}) == 1 |
| 474 | + |
| 475 | + |
| 476 | +def test_bson_regex_copy(): |
| 477 | + rx = BSONRegex("^abc", options="i") |
| 478 | + assert copy.copy(rx) == rx |
| 479 | + assert copy.deepcopy(rx) == rx |
| 480 | + |
| 481 | + |
| 482 | +def test_bson_regex_pickle(): |
| 483 | + rx = BSONRegex("^abc", options="i") |
| 484 | + assert pickle.loads(pickle.dumps(rx)) == rx |
0 commit comments