summaryrefslogtreecommitdiff
path: root/lib/libsqlite3/tool/genfkey.test
blob: 3c0073e953dc0a2e60b421181b631c3bcaa37b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

package require sqlite3

proc do_test {name cmd expected} {
  puts -nonewline "$name ..."
  set res [uplevel $cmd]
  if {$res eq $expected} {
    puts Ok
  } else {
    puts Error
    puts "  Got: $res"
    puts "  Expected: $expected"
    exit
  }
}

proc execsql {sql} {
  uplevel [list db eval $sql]
}

proc catchsql {sql} {
  set rc [catch {uplevel [list db eval $sql]} msg]
  list $rc $msg
}

file delete -force test.db test.db.journal
sqlite3 db test.db

# The following tests - genfkey-1.* - test RESTRICT foreign keys.
#
do_test genfkey-1.1 {
  execsql {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
    CREATE TABLE t2(e REFERENCES t1, f);
    CREATE TABLE t3(g, h, i, FOREIGN KEY (h, i) REFERENCES t1(b, c));
  }
} {}
do_test genfkey-1.2 {
  execsql [exec ./sqlite3 test.db .genfkey]
} {}
do_test genfkey-1.3 {
  catchsql { INSERT INTO t2 VALUES(1, 2) }
} {1 {constraint failed}}
do_test genfkey-1.4 {
  execsql {
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t2 VALUES(1, 2);
  }
} {}
do_test genfkey-1.5 {
  execsql { INSERT INTO t2 VALUES(NULL, 3) }
} {}
do_test genfkey-1.6 {
  catchsql { UPDATE t2 SET e = 5 WHERE e IS NULL }
} {1 {constraint failed}}
do_test genfkey-1.7 {
  execsql { UPDATE t2 SET e = 1 WHERE e IS NULL }
} {}
do_test genfkey-1.8 {
  execsql { UPDATE t2 SET e = NULL WHERE f = 3 }
} {}
do_test genfkey-1.9 {
  catchsql { UPDATE t1 SET a = 10 }
} {1 {constraint failed}}
do_test genfkey-1.9a {
  catchsql { UPDATE t1 SET a = NULL }
} {1 {datatype mismatch}}
do_test genfkey-1.10 {
  catchsql { DELETE FROM t1 }
} {1 {constraint failed}}
do_test genfkey-1.11 {
  execsql { UPDATE t2 SET e = NULL }
} {}
do_test genfkey-1.12 {
  execsql { 
    UPDATE t1 SET a = 10 ;
    DELETE FROM t1;
    DELETE FROM t2;
  }
} {}

do_test genfkey-1.13 {
  execsql {
    INSERT INTO t3 VALUES(1, NULL, NULL);
    INSERT INTO t3 VALUES(1, 2, NULL);
    INSERT INTO t3 VALUES(1, NULL, 3);
  }
} {}
do_test genfkey-1.14 {
  catchsql { INSERT INTO t3 VALUES(3, 1, 4) }
} {1 {constraint failed}}
do_test genfkey-1.15 {
  execsql { 
    INSERT INTO t1 VALUES(1, 1, 4);
    INSERT INTO t3 VALUES(3, 1, 4);
  }
} {}
do_test genfkey-1.16 {
  catchsql { DELETE FROM t1 }
} {1 {constraint failed}}
do_test genfkey-1.17 {
  catchsql { UPDATE t1 SET b = 10}
} {1 {constraint failed}}
do_test genfkey-1.18 {
  execsql { UPDATE t1 SET a = 10}
} {}
do_test genfkey-1.19 {
  catchsql { UPDATE t3 SET h = 'hello' WHERE i = 3}
} {1 {constraint failed}}

do_test genfkey-1.X {
  execsql {
    DROP TABLE t1;
    DROP TABLE t2;
    DROP TABLE t3;
  }
} {}

# The following tests - genfkey-2.* - test CASCADE foreign keys.
#
do_test genfkey-2.1 {
  execsql {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
    CREATE TABLE t2(e REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE, f);
    CREATE TABLE t3(g, h, i, 
        FOREIGN KEY (h, i) 
        REFERENCES t1(b, c) ON UPDATE CASCADE ON DELETE CASCADE
    );
  }
} {}
do_test genfkey-2.2 {
  execsql [exec ./sqlite3 test.db .genfkey]
} {}
do_test genfkey-2.3 {
  execsql {
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t1 VALUES(4, 5, 6);
    INSERT INTO t2 VALUES(1, 'one');
    INSERT INTO t2 VALUES(4, 'four');
  }
} {}
do_test genfkey-2.4 {
  execsql {
    UPDATE t1 SET a = 2 WHERE a = 1;
    SELECT * FROM t2;
  }
} {2 one 4 four}
do_test genfkey-2.5 {
  execsql {
    DELETE FROM t1 WHERE a = 4;
    SELECT * FROM t2;
  }
} {2 one}
do_test genfkey-2.6 {
  execsql {
    INSERT INTO t3 VALUES('hello', 2, 3);
    UPDATE t1 SET c = 2;
    SELECT * FROM t3;
  }
} {hello 2 2}
do_test genfkey-2.7 {
  execsql {
    DELETE FROM t1;
    SELECT * FROM t3;
  }
} {}
do_test genfkey-2.X {
  execsql {
    DROP TABLE t1;
    DROP TABLE t2;
    DROP TABLE t3;
  }
} {}


