Skip to content

Commit fdd4352

Browse files
committed
Migrate TestUnprivileged
1 parent ecbcac1 commit fdd4352

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

tests/schema_privileges.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import datajoint as dj
2+
import inspect
23

3-
schema = dj.Schema()
44

5-
6-
@schema
75
class Parent(dj.Lookup):
86
definition = """
97
id: int
108
"""
119
contents = [(1,)]
1210

1311

14-
@schema
1512
class Child(dj.Computed):
1613
definition = """
1714
-> Parent
@@ -21,15 +18,17 @@ def make(self, key):
2118
self.insert1(key)
2219

2320

24-
@schema
2521
class NoAccess(dj.Lookup):
2622
definition = """
2723
string: varchar(10)
2824
"""
2925

3026

31-
@schema
3227
class NoAccessAgain(dj.Manual):
3328
definition = """
3429
-> NoAccess
3530
"""
31+
32+
33+
LOCALS_PRIV = {k: v for k, v in locals().items() if inspect.isclass(v)}
34+
__all__ = list(LOCALS_PRIV)

tests/test_privileges.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1+
import os
2+
import pytest
13
import importlib
24
import datajoint as dj
35
from . import schema, CONN_INFO_ROOT, PREFIX
4-
from . import schema_privileges as pipeline
5-
from nose.tools import assert_true, raises
6+
from . import schema_privileges
67

78
namespace = locals()
89

10+
@pytest.fixture
11+
def connection_djview(connection_root):
12+
"""
13+
A connection with only SELECT privilege to djtest schemas.
14+
Requires connection_root fixture so that `djview` user exists.
15+
"""
16+
connection = dj.conn(
17+
host=os.getenv("DJ_HOST"),
18+
user="djview",
19+
password="djview",
20+
reset=True,
21+
)
22+
yield connection
923

10-
class TestUnprivileged:
11-
@classmethod
12-
def setup_class(cls):
13-
"""A connection with only SELECT privilege to djtest schemas"""
14-
cls.connection = dj.conn(
15-
host=CONN_INFO_ROOT["host"], user="djview", password="djview", reset=True
16-
)
1724

18-
@raises(dj.DataJointError)
19-
def test_fail_create_schema(self):
25+
class TestUnprivileged:
26+
def test_fail_create_schema(self, connection_djview):
2027
"""creating a schema with no CREATE privilege"""
21-
return dj.Schema("forbidden_schema", namespace, connection=self.connection)
28+
with pytest.raises(dj.DataJointError):
29+
return dj.Schema("forbidden_schema", namespace, connection=connection_djview)
2230

23-
@raises(dj.DataJointError)
24-
def test_insert_failure(self):
31+
def test_insert_failure(self, connection_djview, schema_any):
2532
unprivileged = dj.Schema(
26-
schema.schema.database, namespace, connection=self.connection
33+
schema_any.database, namespace, connection=connection_djview
2734
)
2835
unprivileged.spawn_missing_classes()
29-
assert_true(
30-
issubclass(Language, dj.Lookup)
31-
and len(Language()) == len(schema.Language()),
32-
"failed to spawn missing classes",
33-
)
34-
Language().insert1(("Socrates", "Greek"))
36+
assert issubclass(Language, dj.Lookup) and len(Language()) == len(schema.Language()), "failed to spawn missing classes"
37+
with pytest.raises(dj.DataJointError):
38+
Language().insert1(("Socrates", "Greek"))
3539

36-
@raises(dj.DataJointError)
37-
def test_failure_to_create_table(self):
40+
def test_failure_to_create_table(self, connection_djview, schema_any):
3841
unprivileged = dj.Schema(
39-
schema.schema.database, namespace, connection=self.connection
42+
schema_any.database, namespace, connection=connection_djview
4043
)
4144

4245
@unprivileged
@@ -47,7 +50,8 @@ class Try(dj.Manual):
4750
value : float
4851
"""
4952

50-
Try().insert1((1, 1.5))
53+
with pytest.raises(dj.DataJointError):
54+
Try().insert1((1, 1.5))
5155

5256

5357
class TestSubset:
@@ -61,7 +65,7 @@ def setup_class(cls):
6165
password=CONN_INFO_ROOT["password"],
6266
reset=True,
6367
)
64-
pipeline.schema.activate(f"{PREFIX}_pipeline")
68+
schema_privileges.schema.activate(f"{PREFIX}_schema_privileges")
6569
conn.query(
6670
f"""
6771
CREATE USER IF NOT EXISTS '{cls.USER}'@'%%'
@@ -71,14 +75,14 @@ def setup_class(cls):
7175
conn.query(
7276
f"""
7377
GRANT SELECT, INSERT, UPDATE, DELETE
74-
ON `{PREFIX}_pipeline`.`#parent`
78+
ON `{PREFIX}_schema_privileges`.`#parent`
7579
TO '{cls.USER}'@'%%'
7680
"""
7781
)
7882
conn.query(
7983
f"""
8084
GRANT SELECT, INSERT, UPDATE, DELETE
81-
ON `{PREFIX}_pipeline`.`__child`
85+
ON `{PREFIX}_schema_privileges`.`__child`
8286
TO '{cls.USER}'@'%%'
8387
"""
8488
)
@@ -98,12 +102,12 @@ def teardown_class(cls):
98102
reset=True,
99103
)
100104
conn.query(f"DROP USER {cls.USER}")
101-
conn.query(f"DROP DATABASE {PREFIX}_pipeline")
105+
conn.query(f"DROP DATABASE {PREFIX}_schema_privileges")
102106

103107
def test_populate_activate(self):
104-
importlib.reload(pipeline)
105-
pipeline.schema.activate(
106-
f"{PREFIX}_pipeline", create_schema=True, create_tables=False
108+
importlib.reload(schema_privileges)
109+
schema_privileges.schema.activate(
110+
f"{PREFIX}_schema_privileges", create_schema=True, create_tables=False
107111
)
108-
pipeline.Child.populate()
109-
assert pipeline.Child.progress(display=False)[0] == 0
112+
schema_privileges.Child.populate()
113+
assert schema_privileges.Child.progress(display=False)[0] == 0

0 commit comments

Comments
 (0)