forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_schema.py
More file actions
252 lines (197 loc) · 6.34 KB
/
test_schema.py
File metadata and controls
252 lines (197 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import types
import pytest
import inspect
import datajoint as dj
from inspect import getmembers
from . import schema
class Ephys(dj.Imported):
definition = """ # This is already declare in ./schema.py
"""
def relation_selector(attr):
try:
return issubclass(attr, dj.Table)
except TypeError:
return False
def part_selector(attr):
try:
return issubclass(attr, dj.Part)
except TypeError:
return False
@pytest.fixture
def schema_empty_module(schema_any, schema_empty):
"""
Mock the module tests_old.schema_empty.
The test `test_namespace_population` will check that the module contains all the
classes in schema_any, after running `spawn_missing_classes`.
"""
namespace_dict = {
"_": schema_any,
"schema": schema_empty,
"Ephys": Ephys,
}
module = types.ModuleType("schema_empty")
# Add classes to the module's namespace
for k, v in namespace_dict.items():
setattr(module, k, v)
return module
@pytest.fixture
def schema_empty(connection_test, schema_any, prefix):
context = {**schema.LOCALS_ANY, "Ephys": Ephys}
schema_empty = dj.Schema(
prefix + "_test1", context=context, connection=connection_test
)
schema_empty(Ephys)
# load the rest of the classes
schema_empty.spawn_missing_classes(context=context)
yield schema_empty
schema_empty.drop()
def test_schema_size_on_disk(schema_any):
number_of_bytes = schema_any.size_on_disk
assert isinstance(number_of_bytes, int)
def test_schema_list(schema_any):
schemas = dj.list_schemas()
assert schema_any.database in schemas
def test_drop_unauthorized():
info_schema = dj.schema("information_schema")
with pytest.raises(dj.errors.AccessError):
info_schema.drop()
def test_namespace_population(schema_empty_module):
"""
With the schema_empty_module fixture, this test
mimics the behavior of `spawn_missing_classes`, as if the schema
was declared in a separate module and `spawn_missing_classes` was called in that namespace.
"""
# Spawn missing classes in the caller's (self) namespace.
schema_empty_module.schema.context = None
schema_empty_module.schema.spawn_missing_classes(context=None)
# Then add them to the mock module's namespace.
for k, v in locals().items():
if inspect.isclass(v):
setattr(schema_empty_module, k, v)
for name, rel in getmembers(schema, relation_selector):
assert hasattr(
schema_empty_module, name
), "{name} not found in schema_empty".format(name=name)
assert (
rel.__base__ is getattr(schema_empty_module, name).__base__
), "Wrong tier for {name}".format(name=name)
for name_part in dir(rel):
if name_part[0].isupper() and part_selector(getattr(rel, name_part)):
assert (
getattr(rel, name_part).__base__ is dj.Part
), "Wrong tier for {name}".format(name=name_part)
def test_undecorated_table():
"""
Undecorated user table classes should raise an informative exception upon first use
"""
class UndecoratedClass(dj.Manual):
definition = ""
a = UndecoratedClass()
with pytest.raises(dj.DataJointError):
print(a.full_table_name)
def test_reject_decorated_part(schema_any):
"""
Decorating a dj.Part table should raise an informative exception.
"""
class A(dj.Manual):
definition = ...
class B(dj.Part):
definition = ...
with pytest.raises(dj.DataJointError):
schema_any(A.B)
schema_any(A)
def test_unauthorized_database(db_creds_test):
"""
an attempt to create a database to which user has no privileges should raise an informative exception.
"""
with pytest.raises(dj.DataJointError):
dj.Schema(
"unauthorized_schema", connection=dj.conn(reset=True, **db_creds_test)
)
def test_drop_database(db_creds_test, prefix):
schema = dj.Schema(
prefix + "_drop_test", connection=dj.conn(reset=True, **db_creds_test)
)
assert schema.exists
schema.drop()
assert not schema.exists
schema.drop() # should do nothing
def test_overlapping_name(connection_test, prefix):
test_schema = dj.Schema(prefix + "_overlapping_schema", connection=connection_test)
@test_schema
class Unit(dj.Manual):
definition = """
id: int # simple id
"""
# hack to update the locals dictionary
locals()
@test_schema
class Cell(dj.Manual):
definition = """
type: varchar(32) # type of cell
"""
class Unit(dj.Part):
definition = """
-> master
-> Unit
"""
test_schema.drop()
def test_list_tables(schema_simp):
"""
https://github.com/datajoint/datajoint-python/issues/838
"""
expected = set(
[
"reserved_word",
"#l",
"#a",
"__d",
"__b",
"__b__c",
"__e",
"__e__f",
"__e__g",
"__e__h",
"__e__m",
"__g",
"#outfit_launch",
"#outfit_launch__outfit_piece",
"#i_j",
"#j_i",
"#t_test_update",
"#data_a",
"#data_b",
"f",
"#argmax_test",
"#website",
"profile",
"profile__website",
]
)
actual = set(schema_simp.list_tables())
assert actual == expected, f"Missing from list_tables(): {expected - actual}"
def test_schema_save_any(schema_any):
assert "class Experiment(dj.Imported)" in schema_any.code
def test_schema_save_empty(schema_empty):
assert "class Experiment(dj.Imported)" in schema_empty.code
def test_uppercase_schema(db_creds_root):
"""
https://github.com/datajoint/datajoint-python/issues/564
"""
dj.conn(**db_creds_root, reset=True)
schema1 = dj.Schema("Schema_A")
@schema1
class Subject(dj.Manual):
definition = """
name: varchar(32)
"""
Schema_A = dj.VirtualModule("Schema_A", "Schema_A")
schema2 = dj.Schema("schema_b")
@schema2
class Recording(dj.Manual):
definition = """
-> Schema_A.Subject
id: smallint
"""
schema2.drop()
schema1.drop()