|
| 1 | +import pytest |
| 2 | +import re |
| 3 | +import datajoint as dj |
| 4 | +from . import schema as schema_any_module, PREFIX |
| 5 | + |
| 6 | + |
| 7 | +class Experiment(dj.Imported): |
| 8 | + original_definition = """ # information about experiments |
| 9 | + -> Subject |
| 10 | + experiment_id :smallint # experiment number for this subject |
| 11 | + --- |
| 12 | + experiment_date :date # date when experiment was started |
| 13 | + -> [nullable] User |
| 14 | + data_path="" :varchar(255) # file path to recorded data |
| 15 | + notes="" :varchar(2048) # e.g. purpose of experiment |
| 16 | + entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp |
| 17 | + """ |
| 18 | + |
| 19 | + definition1 = """ # Experiment |
| 20 | + -> Subject |
| 21 | + experiment_id :smallint # experiment number for this subject |
| 22 | + --- |
| 23 | + data_path : int # some number |
| 24 | + extra=null : longblob # just testing |
| 25 | + -> [nullable] User |
| 26 | + subject_notes=null :varchar(2048) # {notes} e.g. purpose of experiment |
| 27 | + entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp |
| 28 | + """ |
| 29 | + |
| 30 | + |
| 31 | +class Parent(dj.Manual): |
| 32 | + definition = """ |
| 33 | + parent_id: int |
| 34 | + """ |
| 35 | + |
| 36 | + class Child(dj.Part): |
| 37 | + definition = """ |
| 38 | + -> Parent |
| 39 | + """ |
| 40 | + definition_new = """ |
| 41 | + -> master |
| 42 | + --- |
| 43 | + child_id=null: int |
| 44 | + """ |
| 45 | + |
| 46 | + class Grandchild(dj.Part): |
| 47 | + definition = """ |
| 48 | + -> master.Child |
| 49 | + """ |
| 50 | + definition_new = """ |
| 51 | + -> master.Child |
| 52 | + --- |
| 53 | + grandchild_id=null: int |
| 54 | + """ |
| 55 | + |
| 56 | + |
| 57 | +LOCALS_ALTER = {"Experiment": Experiment, "Parent": Parent} |
| 58 | +COMBINED_CONTEXT = { |
| 59 | + **schema_any_module.LOCALS_ANY, |
| 60 | + **LOCALS_ALTER, |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def schema_alter(connection_test, schema_any): |
| 66 | + # Overwrite Experiment and Parent nodes |
| 67 | + schema_any(Experiment, context=LOCALS_ALTER) |
| 68 | + schema_any(Parent, context=LOCALS_ALTER) |
| 69 | + yield schema_any |
| 70 | + schema_any.drop() |
| 71 | + |
| 72 | + |
| 73 | +class TestAlter: |
| 74 | + def test_alter(self, schema_alter): |
| 75 | + original = schema_alter.connection.query( |
| 76 | + "SHOW CREATE TABLE " + Experiment.full_table_name |
| 77 | + ).fetchone()[1] |
| 78 | + Experiment.definition = Experiment.definition1 |
| 79 | + Experiment.alter(prompt=False, context=COMBINED_CONTEXT) |
| 80 | + altered = schema_alter.connection.query( |
| 81 | + "SHOW CREATE TABLE " + Experiment.full_table_name |
| 82 | + ).fetchone()[1] |
| 83 | + assert original != altered |
| 84 | + Experiment.definition = Experiment.original_definition |
| 85 | + Experiment().alter(prompt=False, context=COMBINED_CONTEXT) |
| 86 | + restored = schema_alter.connection.query( |
| 87 | + "SHOW CREATE TABLE " + Experiment.full_table_name |
| 88 | + ).fetchone()[1] |
| 89 | + assert altered != restored |
| 90 | + assert original == restored |
| 91 | + |
| 92 | + def verify_alter(self, schema_alter, table, attribute_sql): |
| 93 | + definition_original = schema_alter.connection.query( |
| 94 | + f"SHOW CREATE TABLE {table.full_table_name}" |
| 95 | + ).fetchone()[1] |
| 96 | + table.definition = table.definition_new |
| 97 | + table.alter(prompt=False) |
| 98 | + definition_new = schema_alter.connection.query( |
| 99 | + f"SHOW CREATE TABLE {table.full_table_name}" |
| 100 | + ).fetchone()[1] |
| 101 | + assert ( |
| 102 | + re.sub(f"{attribute_sql},\n ", "", definition_new) == definition_original |
| 103 | + ) |
| 104 | + |
| 105 | + def test_alter_part(self, schema_alter): |
| 106 | + """ |
| 107 | + https://github.com/datajoint/datajoint-python/issues/936 |
| 108 | + """ |
| 109 | + self.verify_alter( |
| 110 | + schema_alter, table=Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL" |
| 111 | + ) |
| 112 | + self.verify_alter( |
| 113 | + schema_alter, |
| 114 | + table=Parent.Grandchild, |
| 115 | + attribute_sql="`grandchild_id` .* DEFAULT NULL", |
| 116 | + ) |
0 commit comments