File size: 2,170 Bytes
0828c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Pytest configuration and fixtures for all tests.

This module provides global test fixtures that are automatically applied
to all tests in the test suite.

Environment Variables:
    SKIP_LLM_MOCK: Set to "true" to disable LLM mocking for integration tests
                   that require actual Ollama connection.

Example:
    # Run tests with actual Ollama (requires Ollama to be running)
    SKIP_LLM_MOCK=true pytest tests/

    # Run tests with mocked LLM (default, works in CI/CD)
    pytest tests/
"""

import os
from unittest.mock import MagicMock, patch

import pytest


@pytest.fixture(autouse=True)
def mock_llm_handler():
    """Mock LLM handler to avoid requiring Ollama in tests.

    This fixture is automatically used for all tests unless explicitly disabled
    via the SKIP_LLM_MOCK environment variable. It prevents tests from trying
    to connect to Ollama, which may not be available in CI/CD environments
    like GitHub Actions.

    The mock provides:
    - A mock LLM that returns test responses
    - A mock system prompt
    - All necessary methods to simulate LLM behavior

    This allows tests to focus on business logic without requiring a running
    Ollama instance.
    """
    # Check if we should skip mocking (e.g., for integration tests)
    if os.environ.get("SKIP_LLM_MOCK") == "true":
        yield
        return

    with patch("src.rag_pipeline.get_llm_handler") as mock_get_handler:
        # Create a mock LLM handler
        mock_handler = MagicMock()

        # Create a mock LLM that returns simple responses
        mock_llm = MagicMock()
        mock_llm.invoke.return_value = "This is a test response from the mocked LLM."
        mock_llm.ainvoke.return_value = "This is a test response from the mocked LLM."

        # Configure the mock handler to return the mock LLM
        mock_handler.get_llm.return_value = mock_llm
        mock_handler.get_system_prompt.return_value = (
            "You are a helpful AI assistant. Answer questions based on the provided context."
        )

        # Make get_llm_handler return our mock
        mock_get_handler.return_value = mock_handler

        yield mock_handler