1
- from nose . tools import assert_false , assert_true , raises
1
+ import pytest
2
2
import datajoint as dj
3
3
from inspect import getmembers
4
4
from . import schema
5
- from . import schema_empty
6
- from . import PREFIX , CONN_INFO , CONN_INFO_ROOT
7
- from .schema_simple import schema as schema_simple
5
+ from . import PREFIX
6
+
7
+
8
+ class Ephys (dj .Imported ):
9
+ definition = """ # This is already declared in ./schema.py
10
+ """
8
11
9
12
10
13
def relation_selector (attr ):
@@ -21,42 +24,47 @@ def part_selector(attr):
21
24
return False
22
25
23
26
24
- def test_schema_size_on_disk ():
25
- number_of_bytes = schema .schema .size_on_disk
27
+ @pytest .fixture
28
+ def schema_empty (connection_test , schema_any ):
29
+ context = {
30
+ ** schema .LOCALS_ANY ,
31
+ "Ephys" : Ephys
32
+ }
33
+ schema_emp = dj .Schema (PREFIX + "_test1" , context = context , connection = connection_test )
34
+ schema_emp (Ephys )
35
+ # load the rest of the classes
36
+ schema_emp .spawn_missing_classes ()
37
+ breakpoint ()
38
+ yield schema_emp
39
+ schema_emp .drop ()
40
+
41
+
42
+ def test_schema_size_on_disk (schema_any ):
43
+ number_of_bytes = schema_any .size_on_disk
26
44
assert isinstance (number_of_bytes , int )
27
45
28
46
29
- def test_schema_list ():
47
+ def test_schema_list (schema_any ):
30
48
schemas = dj .list_schemas ()
31
- assert schema . schema .database in schemas
49
+ assert schema_any .database in schemas
32
50
33
51
34
- @raises (dj .errors .AccessError )
35
52
def test_drop_unauthorized ():
36
53
info_schema = dj .schema ("information_schema" )
37
- info_schema .drop ()
54
+ with pytest .raises (dj .errors .AccessError ):
55
+ info_schema .drop ()
38
56
39
57
40
- def test_namespace_population ():
58
+ def test_namespace_population (schema_empty , schema_any ):
41
59
for name , rel in getmembers (schema , relation_selector ):
42
- assert_true (
43
- hasattr (schema_empty , name ),
44
- "{name} not found in schema_empty" .format (name = name ),
45
- )
46
- assert_true (
47
- rel .__base__ is getattr (schema_empty , name ).__base__ ,
48
- "Wrong tier for {name}" .format (name = name ),
49
- )
60
+ assert hasattr (schema_empty , name ), "{name} not found in schema_empty" .format (name = name )
61
+ assert rel .__base__ is getattr (schema_empty , name ).__base__ , "Wrong tier for {name}" .format (name = name )
50
62
51
63
for name_part in dir (rel ):
52
64
if name_part [0 ].isupper () and part_selector (getattr (rel , name_part )):
53
- assert_true (
54
- getattr (rel , name_part ).__base__ is dj .Part ,
55
- "Wrong tier for {name}" .format (name = name_part ),
56
- )
65
+ assert getattr (rel , name_part ).__base__ is dj .Part , "Wrong tier for {name}" .format (name = name_part )
57
66
58
67
59
- @raises (dj .DataJointError )
60
68
def test_undecorated_table ():
61
69
"""
62
70
Undecorated user table classes should raise an informative exception upon first use
@@ -66,45 +74,48 @@ class UndecoratedClass(dj.Manual):
66
74
definition = ""
67
75
68
76
a = UndecoratedClass ()
69
- print (a .full_table_name )
77
+ with pytest .raises (dj .DataJointError ):
78
+ print (a .full_table_name )
70
79
71
80
72
- @raises (dj .DataJointError )
73
- def test_reject_decorated_part ():
81
+ def test_reject_decorated_part (schema_any ):
74
82
"""
75
83
Decorating a dj.Part table should raise an informative exception.
76
84
"""
77
85
78
- @schema .schema
79
86
class A (dj .Manual ):
80
87
definition = ...
81
88
82
- @schema .schema
83
89
class B (dj .Part ):
84
90
definition = ...
85
91
86
92
87
- @raises (dj .DataJointError )
88
- def test_unauthorized_database ():
93
+ with pytest .raises (dj .DataJointError ):
94
+ schema_any (A .B )
95
+ schema_any (A )
96
+
97
+
98
+ def test_unauthorized_database (db_creds_test ):
89
99
"""
90
100
an attempt to create a database to which user has no privileges should raise an informative exception.
91
101
"""
92
- dj .Schema ("unauthorized_schema" , connection = dj .conn (reset = True , ** CONN_INFO ))
102
+ with pytest .raises (dj .DataJointError ):
103
+ dj .Schema ("unauthorized_schema" , connection = dj .conn (reset = True , ** db_creds_test ))
93
104
94
105
95
- def test_drop_database ():
106
+ def test_drop_database (db_creds_test ):
96
107
schema = dj .Schema (
97
- PREFIX + "_drop_test" , connection = dj .conn (reset = True , ** CONN_INFO )
108
+ PREFIX + "_drop_test" , connection = dj .conn (reset = True , ** db_creds_test )
98
109
)
99
110
assert schema .exists
100
111
schema .drop ()
101
112
assert not schema .exists
102
113
schema .drop () # should do nothing
103
114
104
115
105
- def test_overlapping_name ():
116
+ def test_overlapping_name (connection_test ):
106
117
test_schema = dj .Schema (
107
- PREFIX + "_overlapping_schema" , connection = dj . conn ( ** CONN_INFO )
118
+ PREFIX + "_overlapping_schema" , connection = connection_test
108
119
)
109
120
110
121
@test_schema
@@ -131,8 +142,10 @@ class Unit(dj.Part):
131
142
test_schema .drop ()
132
143
133
144
134
- def test_list_tables ():
135
- # https://github.com/datajoint/datajoint-python/issues/838
145
+ def test_list_tables (schema_simp ):
146
+ """
147
+ https://github.com/datajoint/datajoint-python/issues/838
148
+ """
136
149
assert set (
137
150
[
138
151
"reserved_word" ,
@@ -156,17 +169,22 @@ def test_list_tables():
156
169
"profile" ,
157
170
"profile__website" ,
158
171
]
159
- ) == set (schema_simple .list_tables ())
172
+ ) == set (schema_simp .list_tables ())
173
+
160
174
175
+ def test_schema_save_any (schema_any ):
176
+ assert "class Experiment(dj.Imported)" in schema_any .code
161
177
162
- def test_schema_save ():
163
- assert "class Experiment(dj.Imported)" in schema .schema .code
164
- assert "class Experiment(dj.Imported)" in schema_empty .schema .code
165
178
179
+ def test_schema_save_empty (schema_empty ):
180
+ assert "class Experiment(dj.Imported)" in schema_empty .code
166
181
167
- def test_uppercase_schema ():
168
- # https://github.com/datajoint/datajoint-python/issues/564
169
- dj .conn (** CONN_INFO_ROOT , reset = True )
182
+
183
+ def test_uppercase_schema (db_creds_root ):
184
+ """
185
+ https://github.com/datajoint/datajoint-python/issues/564
186
+ """
187
+ dj .conn (** db_creds_root , reset = True )
170
188
schema1 = dj .Schema ("Schema_A" )
171
189
172
190
@schema1
0 commit comments