Repository navigation
Expand file tree
/
Copy pathtest_vecdb_errors.py
More file actions
172 lines (125 loc) · 5.43 KB
/
Copy pathtest_vecdb_errors.py
File metadata and controls
172 lines (125 loc) · 5.43 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
##
## Copyright (c) 2026 Oracle and/or its affiliates.
## Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
##
from io import StringIO
import contextlib
import pytest
from oracle_vecdb.vecdb_errors import (
InsecureConnectionError,
InvalidIndexJobNameFormatError,
InvalidHostFormatError,
InvalidLoadJobNameFormatError,
InvalidLoadJobLogError,
InvalidIndexJobLogError,
InvalidModelNameFormatError,
DefaultSettingsParameterMismatchError,
ResourceNotFoundError,
InvalidTableNameFormatError,
VecDBError,
)
def test_vecdb_error_message_helpers():
err = VecDBError("boom")
err.cause = "Cause"
err.action = "Action"
err.excep = RuntimeError("detail")
assert "boom" in err.get_error() # nosec B101
assert "detail" in err.get_error() # nosec B101
assert "Cause" in err.get_error_cause_action() # nosec B101
assert "Action" in err.get_error_cause_action() # nosec B101
assert "detail" in err.get_error_cause_action() # nosec B101
def test_vecdb_error_print_error_includes_stack():
err = VecDBError("boom")
buf = StringIO()
with contextlib.redirect_stderr(buf):
err.print_error(printstack=True)
output = buf.getvalue()
assert "boom" in output # nosec B101
def test_vecdb_error_print_error_uses_stderr_by_default(capsys):
err = VecDBError("boom")
err.print_error(printstack=False, printstdout=False)
captured = capsys.readouterr()
assert "boom" in captured.err # nosec B101
assert captured.out == "" # nosec B101
def test_vecdb_error_print_error_supports_stdout(capsys):
err = VecDBError("boom")
err.print_error(printstack=False, printstdout=True)
captured = capsys.readouterr()
assert "boom" in captured.out # nosec B101
assert captured.err == "" # nosec B101
def test_vecdb_error_print_error_suppresses_nested_exception_text(capsys):
err = VecDBError("boom")
err.excep = RuntimeError("hidden detail")
err.print_error(printstack=False, printstdout=False)
captured = capsys.readouterr()
assert "boom" in captured.err # nosec B101
assert (
"Additional exception context suppressed." in captured.err
) # nosec B101
assert "hidden detail" not in captured.err # nosec B101
assert captured.out == "" # nosec B101
def test_vecdb_error_print_oerr(capsys):
err = VecDBError("boom")
err.cause = "Cause"
err.action = "Action"
err.print_oerr()
captured = capsys.readouterr()
assert "boom" in captured.out # nosec B101
assert "Cause" in captured.out # nosec B101
assert "Action" in captured.out # nosec B101
def test_vecdb_error_print_oerr_without_cause_or_action_avoids_none(capsys):
err = VecDBError("boom")
err.print_oerr()
captured = capsys.readouterr()
assert "boom" in captured.out # nosec B101
assert "None" not in captured.out # nosec B101
def test_invalid_host_format_error_derives_messages():
url = "http://bad"
err = InvalidHostFormatError(url)
assert "VECDB-002" in err.get_error() # nosec B101
assert url not in err.get_error() # nosec B101
assert "Action:" in err.action # nosec B101
def test_insecure_connection_error_advises_https():
url = "http://bad"
err = InsecureConnectionError(url)
assert "VECDB-001" in err.get_error() # nosec B101
assert url not in err.get_error() # nosec B101
assert "HTTPS is required" in err.get_error() # nosec B101
def test_error_messages_fallback_to_english_for_requested_locale():
err = InsecureConnectionError("http://bad", locale="es")
assert err.locale == "es" # nosec B101
assert "Insecure REST URL" in err.get_error() # nosec B101
assert "VECDB-001" in err.get_error() # nosec B101
def test_error_messages_fallback_to_english_for_unknown_locale():
err = InvalidTableNameFormatError("bad table", locale="fr-FR")
assert "Invalid table name" in err.get_error() # nosec B101
assert "VECDB-003" in err.get_error() # nosec B101
@pytest.mark.parametrize(
"error_cls,value,code",
[
(InvalidTableNameFormatError, "bad table", "VECDB-003"),
(InvalidModelNameFormatError, "bad model", "VECDB-004"),
(InvalidLoadJobNameFormatError, "bad load job", "VECDB-005"),
(InvalidIndexJobNameFormatError, "bad index job", "VECDB-006"),
],
)
def test_resource_name_errors_include_codes_causes_and_actions(
error_cls, value, code
):
err = error_cls(value)
message = err.get_error_cause_action()
assert code in message # nosec B101
assert value in message # nosec B101
assert "Cause:" in message # nosec B101
assert "Action:" in message # nosec B101
assert "configured Oracle Database" in message # nosec B101
assert "double-quote" in message # nosec B101
assert "only letters, digits, and underscore" not in message # nosec B101
def test_job_and_default_setting_errors_construct_stable_messages():
resource_error = ResourceNotFoundError("caller-resource-value")
load_log_error = InvalidLoadJobLogError("caller-load-job", "RUNNING")
index_log_error = InvalidIndexJobLogError("caller-index-job", "RUNNING")
DefaultSettingsParameterMismatchError("query", ["missing_parameter"])
assert "caller-resource-value" in resource_error.get_error() # nosec B101
assert "caller-load-job" in load_log_error.get_error() # nosec B101
assert "caller-index-job" in index_log_error.get_error() # nosec B101