|
| 1 | +import pytest |
| 2 | +from presidio_analyzer.predefined_recognizers import UuidRecognizer |
| 3 | + |
| 4 | +from tests import assert_result_within_score_range |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="module") |
| 8 | +def recognizer(): |
| 9 | + """Return a UuidRecognizer instance for testing.""" |
| 10 | + return UuidRecognizer() |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module") |
| 14 | +def entities(): |
| 15 | + """Return the entity list this recognizer supports.""" |
| 16 | + return ["UUID"] |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "text, expected_len, expected_positions, expected_score_ranges", |
| 21 | + [ |
| 22 | + # fmt: off |
| 23 | + # Version 4 (random) - most common |
| 24 | + ("Request ID: 550e8400-e29b-41d4-a716-446655440000", |
| 25 | + 1, ((12, 48),), ((0.5, 0.5),)), |
| 26 | + ("User UUID: 6fa459ea-ee8a-3ca4-894e-db77e160355e", |
| 27 | + 1, ((11, 47),), ((0.5, 0.5),)), |
| 28 | +
|
| 29 | + # Version 1 (time-based) |
| 30 | + ("Trace: f47ac10b-58cc-1372-8567-0e02b2c3d479", |
| 31 | + 1, ((7, 43),), ((0.5, 0.5),)), |
| 32 | +
|
| 33 | + # Version 2 (DCE Security, RFC 4122) |
| 34 | + ("DCE UUID: 550e8400-e29b-21d4-a716-446655440000", |
| 35 | + 1, ((10, 46),), ((0.5, 0.5),)), |
| 36 | +
|
| 37 | + # Version 3 (MD5 name-based, RFC 4122) |
| 38 | + ("6ba7b810-9dad-31d1-80b4-00c04fd430c8", |
| 39 | + 1, ((0, 36),), ((0.5, 0.5),)), |
| 40 | +
|
| 41 | + # Version 5 (SHA-1 name-based) |
| 42 | + ("Object id 74738ff5-5367-5958-9aee-98fffdcd1876 created", |
| 43 | + 1, ((10, 46),), ((0.5, 0.5),)), |
| 44 | +
|
| 45 | + # Version 6 (reordered time-based, RFC 9562) |
| 46 | + ("Sortable ID: 1ec9414c-232a-6b00-b3c8-9e6bdeced846", |
| 47 | + 1, ((13, 49),), ((0.5, 0.5),)), |
| 48 | +
|
| 49 | + # Version 7 (timestamp-based, RFC 9562) |
| 50 | + ("New record: 018f4f8e-9a3b-7c3d-8e9f-1a2b3c4d5e6f", |
| 51 | + 1, ((12, 48),), ((0.5, 0.5),)), |
| 52 | +
|
| 53 | + # Version 8 (vendor/implementation-specific, RFC 9562) |
| 54 | + ("Custom UUID: 550e8400-e29b-81d4-a716-446655440000", |
| 55 | + 1, ((13, 49),), ((0.5, 0.5),)), |
| 56 | +
|
| 57 | + # Uppercase |
| 58 | + ("GUID: 550E8400-E29B-41D4-A716-446655440000", |
| 59 | + 1, ((6, 42),), ((0.5, 0.5),)), |
| 60 | +
|
| 61 | + # With context keywords |
| 62 | + ("unique identifier: 550e8400-e29b-41d4-a716-446655440000", |
| 63 | + 1, ((19, 55),), ((0.5, "max"),)), |
| 64 | + ("The guid is 6fa459ea-ee8a-3ca4-894e-db77e160355e", |
| 65 | + 1, ((12, 48),), ((0.5, "max"),)), |
| 66 | +
|
| 67 | + # Multiple UUIDs |
| 68 | + ( |
| 69 | + "IDs: 550e8400-e29b-41d4-a716-446655440000 and " |
| 70 | + "f47ac10b-58cc-1372-8567-0e02b2c3d479", |
| 71 | + 2, |
| 72 | + ((5, 41), (46, 82)), |
| 73 | + ((0.5, 0.5), (0.5, 0.5)), |
| 74 | + ), |
| 75 | +
|
| 76 | + # Invalid cases - should not match |
| 77 | + ("Not a UUID: 550e8400-e29b-41d4-a716", |
| 78 | + 0, (), ()), # Too short |
| 79 | + ("Invalid: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz", |
| 80 | + 0, (), ()), # Invalid hex |
| 81 | + ("Nil UUID: 00000000-0000-0000-0000-000000000000", |
| 82 | + 0, (), ()), # Nil UUID filtered |
| 83 | + ("Bad version: 550e8400-e29b-01d4-a716-446655440000", |
| 84 | + 0, (), ()), # Invalid version nibble (0) |
| 85 | + ("Bad version: 550e8400-e29b-91d4-a716-446655440000", |
| 86 | + 0, (), ()), # Invalid version nibble (9) |
| 87 | + ("Bad variant: 550e8400-e29b-41d4-1716-446655440000", |
| 88 | + 0, (), ()), # Invalid variant nibble |
| 89 | + # fmt: on |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_when_uuids_then_succeed( |
| 93 | + text, |
| 94 | + expected_len, |
| 95 | + expected_positions, |
| 96 | + expected_score_ranges, |
| 97 | + recognizer, |
| 98 | + entities, |
| 99 | + max_score, |
| 100 | +): |
| 101 | + """Verify UuidRecognizer detects valid UUIDs and rejects invalid ones.""" |
| 102 | + results = recognizer.analyze(text, entities) |
| 103 | + assert len(results) == expected_len |
| 104 | + assert len(expected_positions) == expected_len |
| 105 | + assert len(expected_score_ranges) == expected_len |
| 106 | + for res, (st_pos, fn_pos), (st_score, fn_score) in zip( |
| 107 | + results, expected_positions, expected_score_ranges |
| 108 | + ): |
| 109 | + if fn_score == "max": |
| 110 | + fn_score = max_score |
| 111 | + assert_result_within_score_range( |
| 112 | + res, entities[0], st_pos, fn_pos, st_score, fn_score |
| 113 | + ) |
0 commit comments