|
| 1 | +import unittest |
| 2 | + |
| 3 | +from parameterized import parameterized |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | +from ai_data_preprocessing_queue.Pipeline import Pipeline |
| 6 | +from ai_data_preprocessing_queue.Steps.remove_signature import ( |
| 7 | + step, remove_greetings_and_following_text, remove_newline) |
| 8 | + |
| 9 | + |
| 10 | +class TestRemoveSignature(unittest.TestCase): |
| 11 | + @parameterized.expand([ # type: ignore[misc] |
| 12 | + ( |
| 13 | + "multiple_newlines", |
| 14 | + "Could you please review the attached document?\n\n\nI need your feedback by Friday.", |
| 15 | + "Could you please review the attached document? I need your feedback by Friday.", |
| 16 | + ), |
| 17 | + ( |
| 18 | + "multiple_spaces", |
| 19 | + "The meeting is scheduled for 3PM tomorrow.", |
| 20 | + "The meeting is scheduled for 3PM tomorrow.", |
| 21 | + ), |
| 22 | + ( |
| 23 | + "mixed_whitespace", |
| 24 | + "Please find the report attached. \n\n The numbers look good \r\n\r\n for Q3!", |
| 25 | + "Please find the report attached. The numbers look good for Q3!", |
| 26 | + ), |
| 27 | + ( |
| 28 | + "empty_string", |
| 29 | + "", |
| 30 | + "" |
| 31 | + ), |
| 32 | + ( |
| 33 | + "trailing_whitespace", |
| 34 | + "I'll send the updated version tomorrow. \n\n ", |
| 35 | + "I'll send the updated version tomorrow." |
| 36 | + ) |
| 37 | + ]) |
| 38 | + def test_remove_newline(self, name: str, input_text: str, expected: str) -> None: |
| 39 | + self.assertEqual(remove_newline(input_text), expected) |
| 40 | + |
| 41 | + @parameterized.expand([ # type: ignore[misc] |
| 42 | + ( |
| 43 | + "english_signature_basic", |
| 44 | + "Here's the project update. Sincerely, John Smith\nProject Manager", |
| 45 | + "Here's the project update." |
| 46 | + ), |
| 47 | + ( |
| 48 | + "english_signature_with_content", |
| 49 | + "Please review the attached documents. Best regards, Jane Doe\nSenior Developer\nTech Department", |
| 50 | + "Please review the attached documents." |
| 51 | + ), |
| 52 | + ( |
| 53 | + "english_signature_with_content_and_several_newlines", |
| 54 | + "Please review the attached documents. Best regards,\nJane Doe\n\nSenior Developer\n\nTech Department", |
| 55 | + "Please review the attached documents." |
| 56 | + ), |
| 57 | + ( |
| 58 | + "german_signature", |
| 59 | + "Die Unterlagen wurden aktualisiert. Mit freundlichen Grüßen, Hans Schmidt\nPhone: +49 123 456789", |
| 60 | + "Die Unterlagen wurden aktualisiert." |
| 61 | + ), |
| 62 | + ( |
| 63 | + "greeting_with_comma", |
| 64 | + "Meeting is scheduled for tomorrow. Kind regards, Sarah", |
| 65 | + "Meeting is scheduled for tomorrow." |
| 66 | + ), |
| 67 | + ( |
| 68 | + "mixed_case_greeting", |
| 69 | + "Report is ready. BEST REGARDS, Tom Wilson", |
| 70 | + "Report is ready." |
| 71 | + ), |
| 72 | + ( |
| 73 | + "multiple_greetings", |
| 74 | + "Hello team, here's the update. Best regards, Jim\nRegards, HR Team", |
| 75 | + "Hello team, here's the update." |
| 76 | + ), |
| 77 | + ( |
| 78 | + "empty_string", |
| 79 | + "", |
| 80 | + "" |
| 81 | + ), |
| 82 | + ( |
| 83 | + "no_greetings", |
| 84 | + "This is a plain text without any greetings or signatures.", |
| 85 | + "This is a plain text without any greetings or signatures." |
| 86 | + ), |
| 87 | + ]) |
| 88 | + def test_remove_greetings_and_following_text(self, name: str, input_text: str, expected: str) -> None: |
| 89 | + self.assertEqual(remove_greetings_and_following_text(input_text), expected) |
| 90 | + |
| 91 | + @parameterized.expand([ # type: ignore[misc] |
| 92 | + ( |
| 93 | + "remove_signature_basic", |
| 94 | + "We're sending the final draft for review. Best regards, Alice Johnson\nProject Lead", |
| 95 | + "We're sending the final draft for review.", |
| 96 | + ), |
| 97 | + ( |
| 98 | + "remove_signature_extended", |
| 99 | + "Order Mice/keyboard\nGoodmorning, Can you please order the following: 10 x Dell Laser Mouse IL3220 " |
| 100 | + "10 x Dell Business Keyboard AB322 (UK layout) Thx Best regards Jimmy B. " |
| 101 | + "| C Facilities & Reception Klaus+Andreas Nederland | Anonymstraat 47 | 1234 AJ Amsterdam | Netherlands " |
| 102 | + "Phone: +01 23 695 4567 | Mobile: +97 65 445 1234 | Fax: +31 35 695 8825 jim.anonymus@company.com " |
| 103 | + "| www.nl.somecompany.com", |
| 104 | + "Order Mice/keyboard Goodmorning, Can you please order the following: 10 x Dell Laser Mouse IL3220 " |
| 105 | + "10 x Dell Business Keyboard AB322 (UK layout) Thx", |
| 106 | + ), |
| 107 | + ( |
| 108 | + "thanking_at_start", |
| 109 | + "Thank you very much for your support. " |
| 110 | + "I will prepare the contract and send it tomorrow.\n\nBest regards, Bob Brown", |
| 111 | + "I will prepare the contract and send it tomorrow.", |
| 112 | + ), |
| 113 | + ( |
| 114 | + "thanking_in_middle", |
| 115 | + "Thank you very much for your support. " |
| 116 | + "I appreciate your support on this migration. Thanks a lot, I will share the logs shortly.", |
| 117 | + "I appreciate your support on this migration. I will share the logs shortly.", |
| 118 | + ), |
| 119 | + ( |
| 120 | + "single_greeting_word_german", |
| 121 | + "The deliverables are ready. Grüße", |
| 122 | + "The deliverables are ready.", |
| 123 | + ), |
| 124 | + ( |
| 125 | + "german_empty_result", |
| 126 | + "Vielen Dank für Ihre Hilfe. Mit freundlichen Grüßen, Lena Meyer " |
| 127 | + "Und hier kommt noch mehr Text.", |
| 128 | + "", |
| 129 | + ), |
| 130 | + ( |
| 131 | + "no_change", |
| 132 | + "Please schedule the kickoff meeting for next Tuesday morning at 10:00.", |
| 133 | + "Please schedule the kickoff meeting for next Tuesday morning at 10:00.", |
| 134 | + ), |
| 135 | + ]) |
| 136 | + def test_remove_signature(self, name: str, input_text: str, expected: str) -> None: |
| 137 | + pipeline = Pipeline({"remove_signature": None}) |
| 138 | + value = pipeline.consume(input_text) |
| 139 | + self.assertEqual(expected, value) |
| 140 | + |
| 141 | + def test_remove_signature_step_empty_item(self) -> None: |
| 142 | + result = step("", {}, None, "") |
| 143 | + self.assertEqual(result, "") |
| 144 | + |
| 145 | + @patch("ai_data_preprocessing_queue.Steps.remove_signature.remove_greetings_and_following_text", |
| 146 | + side_effect=Exception("Test error")) |
| 147 | + def test_remove_signature_step_error(self, _: MagicMock) -> None: |
| 148 | + with self.assertRaises(Exception): |
| 149 | + step("Please schedule the kickoff meeting for next Tuesday morning at 10:00.", {}, None, "") |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + unittest.main() |
0 commit comments