]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
New test cases for upsert.
authordrh <drh@noemail.net>
Tue, 17 Apr 2018 16:16:40 +0000 (16:16 +0000)
committerdrh <drh@noemail.net>
Tue, 17 Apr 2018 16:16:40 +0000 (16:16 +0000)
FossilOrigin-Name: 907b5a37c539ea67c285c87399b494c7fe15014389a2ea3d49e90640b58946ae

manifest
manifest.uuid
test/distinct.test
test/upsert2.test [new file with mode: 0644]
test/upsert3.test [new file with mode: 0644]

index ec656f2efaa4b5660471b04acb4d2730927540f1..713483e7fb59994e60d2fcd67073f32b23669779 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sthe\sLemon\senhancement\sfrom\strunk,\sresulting\sin\ssmaller\sparser\stables.
-D 2018-04-16T14:36:34.684
+C New\stest\scases\sfor\supsert.
+D 2018-04-17T16:16:40.462
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 5ce9343cba9c189046f1afe6d2bcc1f68079439febc05267b98aec6ecc752439
@@ -756,7 +756,7 @@ F test/descidx1.test 6d03b44c8538fe0eb4924e19fba10cdd8f3c9240
 F test/descidx2.test 9f1a0c83fd57f8667c82310ca21b30a350888b5d
 F test/descidx3.test 09ddbe3f5295f482d2f8b687cf6db8bad7acd9a2
 F test/diskfull.test 106391384780753ea6896b7b4f005d10e9866b6e
-F test/distinct.test a1783b960ad8c15a77cd9f207be072898db1026c
+F test/distinct.test fd7cebccdeb87f561f49000914301dc896ee187aea5cdc2bae55e3d082374456
 F test/distinct2.test df0bb52b754661ea84ec9ff488d48913c97bd31d83ca17ce0bf1334645e660cf
 F test/distinctagg.test 1a6ef9c87a58669438fc771450d7a72577417376
 F test/e_blobbytes.test 439a945953b35cb6948a552edaec4dc31fd70a05
@@ -1508,6 +1508,8 @@ F test/unordered.test ca7adce0419e4ca0c50f039885e76ed2c531eda8
 F test/update.test 6c68446b8a0a33d522a7c72b320934596a2d7d32
 F test/update2.test 5e67667e1c54017d964e626db765cf8bedcf87483c184f4c575bdb8c1dd2313e
 F test/upsert1.test 3b4e8e5932516115bfffb2269a44c416c5c26d0d57cc7dd16954d0b77fbc4cd9
+F test/upsert2.test 82ed5cbd4a13ff74240ece8ec97b6afd07446eac554ad5e93c12b42197844e0b
+F test/upsert3.test 46ee74a7d7ee797ca0ab79d44e78572d6778b7520e4ebb8f8c1553765b4eae2c
 F test/uri.test 3481026f00ade6dfe8adb7acb6e1e47b04369568
 F test/uri2.test 9d3ba7a53ee167572d53a298ee4a5d38ec4a8fb7
 F test/userauth01.test e740a2697a7b40d7c5003a7d7edaee16acd349a9
@@ -1719,7 +1721,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 810d9f63be46c1452718b05e51c519fb64e9fabf2f1813b5e07b1041dbb71880 3b7801acff91905d0e78e06121ebcf2664f6de5f605699dc8726ec9bcb558eb8
-R 8378bae31105ea55776764904c4e0579
+P 641f41722c68f1d51251b10d4d3d7c473d08ee999f6db20023ef6dcd4dc5f6e8
+R 1e259ef65026feaf02be85449e54247a
 U drh
