-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_errors.py
109 lines (87 loc) · 3.26 KB
/
test_errors.py
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
from typing import Type
import pytest
from supabase_functions.errors import (
FunctionsApiErrorDict,
FunctionsError,
FunctionsHttpError,
FunctionsRelayError,
)
@pytest.mark.parametrize(
"error_class,expected_name,expected_status",
[
(FunctionsError, "test_error", 500),
(FunctionsHttpError, "FunctionsHttpError", 400),
(FunctionsRelayError, "FunctionsRelayError", 400),
],
)
def test_error_initialization(
error_class: Type[FunctionsError], expected_name: str, expected_status: int
):
test_message = "Test error message"
if error_class is FunctionsError:
error = error_class(test_message, expected_name, expected_status)
else:
error = error_class(test_message)
assert str(error) == test_message
assert error.message == test_message
assert error.name == expected_name
assert error.status == expected_status
assert isinstance(error, Exception)
@pytest.mark.parametrize(
"error_class,expected_name,expected_status",
[
(FunctionsError, "test_error", 500),
(FunctionsHttpError, "FunctionsHttpError", 400),
(FunctionsRelayError, "FunctionsRelayError", 400),
],
)
def test_error_to_dict(
error_class: Type[FunctionsError], expected_name: str, expected_status: int
):
test_message = "Test error message"
if error_class is FunctionsError:
error = error_class(test_message, expected_name, expected_status)
else:
error = error_class(test_message)
error_dict = error.to_dict()
assert isinstance(error_dict, dict)
assert error_dict["message"] == test_message
assert error_dict["name"] == expected_name
assert error_dict["status"] == expected_status
# Verify the dict matches the TypedDict structure
typed_dict: FunctionsApiErrorDict = error_dict
assert isinstance(typed_dict["name"], str)
assert isinstance(typed_dict["message"], str)
assert isinstance(typed_dict["status"], int)
def test_functions_error_inheritance():
# Test that all error classes inherit from FunctionsError
assert issubclass(FunctionsHttpError, FunctionsError)
assert issubclass(FunctionsRelayError, FunctionsError)
def test_error_as_exception():
# Test that errors can be raised and caught
test_message = "Test exception"
# Test base error
with pytest.raises(FunctionsError) as exc_info:
raise FunctionsError(test_message, "test_error", 500)
assert str(exc_info.value) == test_message
# Test HTTP error
with pytest.raises(FunctionsHttpError) as exc_info:
raise FunctionsHttpError(test_message)
assert str(exc_info.value) == test_message
# Test Relay error
with pytest.raises(FunctionsRelayError) as exc_info:
raise FunctionsRelayError(test_message)
assert str(exc_info.value) == test_message
def test_error_message_types():
# Test that errors handle different message types appropriately
test_cases = [
"Simple string",
"String with unicode: 你好",
"String with special chars: !@#$%^&*()",
"", # Empty string
"A" * 1000, # Long string
]
for message in test_cases:
error = FunctionsError(message, "test", 500)
assert error.message == message
assert error.to_dict()["message"] == message