1
1
import datajoint as dj
2
2
from packaging import version
3
- from typing import Dict
3
+ from typing import Dict , List
4
4
import os
5
5
from os import environ , remove
6
6
import minio
18
18
DataJointError ,
19
19
)
20
20
from . import (
21
- PREFIX ,
22
- CONN_INFO ,
23
- S3_CONN_INFO ,
24
21
schema ,
25
22
schema_simple ,
26
23
schema_advanced ,
30
27
)
31
28
32
29
30
+ @pytest .fixture (scope = "session" )
31
+ def prefix ():
32
+ return os .environ .get ("DJ_TEST_DB_PREFIX" , "djtest" )
33
+
34
+
33
35
@pytest .fixture (scope = "session" )
34
36
def monkeysession ():
35
37
with pytest .MonkeyPatch .context () as mp :
@@ -81,7 +83,7 @@ def connection_root_bare(db_creds_root):
81
83
82
84
83
85
@pytest .fixture (scope = "session" )
84
- def connection_root (connection_root_bare ):
86
+ def connection_root (connection_root_bare , prefix ):
85
87
"""Root user database connection."""
86
88
dj .config ["safemode" ] = False
87
89
conn_root = connection_root_bare
@@ -136,7 +138,7 @@ def connection_root(connection_root_bare):
136
138
137
139
# Teardown
138
140
conn_root .query ("SET FOREIGN_KEY_CHECKS=0" )
139
- cur = conn_root .query ('SHOW DATABASES LIKE "{}\\ _%%"' .format (PREFIX ))
141
+ cur = conn_root .query ('SHOW DATABASES LIKE "{}\\ _%%"' .format (prefix ))
140
142
for db in cur .fetchall ():
141
143
conn_root .query ("DROP DATABASE `{}`" .format (db [0 ]))
142
144
conn_root .query ("SET FOREIGN_KEY_CHECKS=1" )
@@ -151,9 +153,9 @@ def connection_root(connection_root_bare):
151
153
152
154
153
155
@pytest .fixture (scope = "session" )
154
- def connection_test (connection_root , db_creds_test ):
156
+ def connection_test (connection_root , prefix , db_creds_test ):
155
157
"""Test user database connection."""
156
- database = f"{ PREFIX } %%"
158
+ database = f"{ prefix } %%"
157
159
permission = "ALL PRIVILEGES"
158
160
159
161
# Create MySQL users
@@ -191,7 +193,17 @@ def connection_test(connection_root, db_creds_test):
191
193
192
194
193
195
@pytest .fixture (scope = "session" )
194
- def stores_config (tmpdir_factory ):
196
+ def s3_creds () -> Dict :
197
+ return dict (
198
+ endpoint = os .environ .get ("S3_ENDPOINT" , "fakeservices.datajoint.io" ),
199
+ access_key = os .environ .get ("S3_ACCESS_KEY" , "datajoint" ),
200
+ secret_key = os .environ .get ("S3_SECRET_KEY" , "datajoint" ),
201
+ bucket = os .environ .get ("S3_BUCKET" , "datajoint.test" ),
202
+ )
203
+
204
+
205
+ @pytest .fixture (scope = "session" )
206
+ def stores_config (s3_creds , tmpdir_factory ):
195
207
stores_config = {
196
208
"raw" : dict (protocol = "file" , location = tmpdir_factory .mktemp ("raw" )),
197
209
"repo" : dict (
@@ -200,7 +212,7 @@ def stores_config(tmpdir_factory):
200
212
location = tmpdir_factory .mktemp ("repo" ),
201
213
),
202
214
"repo-s3" : dict (
203
- S3_CONN_INFO ,
215
+ s3_creds ,
204
216
protocol = "s3" ,
205
217
location = "dj/repo" ,
206
218
stage = tmpdir_factory .mktemp ("repo-s3" ),
@@ -209,7 +221,7 @@ def stores_config(tmpdir_factory):
209
221
protocol = "file" , location = tmpdir_factory .mktemp ("local" ), subfolding = (1 , 1 )
210
222
),
211
223
"share" : dict (
212
- S3_CONN_INFO , protocol = "s3" , location = "dj/store/repo" , subfolding = (2 , 4 )
224
+ s3_creds , protocol = "s3" , location = "dj/store/repo" , subfolding = (2 , 4 )
213
225
),
214
226
}
215
227
return stores_config
@@ -238,9 +250,9 @@ def mock_cache(tmpdir_factory):
238
250
239
251
240
252
@pytest .fixture
241
- def schema_any (connection_test ):
253
+ def schema_any (connection_test , prefix ):
242
254
schema_any = dj .Schema (
243
- PREFIX + "_test1" , schema .LOCALS_ANY , connection = connection_test
255
+ prefix + "_test1" , schema .LOCALS_ANY , connection = connection_test
244
256
)
245
257
assert schema .LOCALS_ANY , "LOCALS_ANY is empty"
246
258
try :
@@ -292,9 +304,9 @@ def schema_any(connection_test):
292
304
293
305
294
306
@pytest .fixture
295
- def schema_simp (connection_test ):
307
+ def schema_simp (connection_test , prefix ):
296
308
schema = dj .Schema (
297
- PREFIX + "_relational" , schema_simple .LOCALS_SIMPLE , connection = connection_test
309
+ prefix + "_relational" , schema_simple .LOCALS_SIMPLE , connection = connection_test
298
310
)
299
311
schema (schema_simple .IJ )
300
312
schema (schema_simple .JI )
@@ -319,9 +331,9 @@ def schema_simp(connection_test):
319
331
320
332
321
333
@pytest .fixture
322
- def schema_adv (connection_test ):
334
+ def schema_adv (connection_test , prefix ):
323
335
schema = dj .Schema (
324
- PREFIX + "_advanced" ,
336
+ prefix + "_advanced" ,
325
337
schema_advanced .LOCALS_ADVANCED ,
326
338
connection = connection_test ,
327
339
)
@@ -339,9 +351,11 @@ def schema_adv(connection_test):
339
351
340
352
341
353
@pytest .fixture
342
- def schema_ext (connection_test , enable_filepath_feature , mock_stores , mock_cache ):
354
+ def schema_ext (
355
+ connection_test , enable_filepath_feature , mock_stores , mock_cache , prefix
356
+ ):
343
357
schema = dj .Schema (
344
- PREFIX + "_extern" ,
358
+ prefix + "_extern" ,
345
359
context = schema_external .LOCALS_EXTERNAL ,
346
360
connection = connection_test ,
347
361
)
@@ -358,9 +372,9 @@ def schema_ext(connection_test, enable_filepath_feature, mock_stores, mock_cache
358
372
359
373
360
374
@pytest .fixture
361
- def schema_uuid (connection_test ):
375
+ def schema_uuid (connection_test , prefix ):
362
376
schema = dj .Schema (
363
- PREFIX + "_test1" ,
377
+ prefix + "_test1" ,
364
378
context = schema_uuid_module .LOCALS_UUID ,
365
379
connection = connection_test ,
366
380
)
@@ -386,37 +400,110 @@ def http_client():
386
400
387
401
388
402
@pytest .fixture (scope = "session" )
389
- def minio_client_bare (http_client ):
403
+ def minio_client_bare (s3_creds , http_client ):
390
404
"""Initialize MinIO with an endpoint and access/secret keys."""
391
405
client = minio .Minio (
392
- S3_CONN_INFO ["endpoint" ],
393
- access_key = S3_CONN_INFO ["access_key" ],
394
- secret_key = S3_CONN_INFO ["secret_key" ],
406
+ s3_creds ["endpoint" ],
407
+ access_key = s3_creds ["access_key" ],
408
+ secret_key = s3_creds ["secret_key" ],
395
409
secure = True ,
396
410
http_client = http_client ,
397
411
)
398
412
return client
399
413
400
414
401
415
@pytest .fixture (scope = "session" )
402
- def minio_client (minio_client_bare ):
416
+ def minio_client (s3_creds , minio_client_bare ):
403
417
"""Initialize a MinIO client and create buckets for testing session."""
404
418
# Setup MinIO bucket
405
419
aws_region = "us-east-1"
406
420
try :
407
- minio_client_bare .make_bucket (S3_CONN_INFO ["bucket" ], location = aws_region )
421
+ minio_client_bare .make_bucket (s3_creds ["bucket" ], location = aws_region )
408
422
except minio .error .S3Error as e :
409
423
if e .code != "BucketAlreadyOwnedByYou" :
410
424
raise e
411
425
412
426
yield minio_client_bare
413
427
414
428
# Teardown S3
415
- objs = list (minio_client_bare .list_objects (S3_CONN_INFO ["bucket" ], recursive = True ))
429
+ objs = list (minio_client_bare .list_objects (s3_creds ["bucket" ], recursive = True ))
416
430
objs = [
417
431
minio_client_bare .remove_object (
418
- S3_CONN_INFO ["bucket" ], o .object_name .encode ("utf-8" )
432
+ s3_creds ["bucket" ], o .object_name .encode ("utf-8" )
419
433
)
420
434
for o in objs
421
435
]
422
- minio_client_bare .remove_bucket (S3_CONN_INFO ["bucket" ])
436
+ minio_client_bare .remove_bucket (s3_creds ["bucket" ])
437
+
438
+
439
+ @pytest .fixture
440
+ def test (schema_any ):
441
+ yield schema .TTest ()
442
+
443
+
444
+ @pytest .fixture
445
+ def test2 (schema_any ):
446
+ yield schema .TTest2 ()
447
+
448
+
449
+ @pytest .fixture
450
+ def test_extra (schema_any ):
451
+ yield schema .TTestExtra ()
452
+
453
+
454
+ @pytest .fixture
455
+ def test_no_extra (schema_any ):
456
+ yield schema .TTestNoExtra ()
457
+
458
+
459
+ @pytest .fixture
460
+ def user (schema_any ):
461
+ return schema .User ()
462
+
463
+
464
+ @pytest .fixture
465
+ def lang (schema_any ):
466
+ yield schema .Language ()
467
+
468
+
469
+ @pytest .fixture
470
+ def languages (lang ) -> List :
471
+ og_contents = lang .contents
472
+ languages = og_contents .copy ()
473
+ yield languages
474
+ lang .contents = og_contents
475
+
476
+
477
+ @pytest .fixture
478
+ def subject (schema_any ):
479
+ yield schema .Subject ()
480
+
481
+
482
+ @pytest .fixture
483
+ def experiment (schema_any ):
484
+ return schema .Experiment ()
485
+
486
+
487
+ @pytest .fixture
488
+ def ephys (schema_any ):
489
+ return schema .Ephys ()
490
+
491
+
492
+ @pytest .fixture
493
+ def img (schema_any ):
494
+ return schema .Image ()
495
+
496
+
497
+ @pytest .fixture
498
+ def trial (schema_any ):
499
+ return schema .Trial ()
500
+
501
+
502
+ @pytest .fixture
503
+ def channel (schema_any ):
504
+ return schema .Ephys .Channel ()
505
+
506
+
507
+ @pytest .fixture
508
+ def trash (schema_any ):
509
+ return schema .UberTrash ()
0 commit comments