-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtest_serializer.py
214 lines (144 loc) · 4.83 KB
/
test_serializer.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
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
import json
import threading
from dataclasses import dataclass
from datetime import date, datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Any
from uuid import UUID
import pandas as pd
import pytest
from pydantic import BaseModel
import langfuse.serializer
from langfuse.serializer import (
BaseEventSerializer,
EventSerializer,
)
class TestEnum(Enum):
A = 1
B = 2
@dataclass
class TestDataclass:
field: str
class TestBaseModel(BaseModel):
field: str
def test_datetime():
dt = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
serializer = EventSerializer()
assert serializer.encode(dt) == '"2023-01-01T12:00:00Z"'
def test_date():
d = date(2023, 1, 1)
serializer = EventSerializer()
assert serializer.encode(d) == '"2023-01-01"'
def test_enum():
serializer = EventSerializer()
assert serializer.encode(TestEnum.A) == "1"
def test_uuid():
uuid = UUID("123e4567-e89b-12d3-a456-426614174000")
serializer = EventSerializer()
assert serializer.encode(uuid) == '"123e4567-e89b-12d3-a456-426614174000"'
def test_bytes():
b = b"hello"
serializer = EventSerializer()
assert serializer.encode(b) == '"hello"'
def test_dataclass():
dc = TestDataclass(field="test")
serializer = EventSerializer()
assert json.loads(serializer.encode(dc)) == {"field": "test"}
def test_pydantic_model():
model = TestBaseModel(field="test")
serializer = EventSerializer()
assert json.loads(serializer.encode(model)) == {"field": "test"}
def test_path():
path = Path("/tmp/test.txt")
serializer = EventSerializer()
assert serializer.encode(path) == '"/tmp/test.txt"'
def test_tuple_set_frozenset():
data = (1, 2, 3)
serializer = EventSerializer()
assert serializer.encode(data) == "[1, 2, 3]"
data = {1, 2, 3}
assert serializer.encode(data) == "[1, 2, 3]"
data = frozenset([1, 2, 3])
assert json.loads(serializer.encode(data)) == [1, 2, 3]
def test_dict():
data = {"a": 1, "b": "two"}
serializer = EventSerializer()
assert json.loads(serializer.encode(data)) == data
def test_list():
data = [1, "two", 3.0]
serializer = EventSerializer()
assert json.loads(serializer.encode(data)) == data
def test_nested_structures():
data = {"list": [1, 2, 3], "dict": {"a": 1, "b": 2}, "tuple": (4, 5, 6)}
serializer = EventSerializer()
assert json.loads(serializer.encode(data)) == {
"list": [1, 2, 3],
"dict": {"a": 1, "b": 2},
"tuple": [4, 5, 6],
}
def test_custom_object():
class CustomObject:
def __init__(self):
self.field = "value"
obj = CustomObject()
serializer = EventSerializer()
assert json.loads(serializer.encode(obj)) == {"field": "value"}
def test_circular_reference():
class Node:
def __init__(self):
self.next = None
node1 = Node()
node2 = Node()
node1.next = node2
node2.next = node1
serializer = EventSerializer()
result = json.loads(serializer.encode(node1))
assert result == {"next": {"next": "Node"}}
def test_not_serializable():
class NotSerializable:
def __init__(self):
self.lock = threading.Lock()
def __repr__(self):
raise Exception("Cannot represent")
obj = NotSerializable()
serializer = EventSerializer()
assert serializer.encode(obj) == '{"lock": "<lock>"}'
def test_exception():
ex = ValueError("Test exception")
serializer = EventSerializer()
assert serializer.encode(ex) == '"ValueError: Test exception"'
def test_none():
serializer = EventSerializer()
assert serializer.encode(None) == "null"
def test_none_without_langchain(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(langfuse.serializer, "Serializable", type(None), raising=True)
serializer = EventSerializer()
assert serializer.encode(None) == "null"
def test_slots():
class SlotClass:
__slots__ = ["field"]
def __init__(self):
self.field = "value"
obj = SlotClass()
serializer = EventSerializer()
assert json.loads(serializer.encode(obj)) == {"field": "value"}
def test_numpy_float32():
import numpy as np
data = np.float32(1.0)
serializer = EventSerializer()
assert serializer.encode(data) == "1.0"
def test_custom_serializer():
class CustomSerializer(BaseEventSerializer):
def default(self, obj: Any) -> Any:
if isinstance(obj, pd.DataFrame):
return obj.to_dict(orient="records")
return super().default(obj)
df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
serializer = CustomSerializer()
result = json.loads(serializer.encode(df))
assert result == [
{"col1": 1, "col2": "a"},
{"col1": 2, "col2": "b"},
{"col1": 3, "col2": "c"},
]