Skip to content

Commit 8a9d0ee

Browse files
committed
Clean up
1 parent 895971c commit 8a9d0ee

File tree

1 file changed

+35
-49
lines changed

1 file changed

+35
-49
lines changed

tests/test_alter.py

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import datajoint as dj
44
from . import schema as schema_any_module, schema_alter as schema_alter_module, PREFIX
5+
from .schema_alter import Parent, Experiment
56

67
COMBINED_CONTEXT = {
78
**schema_any_module.LOCALS_ANY,
@@ -11,12 +12,8 @@
1112

1213
@pytest.fixture
1314
def schema_alter(connection_test):
14-
context = {
15-
**schema_any_module.LOCALS_ANY,
16-
# **schema_alter_module.LOCALS_ALTER,
17-
}
1815
schema_any = dj.Schema(
19-
PREFIX + "_test1", context=context, connection=connection_test
16+
PREFIX + "_alter", context=schema_any_module.LOCALS_ANY, connection=connection_test
2017
)
2118
schema_any(schema_any_module.TTest)
2219
schema_any(schema_any_module.TTest2)
@@ -55,64 +52,53 @@ def schema_alter(connection_test):
5552
schema_any(schema_any_module.Stimulus)
5653
schema_any(schema_any_module.Longblob)
5754

58-
schema_any(schema_alter_module.Experiment, context=schema_alter_module.LOCALS_ALTER)
59-
schema_any(schema_alter_module.Parent, context=schema_alter_module.LOCALS_ALTER)
55+
# Add nodes from schema_alter_module
56+
schema_any(Experiment, context=schema_alter_module.LOCALS_ALTER)
57+
schema_any(Parent, context=schema_alter_module.LOCALS_ALTER)
6058

6159
yield schema_any
6260
schema_any.drop()
6361

6462

65-
# @pytest.fixture
66-
def _schema_alter(schema_any):
67-
context = {
68-
**schema_any_module.LOCALS_ANY,
69-
**schema_alter_module.LOCALS_ALTER,
70-
}
71-
schema_any(schema_alter_module.Experiment, context=context)
72-
schema_any(schema_alter_module.Parent, context=context)
73-
yield schema_any
74-
schema_any.drop()
75-
76-
77-
78-
def test_alter(schema_alter):
79-
schema = schema_alter
80-
original = schema.connection.query(
81-
"SHOW CREATE TABLE " + schema_alter_module.Experiment.full_table_name
82-
).fetchone()[1]
83-
schema_alter_module.Experiment.definition = schema_alter_module.Experiment.definition1
84-
schema_alter_module.Experiment.alter(prompt=False, context=COMBINED_CONTEXT)
85-
altered = schema.connection.query(
86-
"SHOW CREATE TABLE " + schema_alter_module.Experiment.full_table_name
87-
).fetchone()[1]
88-
assert original != altered
89-
schema_alter_module.Experiment.definition = schema_alter_module.Experiment.original_definition
90-
schema_alter_module.Experiment().alter(prompt=False, context=COMBINED_CONTEXT)
91-
restored = schema.connection.query(
92-
"SHOW CREATE TABLE " + schema_alter_module.Experiment.full_table_name
93-
).fetchone()[1]
94-
assert altered != restored
95-
assert original == restored
96-
63+
class TestAlter:
9764

98-
def test_alter_part(schema_alter):
99-
# https://github.com/datajoint/datajoint-python/issues/936
100-
schema = schema_alter
65+
def test_alter(self, schema_alter):
66+
original = schema_alter.connection.query(
67+
"SHOW CREATE TABLE " + Experiment.full_table_name
68+
).fetchone()[1]
69+
Experiment.definition = Experiment.definition1
70+
Experiment.alter(prompt=False, context=COMBINED_CONTEXT)
71+
altered = schema_alter.connection.query(
72+
"SHOW CREATE TABLE " + Experiment.full_table_name
73+
).fetchone()[1]
74+
assert original != altered
75+
Experiment.definition = Experiment.original_definition
76+
Experiment().alter(prompt=False, context=COMBINED_CONTEXT)
77+
restored = schema_alter.connection.query(
78+
"SHOW CREATE TABLE " + Experiment.full_table_name
79+
).fetchone()[1]
80+
assert altered != restored
81+
assert original == restored
10182

102-
def verify_alter(table, attribute_sql):
103-
definition_original = schema.connection.query(
83+
def verify_alter(self, schema_alter, table, attribute_sql):
84+
definition_original = schema_alter.connection.query(
10485
f"SHOW CREATE TABLE {table.full_table_name}"
10586
).fetchone()[1]
10687
table.definition = table.definition_new
10788
table.alter(prompt=False)
108-
definition_new = schema.connection.query(
89+
definition_new = schema_alter.connection.query(
10990
f"SHOW CREATE TABLE {table.full_table_name}"
11091
).fetchone()[1]
11192
assert (
11293
re.sub(f"{attribute_sql},\n ", "", definition_new) == definition_original
11394
)
11495

115-
verify_alter(table=schema_alter_module.Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL")
116-
verify_alter(
117-
table=schema_alter_module.Parent.Grandchild, attribute_sql="`grandchild_id` .* DEFAULT NULL"
118-
)
96+
def test_alter_part(self, schema_alter):
97+
"""
98+
https://github.com/datajoint/datajoint-python/issues/936
99+
"""
100+
self.verify_alter(schema_alter, table=Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL")
101+
self.verify_alter(
102+
schema_alter,
103+
table=Parent.Grandchild, attribute_sql="`grandchild_id` .* DEFAULT NULL"
104+
)

0 commit comments

Comments
 (0)