-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_logs.py
More file actions
251 lines (207 loc) · 8.33 KB
/
test_logs.py
File metadata and controls
251 lines (207 loc) · 8.33 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pytest
from datetime import datetime
from unittest.mock import Mock, AsyncMock
from quotientai.resources.logs import Log, LogsResource, AsyncLogsResource, LogDocument
# Fixtures
@pytest.fixture
def mock_client():
return Mock()
@pytest.fixture
def sample_log_data():
return {
"id": "test-id",
"app_name": "test-app",
"environment": "test",
"hallucination_detection": True,
"inconsistency_detection": False,
"user_query": "test query",
"model_output": "test output",
"documents": ["doc1", "doc2"],
"message_history": None,
"instructions": None,
"tags": {"test": "tag"},
"created_at": "2024-01-01T00:00:00"
}
# LogDocument Tests
class TestLogDocument:
"""Tests for the LogDocument class"""
def test_log_document_creation(self):
"""Test basic creation of LogDocument"""
doc = LogDocument(
page_content="This is test content",
metadata={"source": "test_source", "author": "test_author"}
)
assert doc.page_content == "This is test content"
assert doc.metadata["source"] == "test_source"
assert doc.metadata["author"] == "test_author"
def test_log_document_with_no_metadata(self):
"""Test LogDocument creation without metadata"""
doc = LogDocument(page_content="Test content only")
assert doc.page_content == "Test content only"
assert doc.metadata is None
def test_log_document_from_dict(self):
"""Test creating LogDocument from dictionary"""
doc_dict = {
"page_content": "Content from dict",
"metadata": {"source": "dictionary"}
}
doc = LogDocument(**doc_dict)
assert doc.page_content == "Content from dict"
assert doc.metadata["source"] == "dictionary"
# Model Tests
class TestLog:
"""Tests for the Log dataclass"""
def test_log_creation(self):
log = Log(
id="test-id",
app_name="test-app",
environment="test",
hallucination_detection=True,
inconsistency_detection=False,
user_query="test query",
model_output="test output",
documents=["doc1"],
message_history=None,
instructions=None,
tags={},
created_at=datetime.now()
)
assert log.id == "test-id"
assert log.app_name == "test-app"
def test_log_rich_repr(self):
log = Log(
id="test-id",
app_name="test-app",
environment="test",
hallucination_detection=True,
inconsistency_detection=False,
user_query="test query",
model_output="test output",
documents=["doc1"],
message_history=None,
instructions=None,
tags={},
created_at=datetime.now()
)
repr_items = list(log.__rich_repr__())
assert ("id", "test-id") in repr_items
assert ("app_name", "test-app") in repr_items
# Synchronous Resource Tests
class TestLogsResource:
"""Tests for the synchronous LogsResource class"""
@pytest.fixture
def logs_resource(self, mock_client):
return LogsResource(mock_client)
def test_create_log(self, logs_resource):
result = logs_resource.create(
app_name="test-app",
environment="test",
hallucination_detection=True,
inconsistency_detection=False,
user_query="test query",
model_output="test output",
documents=["doc1"]
)
assert result is None # Create is non-blocking
def test_list_logs(self, logs_resource, mock_client, sample_log_data):
mock_client._get.return_value = {"logs": [sample_log_data]}
logs = logs_resource.list(
app_name="test-app",
environment="test"
)
assert len(logs) == 1
assert isinstance(logs[0], Log)
assert logs[0].app_name == "test-app"
assert logs[0].environment == "test"
def test_list_logs_with_dates(self, logs_resource, mock_client, sample_log_data):
mock_client._get.return_value = {"logs": [sample_log_data]}
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 1, 2)
logs = logs_resource.list(
start_date=start_date,
end_date=end_date
)
called_params = mock_client._get.call_args[1]['params']
assert called_params['start_date'] == start_date.isoformat()
assert called_params['end_date'] == end_date.isoformat()
def test_list_logs_error_handling(self, logs_resource, mock_client):
mock_client._get.side_effect = Exception("API Error")
with pytest.raises(Exception):
logs_resource.list()
def test_post_log(self, logs_resource):
test_data = {"message": "test log", "level": "info"}
# Test successful post
def mock_successful_post(path, data):
assert path == "/logs"
assert data == test_data
return {}
logs_resource._client._post = mock_successful_post
logs_resource._post_log(test_data)
# Test failed post
def mock_failed_post(path, data):
raise Exception("Network error")
logs_resource._client._post = mock_failed_post
logs_resource._post_log(test_data) # Should complete without error
# Asynchronous Resource Tests
class TestAsyncLogsResource:
"""Tests for the asynchronous AsyncLogsResource class"""
@pytest.fixture
def async_logs_resource(self, mock_client):
mock_client._get = AsyncMock()
return AsyncLogsResource(mock_client)
@pytest.mark.asyncio
async def test_create_log(self, async_logs_resource):
async_logs_resource._client._post = AsyncMock()
result = await async_logs_resource.create(
app_name="test-app",
environment="test",
hallucination_detection=True,
inconsistency_detection=False,
user_query="test query",
model_output="test output",
documents=["doc1"]
)
assert result is None # Create is non-blocking
@pytest.mark.asyncio
async def test_list_logs(self, async_logs_resource, mock_client, sample_log_data):
mock_client._get.return_value = {"logs": [sample_log_data]}
logs = await async_logs_resource.list(
app_name="test-app",
environment="test"
)
assert len(logs) == 1
assert isinstance(logs[0], Log)
assert logs[0].app_name == "test-app"
assert logs[0].environment == "test"
@pytest.mark.asyncio
async def test_list_logs_with_dates(self, async_logs_resource, mock_client, sample_log_data):
mock_client._get.return_value = {"logs": [sample_log_data]}
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 1, 2)
logs = await async_logs_resource.list(
start_date=start_date,
end_date=end_date
)
called_params = mock_client._get.call_args[1]['params']
assert called_params['start_date'] == start_date.isoformat()
assert called_params['end_date'] == end_date.isoformat()
@pytest.mark.asyncio
async def test_list_logs_error_handling(self, async_logs_resource, mock_client):
mock_client._get.side_effect = Exception("API Error")
with pytest.raises(Exception):
await async_logs_resource.list()
@pytest.mark.asyncio
async def test_post_log_in_background(self, async_logs_resource):
test_data = {"message": "test log", "level": "info"}
# Test successful post
async def mock_successful_post(path, data):
assert path == "/logs"
assert data == test_data
return {}
async_logs_resource._client._post = mock_successful_post
await async_logs_resource._post_log_in_background(test_data)
# Test failed post
async def mock_failed_post(path, data):
raise Exception("Network error")
async_logs_resource._client._post = mock_failed_post
await async_logs_resource._post_log_in_background(test_data) # Should complete without error