[2ff88f42] fix(test): Isolate opener test with proper mocking

This commit is contained in:
2026-02-20 16:17:39 +00:00
parent c1e164b180
commit 0143dba33e

View File

@@ -40,14 +40,18 @@ class TestOpenerGeneration(unittest.TestCase):
self.mock_company.ai_opener_secondary = None self.mock_company.ai_opener_secondary = None
@patch('backend.services.classification.call_gemini_flash') @patch('backend.services.classification.call_gemini_flash')
def test_dual_opener_generation(self, mock_gemini_call): @patch('backend.services.classification.ClassificationService._run_llm_classification_prompt')
def test_dual_opener_generation(self, mock_classification_call, mock_gemini_call):
""" """
Test that both primary and secondary openers are generated and stored. Test that both primary and secondary openers are generated and stored.
""" """
print("\n--- Running Integration Test for Dual Opener Generation ---") print("\n--- Running Integration Test for Dual Opener Generation ---")
# --- Configure Mocks --- # --- Configure Mocks ---
# Simulate different return values for primary and secondary calls # 1. Mock the classification call to return the correct industry
mock_classification_call.return_value = "Leisure - Wet & Spa"
# 2. Mock the opener generation calls (Gemini)
mock_gemini_call.side_effect = [ mock_gemini_call.side_effect = [
"Der reibungslose Betrieb ist entscheidend, um maximale Sicherheit zu gewährleisten.", # Mocked Primary "Der reibungslose Betrieb ist entscheidend, um maximale Sicherheit zu gewährleisten.", # Mocked Primary
"Ein einzigartiges Gästeerlebnis ist der Schlüssel zum Erfolg." # Mocked Secondary "Ein einzigartiges Gästeerlebnis ist der Schlüssel zum Erfolg." # Mocked Secondary
@@ -65,24 +69,28 @@ class TestOpenerGeneration(unittest.TestCase):
# --- Assertions --- # --- Assertions ---
print("2. Verifying results...") print("2. Verifying results...")
# 1. Check that Gemini was called twice # 1. Check that Gemini was called twice for the OPENERS
self.assertEqual(mock_gemini_call.call_count, 2, "❌ FAILED: AI model should have been called twice (for primary and secondary).") self.assertEqual(mock_gemini_call.call_count, 2, "❌ FAILED: AI model for OPENERS should have been called twice.")
print(" ✅ AI model was called twice.") print(" ✅ AI model for openers was called twice.")
# 2. Check that the prompts contained the correct focus # 2. Check that the classification prompt was called
self.assertEqual(mock_classification_call.call_count, 1, "❌ FAILED: Classification prompt should have been called once.")
print(" ✅ Classification prompt was called once.")
# 3. Check prompts contained the correct focus
first_call_args, _ = mock_gemini_call.call_args_list[0] first_call_args, _ = mock_gemini_call.call_args_list[0]
second_call_args, _ = mock_gemini_call.call_args_list[1] second_call_args, _ = mock_gemini_call.call_args_list[1]
self.assertIn("FOKUS: PRIMÄR-PROZESSE", first_call_args[0], "❌ FAILED: First call prompt did not have PRIMARY focus.") self.assertIn("FOKUS: PRIMÄR-PROZESSE", first_call_args[0], "❌ FAILED: First call prompt did not have PRIMARY focus.")
print(" ✅ First call had PRIMARY focus.") print(" ✅ First opener call had PRIMARY focus.")
self.assertIn("FOKUS: SEKUNDÄR-PROZESSE", second_call_args[0], "❌ FAILED: Second call prompt did not have SECONDARY focus.") self.assertIn("FOKUS: SEKUNDÄR-PROZESSE", second_call_args[0], "❌ FAILED: Second call prompt did not have SECONDARY focus.")
print(" ✅ Second call had SECONDARY focus.") print(" ✅ Second opener call had SECONDARY focus.")
# 3. Check that the results were stored on the company object # 4. Check that the results were stored on the company object
self.assertIsNotNone(result_company.ai_opener, "❌ FAILED: ai_opener (primary) was not set.") self.assertIsNotNone(result_company.ai_opener, "❌ FAILED: ai_opener (primary) was not set.")
self.assertIsNotNone(result_company.ai_opener_secondary, "❌ FAILED: ai_opener_secondary was not set.") self.assertIsNotNone(result_company.ai_opener_secondary, "❌ FAILED: ai_opener_secondary was not set.")
print(" ✅ Both ai_opener fields were set on the company object.") print(" ✅ Both ai_opener fields were set on the company object.")
# 4. Check content of the fields # 5. Check content of the fields
self.assertIn("Sicherheit", result_company.ai_opener, "❌ FAILED: Primary opener content mismatch.") self.assertIn("Sicherheit", result_company.ai_opener, "❌ FAILED: Primary opener content mismatch.")
print(f" -> Primary Opener: '{result_company.ai_opener}'") print(f" -> Primary Opener: '{result_company.ai_opener}'")
self.assertIn("Gästeerlebnis", result_company.ai_opener_secondary, "❌ FAILED: Secondary opener content mismatch.") self.assertIn("Gästeerlebnis", result_company.ai_opener_secondary, "❌ FAILED: Secondary opener content mismatch.")