Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: third_party/sqlite/src/test/hook.test

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/sqlite/src/test/hexlit.test ('k') | third_party/sqlite/src/test/hook2.test » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # 2004 Jan 14 1 # 2004 Jan 14
2 # 2 #
3 # The author disclaims copyright to this source code. In place of 3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing: 4 # a legal notice, here is a blessing:
5 # 5 #
6 # May you do good and not evil. 6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others. 7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give. 8 # May you share freely, never taking more than you give.
9 # 9 #
10 #*********************************************************************** 10 #***********************************************************************
11 # This file implements regression tests for TCL interface to the 11 # This file implements regression tests for TCL interface to the
12 # SQLite library. 12 # SQLite library.
13 # 13 #
14 # The focus of the tests in this file is the following interface: 14 # The focus of the tests in this file is the following interface:
15 # 15 #
16 # sqlite_commit_hook (tests hook-1..hook-3 inclusive) 16 # sqlite_commit_hook (tests hook-1..hook-3 inclusive)
17 # sqlite_update_hook (tests hook-4-*) 17 # sqlite_update_hook (tests hook-4-*)
18 # sqlite_rollback_hook (tests hook-5.*) 18 # sqlite_rollback_hook (tests hook-5.*)
19 # 19 #
20 # $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $ 20 # $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $
21 21
22 set testdir [file dirname $argv0] 22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl 23 source $testdir/tester.tcl
24 set ::testprefix hook
24 25
25 do_test hook-1.2 { 26 do_test hook-1.2 {
26 db commit_hook 27 db commit_hook
27 } {} 28 } {}
28 29
29 30
30 do_test hook-3.1 { 31 do_test hook-3.1 {
31 set commit_cnt 0 32 set commit_cnt 0
32 proc commit_hook {} { 33 proc commit_hook {} {
33 incr ::commit_cnt 34 incr ::commit_cnt
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 INSERT INTO t1 VALUES('two', 'II'); 432 INSERT INTO t1 VALUES('two', 'II');
432 COMMIT; 433 COMMIT;
433 } 434 }
434 execsql { SELECT * FROM t1 } 435 execsql { SELECT * FROM t1 }
435 } {one I} 436 } {one I}
436 do_test hook-6.2 { 437 do_test hook-6.2 {
437 set ::hooks 438 set ::hooks
438 } {COMMIT ROLLBACK} 439 } {COMMIT ROLLBACK}
439 unset ::hooks 440 unset ::hooks
440 441
442 #----------------------------------------------------------------------------
443 # The following tests - hook-7.* - test the pre-update hook.
444 #
445 ifcapable !preupdate {
446 finish_test
447 return
448 }
449 #
450 # 7.1.1 - INSERT statement.
451 # 7.1.2 - INSERT INTO ... SELECT statement.
452 # 7.1.3 - REPLACE INTO ... (rowid conflict)
453 # 7.1.4 - REPLACE INTO ... (other index conflicts)
454 # 7.1.5 - REPLACE INTO ... (both rowid and other index conflicts)
455 #
456 # 7.2.1 - DELETE statement.
457 # 7.2.2 - DELETE statement that uses the truncate optimization.
458 #
459 # 7.3.1 - UPDATE statement.
460 # 7.3.2 - UPDATE statement that modifies the rowid.
461 # 7.3.3 - UPDATE OR REPLACE ... (rowid conflict).
462 # 7.3.4 - UPDATE OR REPLACE ... (other index conflicts)
463 # 7.3.4 - UPDATE OR REPLACE ... (both rowid and other index conflicts)
464 #
465 # 7.4.1 - Test that the pre-update-hook is invoked only once if a row being
466 # deleted is removed by a BEFORE trigger.
467 #
468 # 7.4.2 - Test that the pre-update-hook is invoked if a BEFORE trigger
469 # removes a row being updated. In this case the update hook should
470 # be invoked with SQLITE_INSERT as the opcode when inserting the
471 # new version of the row.
472 #
473 # TODO: Short records (those created before a column is added to a table
474 # using ALTER TABLE)
475 #
476
477 proc do_preupdate_test {tn sql x} {
478 set X [list]
479 foreach elem $x {lappend X $elem}
480 uplevel do_test $tn [list "
481 set ::preupdate \[list\]
482 execsql { $sql }
483 set ::preupdate
484 "] [list $X]
485 }
486
487 proc preupdate_hook {args} {
488 set type [lindex $args 0]
489 eval lappend ::preupdate $args
490 if {$type != "INSERT"} {
491 for {set i 0} {$i < [db preupdate count]} {incr i} {
492 lappend ::preupdate [db preupdate old $i]
493 }
494 }
495 if {$type != "DELETE"} {
496 for {set i 0} {$i < [db preupdate count]} {incr i} {
497 set rc [catch { db preupdate new $i } v]
498 lappend ::preupdate $v
499 }
500 }
501 }
502
503 db close
504 forcedelete test.db
505 sqlite3 db test.db
506 db preupdate hook preupdate_hook
507
508 # Set up a schema to use for tests 7.1.* to 7.3.*.
509 do_execsql_test 7.0 {
510 CREATE TABLE t1(a, b);
511 CREATE TABLE t2(x, y);
512 CREATE TABLE t3(i, j, UNIQUE(i));
513
514 INSERT INTO t2 VALUES('a', 'b');
515 INSERT INTO t2 VALUES('c', 'd');
516
517 INSERT INTO t3 VALUES(4, 16);
518 INSERT INTO t3 VALUES(5, 25);
519 INSERT INTO t3 VALUES(6, 36);
520 }
521
522 do_preupdate_test 7.1.1 {
523 INSERT INTO t1 VALUES('x', 'y')
524 } {INSERT main t1 1 1 x y}
525
526 # 7.1.2.1 does not use the xfer optimization. 7.1.2.2 does.
527 do_preupdate_test 7.1.2.1 {
528 INSERT INTO t1 SELECT y, x FROM t2;
529 } {INSERT main t1 2 2 b a INSERT main t1 3 3 d c}
530 do_preupdate_test 7.1.2.2 {
531 INSERT INTO t1 SELECT * FROM t2;
532 } {INSERT main t1 4 4 a b INSERT main t1 5 5 c d}
533
534 do_preupdate_test 7.1.3 {
535 REPLACE INTO t1(rowid, a, b) VALUES(1, 1, 1);
536 } {
537 DELETE main t1 1 1 x y
538 INSERT main t1 1 1 1 1
539 }
540
541 do_preupdate_test 7.1.4 {
542 REPLACE INTO t3 VALUES(4, NULL);
543 } {
544 DELETE main t3 1 1 4 16
545 INSERT main t3 4 4 4 {}
546 }
547
548 do_preupdate_test 7.1.5 {
549 REPLACE INTO t3(rowid, i, j) VALUES(2, 6, NULL);
550 } {
551 DELETE main t3 2 2 5 25
552 DELETE main t3 3 3 6 36
553 INSERT main t3 2 2 6 {}
554 }
555
556 do_execsql_test 7.2.0 { SELECT rowid FROM t1 } {1 2 3 4 5}
557
558 do_preupdate_test 7.2.1 {
559 DELETE FROM t1 WHERE rowid = 3
560 } {
561 DELETE main t1 3 3 d c
562 }
563 do_preupdate_test 7.2.2 {
564 DELETE FROM t1
565 } {
566 DELETE main t1 1 1 1 1
567 DELETE main t1 2 2 b a
568 DELETE main t1 4 4 a b
569 DELETE main t1 5 5 c d
570 }
571
572 do_execsql_test 7.3.0 {
573 DELETE FROM t1;
574 DELETE FROM t2;
575 DELETE FROM t3;
576
577 INSERT INTO t2 VALUES('a', 'b');
578 INSERT INTO t2 VALUES('c', 'd');
579
580 INSERT INTO t3 VALUES(4, 16);
581 INSERT INTO t3 VALUES(5, 25);
582 INSERT INTO t3 VALUES(6, 36);
583 }
584
585 do_preupdate_test 7.3.1 {
586 UPDATE t2 SET y = y||y;
587 } {
588 UPDATE main t2 1 1 a b a bb
589 UPDATE main t2 2 2 c d c dd
590 }
591
592 do_preupdate_test 7.3.2 {
593 UPDATE t2 SET rowid = rowid-1;
594 } {
595 UPDATE main t2 1 0 a bb a bb
596 UPDATE main t2 2 1 c dd c dd
597 }
598
599 do_preupdate_test 7.3.3 {
600 UPDATE OR REPLACE t2 SET rowid = 1 WHERE x = 'a'
601 } {
602 DELETE main t2 1 1 c dd
603 UPDATE main t2 0 1 a bb a bb
604 }
605
606 do_preupdate_test 7.3.4.1 {
607 UPDATE OR REPLACE t3 SET i = 5 WHERE i = 6
608 } {
609 DELETE main t3 2 2 5 25
610 UPDATE main t3 3 3 6 36 5 36
611 }
612
613 do_execsql_test 7.3.4.2 {
614 INSERT INTO t3 VALUES(10, 100);
615 SELECT rowid, * FROM t3;
616 } {1 4 16 3 5 36 4 10 100}
617
618 do_preupdate_test 7.3.5 {
619 UPDATE OR REPLACE t3 SET rowid = 1, i = 5 WHERE j = 100;
620 } {
621 DELETE main t3 1 1 4 16
622 DELETE main t3 3 3 5 36
623 UPDATE main t3 4 1 10 100 5 100
624 }
625
626 do_execsql_test 7.4.1.0 {
627 CREATE TABLE t4(a, b);
628 INSERT INTO t4 VALUES('a', 1);
629 INSERT INTO t4 VALUES('b', 2);
630 INSERT INTO t4 VALUES('c', 3);
631
632 CREATE TRIGGER t4t BEFORE DELETE ON t4 BEGIN
633 DELETE FROM t4 WHERE b = 1;
634 END;
635 }
636
637 do_preupdate_test 7.4.1.1 {
638 DELETE FROM t4 WHERE b = 3
639 } {
640 DELETE main t4 1 1 a 1
641 DELETE main t4 3 3 c 3
642 }
643
644 do_execsql_test 7.4.1.2 {
645 INSERT INTO t4(rowid, a, b) VALUES(1, 'a', 1);
646 INSERT INTO t4(rowid, a, b) VALUES(3, 'c', 3);
647 }
648 do_preupdate_test 7.4.1.3 {
649 DELETE FROM t4 WHERE b = 1
650 } {
651 DELETE main t4 1 1 a 1
652 }
653
654 do_execsql_test 7.4.2.0 {
655 CREATE TABLE t5(a, b);
656 INSERT INTO t5 VALUES('a', 1);
657 INSERT INTO t5 VALUES('b', 2);
658 INSERT INTO t5 VALUES('c', 3);
659
660 CREATE TRIGGER t5t BEFORE UPDATE ON t5 BEGIN
661 DELETE FROM t5 WHERE b = 1;
662 END;
663 }
664 do_preupdate_test 7.4.2.1 {
665 UPDATE t5 SET b = 4 WHERE a = 'c'
666 } {
667 DELETE main t5 1 1 a 1
668 UPDATE main t5 3 3 c 3 c 4
669 }
670
671 do_execsql_test 7.4.2.2 {
672 INSERT INTO t5(rowid, a, b) VALUES(1, 'a', 1);
673 }
674
675 do_preupdate_test 7.4.2.3 {
676 UPDATE t5 SET b = 5 WHERE a = 'a'
677 } {
678 DELETE main t5 1 1 a 1
679 }
680
681 do_execsql_test 7.5.1.0 {
682 CREATE TABLE t7(a, b);
683 INSERT INTO t7 VALUES('one', 'two');
684 INSERT INTO t7 VALUES('three', 'four');
685 ALTER TABLE t7 ADD COLUMN c DEFAULT NULL;
686 }
687
688 do_preupdate_test 7.5.1.1 {
689 DELETE FROM t7 WHERE a = 'one'
690 } {
691 DELETE main t7 1 1 one two {}
692 }
693
694 do_preupdate_test 7.5.1.2 {
695 UPDATE t7 SET b = 'five'
696 } {
697 UPDATE main t7 2 2 three four {} three five {}
698 }
699
700 do_execsql_test 7.5.2.0 {
701 CREATE TABLE t8(a, b);
702 INSERT INTO t8 VALUES('one', 'two');
703 INSERT INTO t8 VALUES('three', 'four');
704 ALTER TABLE t8 ADD COLUMN c DEFAULT 'xxx';
705 }
706
707 if 0 {
708 # At time of writing, these two are broken. They demonstrate that the
709 # sqlite3_preupdate_old() method does not handle the case where ALTER TABLE
710 # has been used to add a column with a default value other than NULL.
711 #
712 do_preupdate_test 7.5.2.1 {
713 DELETE FROM t8 WHERE a = 'one'
714 } {
715 DELETE main t8 1 1 one two xxx
716 }
717 do_preupdate_test 7.5.2.2 {
718 UPDATE t8 SET b = 'five'
719 } {
720 UPDATE main t8 2 2 three four xxx three five xxx
721 }
722 }
723
724 # This block of tests verifies that IPK values are correctly reported
725 # by the sqlite3_preupdate_old() and sqlite3_preupdate_new() functions.
726 #
727 do_execsql_test 7.6.1 { CREATE TABLE t9(a, b INTEGER PRIMARY KEY, c) }
728 do_preupdate_test 7.6.2 {
729 INSERT INTO t9 VALUES(1, 2, 3);
730 UPDATE t9 SET b = b+1, c = c+1;
731 DELETE FROM t9 WHERE a = 1;
732 } {
733 INSERT main t9 2 2 1 2 3
734 UPDATE main t9 2 3 1 2 3 1 3 4
735 DELETE main t9 3 3 1 3 4
736 }
737
738 #--------------------------------------------------------------------------
739 # Test that the sqlite3_preupdate_depth() API seems to work.
740 #
741 proc preupdate_hook {args} {
742 set type [lindex $args 0]
743 eval lappend ::preupdate $args
744 eval lappend ::preupdate [db preupdate depth]
745
746 if {$type != "INSERT"} {
747 for {set i 0} {$i < [db preupdate count]} {incr i} {
748 lappend ::preupdate [db preupdate old $i]
749 }
750 }
751 if {$type != "DELETE"} {
752 for {set i 0} {$i < [db preupdate count]} {incr i} {
753 set rc [catch { db preupdate new $i } v]
754 lappend ::preupdate $v
755 }
756 }
757 }
758
759 db close
760 forcedelete test.db
761 sqlite3 db test.db
762 db preupdate hook preupdate_hook
763
764 do_execsql_test 7.6.1 {
765 CREATE TABLE t1(x PRIMARY KEY);
766 CREATE TABLE t2(x PRIMARY KEY);
767 CREATE TABLE t3(x PRIMARY KEY);
768 CREATE TABLE t4(x PRIMARY KEY);
769
770 CREATE TRIGGER a AFTER INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END;
771 CREATE TRIGGER b AFTER INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END;
772 CREATE TRIGGER c AFTER INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END;
773
774 CREATE TRIGGER d AFTER UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END;
775 CREATE TRIGGER e AFTER UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END;
776 CREATE TRIGGER f AFTER UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END;
777
778 CREATE TRIGGER g AFTER DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END;
779 CREATE TRIGGER h AFTER DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END;
780 CREATE TRIGGER i AFTER DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END;
781 }
782
783 do_preupdate_test 7.6.2 {
784 INSERT INTO t1 VALUES('xyz');
785 } {
786 INSERT main t1 1 1 0 xyz
787 INSERT main t2 1 1 1 xyz
788 INSERT main t3 1 1 2 xyz
789 INSERT main t4 1 1 3 xyz
790 }
791 do_preupdate_test 7.6.3 {
792 UPDATE t1 SET x = 'abc';
793 } {
794 UPDATE main t1 1 1 0 xyz abc
795 UPDATE main t2 1 1 1 xyz abc
796 UPDATE main t3 1 1 2 xyz abc
797 UPDATE main t4 1 1 3 xyz abc
798 }
799 do_preupdate_test 7.6.4 {
800 DELETE FROM t1 WHERE 1;
801 } {
802 DELETE main t1 1 1 0 abc
803 DELETE main t2 1 1 1 abc
804 DELETE main t3 1 1 2 abc
805 DELETE main t4 1 1 3 abc
806 }
807
808 do_execsql_test 7.6.5 {
809 DROP TRIGGER a; DROP TRIGGER b; DROP TRIGGER c;
810 DROP TRIGGER d; DROP TRIGGER e; DROP TRIGGER f;
811 DROP TRIGGER g; DROP TRIGGER h; DROP TRIGGER i;
812
813 CREATE TRIGGER a BEFORE INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END;
814 CREATE TRIGGER b BEFORE INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END;
815 CREATE TRIGGER c BEFORE INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END;
816
817 CREATE TRIGGER d BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END;
818 CREATE TRIGGER e BEFORE UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END;
819 CREATE TRIGGER f BEFORE UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END;
820
821 CREATE TRIGGER g BEFORE DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END;
822 CREATE TRIGGER h BEFORE DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END;
823 CREATE TRIGGER i BEFORE DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END;
824 }
825
826 do_preupdate_test 7.6.6 {
827 INSERT INTO t1 VALUES('xyz');
828 } {
829 INSERT main t4 1 1 3 xyz
830 INSERT main t3 1 1 2 xyz
831 INSERT main t2 1 1 1 xyz
832 INSERT main t1 1 1 0 xyz
833 }
834 do_preupdate_test 7.6.3 {
835 UPDATE t1 SET x = 'abc';
836 } {
837 UPDATE main t4 1 1 3 xyz abc
838 UPDATE main t3 1 1 2 xyz abc
839 UPDATE main t2 1 1 1 xyz abc
840 UPDATE main t1 1 1 0 xyz abc
841 }
842 do_preupdate_test 7.6.4 {
843 DELETE FROM t1 WHERE 1;
844 } {
845 DELETE main t4 1 1 3 abc
846 DELETE main t3 1 1 2 abc
847 DELETE main t2 1 1 1 abc
848 DELETE main t1 1 1 0 abc
849 }
850
851 # No preupdate callbacks for modifying sqlite_master.
852 do_preupdate_test 8.1 { CREATE TABLE x1(x, y); } { }
853 do_preupdate_test 8.2 { ALTER TABLE x1 ADD COLUMN z } { }
854 do_preupdate_test 8.3 { ALTER TABLE x1 RENAME TO y1 } { }
855 do_preupdate_test 8.4 { CREATE INDEX y1x ON y1(x) } { }
856 do_preupdate_test 8.5 { CREATE VIEW v1 AS SELECT * FROM y1 } { }
857 do_preupdate_test 8.6 { DROP TABLE y1 } { }
858
859 #-------------------------------------------------------------------------
860 reset_db
861 db preupdate hook preupdate_hook
862 do_execsql_test 9.0 {
863 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
864 CREATE TABLE t2(a, b INTEGER PRIMARY KEY);
865 }
866 do_preupdate_test 9.1 {
867 INSERT INTO t1 VALUES(456, NULL, NULL);
868 } {
869 INSERT main t1 456 456 0 456 {} {}
870 }
871 do_execsql_test 9.2 {
872 ALTER TABLE t1 ADD COLUMN d;
873 }
874 do_preupdate_test 9.3 {
875 INSERT INTO t1(a, b, c) VALUES(457, NULL, NULL);
876 } {
877 INSERT main t1 457 457 0 457 {} {} {}
878 }
879 do_preupdate_test 9.4 {
880 DELETE FROM t1 WHERE a=456
881 } {
882 DELETE main t1 456 456 0 456 {} {} {}
883 }
884 do_preupdate_test 9.5 {
885 INSERT INTO t2 DEFAULT VALUES;
886 } {
887 INSERT main t2 1 1 0 {} 1
888 }
889 do_preupdate_test 9.6 {
890 INSERT INTO t1 DEFAULT VALUES;
891 } {
892 INSERT main t1 458 458 0 458 {} {} {}
893 }
894
895
896 do_execsql_test 10.0 {
897 CREATE TABLE t3(a, b INTEGER PRIMARY KEY);
898 }
899 do_preupdate_test 10.1 {
900 INSERT INTO t3 DEFAULT VALUES
901 } {
902 INSERT main t3 1 1 0 {} 1
903 }
904 do_execsql_test 10.2 { SELECT * FROM t3 } {{} 1}
905 do_preupdate_test 10.3 {
906 DELETE FROM t3 WHERE b=1
907 } {DELETE main t3 1 1 0 {} 1}
908
909
441 finish_test 910 finish_test
OLDNEW
« no previous file with comments | « third_party/sqlite/src/test/hexlit.test ('k') | third_party/sqlite/src/test/hook2.test » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698