-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient_v2_test.py
More file actions
338 lines (294 loc) · 11.7 KB
/
client_v2_test.py
File metadata and controls
338 lines (294 loc) · 11.7 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import logging
import os
from difflib import SequenceMatcher, unified_diff
from pathlib import Path
import pytest
from unstract.llmwhisperer.client_v2 import (
LLMWhispererClientException,
LLMWhispererClientV2,
)
logger = logging.getLogger(__name__)
# Test tolerance constants for better maintainability
COORDINATE_TOLERANCE = 2
PERCENTAGE_TOLERANCE = 0.05
PAGE_HEIGHT_TOLERANCE = 5
OCR_SIMILARITY_THRESHOLD = 0.90
def test_get_usage_info(client_v2: LLMWhispererClientV2) -> None:
usage_info = client_v2.get_usage_info()
logger.info(usage_info)
assert isinstance(usage_info, dict), "usage_info should be a dictionary"
expected_keys = [
"current_page_count",
"current_page_count_document_insights",
"current_page_count_low_cost",
"current_page_count_form",
"current_page_count_high_quality",
"current_page_count_native_text",
"current_page_count_excel",
"daily_quota",
"monthly_quota",
"overage_page_count",
"subscription_plan",
"today_page_count",
"current_page_count_table",
]
assert set(expected_keys).issubset(
usage_info.keys()
), f"usage_info is missing expected keys: {set(expected_keys) - set(usage_info.keys())}"
@pytest.mark.parametrize(
"output_mode, mode, input_file",
[
("layout_preserving", "native_text", "credit_card.pdf"),
("layout_preserving", "low_cost", "credit_card.pdf"),
("layout_preserving", "high_quality", "restaurant_invoice_photo.pdf"),
("layout_preserving", "form", "handwritten-form.pdf"),
("text", "native_text", "credit_card.pdf"),
("text", "low_cost", "credit_card.pdf"),
("text", "high_quality", "restaurant_invoice_photo.pdf"),
("text", "form", "handwritten-form.pdf"),
("layout_preserving", "high_quality", "utf_8_chars.pdf"),
],
)
def test_whisper_v2(
client_v2: LLMWhispererClientV2,
data_dir: str,
output_mode: str,
mode: str,
input_file: str,
) -> None:
file_path = os.path.join(data_dir, input_file)
whisper_result = client_v2.whisper(
mode=mode,
output_mode=output_mode,
file_path=file_path,
wait_for_completion=True,
)
logger.debug(f"Result for '{output_mode}', '{mode}', " f"'{input_file}: {whisper_result}")
exp_basename = f"{Path(input_file).stem}.{mode}.{output_mode}.txt"
exp_file = os.path.join(data_dir, "expected", exp_basename)
# verify extracted text
assert_extracted_text(exp_file, whisper_result, mode, output_mode)
@pytest.mark.parametrize(
"input_file",
[
("credit_card.pdf"),
],
)
def test_highlight(client_v2: LLMWhispererClientV2, data_dir: str, input_file: str) -> None:
file_path = os.path.join(data_dir, input_file)
whisper_result = client_v2.whisper(
add_line_nos=True,
file_path=file_path,
wait_for_completion=True,
)
whisper_hash = whisper_result["whisper_hash"]
highlight_data = client_v2.get_highlight_data(whisper_hash=whisper_hash, lines="1-2")
# Assert the structure and content of highlight_data
assert isinstance(highlight_data, dict)
assert len(highlight_data) == 2
assert "1" in highlight_data
assert "2" in highlight_data
# Assert line 1 data
line1 = highlight_data["1"]
assert line1["base_y"] == 0
assert line1["base_y_percent"] == 0
assert line1["height"] == 0
assert line1["height_percent"] == 0
assert line1["page"] == 0
assert line1["page_height"] == 0
assert line1["raw"] == [0, 0, 0, 0]
# Assert line 2 data
line2 = highlight_data["2"]
assert line2["base_y"] == pytest.approx(155, abs=COORDINATE_TOLERANCE)
assert line2["base_y_percent"] == pytest.approx(4.8927, abs=PERCENTAGE_TOLERANCE)
assert line2["height"] == pytest.approx(51, abs=COORDINATE_TOLERANCE)
assert line2["height_percent"] == pytest.approx(1.6098, abs=PERCENTAGE_TOLERANCE)
assert line2["page"] == 0
assert line2["page_height"] == pytest.approx(3168, abs=PAGE_HEIGHT_TOLERANCE)
@pytest.mark.parametrize(
"output_mode, mode, url, input_file, page_count",
[
(
"layout_preserving",
"native_text",
"https://unstractpocstorage.blob.core.windows.net/public/Amex.pdf",
"credit_card.pdf",
7,
),
(
"layout_preserving",
"low_cost",
"https://unstractpocstorage.blob.core.windows.net/public/Amex.pdf",
"credit_card.pdf",
7,
),
(
"layout_preserving",
"high_quality",
"https://unstractpocstorage.blob.core.windows.net/public/scanned_bill.pdf",
"restaurant_invoice_photo.pdf",
1,
),
(
"layout_preserving",
"form",
"https://unstractpocstorage.blob.core.windows.net/public/scanned_form.pdf",
"handwritten-form.pdf",
1,
),
],
)
def test_whisper_v2_url_in_post(
client_v2: LLMWhispererClientV2,
data_dir: str,
output_mode: str,
mode: str,
url: str,
input_file: str,
page_count: int,
) -> None:
usage_before = client_v2.get_usage_info()
whisper_result = client_v2.whisper(mode=mode, output_mode=output_mode, url=url, wait_for_completion=True)
logger.debug(f"Result for '{output_mode}', '{mode}', " f"'{input_file}: {whisper_result}")
exp_basename = f"{Path(input_file).stem}.{mode}.{output_mode}.txt"
exp_file = os.path.join(data_dir, "expected", exp_basename)
# verify extracted text
assert_extracted_text(exp_file, whisper_result, mode, output_mode)
usage_after = client_v2.get_usage_info()
# Verify usage after extraction
verify_usage(usage_before, usage_after, page_count, mode)
@pytest.mark.parametrize(
"url,token,webhook_name",
[
(
os.getenv(
"WEBHOOK_TEST_URL", "https://httpbin.org/post"
), # configurable via env var, defaults to httpbin.org
"",
"client_v2_test",
),
],
)
def test_webhook(client_v2: LLMWhispererClientV2, url: str, token: str, webhook_name: str) -> None:
"""Tests the registration, retrieval, update, and deletion of a webhook.
This test method performs the following steps:
1. Registers a new webhook with the provided URL, token, and webhook name.
2. Retrieves the details of the registered webhook and verifies the URL, token, and webhook name.
3. Updates the webhook details with a new token.
4. Deletes the webhook and verifies the deletion.
Args:
client_v2 (LLMWhispererClientV2): The client instance for making API requests.
url (str): The URL of the webhook.
token (str): The authentication token for the webhook.
webhook_name (str): The name of the webhook.
Returns:
None
"""
result = client_v2.register_webhook(url, token, webhook_name)
assert isinstance(result, dict)
assert result["message"] == "Webhook created successfully"
result = client_v2.get_webhook_details(webhook_name)
assert isinstance(result, dict)
assert result["url"] == url
assert result["auth_token"] == token
assert result["webhook_name"] == webhook_name
result = client_v2.update_webhook_details(webhook_name, url, "new_token")
assert isinstance(result, dict)
assert result["message"] == "Webhook updated successfully"
result = client_v2.get_webhook_details(webhook_name)
assert isinstance(result, dict)
assert result["auth_token"] == "new_token"
result = client_v2.delete_webhook(webhook_name)
assert isinstance(result, dict)
assert result["message"] == "Webhook deleted successfully"
try:
client_v2.get_webhook_details(webhook_name)
except LLMWhispererClientException as e:
assert e.error_message()["message"] == "Webhook details not found"
assert e.error_message()["status_code"] == 404
def test_whisper_detail(client_v2: LLMWhispererClientV2, data_dir: str) -> None:
"""Test whisper_detail returns extraction metadata after a whisper operation."""
file_path = os.path.join(data_dir, "credit_card.pdf")
whisper_result = client_v2.whisper(
mode="native_text",
output_mode="text",
file_path=file_path,
wait_for_completion=True,
)
whisper_hash = whisper_result["whisper_hash"]
detail = client_v2.whisper_detail(whisper_hash)
assert isinstance(detail, dict)
assert detail["whisper_hash"] == whisper_hash
expected_keys = [
"completed_at",
"mode",
"processed_pages",
"processing_started_at",
"processing_time_in_seconds",
"requested_pages",
"tag",
"total_pages",
"upload_file_size_in_kb",
"whisper_hash",
]
assert set(expected_keys).issubset(
detail.keys()
), f"whisper_detail is missing expected keys: {set(expected_keys) - set(detail.keys())}"
assert detail["mode"] == "native_text"
assert detail["processed_pages"] > 0
assert detail["total_pages"] > 0
def test_whisper_detail_not_found(client_v2: LLMWhispererClientV2) -> None:
"""Test whisper_detail raises exception for a nonexistent whisper_hash."""
with pytest.raises(LLMWhispererClientException) as exc_info:
client_v2.whisper_detail("nonexistent_hash_12345")
error = exc_info.value.error_message()
assert exc_info.value.status_code == 400
assert "message" in error
def assert_error_message(whisper_result: dict) -> None:
assert isinstance(whisper_result, dict)
assert whisper_result["status"] == "error"
assert "error" in whisper_result["message"]
def assert_extracted_text(file_path: str, whisper_result: dict, mode: str, output_mode: str) -> None:
with open(file_path, encoding="utf-8") as f:
exp = f.read()
assert isinstance(whisper_result, dict)
assert whisper_result["status_code"] == 200
# For OCR based processing
threshold = OCR_SIMILARITY_THRESHOLD
# For text based processing
if mode == "native_text" and output_mode == "text":
threshold = 0.99
elif mode == "low_cost":
threshold = OCR_SIMILARITY_THRESHOLD
extracted_text = whisper_result["extraction"]["result_text"]
similarity = SequenceMatcher(None, extracted_text, exp).ratio()
if similarity < threshold:
diff = "\n".join(
unified_diff(
exp.splitlines(),
extracted_text.splitlines(),
fromfile="Expected",
tofile="Extracted",
)
)
pytest.fail(f"Diff:\n{diff}.\n Texts are not similar enough: {similarity * 100:.2f}% similarity. ")
def verify_usage(before_extract: dict, after_extract: dict, page_count: int, mode: str = "form") -> None:
all_modes = ["form", "high_quality", "low_cost", "native_text"]
all_modes.remove(mode)
assert (
after_extract["today_page_count"] == before_extract["today_page_count"] + page_count
), "today_page_count calculation is wrong"
assert (
after_extract["current_page_count"] == before_extract["current_page_count"] + page_count
), "current_page_count calculation is wrong"
if after_extract["overage_page_count"] > 0:
assert (
after_extract["overage_page_count"] == before_extract["overage_page_count"] + page_count
), "overage_page_count calculation is wrong"
assert (
after_extract[f"current_page_count_{mode}"] == before_extract[f"current_page_count_{mode}"] + page_count
), f"{mode} mode calculation is wrong"
for i in range(len(all_modes)):
assert (
after_extract[f"current_page_count_{all_modes[i]}"] == before_extract[f"current_page_count_{all_modes[i]}"]
), f"{all_modes[i]} mode calculation is wrong"