|
| 1 | +from nose.tools import assert_false, assert_true, raises |
| 2 | +import datajoint as dj |
| 3 | +from inspect import getmembers |
| 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 |
| 8 | + |
| 9 | + |
| 10 | +def relation_selector(attr): |
| 11 | + try: |
| 12 | + return issubclass(attr, dj.Table) |
| 13 | + except TypeError: |
| 14 | + return False |
| 15 | + |
| 16 | + |
| 17 | +def part_selector(attr): |
| 18 | + try: |
| 19 | + return issubclass(attr, dj.Part) |
| 20 | + except TypeError: |
| 21 | + return False |
| 22 | + |
| 23 | + |
| 24 | +def test_schema_size_on_disk(): |
| 25 | + number_of_bytes = schema.schema.size_on_disk |
| 26 | + assert_true(isinstance(number_of_bytes, int)) |
| 27 | + |
| 28 | + |
| 29 | +def test_schema_list(): |
| 30 | + schemas = dj.list_schemas() |
| 31 | + assert_true(schema.schema.database in schemas) |
| 32 | + |
| 33 | + |
| 34 | +@raises(dj.errors.AccessError) |
| 35 | +def test_drop_unauthorized(): |
| 36 | + info_schema = dj.schema("information_schema") |
| 37 | + info_schema.drop() |
| 38 | + |
| 39 | + |
| 40 | +def test_namespace_population(): |
| 41 | + 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 | + ) |
| 50 | + |
| 51 | + for name_part in dir(rel): |
| 52 | + 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 | + ) |
| 57 | + |
| 58 | + |
| 59 | +@raises(dj.DataJointError) |
| 60 | +def test_undecorated_table(): |
| 61 | + """ |
| 62 | + Undecorated user table classes should raise an informative exception upon first use |
| 63 | + """ |
| 64 | + |
| 65 | + class UndecoratedClass(dj.Manual): |
| 66 | + definition = "" |
| 67 | + |
| 68 | + a = UndecoratedClass() |
| 69 | + print(a.full_table_name) |
| 70 | + |
| 71 | + |
| 72 | +@raises(dj.DataJointError) |
| 73 | +def test_reject_decorated_part(): |
| 74 | + """ |
| 75 | + Decorating a dj.Part table should raise an informative exception. |
| 76 | + """ |
| 77 | + |
| 78 | + @schema.schema |
| 79 | + class A(dj.Manual): |
| 80 | + definition = ... |
| 81 | + |
| 82 | + @schema.schema |
| 83 | + class B(dj.Part): |
| 84 | + definition = ... |
| 85 | + |
| 86 | + |
| 87 | +@raises(dj.DataJointError) |
| 88 | +def test_unauthorized_database(): |
| 89 | + """ |
| 90 | + an attempt to create a database to which user has no privileges should raise an informative exception. |
| 91 | + """ |
| 92 | + dj.Schema("unauthorized_schema", connection=dj.conn(reset=True, **CONN_INFO)) |
| 93 | + |
| 94 | + |
| 95 | +def test_drop_database(): |
| 96 | + schema = dj.Schema( |
| 97 | + PREFIX + "_drop_test", connection=dj.conn(reset=True, **CONN_INFO) |
| 98 | + ) |
| 99 | + assert_true(schema.exists) |
| 100 | + schema.drop() |
| 101 | + assert_false(schema.exists) |
| 102 | + schema.drop() # should do nothing |
| 103 | + |
| 104 | + |
| 105 | +def test_overlapping_name(): |
| 106 | + test_schema = dj.Schema( |
| 107 | + PREFIX + "_overlapping_schema", connection=dj.conn(**CONN_INFO) |
| 108 | + ) |
| 109 | + |
| 110 | + @test_schema |
| 111 | + class Unit(dj.Manual): |
| 112 | + definition = """ |
| 113 | + id: int # simple id |
| 114 | + """ |
| 115 | + |
| 116 | + # hack to update the locals dictionary |
| 117 | + locals() |
| 118 | + |
| 119 | + @test_schema |
| 120 | + class Cell(dj.Manual): |
| 121 | + definition = """ |
| 122 | + type: varchar(32) # type of cell |
| 123 | + """ |
| 124 | + |
| 125 | + class Unit(dj.Part): |
| 126 | + definition = """ |
| 127 | + -> master |
| 128 | + -> Unit |
| 129 | + """ |
| 130 | + |
| 131 | + test_schema.drop() |
| 132 | + |
| 133 | + |
| 134 | +def test_list_tables(): |
| 135 | + # https://github.com/datajoint/datajoint-python/issues/838 |
| 136 | + assert set( |
| 137 | + [ |
| 138 | + "reserved_word", |
| 139 | + "#l", |
| 140 | + "#a", |
| 141 | + "__d", |
| 142 | + "__b", |
| 143 | + "__b__c", |
| 144 | + "__e", |
| 145 | + "__e__f", |
| 146 | + "#outfit_launch", |
| 147 | + "#outfit_launch__outfit_piece", |
| 148 | + "#i_j", |
| 149 | + "#j_i", |
| 150 | + "#t_test_update", |
| 151 | + "#data_a", |
| 152 | + "#data_b", |
| 153 | + "f", |
| 154 | + "#argmax_test", |
| 155 | + "#website", |
| 156 | + "profile", |
| 157 | + "profile__website", |
| 158 | + ] |
| 159 | + ) == set(schema_simple.list_tables()) |
| 160 | + |
| 161 | + |
| 162 | +def test_schema_save(): |
| 163 | + assert_true("class Experiment(dj.Imported)" in schema.schema.code) |
| 164 | + assert_true("class Experiment(dj.Imported)" in schema_empty.schema.code) |
| 165 | + |
| 166 | + |
| 167 | +def test_uppercase_schema(): |
| 168 | + # https://github.com/datajoint/datajoint-python/issues/564 |
| 169 | + dj.conn(**CONN_INFO_ROOT, reset=True) |
| 170 | + schema1 = dj.Schema("Schema_A") |
| 171 | + |
| 172 | + @schema1 |
| 173 | + class Subject(dj.Manual): |
| 174 | + definition = """ |
| 175 | + name: varchar(32) |
| 176 | + """ |
| 177 | + |
| 178 | + Schema_A = dj.VirtualModule("Schema_A", "Schema_A") |
| 179 | + |
| 180 | + schema2 = dj.Schema("schema_b") |
| 181 | + |
| 182 | + @schema2 |
| 183 | + class Recording(dj.Manual): |
| 184 | + definition = """ |
| 185 | + -> Schema_A.Subject |
| 186 | + id: smallint |
| 187 | + """ |
| 188 | + |
| 189 | + schema2.drop() |
| 190 | + schema1.drop() |
0 commit comments