test_autotag.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Unit tests for the auto-tagging module."""
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from src.autotag import generate_tags
  5. from src.config import settings
  6. class TestAutoTag(unittest.TestCase):
  7. """Test suite for the auto-tagging service."""
  8. def setUp(self) -> None:
  9. """Set up test environment."""
  10. settings.autotag_enabled = True
  11. settings.llm_provider = "groq"
  12. settings.groq_api_key = "test-key"
  13. @patch("src.autotag.get_client")
  14. def test_generate_tags(self, mock_get_client: MagicMock) -> None:
  15. """Test tag generation."""
  16. # Mock the client
  17. mock_client = MagicMock()
  18. mock_get_client.return_value = mock_client
  19. # Mock the OpenAI API response
  20. mock_response = MagicMock()
  21. mock_choice = MagicMock()
  22. mock_message = MagicMock()
  23. mock_message.content = '{"category": "Technology", "tags": ["AI", "LLM"]}'
  24. mock_choice.message = mock_message
  25. mock_response.choices = [mock_choice]
  26. mock_client.chat.completions.create.return_value = mock_response
  27. # Call the function
  28. text = "This is a test text about AI and LLMs."
  29. result = generate_tags(text)
  30. # Assertions
  31. self.assertEqual(result["category"], "Technology")
  32. self.assertEqual(result["tags"], ["AI", "LLM"])
  33. mock_client.chat.completions.create.assert_called_once()