-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_buffer_utils.py
More file actions
133 lines (95 loc) · 4.33 KB
/
Copy pathtest_buffer_utils.py
File metadata and controls
133 lines (95 loc) · 4.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
"""Tests for buffer_utils -- targeting 80%+ coverage.
Ported from packages/adapter-shared/src/buffer-utils.test.ts.
"""
from __future__ import annotations
import pytest
from chat_sdk.shared.buffer_utils import buffer_to_data_uri, to_buffer, to_buffer_sync
# ---------------------------------------------------------------------------
# to_buffer (async)
# ---------------------------------------------------------------------------
class TestToBuffer:
async def test_bytes_passthrough(self):
data = b"hello"
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)
async def test_bytearray_conversion(self):
data = bytearray(b"hello")
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)
async def test_memoryview_conversion(self):
data = memoryview(b"hello")
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)
async def test_raises_for_unsupported_type_string(self):
from chat_sdk.shared.errors import ValidationError
with pytest.raises(ValidationError):
await to_buffer("string", "slack")
async def test_raises_for_unsupported_type_int(self):
from chat_sdk.shared.errors import ValidationError
with pytest.raises(ValidationError):
await to_buffer(123, "slack")
async def test_raises_for_unsupported_type_dict(self):
from chat_sdk.shared.errors import ValidationError
with pytest.raises(ValidationError):
await to_buffer({}, "slack")
async def test_raises_for_unsupported_type_none(self):
from chat_sdk.shared.errors import ValidationError
with pytest.raises(ValidationError):
await to_buffer(None, "slack")
async def test_returns_none_when_throw_disabled(self):
result = await to_buffer("string", "teams", throw_on_unsupported=False)
assert result is None
async def test_includes_platform_in_error(self):
from chat_sdk.shared.errors import ValidationError
try:
await to_buffer("invalid", "slack")
except ValidationError as e:
assert e.adapter == "slack"
# ---------------------------------------------------------------------------
# to_buffer_sync
# ---------------------------------------------------------------------------
class TestToBufferSync:
def test_bytes_passthrough(self):
data = b"hello"
result = to_buffer_sync(data, "slack")
assert result == b"hello"
def test_bytearray_conversion(self):
data = bytearray(b"hello")
result = to_buffer_sync(data, "slack")
assert result == b"hello"
def test_memoryview_conversion(self):
data = memoryview(b"hello")
result = to_buffer_sync(data, "slack")
assert result == b"hello"
def test_raises_for_unsupported_type(self):
from chat_sdk.shared.errors import ValidationError
with pytest.raises(ValidationError):
to_buffer_sync("string", "slack")
def test_returns_none_when_throw_disabled(self):
result = to_buffer_sync("string", "teams", throw_on_unsupported=False)
assert result is None
def test_includes_platform_in_error(self):
from chat_sdk.shared.errors import ValidationError
try:
to_buffer_sync("invalid", "teams")
except ValidationError as e:
assert e.adapter == "teams"
# ---------------------------------------------------------------------------
# buffer_to_data_uri
# ---------------------------------------------------------------------------
class TestBufferToDataUri:
def test_default_mime_type(self):
result = buffer_to_data_uri(b"hello")
assert result == "data:application/octet-stream;base64,aGVsbG8="
def test_custom_mime_type(self):
result = buffer_to_data_uri(b"hello", "text/plain")
assert result == "data:text/plain;base64,aGVsbG8="
def test_image_mime_type(self):
result = buffer_to_data_uri(bytes([0x89, 0x50, 0x4E, 0x47]), "image/png")
assert result.startswith("data:image/png;base64,")
def test_empty_buffer(self):
result = buffer_to_data_uri(b"")
assert result == "data:application/octet-stream;base64,"