Skip to content

Commit ecbcac1

Browse files
committed
cp to tests
1 parent 5cf703f commit ecbcac1

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

tests/schema_privileges.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import datajoint as dj
2+
3+
schema = dj.Schema()
4+
5+
6+
@schema
7+
class Parent(dj.Lookup):
8+
definition = """
9+
id: int
10+
"""
11+
contents = [(1,)]
12+
13+
14+
@schema
15+
class Child(dj.Computed):
16+
definition = """
17+
-> Parent
18+
"""
19+
20+
def make(self, key):
21+
self.insert1(key)
22+
23+
24+
@schema
25+
class NoAccess(dj.Lookup):
26+
definition = """
27+
string: varchar(10)
28+
"""
29+
30+
31+
@schema
32+
class NoAccessAgain(dj.Manual):
33+
definition = """
34+
-> NoAccess
35+
"""

tests/test_privileges.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import importlib
2+
import datajoint as dj
3+
from . import schema, CONN_INFO_ROOT, PREFIX
4+
from . import schema_privileges as pipeline
5+
from nose.tools import assert_true, raises
6+
7+
namespace = locals()
8+
9+
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+
)
17+
18+
@raises(dj.DataJointError)
19+
def test_fail_create_schema(self):
20+
"""creating a schema with no CREATE privilege"""
21+
return dj.Schema("forbidden_schema", namespace, connection=self.connection)
22+
23+
@raises(dj.DataJointError)
24+
def test_insert_failure(self):
25+
unprivileged = dj.Schema(
26+
schema.schema.database, namespace, connection=self.connection
27+
)
28+
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"))
35+
36+
@raises(dj.DataJointError)
37+
def test_failure_to_create_table(self):
38+
unprivileged = dj.Schema(
39+
schema.schema.database, namespace, connection=self.connection
40+
)
41+
42+
@unprivileged
43+
class Try(dj.Manual):
44+
definition = """ # should not matter really
45+
id : int
46+
---
47+
value : float
48+
"""
49+
50+
Try().insert1((1, 1.5))
51+
52+
53+
class TestSubset:
54+
USER = "djsubset"
55+
56+
@classmethod
57+
def setup_class(cls):
58+
conn = dj.conn(
59+
host=CONN_INFO_ROOT["host"],
60+
user=CONN_INFO_ROOT["user"],
61+
password=CONN_INFO_ROOT["password"],
62+
reset=True,
63+
)
64+
pipeline.schema.activate(f"{PREFIX}_pipeline")
65+
conn.query(
66+
f"""
67+
CREATE USER IF NOT EXISTS '{cls.USER}'@'%%'
68+
IDENTIFIED BY '{cls.USER}'
69+
"""
70+
)
71+
conn.query(
72+
f"""
73+
GRANT SELECT, INSERT, UPDATE, DELETE
74+
ON `{PREFIX}_pipeline`.`#parent`
75+
TO '{cls.USER}'@'%%'
76+
"""
77+
)
78+
conn.query(
79+
f"""
80+
GRANT SELECT, INSERT, UPDATE, DELETE
81+
ON `{PREFIX}_pipeline`.`__child`
82+
TO '{cls.USER}'@'%%'
83+
"""
84+
)
85+
cls.connection = dj.conn(
86+
host=CONN_INFO_ROOT["host"],
87+
user=cls.USER,
88+
password=cls.USER,
89+
reset=True,
90+
)
91+
92+
@classmethod
93+
def teardown_class(cls):
94+
conn = dj.conn(
95+
host=CONN_INFO_ROOT["host"],
96+
user=CONN_INFO_ROOT["user"],
97+
password=CONN_INFO_ROOT["password"],
98+
reset=True,
99+
)
100+
conn.query(f"DROP USER {cls.USER}")
101+
conn.query(f"DROP DATABASE {PREFIX}_pipeline")
102+
103+
def test_populate_activate(self):
104+
importlib.reload(pipeline)
105+
pipeline.schema.activate(
106+
f"{PREFIX}_pipeline", create_schema=True, create_tables=False
107+
)
108+
pipeline.Child.populate()
109+
assert pipeline.Child.progress(display=False)[0] == 0

0 commit comments

Comments
 (0)