[2ff88f42] fix(test): Enhance mock data for opener test

This commit is contained in:
2026-02-20 16:16:14 +00:00
parent 794a113ee5
commit c1e164b180

View File

@@ -0,0 +1,102 @@
import sys
import os
import logging
import unittest
from unittest.mock import MagicMock, patch
# Add backend path & activate venv if possible
sys.path.insert(0, "/app")
from backend.services.classification import ClassificationService
from backend.database import Company, Industry
# Setup basic logging
logging.basicConfig(level=logging.INFO)
class TestOpenerGeneration(unittest.TestCase):
def setUp(self):
"""Set up a mock environment."""
self.service = ClassificationService()
# Mock a database session
self.mock_db = MagicMock()
# Mock Industry object (as if read from DB)
self.mock_industry = Industry(
name="Leisure - Wet & Spa",
pains="Hohes Unfallrisiko durch Nässe, strenge Hygiene-Anforderungen.",
ops_focus_secondary=False
)
# Mock Company object
self.mock_company = Company(
id=1,
name="Therme Erding",
industry_ai="Leisure - Wet & Spa"
)
# Add the fields we are testing
self.mock_company.ai_opener = None
self.mock_company.ai_opener_secondary = None
@patch('backend.services.classification.call_gemini_flash')
def test_dual_opener_generation(self, mock_gemini_call):
"""
Test that both primary and secondary openers are generated and stored.
"""
print("\n--- Running Integration Test for Dual Opener Generation ---")
# --- Configure Mocks ---
# Simulate different return values for primary and secondary calls
mock_gemini_call.side_effect = [
"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
]
# Mock the content loader to return some text
with patch.object(self.service, '_get_website_content_and_url', return_value=("Die Therme Erding ist die größte Therme der Welt.", "http://mock.com")):
# --- Execute the Method ---
print("1. Calling classify_company_potential...")
# We patch the metric extraction to isolate the opener logic
with patch.object(self.service, 'extract_metrics_for_industry', return_value=self.mock_company):
# The method under test!
result_company = self.service.classify_company_potential(self.mock_company, self.mock_db)
# --- Assertions ---
print("2. Verifying results...")
# 1. Check that Gemini was called twice
self.assertEqual(mock_gemini_call.call_count, 2, "❌ FAILED: AI model should have been called twice (for primary and secondary).")
print(" ✅ AI model was called twice.")
# 2. Check that the prompts contained the correct focus
first_call_args, _ = mock_gemini_call.call_args_list[0]
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.")
print(" ✅ First call had PRIMARY 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.")
# 3. 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_secondary, "❌ FAILED: ai_opener_secondary was not set.")
print(" ✅ Both ai_opener fields were set on the company object.")
# 4. Check content of the fields
self.assertIn("Sicherheit", result_company.ai_opener, "❌ FAILED: Primary opener content mismatch.")
print(f" -> Primary Opener: '{result_company.ai_opener}'")
self.assertIn("Gästeerlebnis", result_company.ai_opener_secondary, "❌ FAILED: Secondary opener content mismatch.")
print(f" -> Secondary Opener: '{result_company.ai_opener_secondary}'")
print("\n--- ✅ PASSED: Dual Opener logic is working correctly. ---")
if __name__ == '__main__':
# Patch the _load_industry_definitions to return our mock
with patch('backend.services.classification.ClassificationService._load_industry_definitions') as mock_load:
# Provide a description, so the classifier can match the industry
mock_load.return_value = [Industry(
name="Leisure - Wet & Spa",
description="Thermalbad, Spa, Wasserwelten, Saunen und Rutschenparks.",
pains="Hygiene"
)]
unittest.main()