collate6.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # 2001 September 15
  3. #
  4. # The author disclaims copyright to this source code. In place of
  5. # a legal notice, here is a blessing:
  6. #
  7. # May you do good and not evil.
  8. # May you find forgiveness for yourself and forgive others.
  9. # May you share freely, never taking more than you give.
  10. #
  11. #***********************************************************************
  12. # This file implements regression tests for SQLite library. The
  13. # focus of this script is collation sequences in concert with triggers.
  14. #
  15. # $Id: collate6.test,v 1.4 2007/07/30 14:40:48 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # There are no tests in this file that will work without
  19. # trigger support.
  20. #
  21. ifcapable {!trigger} {
  22. finish_test
  23. return
  24. }
  25. # Create a case-insensitive collation type NOCASE for use in testing.
  26. # Normally, capital letters are less than their lower-case counterparts.
  27. db collate NOCASE nocase_collate
  28. proc nocase_collate {a b} {
  29. return [string compare -nocase $a $b]
  30. }
  31. #
  32. # Tests are organized as follows:
  33. # collate6-1.* - triggers.
  34. #
  35. do_test collate6-1.0 {
  36. execsql {
  37. CREATE TABLE collate6log(a, b);
  38. CREATE TABLE collate6tab(a COLLATE NOCASE, b COLLATE BINARY);
  39. }
  40. } {}
  41. # Test that the default collation sequence applies to new.* references
  42. # in WHEN clauses.
  43. do_test collate6-1.1 {
  44. execsql {
  45. CREATE TRIGGER collate6trig BEFORE INSERT ON collate6tab
  46. WHEN new.a = 'a' BEGIN
  47. INSERT INTO collate6log VALUES(new.a, new.b);
  48. END;
  49. }
  50. } {}
  51. do_test collate6-1.2 {
  52. execsql {
  53. INSERT INTO collate6tab VALUES('a', 'b');
  54. SELECT * FROM collate6log;
  55. }
  56. } {a b}
  57. do_test collate6-1.3 {
  58. execsql {
  59. INSERT INTO collate6tab VALUES('A', 'B');
  60. SELECT * FROM collate6log;
  61. }
  62. } {a b A B}
  63. do_test collate6-1.4 {
  64. execsql {
  65. DROP TRIGGER collate6trig;
  66. DELETE FROM collate6log;
  67. }
  68. } {}
  69. # Test that the default collation sequence applies to new.* references
  70. # in the body of triggers.
  71. do_test collate6-1.5 {
  72. execsql {
  73. CREATE TRIGGER collate6trig BEFORE INSERT ON collate6tab BEGIN
  74. INSERT INTO collate6log VALUES(new.a='a', new.b='b');
  75. END;
  76. }
  77. } {}
  78. do_test collate6-1.6 {
  79. execsql {
  80. INSERT INTO collate6tab VALUES('a', 'b');
  81. SELECT * FROM collate6log;
  82. }
  83. } {1 1}
  84. do_test collate6-1.7 {
  85. execsql {
  86. INSERT INTO collate6tab VALUES('A', 'B');
  87. SELECT * FROM collate6log;
  88. }
  89. } {1 1 1 0}
  90. do_test collate6-1.8 {
  91. execsql {
  92. DROP TRIGGER collate6trig;
  93. DELETE FROM collate6log;
  94. }
  95. } {}
  96. do_test collate6-1.9 {
  97. execsql {
  98. DROP TABLE collate6tab;
  99. }
  100. } {}
  101. # Test that an explicit collation sequence overrides an implicit
  102. # one attached to a 'new' reference.
  103. #
  104. do_test collate6-2.1 {
  105. execsql {
  106. CREATE TABLE abc(a COLLATE binary, b, c);
  107. CREATE TABLE def(a, b, c);
  108. CREATE TRIGGER abc_t1 AFTER INSERT ON abc BEGIN
  109. INSERT INTO def SELECT * FROM abc WHERE a < new.a COLLATE nocase;
  110. END
  111. }
  112. } {}
  113. do_test collate6-2.2 {
  114. execsql {
  115. INSERT INTO abc VALUES('One', 'Two', 'Three');
  116. INSERT INTO abc VALUES('one', 'two', 'three');
  117. SELECT * FROM def;
  118. }
  119. } {}
  120. do_test collate6-2.3 {
  121. execsql {
  122. UPDATE abc SET a = 'four' WHERE a = 'one';
  123. CREATE TRIGGER abc_t2 AFTER UPDATE ON abc BEGIN
  124. INSERT INTO def SELECT * FROM abc WHERE a < new.a COLLATE nocase;
  125. END;
  126. SELECT * FROM def;
  127. }
  128. } {}
  129. # At one point the 6-3.2 (but not 6-3.1) was causing an assert() to fail.
  130. #
  131. do_test collate6-3.1 {
  132. execsql {
  133. SELECT 1 FROM sqlite_master WHERE name COLLATE nocase = 'hello';
  134. }
  135. } {}
  136. do_test collate6-3.2 {
  137. execsql {
  138. SELECT 1 FROM sqlite_master WHERE 'hello' = name COLLATE nocase;
  139. }
  140. } {}
  141. finish_test