|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from datafog.services.text_service import TextService |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mock_annotator(): |
| 10 | + mock = Mock() |
| 11 | + mock.annotate.return_value = {"PER": ["John Doe"], "ORG": ["Acme Inc"]} |
| 12 | + return mock |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def text_service(mock_annotator): |
| 17 | + with patch( |
| 18 | + "datafog.services.text_service.SpacyPIIAnnotator.create", |
| 19 | + return_value=mock_annotator, |
| 20 | + ): |
| 21 | + return TextService(text_chunk_length=10) |
| 22 | + |
| 23 | + |
| 24 | +def test_init(text_service): |
| 25 | + assert text_service.text_chunk_length == 10 |
| 26 | + |
| 27 | + |
| 28 | +def test_chunk_text(text_service): |
| 29 | + text = "This is a test sentence for chunking." |
| 30 | + chunks = text_service._chunk_text(text) |
| 31 | + assert len(chunks) == 4 |
| 32 | + assert chunks == ["This is a ", "test sente", "nce for ch", "unking."] |
| 33 | + |
| 34 | + |
| 35 | +def test_combine_annotations(text_service): |
| 36 | + annotations = [ |
| 37 | + {"PER": ["John"], "ORG": ["Acme"]}, |
| 38 | + {"PER": ["Doe"], "LOC": ["New York"]}, |
| 39 | + ] |
| 40 | + combined = text_service._combine_annotations(annotations) |
| 41 | + assert combined == {"PER": ["John", "Doe"], "ORG": ["Acme"], "LOC": ["New York"]} |
| 42 | + |
| 43 | + |
| 44 | +def test_annotate_text_sync(text_service): |
| 45 | + result = text_service.annotate_text_sync("John Doe works at Acme Inc") |
| 46 | + assert result == { |
| 47 | + "PER": ["John Doe", "John Doe", "John Doe"], |
| 48 | + "ORG": ["Acme Inc", "Acme Inc", "Acme Inc"], |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +def test_batch_annotate_text_sync(text_service): |
| 53 | + texts = ["John Doe", "Acme Inc"] |
| 54 | + result = text_service.batch_annotate_text_sync(texts) |
| 55 | + assert result == { |
| 56 | + "John Doe": {"PER": ["John Doe"], "ORG": ["Acme Inc"]}, |
| 57 | + "Acme Inc": {"PER": ["John Doe"], "ORG": ["Acme Inc"]}, |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_annotate_text_async(text_service): |
| 63 | + result = await text_service.annotate_text_async("John Doe works at Acme Inc") |
| 64 | + assert result == { |
| 65 | + "PER": ["John Doe", "John Doe", "John Doe"], |
| 66 | + "ORG": ["Acme Inc", "Acme Inc", "Acme Inc"], |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_batch_annotate_text_async(text_service): |
| 72 | + texts = ["John Doe", "Acme Inc"] |
| 73 | + result = await text_service.batch_annotate_text_async(texts) |
| 74 | + assert result == { |
| 75 | + "John Doe": {"PER": ["John Doe"], "ORG": ["Acme Inc"]}, |
| 76 | + "Acme Inc": {"PER": ["John Doe"], "ORG": ["Acme Inc"]}, |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +def test_long_text_chunking(text_service): |
| 81 | + long_text = "John Doe works at Acme Inc. Jane Smith is from New York City." |
| 82 | + result = text_service.annotate_text_sync(long_text) |
| 83 | + expected_count = len(text_service._chunk_text(long_text)) |
| 84 | + assert result == { |
| 85 | + "PER": ["John Doe"] * expected_count, |
| 86 | + "ORG": ["Acme Inc"] * expected_count, |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test_long_text_chunking_async(text_service): |
| 92 | + long_text = "John Doe works at Acme Inc. Jane Smith is from New York City." |
| 93 | + result = await text_service.annotate_text_async(long_text) |
| 94 | + expected_count = len(text_service._chunk_text(long_text)) |
| 95 | + assert result == { |
| 96 | + "PER": ["John Doe"] * expected_count, |
| 97 | + "ORG": ["Acme Inc"] * expected_count, |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +def test_empty_string(text_service): |
| 102 | + result = text_service.annotate_text_sync("") |
| 103 | + assert result == {} |
| 104 | + |
| 105 | + |
| 106 | +def test_short_string(text_service): |
| 107 | + result = text_service.annotate_text_sync("Short") |
| 108 | + assert result == {"PER": ["John Doe"], "ORG": ["Acme Inc"]} |
| 109 | + |
| 110 | + |
| 111 | +def test_special_characters(text_service): |
| 112 | + result = text_service.annotate_text_sync("John@Doe.com works at Acme-Inc!!!") |
| 113 | + expected_count = len(text_service._chunk_text("John@Doe.com works at Acme-Inc!!!")) |
| 114 | + assert result == { |
| 115 | + "PER": ["John Doe"] * expected_count, |
| 116 | + "ORG": ["Acme Inc"] * expected_count, |
| 117 | + } |
0 commit comments