-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
146 lines (104 loc) · 4.29 KB
/
Copy pathconftest.py
File metadata and controls
146 lines (104 loc) · 4.29 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Pytest configuration and shared fixtures for tests."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
import pandas as pd
import pytest
# Paths
FIXTURES_DIR = Path(__file__).parent / "fixtures"
TEST_CONFIG_PATH = FIXTURES_DIR / "test_config.yaml"
@pytest.fixture(scope="session")
def test_settings():
"""Load test settings from YAML configuration."""
from src.common.config import load_config
return load_config(str(TEST_CONFIG_PATH))
@pytest.fixture(scope="session")
def sample_features_df() -> pd.DataFrame:
"""Load sample features dataframe from fixtures."""
return pd.read_csv(FIXTURES_DIR / "sample_100.csv")
@pytest.fixture(scope="session")
def sample_targets_df() -> pd.DataFrame:
"""Load sample targets dataframe from fixtures."""
return pd.read_csv(FIXTURES_DIR / "sample_targets_100.csv")
@pytest.fixture
def sample_feature_dict(sample_features_df: pd.DataFrame) -> dict[str, float]:
"""Get a single row of features as a dictionary."""
from src.serving.schemas import get_pre_campaign_features
row = sample_features_df.iloc[0]
pre_campaign = get_pre_campaign_features()
return {feat: float(row[feat]) for feat in pre_campaign}
@pytest.fixture
def sample_feature_list(sample_feature_dict: dict[str, float]) -> list[float]:
"""Get a single row of features as a list in standard order."""
from src.serving.schemas import get_pre_campaign_features
pre_campaign = get_pre_campaign_features()
return [sample_feature_dict[feat] for feat in pre_campaign]
@pytest.fixture
def categorical_features() -> list[str]:
"""Get list of categorical feature names."""
return ["c_5", "c_6", "c_7", "c_8", "c_24"]
@pytest.fixture
def all_features() -> list[str]:
"""Get list of all pre-campaign feature names."""
from src.serving.schemas import get_pre_campaign_features
return get_pre_campaign_features()
@pytest.fixture
def target_classes() -> dict[int, str]:
"""Get target class mapping."""
from src.serving.schemas import get_target_classes
return get_target_classes()
# Mock GCP client fixtures
@pytest.fixture
def mock_storage_client() -> MagicMock:
"""Create a mock GCS storage client."""
with patch("google.cloud.storage.Client") as mock_cls:
client = MagicMock()
bucket = MagicMock()
blob = MagicMock()
mock_cls.return_value = client
client.bucket.return_value = bucket
bucket.blob.return_value = blob
blob.exists.return_value = True
blob.download_as_bytes.return_value = b"content"
yield {"class": mock_cls, "client": client, "bucket": bucket, "blob": blob}
@pytest.fixture
def mock_bigquery_client() -> MagicMock:
"""Create a mock BigQuery client."""
with patch("google.cloud.bigquery.Client") as mock_cls:
client = MagicMock()
mock_cls.return_value = client
table = MagicMock()
table.num_rows = 100
client.get_table.return_value = table
yield {"class": mock_cls, "client": client, "table": table}
@pytest.fixture
def mock_vertex_ai():
"""Create a mock Vertex AI client."""
with patch("google.cloud.aiplatform") as mock:
run = MagicMock()
execution = MagicMock()
mock.start_run.return_value.__enter__.return_value = run
mock.start_execution.return_value.__enter__.return_value = execution
yield mock
@pytest.fixture
def mock_catboost_model() -> MagicMock:
"""Create a mock CatBoost model."""
import numpy as np
mock_model = MagicMock()
mock_model.predict.return_value = np.array([[1]])
mock_model.predict_proba.return_value = np.array([[0.1, 0.7, 0.2]])
mock_model.get_feature_importance.return_value = np.random.rand(67)
return mock_model
# Async fixtures for Litestar testing
@pytest.fixture
def anyio_backend() -> str:
"""Configure anyio backend for async tests."""
return "asyncio"
def pytest_configure(config: Any) -> None:
"""Configure pytest with custom markers."""
config.addinivalue_line(
"markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
)
config.addinivalue_line("markers", "integration: marks tests as integration tests")
config.addinivalue_line("markers", "unit: marks tests as unit tests")