forked from terminusdb/terminusdb-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
403 lines (356 loc) · 13.2 KB
/
conftest.py
File metadata and controls
403 lines (356 loc) · 13.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import os
import subprocess
import time
import pytest
import requests
MAX_CONTAINER_STARTUP_TIME = 120 # Increased from 30 to 120 seconds for slower systems
# Check if a local TerminusDB test server is already running
def is_local_server_running():
"""Check if local TerminusDB server is running at http://127.0.0.1:6363"""
try:
response = requests.get("http://127.0.0.1:6363", timeout=2)
# Server responds with 404 for root path, which means it's running
return response.status_code in [200, 404]
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
return False
def is_docker_server_running():
"""Check if Docker TerminusDB server is already running at http://127.0.0.1:6366"""
try:
response = requests.get("http://127.0.0.1:6366", timeout=2)
# Server responds with 404 for root path, which means it's running
return response.status_code in [200, 404]
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
return False
def is_jwt_server_running():
"""Check if JWT Docker TerminusDB server is already running at http://127.0.0.1:6367"""
try:
response = requests.get("http://127.0.0.1:6367", timeout=2)
# Server responds with 404 for root path, which means it's running
return response.status_code in [200, 404]
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
return False
def is_docker_installed():
try:
output = subprocess.run(["docker", "--version"], stderr=subprocess.PIPE)
except FileNotFoundError:
return False
return output.returncode == 0
pytestmark = pytest.mark.skipif(
not is_docker_installed(), reason="docker not installed"
)
@pytest.fixture(scope="module")
def test_csv():
with open(os.path.dirname(os.path.abspath(__file__)) + "/test_csv.csv") as reader:
csv = reader.read()
return csv
@pytest.fixture(scope="module")
def terminusx_token():
os.environ["TERMINUSDB_ACCESS_TOKEN"] = os.environ.get("TERMINUSX_TOKEN")
return os.environ["TERMINUSDB_ACCESS_TOKEN"]
@pytest.fixture(scope="module")
def docker_url_jwt(pytestconfig):
# we are using subprocess in case we need to access some of the outputs
# most likely
jwt_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InRlc3RrZXkifQ.eyJodHRwOi8vdGVybWludXNkYi5jb20vc2NoZW1hL3N5c3RlbSNhZ2VudF9uYW1lIjoiYWRtaW4iLCJodHRwOi8vdGVybWludXNkYi5jb20vc2NoZW1hL3N5c3RlbSN1c2VyX2lkZW50aWZpZXIiOiJhZG1pbkB1c2VyLmNvbSIsImlzcyI6Imh0dHBzOi8vdGVybWludXNodWIuZXUuYXV0aDAuY29tLyIsInN1YiI6ImFkbWluIiwiYXVkIjpbImh0dHBzOi8vdGVybWludXNodWIvcmVnaXN0ZXJVc2VyIiwiaHR0cHM6Ly90ZXJtaW51c2h1Yi5ldS5hdXRoMC5jb20vdXNlcmluZm8iXSwiaWF0IjoxNTkzNzY5MTgzLCJhenAiOiJNSkpuZEdwMHpVZE03bzNQT1RRUG1SSkltWTJobzBhaSIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwifQ.Ru03Bi6vSIQ57bC41n6fClSdxlb61m0xX6Q34Yh91gql0_CyfYRWTuqzqPMFoCefe53hPC5E-eoSFdID_u6w1ih_pH-lTTqus9OWgi07Qou3QNs8UZBLiM4pgLqcBKs0N058jfg4y6h9GjIBGVhX9Ni2ez3JGNcz1_U45BhnreE"
# Check if JWT server is already running (port 6367)
if is_jwt_server_running():
print("\n✓ Using existing JWT Docker TerminusDB server at http://127.0.0.1:6367")
yield ("http://127.0.0.1:6367", jwt_token)
return # Don't clean up - server was already running
pytestconfig.getoption("docker_compose")
output = subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__))
+ "/test-docker-compose-jwt.yml",
"up",
"-d",
],
stderr=subprocess.PIPE,
)
if output.returncode != 0:
raise RuntimeError(output.stderr)
test_url = "http://127.0.0.1:6367"
is_server_started = False
seconds_waited = 0
while not is_server_started:
service = subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__))
+ "/test-docker-compose-jwt.yml",
"ps",
"--services",
"--filter",
"status=running",
],
stdout=subprocess.PIPE,
check=True,
)
if service.stdout == b"terminusdb-server\n":
try:
response = requests.get(test_url)
# Server responds with 404 for root path, which means it's running
assert response.status_code in [200, 404]
break
except (requests.exceptions.ConnectionError, AssertionError):
pass
seconds_waited += 1
time.sleep(1)
if seconds_waited > MAX_CONTAINER_STARTUP_TIME:
clean_up_container()
raise RuntimeError(f"JWT Container was too slow to startup (waited {MAX_CONTAINER_STARTUP_TIME}s)")
yield (test_url, jwt_token)
clean_up_container()
@pytest.fixture(scope="module")
def docker_url(pytestconfig):
"""
Provides a TerminusDB server URL for integration tests.
Prefers local test server if running, otherwise starts Docker container.
NOTE: This fixture returns just the URL. Tests expect AUTOLOGIN mode (no authentication).
If using local server with authentication, use TERMINUSDB_AUTOLOGIN=true when starting it.
"""
# Check if local test server is already running (port 6363)
if is_local_server_running():
print("\n✓ Using existing local TerminusDB test server at http://127.0.0.1:6363")
print("⚠️ WARNING: Local server should be started with TERMINUSDB_AUTOLOGIN=true")
print(" Or use: TERMINUSDB_SERVER_AUTOLOGIN=true ./tests/terminusdb-test-server.sh restart")
yield "http://127.0.0.1:6363"
return # Don't clean up - server was already running
# Check if Docker container is already running (port 6366)
if is_docker_server_running():
print("\n✓ Using existing Docker TerminusDB server at http://127.0.0.1:6366")
yield "http://127.0.0.1:6366"
return # Don't clean up - server was already running
# No server found, start Docker container
print("\n⚠ No server found, starting Docker container with AUTOLOGIN...")
pytestconfig.getoption("docker_compose")
output = subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__)) + "/test-docker-compose.yml",
"up",
"-d",
],
stderr=subprocess.PIPE,
)
if output.returncode != 0:
raise RuntimeError(output.stderr)
test_url = "http://127.0.0.1:6366"
is_server_started = False
seconds_waited = 0
while not is_server_started:
service = subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__))
+ "/test-docker-compose.yml",
"ps",
"--services",
"--filter",
"status=running",
],
stdout=subprocess.PIPE,
check=True,
)
if service.stdout == b"terminusdb-server\n":
try:
response = requests.get(test_url)
# Server responds with 404 for root path, which means it's running
assert response.status_code in [200, 404]
print(f"✓ Docker container started successfully after {seconds_waited}s")
break
except (requests.exceptions.ConnectionError, AssertionError):
pass
seconds_waited += 1
time.sleep(1)
if seconds_waited > MAX_CONTAINER_STARTUP_TIME:
clean_up_container()
raise RuntimeError(f"Container was too slow to startup (waited {MAX_CONTAINER_STARTUP_TIME}s)")
yield test_url
clean_up_container()
def clean_up_container():
subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__)) + "/test-docker-compose.yml",
"down",
],
check=True,
)
subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__)) + "/test-docker-compose.yml",
"rm",
"--force",
"--stop",
"-v",
],
check=True,
)
subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__))
+ "/test-docker-compose-jwt.yml",
"down",
],
check=True,
)
subprocess.run(
[
"docker",
"compose",
"--file",
os.path.dirname(os.path.realpath(__file__))
+ "/test-docker-compose-jwt.yml",
"rm",
"--force",
"--stop",
"-v",
],
check=True,
)
subprocess.run(["docker", "compose", "down"])
subprocess.run(["docker", "compose", "rm", "--force", "--stop", "-v"])
@pytest.fixture(scope="module")
def one_class_obj():
return {
"@type": "api:WoqlResponse",
"api:status": "api:success",
"api:variable_names": [
"Class ID",
"Class Name",
"Description",
"Parents",
"Children",
"Abstract",
],
"bindings": [
{
"Abstract": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "No",
},
"Children": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "",
},
"Class ID": "terminusdb:///schema#Journey",
"Class Name": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "Bike Journey",
},
"Description": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "Bike Journey object that capture each bike joourney.",
},
"Parents": ["http://terminusdb.com/schema/system#Document"],
}
],
"deletes": 0,
"inserts": 0,
"transaction_retry_count": 0,
}
@pytest.fixture(scope="module")
def one_class_prop():
return {
"@type": "api:WoqlResponse",
"api:status": "api:success",
"api:variable_names": [
"Property ID",
"Property Name",
"Property Domain",
"Property Type",
"Property Range",
"Property Description",
],
"bindings": [
{
"Property Description": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "",
},
"Property Domain": "terminusdb:///schema#Journey",
"Property ID": "terminusdb:///schema#Duration",
"Property Name": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "",
},
"Property Range": "http://www.w3.org/2001/XMLSchema#integer",
"Property Type": {
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@value": "Data",
},
}
],
"deletes": 0,
"inserts": 0,
"transaction_retry_count": 0,
}
@pytest.fixture(scope="module")
def one_object():
return {
"@type": "api:WoqlResponse",
"api:status": "api:success",
"api:variable_names": ["Object ID", "Object Type"],
"bindings": [
{
"Object ID": "terminusdb:///data/Journey_myobj",
"Object Type": "terminusdb:///schema#Journey",
}
],
"deletes": 0,
"inserts": 0,
"transaction_retry_count": 0,
}
@pytest.fixture(scope="module")
def one_prop_val():
return {
"@type": "api:WoqlResponse",
"api:status": "api:success",
"api:variable_names": [
"Object ID",
"Property ID",
"Property Value",
"Value ID",
"Value Class",
],
"bindings": [
{
"Object ID": "terminusdb:///data/Journey_myobj",
"Property ID": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"Property Value": "terminusdb:///schema#Journey",
"Value Class": "system:unknown",
"Value ID": "system:unknown",
},
{
"Object ID": "terminusdb:///data/Journey_myobj",
"Property ID": "terminusdb:///schema#Duration",
"Property Value": {
"@type": "http://www.w3.org/2001/XMLSchema#integer",
"@value": 75,
},
"Value Class": "system:unknown",
"Value ID": "system:unknown",
},
],
"deletes": 0,
"inserts": 0,
"transaction_retry_count": 0,
}