1
1
from nose .tools import assert_equal , assert_not_equal
2
+ import re
2
3
from .schema import *
3
4
4
5
@@ -28,6 +29,33 @@ class Experiment(dj.Imported):
28
29
"""
29
30
30
31
32
+ @schema
33
+ class Parent (dj .Manual ):
34
+ definition = """
35
+ parent_id: int
36
+ """
37
+
38
+ class Child (dj .Part ):
39
+ definition = """
40
+ -> Parent
41
+ """
42
+ definition_new = """
43
+ -> master
44
+ ---
45
+ child_id=null: int
46
+ """
47
+
48
+ class Grandchild (dj .Part ):
49
+ definition = """
50
+ -> master.Child
51
+ """
52
+ definition_new = """
53
+ -> master.Child
54
+ ---
55
+ grandchild_id=null: int
56
+ """
57
+
58
+
31
59
def test_alter ():
32
60
original = schema .connection .query ("SHOW CREATE TABLE " + Experiment .full_table_name ).fetchone ()[1 ]
33
61
Experiment .definition = Experiment .definition1
@@ -41,24 +69,23 @@ def test_alter():
41
69
assert_equal (original , restored )
42
70
43
71
44
- @schema
45
- class AlterMaster (dj .Manual ):
46
- definition = """
47
- master_id : int
48
- """
72
+ def test_alter_part ():
73
+ # https://github.com/datajoint/datajoint-python/issues/936
49
74
50
- class AlterPart (dj .Part ):
51
- definition = """
52
- -> master
53
- """
54
-
55
- definition1 = """
56
- -> AlterMaster
57
- """
75
+ def verify_alter (table , attribute_sql ):
76
+ definition_original = schema .connection .query (
77
+ f"SHOW CREATE TABLE { table .full_table_name } "
78
+ ).fetchone ()[1 ]
79
+ table .definition = table .definition_new
80
+ table .alter (prompt = False )
81
+ definition_new = schema .connection .query (
82
+ f"SHOW CREATE TABLE { table .full_table_name } "
83
+ ).fetchone ()[1 ]
84
+ assert (
85
+ re .sub (f"{ attribute_sql } ,\n " , "" , definition_new ) == definition_original
86
+ )
58
87
59
- def test_alter_part_master_keyword ():
60
- original = schema .connection .query ("SHOW CREATE TABLE " + AlterMaster .AlterPart .full_table_name ).fetchone ()[1 ]
61
- AlterMaster .AlterPart .definition = AlterMaster .AlterPart .definition1
62
- AlterMaster .AlterPart .alter (prompt = False )
63
- altered = schema .connection .query ("SHOW CREATE TABLE " + AlterMaster .AlterPart .full_table_name ).fetchone ()[1 ]
64
- assert_equal (original , altered )
88
+ verify_alter (table = Parent .Child , attribute_sql = "`child_id` .* DEFAULT NULL" )
89
+ verify_alter (
90
+ table = Parent .Grandchild , attribute_sql = "`grandchild_id` .* DEFAULT NULL"
91
+ )
0 commit comments