mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-02-12 22:52:51 +00:00
service layer tests
This commit is contained in:
7
src/text_generation/config.py
Normal file
7
src/text_generation/config.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import os
|
||||
|
||||
|
||||
def get_api_url():
|
||||
host = os.environ.get("API_HOST", "localhost")
|
||||
port = 9999 if host == "localhost" else 80
|
||||
return f"http://{host}:{port}"
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@@ -1,5 +1,6 @@
|
||||
# conftest.py - Shared test configuration and fixtures
|
||||
|
||||
import time
|
||||
import pytest
|
||||
import os
|
||||
import tempfile
|
||||
@@ -8,6 +9,8 @@ from unittest.mock import Mock, MagicMock
|
||||
from datetime import datetime, timedelta
|
||||
import requests
|
||||
from typing import Generator, Dict, Any
|
||||
from tenacity import retry, stop_after_delay
|
||||
from src.text_generation import config
|
||||
|
||||
# ==============================================================================
|
||||
# SESSION-SCOPED FIXTURES (created once per test session)
|
||||
@@ -43,6 +46,16 @@ def api_client():
|
||||
# FUNCTION-SCOPED FIXTURES (created for each test function)
|
||||
# ==============================================================================
|
||||
|
||||
@retry(stop=stop_after_delay(10))
|
||||
def wait_for_responsive_http_api():
|
||||
return requests.get(config.get_api_url())
|
||||
|
||||
@pytest.fixture
|
||||
def restart_api():
|
||||
(Path(__file__).parent / "../src/text_generation/entrypoints/server.py").touch()
|
||||
time.sleep(0.5)
|
||||
wait_for_responsive_http_api()
|
||||
|
||||
@pytest.fixture
|
||||
def sample_user_data():
|
||||
"""Sample user data for testing."""
|
||||
|
||||
@@ -38,7 +38,7 @@ addopts =
|
||||
--cov-report=html:htmlcov
|
||||
--cov-report=xml
|
||||
# Fail if coverage is below threshold
|
||||
--cov-fail-under=90
|
||||
--cov-fail-under=80
|
||||
|
||||
# Test markers - define custom markers to avoid warnings
|
||||
markers =
|
||||
|
||||
30
tests/unit/test_services.py
Normal file
30
tests/unit/test_services.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import logging
|
||||
import pytest
|
||||
|
||||
from src.text_generation.services.logging.file_logging_service import FileLoggingService
|
||||
from src.text_generation.services.language_models.fake_language_model_response_service import FakeLanguageModelResponseService
|
||||
|
||||
|
||||
def test_file_logging_service_has_filehandler():
|
||||
logfile = 'test.log'
|
||||
svc = FileLoggingService(filename=logfile)
|
||||
assert svc.logger.hasHandlers() == True
|
||||
assert any(type(handler) == logging.FileHandler for handler in svc.logger.handlers)
|
||||
|
||||
|
||||
def test_language_model_response_service_valid_input():
|
||||
svc = FakeLanguageModelResponseService()
|
||||
response = svc.invoke('what is 1 + 1?')
|
||||
assert response != None
|
||||
assert response != ''
|
||||
|
||||
|
||||
def test_language_model_response_service_empty_input():
|
||||
svc = FakeLanguageModelResponseService()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
_ = svc.invoke(user_prompt='')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
user_prompt = None
|
||||
_ = svc.invoke(user_prompt=user_prompt)
|
||||
Reference in New Issue
Block a user