tkt2251.test 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # 2007 Febuary 24
  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. # This file implements regression tests for SQLite library.
  12. #
  13. # This file implements tests to verify that table column values
  14. # are pulled out of the database correctly.
  15. #
  16. # Long ago, the OP_Column opcode was sufficient to pull out the
  17. # value of a table column. But then we added the ALTER TABLE ADD COLUMN
  18. # feature. An added column might not actually exist in every row,
  19. # and so the OP_Column opcode has to contain a default value. Later
  20. # still we added a feature whereby a REAL value with no fractional
  21. # part is stored in the database file as an integer to save space.
  22. # After extracting the value, we have to call OP_RealAffinity to
  23. # convert it back to a REAL.
  24. #
  25. # The sqlite3ExprCodeGetColumn() routine was added to take care of
  26. # all of the complications above. The tests in this file attempt
  27. # to verify that sqlite3ExprCodeGetColumn() is used instead of a
  28. # raw OP_Column in all places where a table column is extracted from
  29. # the database.
  30. #
  31. # $Id: tkt2251.test,v 1.2 2007/09/12 17:01:45 danielk1977 Exp $
  32. set testdir [file dirname $argv0]
  33. source $testdir/tester.tcl
  34. ifcapable !altertable {
  35. finish_test
  36. return
  37. }
  38. # Create sample data. Verify that the default value and type of an added
  39. # column is correct for aggregates.
  40. do_test tkt2251-1.1 {
  41. execsql {
  42. CREATE TABLE t1(a INTEGER);
  43. INSERT INTO t1 VALUES(1);
  44. INSERT INTO t1 VALUES(1);
  45. INSERT INTO t1 VALUES(2);
  46. INSERT INTO t1 VALUES(9);
  47. INSERT INTO t1 VALUES(9);
  48. INSERT INTO t1 VALUES(9);
  49. INSERT INTO t1 VALUES(3);
  50. INSERT INTO t1 VALUES(2);
  51. ALTER TABLE t1 ADD COLUMN b REAL DEFAULT 4.0;
  52. SELECT avg(b), typeof(avg(b)) FROM t1;
  53. }
  54. } {4.0 real}
  55. do_test tkt2251-1.2 {
  56. execsql {
  57. SELECT sum(b), typeof(sum(b)) FROM t1;
  58. }
  59. } {32.0 real}
  60. do_test tkt2251-1.3 {
  61. execsql {
  62. SELECT a, sum(b), typeof(sum(b)) FROM t1 GROUP BY a ORDER BY a;
  63. }
  64. } {1 8.0 real 2 8.0 real 3 4.0 real 9 12.0 real}
  65. # Make sure that the REAL value comes out when values are accessed
  66. # by index.
  67. #
  68. do_test tkt2251-2.1 {
  69. execsql {
  70. SELECT b, typeof(b) FROM t1 WHERE a=3;
  71. }
  72. } {4.0 real}
  73. do_test tkt2251-2.2 {
  74. execsql {
  75. CREATE INDEX t1i1 ON t1(a,b);
  76. SELECT b, typeof(b) FROM t1 WHERE a=3;
  77. }
  78. } {4.0 real}
  79. do_test tkt2251-2.3 {
  80. execsql {
  81. REINDEX;
  82. SELECT b, typeof(b) FROM t1 WHERE a=3;
  83. }
  84. } {4.0 real}
  85. # Make sure the correct REAL value is used when copying from one
  86. # table to another.
  87. #
  88. do_test tkt2251-3.1 {
  89. execsql {
  90. CREATE TABLE t2(x,y);
  91. INSERT INTO t2 SELECT * FROM t1;
  92. SELECT y, typeof(y) FROM t2 WHERE x=3;
  93. }
  94. } {4.0 real}
  95. do_test tkt2251-3.2 {
  96. execsql {
  97. CREATE TABLE t3 AS SELECT * FROM t1;
  98. SELECT b, typeof(b) FROM t3 WHERE a=3;
  99. }
  100. } {4.0 real}
  101. finish_test