123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- # 2001 September 15
- #
- # 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.
- #
- #***********************************************************************
- # This file implements regression tests for SQLite library.
- #
- # This file implements tests for proper treatment of the special
- # value NULL.
- #
- set testdir [file dirname $argv0]
- source $testdir/tester.tcl
- # Create a table and some data to work with.
- #
- do_test null-1.0 {
- execsql {
- begin;
- create table t1(a,b,c);
- insert into t1 values(1,0,0);
- insert into t1 values(2,0,1);
- insert into t1 values(3,1,0);
- insert into t1 values(4,1,1);
- insert into t1 values(5,null,0);
- insert into t1 values(6,null,1);
- insert into t1 values(7,null,null);
- commit;
- select * from t1;
- }
- } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
- # Check for how arithmetic expressions handle NULL
- #
- do_test null-1.1 {
- execsql {
- select ifnull(a+b,99) from t1;
- }
- } {1 2 4 5 99 99 99}
- do_test null-1.2 {
- execsql {
- select ifnull(b*c,99) from t1;
- }
- } {0 0 0 1 99 99 99}
- # Check to see how the CASE expression handles NULL values. The
- # first WHEN for which the test expression is TRUE is selected.
- # FALSE and UNKNOWN test expressions are skipped.
- #
- do_test null-2.1 {
- execsql {
- select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
- }
- } {0 0 1 1 0 0 0}
- do_test null-2.2 {
- execsql {
- select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
- }
- } {1 1 0 0 0 0 0}
- do_test null-2.3 {
- execsql {
- select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
- }
- } {0 0 0 1 0 0 0}
- do_test null-2.4 {
- execsql {
- select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
- }
- } {1 1 1 0 1 0 0}
- do_test null-2.5 {
- execsql {
- select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
- }
- } {0 1 1 1 0 1 0}
- do_test null-2.6 {
- execsql {
- select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
- }
- } {1 0 0 0 0 0 0}
- do_test null-2.7 {
- execsql {
- select ifnull(case b when c then 1 else 0 end, 99) from t1;
- }
- } {1 0 0 1 0 0 0}
- do_test null-2.8 {
- execsql {
- select ifnull(case c when b then 1 else 0 end, 99) from t1;
- }
- } {1 0 0 1 0 0 0}
- # Check to see that NULL values are ignored in aggregate functions.
- #
- do_test null-3.1 {
- execsql {
- select count(*), count(b), count(c), sum(b), sum(c),
- avg(b), avg(c), min(b), max(b) from t1;
- }
- } {7 4 6 2 3 0.5 0.5 0 1}
- # Check to see how WHERE clauses handle NULL values. A NULL value
- # is the same as UNKNOWN. The WHERE clause should only select those
- # rows that are TRUE. FALSE and UNKNOWN rows are rejected.
- #
- do_test null-4.1 {
- execsql {
- select a from t1 where b<10
- }
- } {1 2 3 4}
- do_test null-4.2 {
- execsql {
- select a from t1 where not b>10
- }
- } {1 2 3 4}
- do_test null-4.3 {
- execsql {
- select a from t1 where b<10 or c=1;
- }
- } {1 2 3 4 6}
- do_test null-4.4 {
- execsql {
- select a from t1 where b<10 and c=1;
- }
- } {2 4}
- do_test null-4.5 {
- execsql {
- select a from t1 where not (b<10 and c=1);
- }
- } {1 3 5}
- # The DISTINCT keyword on a SELECT statement should treat NULL values
- # as distinct
- #
- do_test null-5.1 {
- execsql {
- select distinct b from t1 order by b;
- }
- } {{} 0 1}
- # A UNION to two queries should treat NULL values
- # as distinct
- #
- do_test null-6.1 {
- execsql {
- select b from t1 union select c from t1 order by c;
- }
- } {{} 0 1}
- # The UNIQUE constraint only applies to non-null values
- #
- do_test null-7.1 {
- execsql {
- create table t2(a, b unique on conflict ignore);
- insert into t2 values(1,1);
- insert into t2 values(2,null);
- insert into t2 values(3,null);
- insert into t2 values(4,1);
- select a from t2;
- }
- } {1 2 3}
- do_test null-7.2 {
- execsql {
- create table t3(a, b, c, unique(b,c) on conflict ignore);
- insert into t3 values(1,1,1);
- insert into t3 values(2,null,1);
- insert into t3 values(3,null,1);
- insert into t3 values(4,1,1);
- select a from t3;
- }
- } {1 2 3}
- finish_test
|