-Z 93044c8cf04465dc31bbf55ccd133682
+Z a453e1f58e770e0cd13a1e67daf6b8f7
index 2ebae7a3160339594b03171bd4bf84dfa3af671e..ef5b242ab31650c293223d27805185aa923acade 100644 (file)
@@ -1 +1 @@
-641f41722c68f1d51251b10d4d3d7c473d08ee999f6db20023ef6dcd4dc5f6e8
\ No newline at end of file
+907b5a37c539ea67c285c87399b494c7fe15014389a2ea3d49e90640b58946ae
\ No newline at end of file
index dac2269b0bcf2321e4b28495e73b933251707c44..807fb0dd37139591670673fc5e0eabe4cc17aa89 100644 (file)
@@ -168,7 +168,7 @@ foreach {tn sql temptables res} {
   6   "b FROM t1 ORDER BY +b COLLATE binary"          {btree hash} {B b}
   7   "a FROM t1"                                          {}      {A a}
   8   "b COLLATE nocase FROM t1"                           {}      {b}
-  9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {b}
+  9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {B}
 } {
   do_execsql_test    2.$tn.1 "SELECT DISTINCT $sql" $res
   do_temptables_test 2.$tn.2 "SELECT DISTINCT $sql" $temptables
diff --git a/test/upsert2.test b/test/upsert2.test
new file mode 100644 (file)
index 0000000..7419564
--- /dev/null
@@ -0,0 +1,170 @@
+# 2018-04-17
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# Test cases for UPSERT
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix zipfile
+
+do_execsql_test upsert2-100 {
+  CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0);
+  INSERT INTO t1(a,b) VALUES(1,2),(3,4);
+  INSERT INTO t1(a,b) VALUES(1,8),(2,11),(3,1)
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b;
+  SELECT *, 'x' FROM t1 ORDER BY a;
+} {1 8 1 x 2 11 0 x 3 4 0 x}
+do_execsql_test upsert2-110 {
+  DROP TABLE t1;
+  CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID;
+  INSERT INTO t1(a,b) VALUES(1,2),(3,4);
+  INSERT INTO t1(a,b) VALUES(1,8),(2,11),(3,1)
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b;
+  SELECT *, 'x' FROM t1 ORDER BY a;
+} {1 8 1 x 2 11 0 x 3 4 0 x}
+
+do_execsql_test upsert2-200 {
+  DROP TABLE t1;
+  CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0);
+  INSERT INTO t1(a,b) VALUES(1,2),(3,4);
+  WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
+  INSERT INTO t1(a,b) SELECT a, b FROM nx WHERE true
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b;
+  SELECT *, 'x' FROM t1 ORDER BY a;
+} {1 99 2 x 2 15 1 x 3 4 0 x}
+do_execsql_test upsert2-201 {
+  DELETE FROM t1;
+  INSERT INTO t1(a,b) VALUES(1,2),(3,4);
+  WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
+  INSERT INTO t1(a,b) AS t2 SELECT a, b FROM nx WHERE true
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t2.c+1 WHERE t2.b<excluded.b;
+  SELECT *, 'x' FROM t1 ORDER BY a;
+} {1 99 2 x 2 15 1 x 3 4 0 x}
+do_catchsql_test upsert2-202 {
+  WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
+  INSERT INTO t1(a,b) AS t2 SELECT a, b FROM nx WHERE true
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t1.c+1 WHERE t1.b<excluded.b;
+} {1 {no such column: t1.c}}
+do_execsql_test upsert2-210 {
+  DROP TABLE t1;
+  CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID;
+  INSERT INTO t1(a,b) VALUES(1,2),(3,4);
+  WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
+  INSERT INTO t1(a,b) SELECT a, b FROM nx WHERE true
+    ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b;
+  SELECT *, 'x' FROM t1 ORDER BY a;
+} {1 99 2 x 2 15 1 x 3 4 0 x}
+
+# On an ON CONFLICT DO UPDATE, the before-insert, before-update, and
+# after-update triggers fire.
+#
+do_execsql_test upsert2-300 {
+  DROP TABLE t1;
+  CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0);
+  CREATE TABLE record(x TEXT, y TEXT);
+  CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('before-insert',printf('%d,%d,%d',new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('after-insert',printf('%d,%d,%d',new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r3 BEFORE UPDATE ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('before-update',printf('%d,%d,%d/%d,%d,%d',
+                                      old.a,old.b,old.c,new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r4 AFTER UPDATE ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('after-update',printf('%d,%d,%d/%d,%d,%d',
+                                      old.a,old.b,old.c,new.a,new.b,new.c));
+  END;
+  INSERT INTO t1(a,b) VALUES(1,2);
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2)
+    ON CONFLICT(a) DO UPDATE SET c=t1.c+1;
+  SELECT * FROM record
+} {before-insert 1,2,0 before-update 1,2,0/1,2,1 after-update 1,2,0/1,2,1}
+
+# On an ON CONFLICT DO NOTHING, only the before-insert trigger fires.
+#
+do_execsql_test upsert2-310 {
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING;
+  SELECT * FROM record;
+} {before-insert 1,2,0}
+
+# With ON CONFLICT DO UPDATE and a failed WHERE, only the before-insert
+# trigger fires.
+#
+do_execsql_test upsert2-320 {
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2)
+    ON CONFLICT(a) DO UPDATE SET c=c+1 WHERE c<0;
+  SELECT * FROM record;
+} {before-insert 1,2,0}
+do_execsql_test upsert2-321 {
+  SELECT * FROM t1;
+} {1 2 1}
+
+# Trigger tests repeated for a WITHOUT ROWID table.
+#
+do_execsql_test upsert2-400 {
+  DROP TABLE t1;
+  CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID;
+  CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('before-insert',printf('%d,%d,%d',new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('after-insert',printf('%d,%d,%d',new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r3 BEFORE UPDATE ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('before-update',printf('%d,%d,%d/%d,%d,%d',
+                                      old.a,old.b,old.c,new.a,new.b,new.c));
+  END;
+  CREATE TRIGGER r4 AFTER UPDATE ON t1 BEGIN
+    INSERT INTO record(x,y)
+        VALUES('after-update',printf('%d,%d,%d/%d,%d,%d',
+                                      old.a,old.b,old.c,new.a,new.b,new.c));
+  END;
+  INSERT INTO t1(a,b) VALUES(1,2);
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2)
+    ON CONFLICT(a) DO UPDATE SET c=t1.c+1;
+  SELECT * FROM record
+} {before-insert 1,2,0 before-update 1,2,0/1,2,1 after-update 1,2,0/1,2,1}
+
+# On an ON CONFLICT DO NOTHING, only the before-insert trigger fires.
+#
+do_execsql_test upsert2-410 {
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING;
+  SELECT * FROM record;
+} {before-insert 1,2,0}
+
+# With ON CONFLICT DO UPDATE and a failed WHERE, only the before-insert
+# trigger fires.
+#
+do_execsql_test upsert2-420 {
+  DELETE FROM record;
+  INSERT INTO t1(a,b) VALUES(1,2)
+    ON CONFLICT(a) DO UPDATE SET c=c+1 WHERE c<0;
+  SELECT * FROM record;
+} {before-insert 1,2,0}
+do_execsql_test upsert2-421 {
+  SELECT * FROM t1;
+} {1 2 1}
+
+finish_test
diff --git a/test/upsert3.test b/test/upsert3.test
new file mode 100644 (file)
index 0000000..30a2479
--- /dev/null
@@ -0,0 +1,58 @@
+# 2018-04-17
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# Test cases for UPSERT
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix zipfile
+
+do_execsql_test upsert3-100 {
+  CREATE TABLE t1(k int, v text);
+  CREATE UNIQUE INDEX x1 ON t1(k, v);
+} {}
+do_catchsql_test upsert3-110 {
+  INSERT INTO t1 VALUES(0,'abcdefghij')
+     ON CONFLICT(k) DO NOTHING;
+} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
+do_catchsql_test upsert3-120 {
+  INSERT INTO t1 VALUES(0,'abcdefghij')
+     ON CONFLICT(v) DO NOTHING;
+} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
+
+do_execsql_test upsert3-130 {
+  INSERT INTO t1 VALUES(0, 'abcdefghij')
+      ON CONFLICT(k,v) DO NOTHING;
+  SELECT * FROM t1;
+} {0 abcdefghij}
+do_execsql_test upsert3-140 {
+  INSERT INTO t1 VALUES(0, 'abcdefghij')
+      ON CONFLICT(v,k) DO NOTHING;
+  SELECT * FROM t1;
+} {0 abcdefghij}
+
+do_execsql_test upsert3-200 {
+  CREATE TABLE excluded(a INT, b INT, c INT DEFAULT 0);
+  CREATE UNIQUE INDEX excludedab ON excluded(a,b);
+  INSERT INTO excluded(a,b) VALUES(1,2),(1,2),(3,4),(1,2),(5,6),(3,4)
+    ON CONFLICT(b,a) DO UPDATE SET c=excluded.c+1;
+  SELECT *, 'x' FROM excluded ORDER BY a;
+} {1 2 2 x 3 4 1 x 5 6 0 x}
+do_execsql_test upsert3-210 {
+  INSERT INTO excluded(a,b,c) AS base VALUES(1,2,8),(1,2,3)
+    ON CONFLICT(b,a) DO UPDATE SET c=excluded.c+1 WHERE base.c<excluded.c;
+  SELECT *, 'x' FROM excluded ORDER BY a;
+} {1 2 9 x 3 4 1 x 5 6 0 x}
+
+
+
+finish_test