|
2 | 2 | import re |
3 | 3 | import datajoint as dj |
4 | 4 | from . import schema as schema_any_module, schema_alter as schema_alter_module, PREFIX |
| 5 | +from .schema_alter import Parent, Experiment |
5 | 6 |
|
6 | 7 | COMBINED_CONTEXT = { |
7 | 8 | **schema_any_module.LOCALS_ANY, |
|
11 | 12 |
|
12 | 13 | @pytest.fixture |
13 | 14 | def schema_alter(connection_test): |
14 | | - context = { |
15 | | - **schema_any_module.LOCALS_ANY, |
16 | | - # **schema_alter_module.LOCALS_ALTER, |
17 | | - } |
18 | 15 | schema_any = dj.Schema( |
19 | | - PREFIX + "_test1", context=context, connection=connection_test |
| 16 | + PREFIX + "_alter", context=schema_any_module.LOCALS_ANY, connection=connection_test |
20 | 17 | ) |
21 | 18 | schema_any(schema_any_module.TTest) |
22 | 19 | schema_any(schema_any_module.TTest2) |
@@ -55,64 +52,53 @@ def schema_alter(connection_test): |
55 | 52 | schema_any(schema_any_module.Stimulus) |
56 | 53 | schema_any(schema_any_module.Longblob) |
57 | 54 |
|
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) |
60 | 58 |
|
61 | 59 | yield schema_any |
62 | 60 | schema_any.drop() |
63 | 61 |
|
64 | 62 |
|
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: |
97 | 64 |
|
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 |
101 | 82 |
|
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( |
104 | 85 | f"SHOW CREATE TABLE {table.full_table_name}" |
105 | 86 | ).fetchone()[1] |
106 | 87 | table.definition = table.definition_new |
107 | 88 | table.alter(prompt=False) |
108 | | - definition_new = schema.connection.query( |
| 89 | + definition_new = schema_alter.connection.query( |
109 | 90 | f"SHOW CREATE TABLE {table.full_table_name}" |
110 | 91 | ).fetchone()[1] |
111 | 92 | assert ( |
112 | 93 | re.sub(f"{attribute_sql},\n ", "", definition_new) == definition_original |
113 | 94 | ) |
114 | 95 |
|
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