1- from nose . tools import assert_false , assert_true , raises
1+ import pytest
22import datajoint as dj
33from inspect import getmembers
44from . 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+ """
811
912
1013def relation_selector (attr ):
@@ -21,42 +24,47 @@ def part_selector(attr):
2124 return False
2225
2326
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
2644 assert isinstance (number_of_bytes , int )
2745
2846
29- def test_schema_list ():
47+ def test_schema_list (schema_any ):
3048 schemas = dj .list_schemas ()
31- assert schema . schema .database in schemas
49+ assert schema_any .database in schemas
3250
3351
34- @raises (dj .errors .AccessError )
3552def test_drop_unauthorized ():
3653 info_schema = dj .schema ("information_schema" )
37- info_schema .drop ()
54+ with pytest .raises (dj .errors .AccessError ):
55+ info_schema .drop ()
3856
3957
40- def test_namespace_population ():
58+ def test_namespace_population (schema_empty , schema_any ):
4159 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 )
5062
5163 for name_part in dir (rel ):
5264 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 )
5766
5867
59- @raises (dj .DataJointError )
6068def test_undecorated_table ():
6169 """
6270 Undecorated user table classes should raise an informative exception upon first use
@@ -66,45 +74,48 @@ class UndecoratedClass(dj.Manual):
6674 definition = ""
6775
6876 a = UndecoratedClass ()
69- print (a .full_table_name )
77+ with pytest .raises (dj .DataJointError ):
78+ print (a .full_table_name )
7079
7180
72- @raises (dj .DataJointError )
73- def test_reject_decorated_part ():
81+ def test_reject_decorated_part (schema_any ):
7482 """
7583 Decorating a dj.Part table should raise an informative exception.
7684 """
7785
78- @schema .schema
7986 class A (dj .Manual ):
8087 definition = ...
8188
82- @schema .schema
8389 class B (dj .Part ):
8490 definition = ...
8591
8692
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 ):
8999 """
90100 an attempt to create a database to which user has no privileges should raise an informative exception.
91101 """
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 ))
93104
94105
95- def test_drop_database ():
106+ def test_drop_database (db_creds_test ):
96107 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 )
98109 )
99110 assert schema .exists
100111 schema .drop ()
101112 assert not schema .exists
102113 schema .drop () # should do nothing
103114
104115
105- def test_overlapping_name ():
116+ def test_overlapping_name (connection_test ):
106117 test_schema = dj .Schema (
107- PREFIX + "_overlapping_schema" , connection = dj . conn ( ** CONN_INFO )
118+ PREFIX + "_overlapping_schema" , connection = connection_test
108119 )
109120
110121 @test_schema
@@ -131,8 +142,10 @@ class Unit(dj.Part):
131142 test_schema .drop ()
132143
133144
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+ """
136149 assert set (
137150 [
138151 "reserved_word" ,
@@ -156,17 +169,22 @@ def test_list_tables():
156169 "profile" ,
157170 "profile__website" ,
158171 ]
159- ) == set (schema_simple .list_tables ())
172+ ) == set (schema_simp .list_tables ())
173+
160174
175+ def test_schema_save_any (schema_any ):
176+ assert "class Experiment(dj.Imported)" in schema_any .code
161177
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
165178
179+ def test_schema_save_empty (schema_empty ):
180+ assert "class Experiment(dj.Imported)" in schema_empty .code
166181
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 )
170188 schema1 = dj .Schema ("Schema_A" )
171189
172190 @schema1
0 commit comments