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