|
| 1 | +# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from haystack import Pipeline |
| 8 | +from haystack.components.extractors.regex_text_extractor import RegexTextExtractor |
| 9 | +from haystack.dataclasses import ChatMessage |
| 10 | + |
| 11 | + |
| 12 | +class TestRegexTextExtractor: |
| 13 | + def test_init_with_capture_group(self): |
| 14 | + pattern = r'<issue url="(.+?)">' |
| 15 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 16 | + assert extractor.regex_pattern == pattern |
| 17 | + |
| 18 | + def test_init_without_capture_group(self): |
| 19 | + pattern = r"<issue>" |
| 20 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 21 | + assert extractor.regex_pattern == pattern |
| 22 | + |
| 23 | + def test_extract_from_string_with_capture_group(self): |
| 24 | + pattern = r'<issue url="(.+?)">' |
| 25 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 26 | + text = '<issue url="github.com/hahahaha">hahahah</issue>' |
| 27 | + result = extractor.run(text_or_messages=text) |
| 28 | + assert result == {"captured_text": "github.com/hahahaha"} |
| 29 | + |
| 30 | + def test_extract_from_string_without_capture_group(self): |
| 31 | + pattern = r"<issue>" |
| 32 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 33 | + text = "This is an <issue> tag in the text" |
| 34 | + result = extractor.run(text_or_messages=text) |
| 35 | + assert result == {"captured_text": "<issue>"} |
| 36 | + |
| 37 | + def test_extract_from_string_no_match(self): |
| 38 | + pattern = r'<issue url="(.+?)">' |
| 39 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 40 | + text = "This text has no matching pattern" |
| 41 | + result = extractor.run(text_or_messages=text) |
| 42 | + assert result == {} |
| 43 | + |
| 44 | + def test_extract_from_string_empty_input(self): |
| 45 | + pattern = r'<issue url="(.+?)">' |
| 46 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 47 | + text = "" |
| 48 | + result = extractor.run(text_or_messages=text) |
| 49 | + assert result == {} |
| 50 | + |
| 51 | + def test_extract_from_chat_messages_single_message(self): |
| 52 | + pattern = r'<issue url="(.+?)">' |
| 53 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 54 | + messages = [ChatMessage.from_user('<issue url="github.com/test">test issue</issue>')] |
| 55 | + result = extractor.run(text_or_messages=messages) |
| 56 | + assert result == {"captured_text": "github.com/test"} |
| 57 | + |
| 58 | + def test_extract_from_chat_messages_multiple_messages(self): |
| 59 | + pattern = r'<issue url="(.+?)">' |
| 60 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 61 | + messages = [ |
| 62 | + ChatMessage.from_user('First message with <issue url="first.com">first</issue>'), |
| 63 | + ChatMessage.from_user('Second message with <issue url="second.com">second</issue>'), |
| 64 | + ChatMessage.from_user('Last message with <issue url="last.com">last</issue>'), |
| 65 | + ] |
| 66 | + result = extractor.run(text_or_messages=messages) |
| 67 | + assert result == {"captured_text": "last.com"} |
| 68 | + |
| 69 | + def test_extract_from_chat_messages_no_match_in_last(self): |
| 70 | + pattern = r'<issue url="(.+?)">' |
| 71 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 72 | + messages = [ |
| 73 | + ChatMessage.from_user('First message with <issue url="first.com">first</issue>'), |
| 74 | + ChatMessage.from_user("Last message with no matching pattern"), |
| 75 | + ] |
| 76 | + result = extractor.run(text_or_messages=messages) |
| 77 | + assert result == {} |
| 78 | + |
| 79 | + def test_extract_from_chat_messages_empty_list(self): |
| 80 | + pattern = r'<issue url="(.+?)">' |
| 81 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 82 | + messages = [] |
| 83 | + result = extractor.run(text_or_messages=messages) |
| 84 | + assert result == {} |
| 85 | + |
| 86 | + def test_extract_from_chat_messages_invalid_type(self): |
| 87 | + pattern = r'<issue url="(.+?)">' |
| 88 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 89 | + messages = ["not a ChatMessage object"] |
| 90 | + with pytest.raises(ValueError, match="Expected ChatMessage object, got <class 'str'>"): |
| 91 | + extractor.run(text_or_messages=messages) |
| 92 | + |
| 93 | + def test_multiple_capture_groups(self): |
| 94 | + pattern = r"(\w+)@(\w+)\.(\w+)" |
| 95 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 96 | + text = "Contact us at [email protected] for support" |
| 97 | + result = extractor.run(text_or_messages=text) |
| 98 | + # return the first capture group (username) |
| 99 | + assert result == {"captured_text": "user"} |
| 100 | + |
| 101 | + def test_special_characters_in_pattern(self): |
| 102 | + """Test regex pattern with special characters.""" |
| 103 | + pattern = r"\[(\w+)\]" |
| 104 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 105 | + |
| 106 | + text = "This has [special] characters [in] brackets" |
| 107 | + result = extractor.run(text_or_messages=text) |
| 108 | + |
| 109 | + assert result == {"captured_text": "special"} |
| 110 | + |
| 111 | + def test_whitespace_handling(self): |
| 112 | + """Test regex pattern with whitespace handling.""" |
| 113 | + pattern = r"\s+(\w+)\s+" |
| 114 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 115 | + |
| 116 | + text = "word1 word2 word3" |
| 117 | + result = extractor.run(text_or_messages=text) |
| 118 | + |
| 119 | + assert result == {"captured_text": "word2"} |
| 120 | + |
| 121 | + def test_nested_capture_groups(self): |
| 122 | + """Test regex with nested capture groups.""" |
| 123 | + pattern = r'<(\w+)\s+attr="([^"]+)">' |
| 124 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 125 | + |
| 126 | + text = '<div attr="value">content</div>' |
| 127 | + result = extractor.run(text_or_messages=text) |
| 128 | + |
| 129 | + # Should return the first capture group (tag name) |
| 130 | + assert result == {"captured_text": "div"} |
| 131 | + |
| 132 | + def test_optional_capture_group(self): |
| 133 | + """Test regex with optional capture group.""" |
| 134 | + pattern = r"(\w+)(?:@(\w+))?" |
| 135 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 136 | + |
| 137 | + text = "username@domain" |
| 138 | + result = extractor.run(text_or_messages=text) |
| 139 | + |
| 140 | + assert result == {"captured_text": "username"} |
| 141 | + |
| 142 | + def test_optional_capture_group_no_match(self): |
| 143 | + """Test regex with optional capture group when optional part is missing.""" |
| 144 | + pattern = r"(\w+)(?:@(\w+))?" |
| 145 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 146 | + |
| 147 | + text = "username" |
| 148 | + result = extractor.run(text_or_messages=text) |
| 149 | + |
| 150 | + assert result == {"captured_text": "username"} |
| 151 | + |
| 152 | + def test_pipeline_integration(self): |
| 153 | + """Test component integration in a Haystack pipeline.""" |
| 154 | + pattern = r'<issue url="(.+?)">' |
| 155 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 156 | + |
| 157 | + pipe = Pipeline() |
| 158 | + pipe.add_component("extractor", extractor) |
| 159 | + |
| 160 | + text = '<issue url="github.com/pipeline-test">pipeline test</issue>' |
| 161 | + result = pipe.run(data={"extractor": {"text_or_messages": text}}) |
| 162 | + |
| 163 | + assert result["extractor"] == {"captured_text": "github.com/pipeline-test"} |
| 164 | + |
| 165 | + def test_pipeline_integration_with_chat_messages(self): |
| 166 | + """Test component integration in pipeline with ChatMessages.""" |
| 167 | + pattern = r'<issue url="(.+?)">' |
| 168 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 169 | + |
| 170 | + pipe = Pipeline() |
| 171 | + pipe.add_component("extractor", extractor) |
| 172 | + |
| 173 | + messages = [ChatMessage.from_user('<issue url="github.com/chat-test">chat test</issue>')] |
| 174 | + result = pipe.run(data={"extractor": {"text_or_messages": messages}}) |
| 175 | + |
| 176 | + assert result["extractor"] == {"captured_text": "github.com/chat-test"} |
| 177 | + |
| 178 | + def test_very_long_text(self): |
| 179 | + pattern = r"(\d+)" |
| 180 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 181 | + long_text = "a" * 10000 + "123" + "b" * 10000 |
| 182 | + result = extractor.run(text_or_messages=long_text) |
| 183 | + assert result == {"captured_text": "123"} |
| 184 | + |
| 185 | + def test_multiple_matches_first_is_captured(self): |
| 186 | + pattern = r"(\d+)" |
| 187 | + extractor = RegexTextExtractor(regex_pattern=pattern) |
| 188 | + text = "First: 123, Second: 456, Third: 789" |
| 189 | + result = extractor.run(text_or_messages=text) |
| 190 | + assert result == {"captured_text": "123"} |
0 commit comments