-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_arcadedb.py
More file actions
280 lines (225 loc) · 10.3 KB
/
Copy pathtest_arcadedb.py
File metadata and controls
280 lines (225 loc) · 10.3 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#
# Copyright © 2021-present Arcade Data Ltd ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import random
import string
import time
from time import sleep
import psycopg
import pytest
import requests
from pytest_check import check
from testcontainers.core.container import DockerContainer
arcadedb = (DockerContainer("arcadedata/arcadedb:latest")
.with_exposed_ports(2480, 2424, 5432)
.with_env("JAVA_OPTS",
"-Darcadedb.server.rootPassword=playwithdata "
"-Darcadedb.server.defaultDatabases=beer[root]{import:https://github.com/ArcadeData/arcadedb-datasets/raw/main/orientdb/OpenBeer.gz} "
"-Darcadedb.server.plugins=PostgresProtocolPlugin,GremlinServerPlugin,PrometheusMetricsPlugin"))
def get_connection_params(container):
host = container.get_container_host_ip()
port = container.get_exposed_port(5432)
return {
"host": host,
"port": port,
"user": "root",
"password": "playwithdata",
"dbname": "beer",
"sslmode": "disable"
}
def wait_for_http_endpoint(container, path, port, expected_status, timeout=60):
"""
Wait for an HTTP endpoint to return the expected status code.
Args:
path: The HTTP path to check
port: The port to connect to
expected_status: The expected HTTP status code
timeout: Maximum time to wait in seconds
"""
host = container.get_container_host_ip()
url = f"http://{host}:{container.get_exposed_port(port)}{path}"
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = requests.get(url, timeout=2)
if response.status_code == expected_status:
return True
except (requests.exceptions.ConnectionError,
requests.exceptions.Timeout) as e:
pass
sleep(1)
raise TimeoutError(f"Container didn't respond with status {expected_status} at {url} within {timeout} seconds")
@pytest.fixture(scope="module", autouse=True)
def setup(request):
arcadedb.start()
# wait_for_logs(arcadedb, "started", 60)
wait_for_http_endpoint(arcadedb, "/api/v1/ready", 2480, 204, 10)
def test_psycopg2_basic_queries():
"""Test basic connectivity and queries using psycopg2."""
# Get connection parameters
params = get_connection_params(arcadedb)
# Connect to the database
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
# Query existing data from Beer dataset
cursor.execute("SELECT FROM Beer LIMIT 10")
beers = cursor.fetchall()
assert len(beers) == 10
# Create a new document type
cursor.execute("CREATE DOCUMENT TYPE TestProduct")
cursor.execute("CREATE PROPERTY TestProduct.price INTEGER")
# Insert data
cursor.execute("INSERT INTO TestProduct (name, price) VALUES ('TestItem', 29)")
# Query the inserted data
cursor.execute("SELECT * FROM TestProduct")
products = cursor.fetchall()
assert len(products) == 1
# Check if data was correctly inserted
# The first column is typically the @rid, so we check the subsequent columns
product = products[0]
print(f"Product: {product}")
assert 'TestItem' in product
# price is INTEGER on the wire, so psycopg returns it as a native int
assert 29 in product # nosec B101
finally:
conn.close()
def test_psycopg2_return_array_floats():
"""Check if the driver correctly sends the array of floats as a list of floats to the python driver"""
# Get connection parameters
params = get_connection_params(arcadedb)
# Connect to the database
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
cursor.execute("create vertex type `TEXT_EMBEDDING` if not exists;")
cursor.execute("create property TEXT_EMBEDDING.str if not exists STRING;")
cursor.execute("create property TEXT_EMBEDDING.embedding if not exists ARRAY_OF_FLOATS;")
cursor.execute('INSERT INTO `TEXT_EMBEDDING` SET str = "meow", embedding = [0.1,0.2,0.3] RETURN embedding')
embeddings = cursor.fetchone()[0]
assert isinstance(embeddings, list) and all(isinstance(item, float) for item in
embeddings), f"Type ARRAY_OF_FLOATS is returned as {type(embeddings)} instead of list of floats"
finally:
conn.close()
def random_values(_type, size=64):
if _type == bool: # Note: fixed the '=' to '==' for comparison
return [random.choice([True, False]) for _ in range(size)]
elif _type == float:
return [random.uniform(-100, 100) for _ in range(size)]
elif _type == int:
return [random.randint(-100, 100) for _ in range(size)]
elif _type == str:
# Generate random strings of length between 5 and 15
return [''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(5, 15)))
for _ in range(size)]
else:
raise ValueError(f"Unsupported type: {_type}")
def test_psycopg2_return_array_common():
"""Check if the driver correctly sends the array of floats as a list of floats to the python driver"""
# Get connection parameters
params = get_connection_params(arcadedb)
# Connect to the database
conn = psycopg.connect(**params)
conn.autocommit = True
types_to_test = [bool, float, int, str]
try:
for type_to_test in types_to_test:
# Extract just the type name from the class representation
type_name = type_to_test.__name__
arcade_name = f"TEXT_{type_name}"
with conn.cursor() as cursor:
cursor.execute(f"create vertex type `{arcade_name}` if not exists;")
cursor.execute(f"create property {arcade_name}.str if not exists STRING;")
cursor.execute(f"create property {arcade_name}.data if not exists LIST;")
cursor.execute(
f'INSERT INTO `{arcade_name}` SET str = "meow", data = {json.dumps(random_values(type_to_test))} RETURN data')
datas = cursor.fetchone()[0]
# Use pytest-check to continue even after assertion failures
with check:
assert isinstance(datas, list), f"For {type_name}: Type LIST is returned as {type(datas)} instead of list"
if isinstance(datas, list):
with check:
assert all(isinstance(item, type_to_test) for item in
datas), f"For {type_name}: Not all items are of type {type_name}"
finally:
conn.close()
def test_psycopg2_with_named_parameterized_query():
"""Check if the driver correctly handles parameterized named queries"""
params = get_connection_params(arcadedb)
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
query_params = {'name': 'Stout', 'brewery_id': 350}
cursor.execute('SELECT * FROM Beer WHERE name = %(name)s AND brewery_id = %(brewery_id)s', query_params)
beer = cursor.fetchall()[0]
assert 'Stout' in beer
finally:
conn.close()
def test_psycopg2_with_named_parameterized_cypher_query():
"""Check if the driver correctly handles parameterized named queries"""
params = get_connection_params(arcadedb)
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
query_params = {'name': 'Stout', 'brewery_id': 350}
cursor.execute('{opencypher} MATCH (b:Beer) WHERE b.name =%(name)s AND b.brewery_id = %(brewery_id)s RETURN b', query_params)
beer = cursor.fetchall()[0]
assert 'Stout' in beer
finally:
conn.close()
def test_psycopg2_with_positional_parameterized_query():
"""Check if the driver correctly handles parameterized positional queries"""
params = get_connection_params(arcadedb)
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
cursor.execute('SELECT * FROM Beer WHERE name = %s AND brewery_id = %s', ("Stout", 350))
beer = cursor.fetchall()[0]
assert 'Stout' in beer
finally:
conn.close()
def test_psycopg2_cypher_with_array_parameter_in_clause():
"""Test Cypher query with array parameter using IN clause - reproduces the ClassCastException issue"""
params = get_connection_params(arcadedb)
conn = psycopg.connect(**params)
conn.autocommit = True
try:
with conn.cursor() as cursor:
# Create test vertices first
cursor.execute('{opencypher} CREATE (n:CHUNK {text: "chunk1"}) RETURN ID(n)')
rid1 = cursor.fetchone()[0]
cursor.execute('{opencypher} CREATE (n:CHUNK {text: "chunk2"}) RETURN ID(n)')
rid2 = cursor.fetchone()[0]
cursor.execute('{opencypher} CREATE (n:CHUNK {text: "chunk3"}) RETURN ID(n)')
rid3 = cursor.fetchone()[0]
# Now query with IN clause using array parameter
rids_list = [rid1, rid2, rid3]
query_params = {'ids': rids_list}
cursor.execute('{opencypher} MATCH (n:CHUNK) WHERE ID(n) IN %(ids)s RETURN n.text as text, ID(n) as id', query_params)
results = cursor.fetchall()
assert len(results) == 3
# Verify we got all three chunks
texts = [r[0] for r in results]
assert 'chunk1' in texts
assert 'chunk2' in texts
assert 'chunk3' in texts
finally:
conn.close()