|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test script for passive voice detection module. |
| 3 | +
|
| 4 | +This script tests the passive_voice_detection module to ensure it works correctly |
| 5 | +with the simplified OpenAI responses API approach. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +# Add the formfyxer package to the path so we can import it |
| 12 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) |
| 13 | + |
| 14 | +# Import the passive voice detection module directly |
| 15 | +from formfyxer.passive_voice_detection import detect_passive_voice_segments |
| 16 | + |
| 17 | + |
| 18 | +def test_basic_functionality(): |
| 19 | + """Test basic passive voice detection with simple examples.""" |
| 20 | + print("🔬 Testing basic functionality...") |
| 21 | + |
| 22 | + # Test clearly passive sentences |
| 23 | + test_cases = [ |
| 24 | + ("The ball was thrown by John.", True), |
| 25 | + ("The president was impeached by Congress.", True), |
| 26 | + ("Letters were sent to all customers.", True), |
| 27 | + ("The door is being opened.", True), |
| 28 | + ("The work has been completed.", True), |
| 29 | + ( |
| 30 | + "The politics being discussed were causing a scene.", |
| 31 | + True, |
| 32 | + ), # Tricky case from our tests |
| 33 | + ] |
| 34 | + |
| 35 | + for sentence, should_be_passive in test_cases: |
| 36 | + try: |
| 37 | + result = detect_passive_voice_segments(sentence) |
| 38 | + |
| 39 | + if not result: |
| 40 | + print(f"❌ No result returned for: {sentence}") |
| 41 | + continue |
| 42 | + |
| 43 | + sentence_result, fragments = result[0] |
| 44 | + is_passive = len(fragments) > 0 |
| 45 | + |
| 46 | + status = "✅" if is_passive == should_be_passive else "❌" |
| 47 | + voice_type = "passive" if is_passive else "active" |
| 48 | + expected_type = "passive" if should_be_passive else "active" |
| 49 | + |
| 50 | + print(f"{status} '{sentence}' → {voice_type} (expected {expected_type})") |
| 51 | + if fragments: |
| 52 | + print(f" Fragments: {fragments}") |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + print(f"❌ Error processing '{sentence}': {e}") |
| 56 | + |
| 57 | + |
| 58 | +def test_active_voice(): |
| 59 | + """Test sentences that should be classified as active voice.""" |
| 60 | + print("\n🔬 Testing active voice detection...") |
| 61 | + |
| 62 | + active_sentences = [ |
| 63 | + "John threw the ball.", |
| 64 | + "The President gave a speech about COVID relief.", |
| 65 | + "I don't usually participate in discussions about politics.", |
| 66 | + "The world of politics is quite fascinating.", |
| 67 | + "Sanders fights for the working class.", |
| 68 | + "Politics is a topic for the commoners.", |
| 69 | + ] |
| 70 | + |
| 71 | + for sentence in active_sentences: |
| 72 | + try: |
| 73 | + result = detect_passive_voice_segments(sentence) |
| 74 | + |
| 75 | + if not result: |
| 76 | + print(f"❌ No result returned for: {sentence}") |
| 77 | + continue |
| 78 | + |
| 79 | + sentence_result, fragments = result[0] |
| 80 | + is_passive = len(fragments) > 0 |
| 81 | + |
| 82 | + status = "✅" if not is_passive else "❌" |
| 83 | + voice_type = "passive" if is_passive else "active" |
| 84 | + |
| 85 | + print(f"{status} '{sentence}' → {voice_type} (expected active)") |
| 86 | + if fragments: |
| 87 | + print(f" Unexpected fragments: {fragments}") |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + print(f"❌ Error processing '{sentence}': {e}") |
| 91 | + |
| 92 | + |
| 93 | +def test_multiple_sentences(): |
| 94 | + """Test processing multiple sentences at once.""" |
| 95 | + print("\n🔬 Testing multiple sentence processing...") |
| 96 | + |
| 97 | + text = """ |
| 98 | + The ball was thrown by John. |
| 99 | + John threw the ball back. |
| 100 | + The game was enjoyed by everyone. |
| 101 | + Everyone enjoyed the game. |
| 102 | + """ |
| 103 | + |
| 104 | + try: |
| 105 | + results = detect_passive_voice_segments(text) |
| 106 | + |
| 107 | + print(f"Processed {len(results)} sentences:") |
| 108 | + for sentence, fragments in results: |
| 109 | + voice_type = "passive" if fragments else "active" |
| 110 | + print(f" • '{sentence.strip()}' → {voice_type}") |
| 111 | + if fragments: |
| 112 | + print(f" Fragments: {fragments}") |
| 113 | + |
| 114 | + except Exception as e: |
| 115 | + print(f"❌ Error processing multiple sentences: {e}") |
| 116 | + |
| 117 | + |
| 118 | +def test_edge_cases(): |
| 119 | + """Test edge cases and error conditions.""" |
| 120 | + print("\n🔬 Testing edge cases...") |
| 121 | + |
| 122 | + # Test empty input |
| 123 | + try: |
| 124 | + result = detect_passive_voice_segments("") |
| 125 | + print("❌ Empty string should raise ValueError") |
| 126 | + except ValueError: |
| 127 | + print("✅ Empty string correctly raises ValueError") |
| 128 | + except Exception as e: |
| 129 | + print(f"❌ Unexpected error for empty string: {e}") |
| 130 | + |
| 131 | + # Test very short sentences (should be filtered out) |
| 132 | + try: |
| 133 | + result = detect_passive_voice_segments("Hi. Yes.") |
| 134 | + print("❌ Short sentences should raise ValueError") |
| 135 | + except ValueError: |
| 136 | + print("✅ Short sentences correctly raises ValueError") |
| 137 | + except Exception as e: |
| 138 | + print(f"❌ Unexpected error for short sentences: {e}") |
| 139 | + |
| 140 | + # Test list input |
| 141 | + try: |
| 142 | + sentences = [ |
| 143 | + "The document was reviewed carefully.", |
| 144 | + "We reviewed the document carefully.", |
| 145 | + ] |
| 146 | + results = detect_passive_voice_segments(sentences) |
| 147 | + |
| 148 | + print(f"✅ List input processed {len(results)} sentences:") |
| 149 | + for sentence, fragments in results: |
| 150 | + voice_type = "passive" if fragments else "active" |
| 151 | + print(f" • '{sentence}' → {voice_type}") |
| 152 | + |
| 153 | + except Exception as e: |
| 154 | + print(f"❌ Error processing list input: {e}") |
| 155 | + |
| 156 | + |
| 157 | +def test_api_availability(): |
| 158 | + """Test if the OpenAI API is properly configured.""" |
| 159 | + print("\n🔬 Testing API availability...") |
| 160 | + |
| 161 | + try: |
| 162 | + # Try a simple test |
| 163 | + result = detect_passive_voice_segments("This is a simple test sentence.") |
| 164 | + |
| 165 | + if result: |
| 166 | + sentence, fragments = result[0] |
| 167 | + voice_type = "passive" if fragments else "active" |
| 168 | + print( |
| 169 | + f"✅ API connection successful! Test sentence classified as: {voice_type}" |
| 170 | + ) |
| 171 | + else: |
| 172 | + print("❌ API returned empty result") |
| 173 | + |
| 174 | + except Exception as e: |
| 175 | + print(f"❌ API connection failed: {e}") |
| 176 | + print("💡 Make sure OPENAI_API_KEY is set in your environment or .env file") |
| 177 | + |
| 178 | + |
| 179 | +def main(): |
| 180 | + """Run all tests.""" |
| 181 | + print("🧪 Testing FormFyxer Passive Voice Detection Module") |
| 182 | + print("=" * 55) |
| 183 | + |
| 184 | + test_api_availability() |
| 185 | + test_basic_functionality() |
| 186 | + test_active_voice() |
| 187 | + test_multiple_sentences() |
| 188 | + test_edge_cases() |
| 189 | + |
| 190 | + print("\n" + "=" * 55) |
| 191 | + print("🏁 Test suite completed!") |
| 192 | + print("\n💡 Tips:") |
| 193 | + print(" - If API tests fail, check your OPENAI_API_KEY environment variable") |
| 194 | + print(" - Compare results with the promptfoo evaluation for consistency") |
| 195 | + print(" - Check that the model (gpt-5-nano) is accessible with your API key") |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + main() |
0 commit comments