11import datajoint as dj
22from packaging import version
3- from typing import Dict
3+ from typing import Dict , List
44import os
55from os import environ , remove
66import minio
1818 DataJointError ,
1919)
2020from . import (
21- PREFIX ,
22- CONN_INFO ,
23- S3_CONN_INFO ,
2421 schema ,
2522 schema_simple ,
2623 schema_advanced ,
3027)
3128
3229
30+ @pytest .fixture (scope = "session" )
31+ def prefix ():
32+ return os .environ .get ("DJ_TEST_DB_PREFIX" , "djtest" )
33+
34+
3335@pytest .fixture (scope = "session" )
3436def monkeysession ():
3537 with pytest .MonkeyPatch .context () as mp :
@@ -81,7 +83,7 @@ def connection_root_bare(db_creds_root):
8183
8284
8385@pytest .fixture (scope = "session" )
84- def connection_root (connection_root_bare ):
86+ def connection_root (connection_root_bare , prefix ):
8587 """Root user database connection."""
8688 dj .config ["safemode" ] = False
8789 conn_root = connection_root_bare
@@ -136,7 +138,7 @@ def connection_root(connection_root_bare):
136138
137139 # Teardown
138140 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 ))
140142 for db in cur .fetchall ():
141143 conn_root .query ("DROP DATABASE `{}`" .format (db [0 ]))
142144 conn_root .query ("SET FOREIGN_KEY_CHECKS=1" )
@@ -151,9 +153,9 @@ def connection_root(connection_root_bare):
151153
152154
153155@pytest .fixture (scope = "session" )
154- def connection_test (connection_root , db_creds_test ):
156+ def connection_test (connection_root , prefix , db_creds_test ):
155157 """Test user database connection."""
156- database = f"{ PREFIX } %%"
158+ database = f"{ prefix } %%"
157159 permission = "ALL PRIVILEGES"
158160
159161 # Create MySQL users
@@ -191,7 +193,17 @@ def connection_test(connection_root, db_creds_test):
191193
192194
193195@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 ):
195207 stores_config = {
196208 "raw" : dict (protocol = "file" , location = tmpdir_factory .mktemp ("raw" )),
197209 "repo" : dict (
@@ -200,7 +212,7 @@ def stores_config(tmpdir_factory):
200212 location = tmpdir_factory .mktemp ("repo" ),
201213 ),
202214 "repo-s3" : dict (
203- S3_CONN_INFO ,
215+ s3_creds ,
204216 protocol = "s3" ,
205217 location = "dj/repo" ,
206218 stage = tmpdir_factory .mktemp ("repo-s3" ),
@@ -209,7 +221,7 @@ def stores_config(tmpdir_factory):
209221 protocol = "file" , location = tmpdir_factory .mktemp ("local" ), subfolding = (1 , 1 )
210222 ),
211223 "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 )
213225 ),
214226 }
215227 return stores_config
@@ -238,9 +250,9 @@ def mock_cache(tmpdir_factory):
238250
239251
240252@pytest .fixture
241- def schema_any (connection_test ):
253+ def schema_any (connection_test , prefix ):
242254 schema_any = dj .Schema (
243- PREFIX + "_test1" , schema .LOCALS_ANY , connection = connection_test
255+ prefix + "_test1" , schema .LOCALS_ANY , connection = connection_test
244256 )
245257 assert schema .LOCALS_ANY , "LOCALS_ANY is empty"
246258 try :
@@ -292,9 +304,9 @@ def schema_any(connection_test):
292304
293305
294306@pytest .fixture
295- def schema_simp (connection_test ):
307+ def schema_simp (connection_test , prefix ):
296308 schema = dj .Schema (
297- PREFIX + "_relational" , schema_simple .LOCALS_SIMPLE , connection = connection_test
309+ prefix + "_relational" , schema_simple .LOCALS_SIMPLE , connection = connection_test
298310 )
299311 schema (schema_simple .IJ )
300312 schema (schema_simple .JI )
@@ -319,9 +331,9 @@ def schema_simp(connection_test):
319331
320332
321333@pytest .fixture
322- def schema_adv (connection_test ):
334+ def schema_adv (connection_test , prefix ):
323335 schema = dj .Schema (
324- PREFIX + "_advanced" ,
336+ prefix + "_advanced" ,
325337 schema_advanced .LOCALS_ADVANCED ,
326338 connection = connection_test ,
327339 )
@@ -339,9 +351,11 @@ def schema_adv(connection_test):
339351
340352
341353@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+ ):
343357 schema = dj .Schema (
344- PREFIX + "_extern" ,
358+ prefix + "_extern" ,
345359 context = schema_external .LOCALS_EXTERNAL ,
346360 connection = connection_test ,
347361 )
@@ -358,9 +372,9 @@ def schema_ext(connection_test, enable_filepath_feature, mock_stores, mock_cache
358372
359373
360374@pytest .fixture
361- def schema_uuid (connection_test ):
375+ def schema_uuid (connection_test , prefix ):
362376 schema = dj .Schema (
363- PREFIX + "_test1" ,
377+ prefix + "_test1" ,
364378 context = schema_uuid_module .LOCALS_UUID ,
365379 connection = connection_test ,
366380 )
@@ -386,37 +400,110 @@ def http_client():
386400
387401
388402@pytest .fixture (scope = "session" )
389- def minio_client_bare (http_client ):
403+ def minio_client_bare (s3_creds , http_client ):
390404 """Initialize MinIO with an endpoint and access/secret keys."""
391405 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" ],
395409 secure = True ,
396410 http_client = http_client ,
397411 )
398412 return client
399413
400414
401415@pytest .fixture (scope = "session" )
402- def minio_client (minio_client_bare ):
416+ def minio_client (s3_creds , minio_client_bare ):
403417 """Initialize a MinIO client and create buckets for testing session."""
404418 # Setup MinIO bucket
405419 aws_region = "us-east-1"
406420 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 )
408422 except minio .error .S3Error as e :
409423 if e .code != "BucketAlreadyOwnedByYou" :
410424 raise e
411425
412426 yield minio_client_bare
413427
414428 # 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 ))
416430 objs = [
417431 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" )
419433 )
420434 for o in objs
421435 ]
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