Skip to content

Commit bfe3608

Browse files
committed
Merge branch 'dev-tests-cleanup' into dev-tests-plat-143-modernpy
2 parents b58e238 + bf17e75 commit bfe3608

33 files changed

+1506
-1594
lines changed

tests/__init__.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +0,0 @@
1-
import datajoint as dj
2-
from packaging import version
3-
import pytest
4-
import os
5-
6-
PREFIX = os.environ.get("DJ_TEST_DB_PREFIX", "djtest")
7-
8-
# Connection for testing
9-
CONN_INFO = dict(
10-
host=os.environ.get("DJ_TEST_HOST", "fakeservices.datajoint.io"),
11-
user=os.environ.get("DJ_TEST_USER", "datajoint"),
12-
password=os.environ.get("DJ_TEST_PASSWORD", "datajoint"),
13-
)
14-
15-
CONN_INFO_ROOT = dict(
16-
host=os.environ.get("DJ_HOST", "fakeservices.datajoint.io"),
17-
user=os.environ.get("DJ_USER", "root"),
18-
password=os.environ.get("DJ_PASS", "simple"),
19-
)
20-
21-
S3_CONN_INFO = dict(
22-
endpoint=os.environ.get("S3_ENDPOINT", "fakeservices.datajoint.io"),
23-
access_key=os.environ.get("S3_ACCESS_KEY", "datajoint"),
24-
secret_key=os.environ.get("S3_SECRET_KEY", "datajoint"),
25-
bucket=os.environ.get("S3_BUCKET", "datajoint.test"),
26-
)

tests/conftest.py

Lines changed: 117 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datajoint as dj
22
from packaging import version
3-
from typing import Dict
3+
from typing import Dict, List
44
import os
55
from os import environ, remove
66
import minio
@@ -18,9 +18,6 @@
1818
DataJointError,
1919
)
2020
from . import (
21-
PREFIX,
22-
CONN_INFO,
23-
S3_CONN_INFO,
2421
schema,
2522
schema_simple,
2623
schema_advanced,
@@ -30,6 +27,11 @@
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")
3436
def 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()

tests/schema_alter.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import datajoint as dj
2+
import inspect
3+
4+
5+
class Experiment(dj.Imported):
6+
original_definition = """ # information about experiments
7+
-> Subject
8+
experiment_id :smallint # experiment number for this subject
9+
---
10+
experiment_date :date # date when experiment was started
11+
-> [nullable] User
12+
data_path="" :varchar(255) # file path to recorded data
13+
notes="" :varchar(2048) # e.g. purpose of experiment
14+
entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp
15+
"""
16+
17+
definition1 = """ # Experiment
18+
-> Subject
19+
experiment_id :smallint # experiment number for this subject
20+
---
21+
data_path : int # some number
22+
extra=null : longblob # just testing
23+
-> [nullable] User
24+
subject_notes=null :varchar(2048) # {notes} e.g. purpose of experiment
25+
entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp
26+
"""
27+
28+
29+
class Parent(dj.Manual):
30+
definition = """
31+
parent_id: int
32+
"""
33+
34+
class Child(dj.Part):
35+
definition = """
36+
-> Parent
37+
"""
38+
definition_new = """
39+
-> master
40+
---
41+
child_id=null: int
42+
"""
43+
44+
class Grandchild(dj.Part):
45+
definition = """
46+
-> master.Child
47+
"""
48+
definition_new = """
49+
-> master.Child
50+
---
51+
grandchild_id=null: int
52+
"""
53+
54+
55+
LOCALS_ALTER = {k: v for k, v in locals().items() if inspect.isclass(v)}
56+
__all__ = list(LOCALS_ALTER)

tests/schema_external.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tempfile
66
import inspect
77
import datajoint as dj
8-
from . import PREFIX, CONN_INFO, S3_CONN_INFO
98
import numpy as np
109

1110

tests/schema_uuid.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import uuid
22
import inspect
33
import datajoint as dj
4-
from . import PREFIX, CONN_INFO
54

65
top_level_namespace_id = uuid.UUID("00000000-0000-0000-0000-000000000000")
76

0 commit comments

Comments
 (0)