# The following tests - genfkey-3.* - test SET NULL foreign keys.
#
do_test genfkey-3.1 {
  execsql {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(c, b));
    CREATE TABLE t2(e REFERENCES t1 ON UPDATE SET NULL ON DELETE SET NULL, f);
    CREATE TABLE t3(g, h, i, 
        FOREIGN KEY (h, i) 
        REFERENCES t1(b, c) ON UPDATE SET NULL ON DELETE SET NULL
    );
  }
} {}
do_test genfkey-3.2 {
  execsql [exec ./sqlite3 test.db .genfkey]
} {}
do_test genfkey-3.3 {
  execsql {
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t1 VALUES(4, 5, 6);
    INSERT INTO t2 VALUES(1, 'one');
    INSERT INTO t2 VALUES(4, 'four');
  }
} {}
do_test genfkey-3.4 {
  execsql {
    UPDATE t1 SET a = 2 WHERE a = 1;
    SELECT * FROM t2;
  }
} {{} one 4 four}
do_test genfkey-3.5 {
  execsql {
    DELETE FROM t1 WHERE a = 4;
    SELECT * FROM t2;
  }
} {{} one {} four}
do_test genfkey-3.6 {
  execsql {
    INSERT INTO t3 VALUES('hello', 2, 3);
    UPDATE t1 SET c = 2;
    SELECT * FROM t3;
  }
} {hello {} {}}
do_test genfkey-2.7 {
  execsql {
    UPDATE t3 SET h = 2, i = 2;
    DELETE FROM t1;
    SELECT * FROM t3;
  }
} {hello {} {}}
do_test genfkey-3.X {
  execsql {
    DROP TABLE t1;
    DROP TABLE t2;
    DROP TABLE t3;
  }
} {}

# The following tests - genfkey-4.* - test that errors in the schema
# are detected correctly.
#
do_test genfkey-4.1 {
  execsql {
    CREATE TABLE t1(a REFERENCES nosuchtable, b);
    CREATE TABLE t2(a REFERENCES t1, b);

    CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b));
    CREATE TABLE t4(a, b, c, FOREIGN KEY(c, b) REFERENCES t3);

    CREATE TABLE t5(a REFERENCES t4(d), b, c);
    CREATE TABLE t6(a REFERENCES t4(a), b, c);
    CREATE TABLE t7(a REFERENCES t3(a), b, c);
    CREATE TABLE t8(a REFERENCES nosuchtable(a), b, c);
  }
} {}

do_test genfkey-4.X {
  set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
  list $rc $msg
} "1 {[string trim {
Error in table t5: foreign key columns do not exist
Error in table t8: foreign key columns do not exist
Error in table t4: implicit mapping to composite primary key
Error in table t1: implicit mapping to non-existant primary key
Error in table t2: implicit mapping to non-existant primary key
Error in table t6: foreign key is not unique
Error in table t7: foreign key is not unique
}]}"

# Test that ticket #3800 has been resolved.
#
do_test genfkey-5.1 {
  execsql {
    DROP TABLE t1; DROP TABLE t2; DROP TABLE t3;
    DROP TABLE t4; DROP TABLE t5; DROP TABLE t6;
    DROP TABLE t7; DROP TABLE t8;
  }
} {}
do_test genfkey-5.2 {
  execsql {
    CREATE TABLE "t.3" (c1 PRIMARY KEY);
    CREATE TABLE t13 (c1, foreign key(c1) references "t.3"(c1));
  }
} {}
do_test genfkey-5.3 {
  set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
} {0}
do_test genfkey-5.4 {
  db eval $msg
} {}
do_test genfkey-5.5 {
  catchsql { INSERT INTO t13 VALUES(1) }
} {1 {constraint failed}}
do_test genfkey-5.5 {
  catchsql { 
    INSERT INTO "t.3" VALUES(1);
    INSERT INTO t13 VALUES(1);
  }
} {0 {}}

# Test also column names that require quoting.
do_test genfkey-6.1 {
  execsql {
    DROP TABLE "t.3";
    DROP TABLE t13;
    CREATE TABLE p(
      "a.1 first", "b.2 second", 
      UNIQUE("a.1 first", "b.2 second")
    );
    CREATE TABLE c(
      "c.1 I", "d.2 II", 
        FOREIGN KEY("c.1 I", "d.2 II") 
        REFERENCES p("a.1 first", "b.2 second")
        ON UPDATE CASCADE ON DELETE CASCADE
    );
  }
} {}
do_test genfkey-6.2 {
  set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
} {0}
do_test genfkey-6.3 {
  execsql $msg
  execsql {
    INSERT INTO p VALUES('A', 'B');
    INSERT INTO p VALUES('C', 'D');
    INSERT INTO c VALUES('A', 'B');
    INSERT INTO c VALUES('C', 'D');
    UPDATE p SET "a.1 first" = 'X' WHERE rowid = 1;
    DELETE FROM p WHERE rowid = 2;
  }
  execsql { SELECT * FROM c }
} {X B}

do_test genfkey-6.4 {
  execsql {
    DROP TABLE p;
    DROP TABLE c;
    CREATE TABLE parent("a.1", PRIMARY KEY("a.1"));
    CREATE TABLE child("b.2", FOREIGN KEY("b.2") REFERENCES parent("a.1"));
  }
  set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
} {0}
do_test genfkey-6.5 {
  execsql $msg
  execsql {
    INSERT INTO parent VALUES(1);
    INSERT INTO child VALUES(1);
  }
  catchsql { UPDATE parent SET "a.1"=0 }
} {1 {constraint failed}}
do_test genfkey-6.6 {
  catchsql { UPDATE child SET "b.2"=7 }
} {1 {constraint failed}}
do_test genfkey-6.7 {
  execsql {
    SELECT * FROM parent;
    SELECT * FROM child;
  }
} {1 1}