Skip to content

Commit 156d229

Browse files
committed
First pass at migrating test_privileges
1 parent 08fcac0 commit 156d229

File tree

1 file changed

+58
-55
lines changed

1 file changed

+58
-55
lines changed

tests/test_privileges.py

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,74 @@
11
import os
22
import pytest
3-
import importlib
43
import datajoint as dj
54
from . import schema, CONN_INFO_ROOT, PREFIX
65
from . import schema_privileges
76

87
namespace = locals()
98

9+
USER = "djsubset"
10+
11+
12+
@pytest.fixture
13+
def schema_priv(connection_test):
14+
schema_priv = dj.Schema(
15+
# PREFIX + "_schema_privileges",
16+
context=schema_privileges.LOCALS_PRIV,
17+
connection=connection_test,
18+
)
19+
schema_priv(schema_privileges.Parent)
20+
schema_priv(schema_privileges.Child)
21+
schema_priv(schema_privileges.NoAccess)
22+
schema_priv(schema_privileges.NoAccessAgain)
23+
yield schema_priv
24+
if schema_priv.is_activated():
25+
schema_priv.drop()
26+
27+
28+
@pytest.fixture
29+
def connection_djsubset(connection_root, db_creds_root, schema_priv):
30+
user = "djsubset"
31+
conn = dj.conn(**db_creds_root, reset=True)
32+
schema_priv.activate(f"{PREFIX}_schema_privileges")
33+
conn.query(
34+
f"""
35+
CREATE USER IF NOT EXISTS '{user}'@'%%'
36+
IDENTIFIED BY '{user}'
37+
"""
38+
)
39+
conn.query(
40+
f"""
41+
GRANT SELECT, INSERT, UPDATE, DELETE
42+
ON `{PREFIX}_schema_privileges`.`#parent`
43+
TO '{user}'@'%%'
44+
"""
45+
)
46+
conn.query(
47+
f"""
48+
GRANT SELECT, INSERT, UPDATE, DELETE
49+
ON `{PREFIX}_schema_privileges`.`__child`
50+
TO '{user}'@'%%'
51+
"""
52+
)
53+
conn_djsubset = dj.conn(
54+
host=db_creds_root["host"],
55+
user=user,
56+
password=user,
57+
reset=True,
58+
)
59+
yield conn_djsubset
60+
conn.query(f"DROP USER {user}")
61+
conn.query(f"DROP DATABASE {PREFIX}_schema_privileges")
62+
1063

1164
@pytest.fixture
12-
def connection_djview(connection_root):
65+
def connection_djview(connection_root, db_creds_root):
1366
"""
1467
A connection with only SELECT privilege to djtest schemas.
1568
Requires connection_root fixture so that `djview` user exists.
1669
"""
1770
connection = dj.conn(
18-
host=os.getenv("DJ_HOST"),
71+
host=db_creds_root["host"],
1972
user="djview",
2073
password="djview",
2174
reset=True,
@@ -60,58 +113,8 @@ class Try(dj.Manual):
60113

61114

62115
class TestSubset:
63-
USER = "djsubset"
64-
65-
@classmethod
66-
def setup_class(cls):
67-
conn = dj.conn(
68-
host=CONN_INFO_ROOT["host"],
69-
user=CONN_INFO_ROOT["user"],
70-
password=CONN_INFO_ROOT["password"],
71-
reset=True,
72-
)
73-
schema_privileges.schema.activate(f"{PREFIX}_schema_privileges")
74-
conn.query(
75-
f"""
76-
CREATE USER IF NOT EXISTS '{cls.USER}'@'%%'
77-
IDENTIFIED BY '{cls.USER}'
78-
"""
79-
)
80-
conn.query(
81-
f"""
82-
GRANT SELECT, INSERT, UPDATE, DELETE
83-
ON `{PREFIX}_schema_privileges`.`#parent`
84-
TO '{cls.USER}'@'%%'
85-
"""
86-
)
87-
conn.query(
88-
f"""
89-
GRANT SELECT, INSERT, UPDATE, DELETE
90-
ON `{PREFIX}_schema_privileges`.`__child`
91-
TO '{cls.USER}'@'%%'
92-
"""
93-
)
94-
cls.connection = dj.conn(
95-
host=CONN_INFO_ROOT["host"],
96-
user=cls.USER,
97-
password=cls.USER,
98-
reset=True,
99-
)
100-
101-
@classmethod
102-
def teardown_class(cls):
103-
conn = dj.conn(
104-
host=CONN_INFO_ROOT["host"],
105-
user=CONN_INFO_ROOT["user"],
106-
password=CONN_INFO_ROOT["password"],
107-
reset=True,
108-
)
109-
conn.query(f"DROP USER {cls.USER}")
110-
conn.query(f"DROP DATABASE {PREFIX}_schema_privileges")
111-
112-
def test_populate_activate(self):
113-
importlib.reload(schema_privileges)
114-
schema_privileges.schema.activate(
116+
def test_populate_activate(self, connection_djsubset, schema_priv):
117+
schema_priv.activate(
115118
f"{PREFIX}_schema_privileges", create_schema=True, create_tables=False
116119
)
117120
schema_privileges.Child.populate()

0 commit comments

Comments
 (0)