@@ -3,7 +3,8 @@ use warnings;
3
3
4
4
use DBIx::DataModel::Schema::Generator;
5
5
6
- use constant NTESTS => 7;
6
+ use constant NTESTS => 17;
7
+
7
8
use Test::More tests => NTESTS;
8
9
9
10
@@ -45,6 +46,26 @@ SKIP: {
45
46
emp_id INTEGER NOT NULL REFERENCES employee(emp_id),
46
47
status_name TEXT
47
48
);
49
+
50
+ -- two foreign keys to one reftable, one cascades
51
+ CREATE TABLE fk_reftable_1 (
52
+ emp_id_status INTEGER NOT NULL REFERENCES employee_status(emp_id_status),
53
+ emp_id INTEGER NOT NULL REFERENCES employee_status(emp_id) ON DELETE CASCADE
54
+ );
55
+
56
+ -- two foreign keys to one reftable, both cascade
57
+ CREATE TABLE fk_reftable_2 (
58
+ emp_id_status INTEGER NOT NULL REFERENCES employee_status(emp_id_status) ON DELETE CASCADE,
59
+ emp_id INTEGER NOT NULL REFERENCES employee_status(emp_id) ON DELETE CASCADE
60
+ );
61
+
62
+ -- two foreign keys to two reftables, both cascade
63
+ CREATE TABLE fk_reftable_3 (
64
+ emp_id_status INTEGER NOT NULL REFERENCES employee_status(emp_id_status) ON DELETE CASCADE,
65
+ emp_id INTEGER NOT NULL REFERENCES employee(emp_id) ON DELETE CASCADE
66
+ );
67
+
68
+
48
69
} );
49
70
50
71
my $generator = DBIx::DataModel::Schema::Generator-> new(
@@ -54,11 +75,98 @@ SKIP: {
54
75
$generator -> parse_DBI($dbh );
55
76
my $perl_code = $generator -> perl_code;
56
77
57
- like($perl_code , qr { Table\( qw/Activity} , " Table Activity" );
58
- like($perl_code , qr { Table\( qw/ActivityEvent} , " Table ActivityEvent" );
59
- like($perl_code , qr { Composition.*?activity_events} s , " Composition" );
60
- like($perl_code , qr { Association.*?activit(ie|y)s} s , " Association" );
61
- like($perl_code , qr { employee_2} s , " avoid duplicate associations" );
78
+ sub match_entry {
79
+
80
+ # $type, [ $class, @etc ], [ ... ], $msg
81
+ my $msg = pop ;
82
+ my $type = quotemeta (shift );
83
+
84
+ # match start and end of an line, depends if there are one or two lines per entry
85
+ my ( $start , $end ) = ( @_ > 1) ? ( qr {\[ qw/\s *} , qr {\s */\] } ) : ( qr { qw/\s *} , qr {\s */} ) ;
86
+
87
+ my $re = join (' ' ,
88
+ qr {$type \s *\(\s *} ,
89
+ join ( qr {\s *,\s *} , # join multiple lines
90
+ map {
91
+ join ( ' ' ,
92
+ $start ,
93
+ join ( qr /\s +/ ,
94
+ map { defined ($_ ) ? quotemeta ( $_ ) # so multiplicity of '*' passes through
95
+ : qr { [^)]*?} # undef means match to end of line (matches any char except right paren)
96
+ } @{$_ },
97
+ ),
98
+ $end ,
99
+ )
100
+ } @_ # iterate over lines
101
+ ),
102
+ );
103
+
104
+
105
+ like( $perl_code , qr /$re / , $msg );
106
+ }
107
+
108
+ # ensure Tables are created
109
+ match_entry( ' Table' , [ $_ , undef ], " created Table $_ " )
110
+ foreach qw[ Activity ActivityEvent Department Employee EmployeeStatus FkReftable1 ] ;
111
+
112
+ match_entry( ' Association' ,
113
+ [ qw( Employee employee 1 emp_id emp_id ) ],
114
+ [ qw( Activity activities * supervisor emp_id ) ],
115
+ ' Merged Association' ,
116
+ );
117
+
118
+
119
+ match_entry( ' Association' ,
120
+ [ qw( Department department 1 dpt_id ) ],
121
+ [ qw( Activity activities * dpt_id ) ],
122
+ ' Association: Department, Activity' ,
123
+ );
124
+
125
+
126
+ match_entry( ' Composition' ,
127
+ [ qw( Activity activity 1 act_id ) ],
128
+ [ qw( ActivityEvent activity_events * act_id ) ],
129
+ ' Composition Activity, ActivityEvent'
130
+ );
131
+
132
+ match_entry( ' Association' ,
133
+ [ qw( Employee employee 1 emp_id ) ],
134
+ [ qw( EmployeeStatus employee_statuses * emp_id ) ],
135
+ ' Association: Employee, EmployeeStatus' ,
136
+ );
137
+
138
+ match_entry( ' Association' ,
139
+ [ qw( EmployeeStatus employee_status 1 emp_id_status ) ],
140
+ [ qw( FkReftable1 fk_reftable_1s * emp_id_status ) ],
141
+ ' Association: two foreign keys to one reftable, one cascades' ,
142
+ );
143
+
144
+ # checks for duplicate role as well.
145
+ match_entry( ' Composition' ,
146
+ [ qw( EmployeeStatus employee_status_2 1 emp_id ) ],
147
+ [ qw( FkReftable1 fk_reftable_1s_2 * emp_id ) ],
148
+ ' Composition: two foreign keys to one reftable, one cascades' ,
149
+ );
150
+
151
+ match_entry( ' Composition' ,
152
+ [ qw( EmployeeStatus employee_status 1 emp_id emp_id_status ) ],
153
+ [ qw( FkReftable2 fk_reftable_2s * emp_id emp_id_status ) ],
154
+ ' Merged Composition: two foreign keys to one reftable, both cascade' ,
155
+ );
156
+
157
+ match_entry( ' Association' ,
158
+ [ qw( EmployeeStatus employee_status 1 emp_id_status ) ],
159
+ [ qw( FkReftable3 fk_reftable_3s * emp_id_status ) ],
160
+ ' Forced Association: two foreign keys to two reftables, both cascade (1)' ,
161
+ );
162
+
163
+
164
+ match_entry( ' Association' ,
165
+ [ qw( Employee employee 1 emp_id ) ],
166
+ [ qw( FkReftable3 fk_reftable_3s * emp_id ) ],
167
+ ' Forced Association: two foreign keys to two reftables, both cascade (2)' ,
168
+ );
169
+
62
170
63
171
# diag($perl_code);
64
172
@@ -78,8 +186,8 @@ SKIP: {
78
186
);
79
187
$generator -> parse_DBI($dbh );
80
188
$perl_code = $generator -> perl_code;
81
- like($perl_code , qr { Table\( qw/Foo} , " Table foo" );
82
- like($perl_code , qr { Table\( qw/Bar} , " Table bar" );
189
+ like($perl_code , qr { Table\( qw/Foo} , " created Table foo" );
190
+ like($perl_code , qr { Table\( qw/Bar} , " created Table bar" );
83
191
}
84
192
85
193
0 commit comments