Annotation of embedaddon/sqlite3/ext/rtree/rtree1.test, revision 1.1.1.1
1.1 misho 1: # 2008 Feb 19
2: #
3: # The author disclaims copyright to this source code. In place of
4: # a legal notice, here is a blessing:
5: #
6: # May you do good and not evil.
7: # May you find forgiveness for yourself and forgive others.
8: # May you share freely, never taking more than you give.
9: #
10: #***********************************************************************
11: #
12: # The focus of this file is testing the r-tree extension.
13: #
14:
15: if {![info exists testdir]} {
16: set testdir [file join [file dirname [info script]] .. .. test]
17: }
18: source [file join [file dirname [info script]] rtree_util.tcl]
19: source $testdir/tester.tcl
20:
21: # Test plan:
22: #
23: # rtree-1.*: Creating/destroying r-tree tables.
24: # rtree-2.*: Test the implicit constraints - unique rowid and
25: # (coord[N]<=coord[N+1]) for even values of N. Also
26: # automatic assigning of rowid values.
27: # rtree-3.*: Linear scans of r-tree data.
28: # rtree-4.*: Test INSERT
29: # rtree-5.*: Test DELETE
30: # rtree-6.*: Test UPDATE
31: # rtree-7.*: Test renaming an r-tree table.
32: # rtree-8.*: Test constrained scans of r-tree data.
33: #
34: # rtree-12.*: Test that on-conflict clauses are supported.
35: #
36:
37: ifcapable !rtree {
38: finish_test
39: return
40: }
41:
42: #----------------------------------------------------------------------------
43: # Test cases rtree-1.* test CREATE and DROP table statements.
44: #
45:
46: # Test creating and dropping an rtree table.
47: #
48: do_test rtree-1.1.1 {
49: execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
50: } {}
51: do_test rtree-1.1.2 {
52: execsql { SELECT name FROM sqlite_master ORDER BY name }
53: } {t1 t1_node t1_parent t1_rowid}
54: do_test rtree-1.1.3 {
55: execsql {
56: DROP TABLE t1;
57: SELECT name FROM sqlite_master ORDER BY name;
58: }
59: } {}
60:
61: # Test creating and dropping an rtree table with an odd name in
62: # an attached database.
63: #
64: do_test rtree-1.2.1 {
65: file delete -force test2.db
66: execsql {
67: ATTACH 'test2.db' AS aux;
68: CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
69: }
70: } {}
71: do_test rtree-1.2.2 {
72: execsql { SELECT name FROM sqlite_master ORDER BY name }
73: } {}
74: do_test rtree-1.2.3 {
75: execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
76: } {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
77: do_test rtree-1.2.4 {
78: execsql {
79: DROP TABLE aux.'a" "b';
80: SELECT name FROM aux.sqlite_master ORDER BY name;
81: }
82: } {}
83:
84: # Test that the logic for checking the number of columns specified
85: # for an rtree table. Acceptable values are odd numbers between 3 and
86: # 11, inclusive.
87: #
88: set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
89: for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
90:
91: set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
92:
93: set X {0 {}}
94: if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
95: if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
96: if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
97:
98: do_test rtree-1.3.$nCol {
99: catchsql "
100: CREATE VIRTUAL TABLE t1 USING rtree($columns);
101: "
102: } $X
103:
104: catchsql { DROP TABLE t1 }
105: }
106:
107: # Test that it is possible to open an existing database that contains
108: # r-tree tables.
109: #
110: do_test rtree-1.4.1 {
111: execsql {
112: CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
113: INSERT INTO t1 VALUES(1, 5.0, 10.0);
114: INSERT INTO t1 VALUES(2, 15.0, 20.0);
115: }
116: } {}
117: do_test rtree-1.4.2 {
118: db close
119: sqlite3 db test.db
120: execsql { SELECT * FROM t1 ORDER BY ii }
121: } {1 5.0 10.0 2 15.0 20.0}
122: do_test rtree-1.4.3 {
123: execsql { DROP TABLE t1 }
124: } {}
125:
126: # Test that it is possible to create an r-tree table with ridiculous
127: # column names.
128: #
129: do_test rtree-1.5.1 {
130: execsql {
131: CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
132: INSERT INTO t1 VALUES(1, 2, 3);
133: SELECT "the key", "x dim.", "x2'dim" FROM t1;
134: }
135: } {1 2.0 3.0}
136: do_test rtree-1.5.1 {
137: execsql { DROP TABLE t1 }
138: } {}
139:
140: # Force the r-tree constructor to fail.
141: #
142: do_test rtree-1.6.1 {
143: execsql { CREATE TABLE t1_rowid(a); }
144: catchsql {
145: CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
146: }
147: } {1 {table "t1_rowid" already exists}}
148: do_test rtree-1.6.1 {
149: execsql { DROP TABLE t1_rowid }
150: } {}
151:
152: #----------------------------------------------------------------------------
153: # Test cases rtree-2.*
154: #
155: do_test rtree-2.1.1 {
156: execsql {
157: CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
158: SELECT * FROM t1;
159: }
160: } {}
161:
162: do_test rtree-2.1.2 {
163: execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
164: execsql { SELECT * FROM t1 }
165: } {1 1.0 3.0 2.0 4.0}
166: do_test rtree-2.1.3 {
167: execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
168: execsql { SELECT rowid FROM t1 ORDER BY rowid }
169: } {1 2}
170: do_test rtree-2.1.3 {
171: execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
172: execsql { SELECT ii FROM t1 ORDER BY ii }
173: } {1 2 3}
174:
175: do_test rtree-2.2.1 {
176: catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
177: } {1 {constraint failed}}
178: do_test rtree-2.2.2 {
179: catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
180: } {1 {constraint failed}}
181: do_test rtree-2.2.3 {
182: catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
183: } {1 {constraint failed}}
184: do_test rtree-2.2.4 {
185: execsql { SELECT ii FROM t1 ORDER BY ii }
186: } {1 2 3}
187:
188: do_test rtree-2.X {
189: execsql { DROP TABLE t1 }
190: } {}
191:
192: #----------------------------------------------------------------------------
193: # Test cases rtree-3.* test linear scans of r-tree table data. To test
194: # this we have to insert some data into an r-tree, but that is not the
195: # focus of these tests.
196: #
197: do_test rtree-3.1.1 {
198: execsql {
199: CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
200: SELECT * FROM t1;
201: }
202: } {}
203: do_test rtree-3.1.2 {
204: execsql {
205: INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
206: SELECT * FROM t1;
207: }
208: } {5 1.0 3.0 2.0 4.0}
209: do_test rtree-3.1.3 {
210: execsql {
211: INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
212: SELECT * FROM t1;
213: }
214: } {5 1.0 3.0 2.0 4.0 6 2.0 6.0 4.0 8.0}
215:
216: # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
217: do_test rtree-3.2.1 {
218: catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
219: } {1 {constraint failed}}
220: do_test rtree-3.2.2 {
221: catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
222: } {0 {}}
223:
224: #----------------------------------------------------------------------------
225: # Test cases rtree-5.* test DELETE operations.
226: #
227: do_test rtree-5.1.1 {
228: execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
229: } {}
230: do_test rtree-5.1.2 {
231: execsql {
232: INSERT INTO t2 VALUES(1, 10, 20);
233: INSERT INTO t2 VALUES(2, 30, 40);
234: INSERT INTO t2 VALUES(3, 50, 60);
235: SELECT * FROM t2 ORDER BY ii;
236: }
237: } {1 10.0 20.0 2 30.0 40.0 3 50.0 60.0}
238: do_test rtree-5.1.3 {
239: execsql {
240: DELETE FROM t2 WHERE ii=2;
241: SELECT * FROM t2 ORDER BY ii;
242: }
243: } {1 10.0 20.0 3 50.0 60.0}
244: do_test rtree-5.1.4 {
245: execsql {
246: DELETE FROM t2 WHERE ii=1;
247: SELECT * FROM t2 ORDER BY ii;
248: }
249: } {3 50.0 60.0}
250: do_test rtree-5.1.5 {
251: execsql {
252: DELETE FROM t2 WHERE ii=3;
253: SELECT * FROM t2 ORDER BY ii;
254: }
255: } {}
256: do_test rtree-5.1.6 {
257: execsql { SELECT * FROM t2_rowid }
258: } {}
259:
260: #----------------------------------------------------------------------------
261: # Test cases rtree-5.* test UPDATE operations.
262: #
263: do_test rtree-6.1.1 {
264: execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
265: } {}
266: do_test rtree-6.1.2 {
267: execsql {
268: INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
269: UPDATE t3 SET x2=5;
270: SELECT * FROM t3;
271: }
272: } {1 2.0 5.0 4.0 5.0}
273: do_test rtree-6.1.3 {
274: execsql { UPDATE t3 SET ii = 2 }
275: execsql { SELECT * FROM t3 }
276: } {2 2.0 5.0 4.0 5.0}
277:
278: #----------------------------------------------------------------------------
279: # Test cases rtree-7.* test rename operations.
280: #
281: do_test rtree-7.1.1 {
282: execsql {
283: CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
284: INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
285: }
286: } {}
287: do_test rtree-7.1.2 {
288: execsql { ALTER TABLE t4 RENAME TO t5 }
289: execsql { SELECT * FROM t5 }
290: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
291: do_test rtree-7.1.3 {
292: db close
293: sqlite3 db test.db
294: execsql { SELECT * FROM t5 }
295: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
296: do_test rtree-7.1.4 {
297: execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
298: execsql { SELECT * FROM "raisara ""one""'" }
299: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
300: do_test rtree-7.1.5 {
301: execsql { SELECT * FROM 'raisara "one"''' }
302: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
303: do_test rtree-7.1.6 {
304: execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
305: execsql { SELECT * FROM "abc 123" }
306: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
307: do_test rtree-7.1.7 {
308: db close
309: sqlite3 db test.db
310: execsql { SELECT * FROM "abc 123" }
311: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
312:
313: # An error midway through a rename operation.
314: do_test rtree-7.2.1 {
315: execsql {
316: CREATE TABLE t4_node(a);
317: }
318: catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
319: } {1 {SQL logic error or missing database}}
320: do_test rtree-7.2.2 {
321: execsql { SELECT * FROM "abc 123" }
322: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
323: do_test rtree-7.2.3 {
324: execsql {
325: DROP TABLE t4_node;
326: CREATE TABLE t4_rowid(a);
327: }
328: catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
329: } {1 {SQL logic error or missing database}}
330: do_test rtree-7.2.4 {
331: db close
332: sqlite3 db test.db
333: execsql { SELECT * FROM "abc 123" }
334: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
335: do_test rtree-7.2.5 {
336: execsql { DROP TABLE t4_rowid }
337: execsql { ALTER TABLE "abc 123" RENAME TO t4 }
338: execsql { SELECT * FROM t4 }
339: } {1 2.0 3.0 4.0 5.0 6.0 7.0}
340:
341:
342: #----------------------------------------------------------------------------
343: # Test cases rtree-8.*
344: #
345:
346: # Test that the function to determine if a leaf cell is part of the
347: # result set works.
348: do_test rtree-8.1.1 {
349: execsql {
350: CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
351: INSERT INTO t6 VALUES(1, 3, 7);
352: INSERT INTO t6 VALUES(2, 4, 6);
353: }
354: } {}
355: do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
356: do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
357: do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
358: do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
359: do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
360: do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
361: do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
362:
363: #----------------------------------------------------------------------------
364: # Test cases rtree-9.*
365: #
366: # Test that ticket #3549 is fixed.
367: do_test rtree-9.1 {
368: execsql {
369: CREATE TABLE foo (id INTEGER PRIMARY KEY);
370: CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
371: INSERT INTO foo VALUES (null);
372: INSERT INTO foo SELECT null FROM foo;
373: INSERT INTO foo SELECT null FROM foo;
374: INSERT INTO foo SELECT null FROM foo;
375: INSERT INTO foo SELECT null FROM foo;
376: INSERT INTO foo SELECT null FROM foo;
377: INSERT INTO foo SELECT null FROM foo;
378: DELETE FROM foo WHERE id > 40;
379: INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
380: }
381: } {}
382:
383: # This used to crash.
384: do_test rtree-9.2 {
385: execsql {
386: SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
387: }
388: } {1600}
389: do_test rtree-9.3 {
390: execsql {
391: SELECT count(*) FROM bar b1, bar b2, foo s1
392: WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
393: }
394: } {1600}
395:
396: #-------------------------------------------------------------------------
397: # Ticket #3970: Check that the error message is meaningful when a
398: # keyword is used as a column name.
399: #
400: do_test rtree-10.1 {
401: catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
402: } {1 {near "index": syntax error}}
403:
404: #-------------------------------------------------------------------------
405: # Test last_insert_rowid().
406: #
407: do_test rtree-11.1 {
408: execsql {
409: CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
410: INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
411: SELECT last_insert_rowid();
412: }
413: } {1}
414: do_test rtree-11.2 {
415: execsql {
416: INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
417: SELECT last_insert_rowid();
418: }
419: } {2}
420:
421: #-------------------------------------------------------------------------
422: # Test on-conflict clause handling.
423: #
424: db_delete_and_reopen
425: do_execsql_test 12.0 {
426: CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
427: INSERT INTO t1 VALUES(1, 1, 2, 3, 4);
428: INSERT INTO t1 VALUES(2, 2, 3, 4, 5);
429: INSERT INTO t1 VALUES(3, 3, 4, 5, 6);
430:
431: CREATE TABLE source(idx, x1, x2, y1, y2);
432: INSERT INTO source VALUES(5, 8, 8, 8, 8);
433: INSERT INTO source VALUES(2, 7, 7, 7, 7);
434:
435: }
436: db_save_and_close
437: foreach {tn sql_template testdata} {
438: 1 "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
439: ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
440: ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
441: IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
442: FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
443: REPLACE 0 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7}
444: }
445:
446: 2 "INSERT %CONF% INTO t1 SELECT * FROM source" {
447: ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
448: ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
449: IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
450: FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
451: REPLACE 1 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
452: }
453:
454: 3 "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
455: ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
456: ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
457: IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
458: FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
459: REPLACE 1 0 {1 1 2 3 4 2 4 5 6 7 3 3 4 5 6}
460: }
461:
462: 3 "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
463: ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
464: ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
465: IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
466: FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
467: REPLACE 1 0 {1 4 5 6 7 2 2 3 4 5 5 3 4 5 6}
468: }
469:
470: 4 "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
471: ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
472: ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
473: IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
474: FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
475: REPLACE 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
476: }
477:
478: } {
479: foreach {mode uses error data} $testdata {
480: db_restore_and_reopen
481:
482: set sql [string map [list %CONF% "OR $mode"] $sql_template]
483: set testname "12.$tn.[string tolower $mode]"
484:
485: execsql {
486: BEGIN;
487: INSERT INTO t1 VALUES(4, 4, 5, 6, 7);
488: }
489:
490: set res(0) {0 {}}
491: set res(1) {1 {constraint failed}}
492: do_catchsql_test $testname.1 $sql $res($error)
493: do_test $testname.2 [list sql_uses_stmt db $sql] $uses
494: do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
495:
496: do_test $testname.4 { rtree_check db t1 } 0
497: db close
498: }
499: }
500: finish_test
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>