-
Notifications
You must be signed in to change notification settings - Fork 1k
/
test_sql_alchemy.py
218 lines (177 loc) · 6.79 KB
/
test_sql_alchemy.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
215
216
217
218
import uuid
from pathlib import Path
import pytest
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
from chainlit import User
from chainlit.data.sql_alchemy import SQLAlchemyDataLayer
from chainlit.data.storage_clients.base import BaseStorageClient
from chainlit.element import Text
@pytest.fixture
async def data_layer(mock_storage_client: BaseStorageClient, tmp_path: Path):
db_file = tmp_path / "test_db.sqlite"
conninfo = f"sqlite+aiosqlite:///{db_file}"
# Create async engine
engine = create_async_engine(conninfo)
# Execute initialization statements
# Ref: https://docs.chainlit.io/data-persistence/custom#sql-alchemy-data-layer
async with engine.begin() as conn:
await conn.execute(
text(
"""
CREATE TABLE users (
"id" UUID PRIMARY KEY,
"identifier" TEXT NOT NULL UNIQUE,
"metadata" JSONB NOT NULL,
"createdAt" TEXT
);
"""
)
)
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS threads (
"id" UUID PRIMARY KEY,
"createdAt" TEXT,
"name" TEXT,
"userId" UUID,
"userIdentifier" TEXT,
"tags" TEXT[],
"metadata" JSONB,
FOREIGN KEY ("userId") REFERENCES users("id") ON DELETE CASCADE
);
"""
)
)
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS steps (
"id" UUID PRIMARY KEY,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"threadId" UUID NOT NULL,
"parentId" UUID,
"disableFeedback" BOOLEAN NOT NULL,
"streaming" BOOLEAN NOT NULL,
"waitForAnswer" BOOLEAN,
"isError" BOOLEAN,
"metadata" JSONB,
"tags" TEXT[],
"input" TEXT,
"output" TEXT,
"createdAt" TEXT,
"start" TEXT,
"end" TEXT,
"generation" JSONB,
"showInput" TEXT,
"language" TEXT,
"indent" INT
);
"""
)
)
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS elements (
"id" UUID PRIMARY KEY,
"threadId" UUID,
"type" TEXT,
"url" TEXT,
"chainlitKey" TEXT,
"name" TEXT NOT NULL,
"display" TEXT,
"objectKey" TEXT,
"size" TEXT,
"page" INT,
"language" TEXT,
"forId" UUID,
"mime" TEXT
);
"""
)
)
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS feedbacks (
"id" UUID PRIMARY KEY,
"forId" UUID NOT NULL,
"threadId" UUID NOT NULL,
"value" INT NOT NULL,
"comment" TEXT
);
"""
)
)
# Create SQLAlchemyDataLayer instance
data_layer = SQLAlchemyDataLayer(conninfo, storage_provider=mock_storage_client)
return data_layer
async def test_create_and_get_element(
mock_chainlit_context, data_layer: SQLAlchemyDataLayer
):
async with mock_chainlit_context:
text_element = Text(
id=str(uuid.uuid4()),
name="test.txt",
mime="text/plain",
content="test content",
for_id="test_step_id",
)
# Needs context because of wrapper in utils.py
await data_layer.create_element(text_element)
retrieved_element = await data_layer.get_element(
text_element.thread_id, text_element.id
)
assert retrieved_element is not None
assert retrieved_element["id"] == text_element.id
assert retrieved_element["name"] == text_element.name
assert retrieved_element["mime"] == text_element.mime
# The 'content' field is not part of the ElementDict, so we remove this assertion
async def test_get_current_timestamp(data_layer: SQLAlchemyDataLayer):
timestamp = await data_layer.get_current_timestamp()
assert isinstance(timestamp, str)
async def test_get_user(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
fetched_user = await data_layer.get_user(persisted_user.identifier)
assert fetched_user
assert fetched_user.createdAt == persisted_user.createdAt
assert fetched_user.id == persisted_user.id
nonexistent_user = await data_layer.get_user("nonexistent")
assert nonexistent_user is None
async def test_create_user(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
assert persisted_user.identifier == test_user.identifier
assert persisted_user.createdAt
assert persisted_user.id
# Assert id is valid uuid
assert uuid.UUID(persisted_user.id)
async def test_update_thread(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
await data_layer.update_thread("test_thread")
async def test_get_thread_author(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
await data_layer.update_thread("test_thread", user_id=persisted_user.id)
author = await data_layer.get_thread_author("test_thread")
assert author == persisted_user.identifier
async def test_get_thread(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
await data_layer.update_thread("test_thread")
result = await data_layer.get_thread("test_thread")
assert result is not None
result = await data_layer.get_thread("nonexisting_thread")
assert result is None
async def test_delete_thread(test_user: User, data_layer: SQLAlchemyDataLayer):
persisted_user = await data_layer.create_user(test_user)
assert persisted_user
await data_layer.update_thread("test_thread", "test_user")
await data_layer.delete_thread("test_thread")
thread = await data_layer.get_thread("test_thread")
assert thread is None