os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/check.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
# 2005 November 2
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing CHECK constraints
sl@0
    13
#
sl@0
    14
# $Id: check.test,v 1.11 2007/07/23 19:39:47 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# Only run these tests if the build includes support for CHECK constraints
sl@0
    20
ifcapable !check {
sl@0
    21
  finish_test
sl@0
    22
  return
sl@0
    23
}
sl@0
    24
sl@0
    25
do_test check-1.1 {
sl@0
    26
  execsql {
sl@0
    27
    CREATE TABLE t1(
sl@0
    28
      x INTEGER CHECK( x<5 ),
sl@0
    29
      y REAL CHECK( y>x )
sl@0
    30
    );
sl@0
    31
  }
sl@0
    32
} {}
sl@0
    33
do_test check-1.2 {
sl@0
    34
  execsql {
sl@0
    35
    INSERT INTO t1 VALUES(3,4);
sl@0
    36
    SELECT * FROM t1;
sl@0
    37
  }  
sl@0
    38
} {3 4.0}
sl@0
    39
do_test check-1.3 {
sl@0
    40
  catchsql {
sl@0
    41
    INSERT INTO t1 VALUES(6,7);
sl@0
    42
  }
sl@0
    43
} {1 {constraint failed}}
sl@0
    44
do_test check-1.4 {
sl@0
    45
  execsql {
sl@0
    46
    SELECT * FROM t1;
sl@0
    47
  }  
sl@0
    48
} {3 4.0}
sl@0
    49
do_test check-1.5 {
sl@0
    50
  catchsql {
sl@0
    51
    INSERT INTO t1 VALUES(4,3);
sl@0
    52
  }
sl@0
    53
} {1 {constraint failed}}
sl@0
    54
do_test check-1.6 {
sl@0
    55
  execsql {
sl@0
    56
    SELECT * FROM t1;
sl@0
    57
  }  
sl@0
    58
} {3 4.0}
sl@0
    59
do_test check-1.7 {
sl@0
    60
  catchsql {
sl@0
    61
    INSERT INTO t1 VALUES(NULL,6);
sl@0
    62
  }
sl@0
    63
} {0 {}}
sl@0
    64
do_test check-1.8 {
sl@0
    65
  execsql {
sl@0
    66
    SELECT * FROM t1;
sl@0
    67
  }  
sl@0
    68
} {3 4.0 {} 6.0}
sl@0
    69
do_test check-1.9 {
sl@0
    70
  catchsql {
sl@0
    71
    INSERT INTO t1 VALUES(2,NULL);
sl@0
    72
  }
sl@0
    73
} {0 {}}
sl@0
    74
do_test check-1.10 {
sl@0
    75
  execsql {
sl@0
    76
    SELECT * FROM t1;
sl@0
    77
  }  
sl@0
    78
} {3 4.0 {} 6.0 2 {}}
sl@0
    79
do_test check-1.11 {
sl@0
    80
  execsql {
sl@0
    81
    DELETE FROM t1 WHERE x IS NULL OR x!=3;
sl@0
    82
    UPDATE t1 SET x=2 WHERE x==3;
sl@0
    83
    SELECT * FROM t1;
sl@0
    84
  }
sl@0
    85
} {2 4.0}
sl@0
    86
do_test check-1.12 {
sl@0
    87
  catchsql {
sl@0
    88
    UPDATE t1 SET x=7 WHERE x==2
sl@0
    89
  }
sl@0
    90
} {1 {constraint failed}}
sl@0
    91
do_test check-1.13 {
sl@0
    92
  execsql {
sl@0
    93
    SELECT * FROM t1;
sl@0
    94
  }
sl@0
    95
} {2 4.0}
sl@0
    96
do_test check-1.14 {
sl@0
    97
  catchsql {
sl@0
    98
    UPDATE t1 SET x=5 WHERE x==2
sl@0
    99
  }
sl@0
   100
} {1 {constraint failed}}
sl@0
   101
do_test check-1.15 {
sl@0
   102
  execsql {
sl@0
   103
    SELECT * FROM t1;
sl@0
   104
  }
sl@0
   105
} {2 4.0}
sl@0
   106
do_test check-1.16 {
sl@0
   107
  catchsql {
sl@0
   108
    UPDATE t1 SET x=4, y=11 WHERE x==2
sl@0
   109
  }
sl@0
   110
} {0 {}}
sl@0
   111
do_test check-1.17 {
sl@0
   112
  execsql {
sl@0
   113
    SELECT * FROM t1;
sl@0
   114
  }
sl@0
   115
} {4 11.0}
sl@0
   116
sl@0
   117
do_test check-2.1 {
sl@0
   118
  execsql {
sl@0
   119
    CREATE TABLE t2(
sl@0
   120
      x INTEGER CHECK( typeof(coalesce(x,0))=="integer" ),
sl@0
   121
      y REAL CHECK( typeof(coalesce(y,0.1))=="real" ),
sl@0
   122
      z TEXT CHECK( typeof(coalesce(z,''))=="text" )
sl@0
   123
    );
sl@0
   124
  }
sl@0
   125
} {}
sl@0
   126
do_test check-2.2 {
sl@0
   127
  execsql {
sl@0
   128
    INSERT INTO t2 VALUES(1,2.2,'three');
sl@0
   129
    SELECT * FROM t2;
sl@0
   130
  }
sl@0
   131
} {1 2.2 three}
sl@0
   132
do_test check-2.3 {
sl@0
   133
  execsql {
sl@0
   134
    INSERT INTO t2 VALUES(NULL, NULL, NULL);
sl@0
   135
    SELECT * FROM t2;
sl@0
   136
  }
sl@0
   137
} {1 2.2 three {} {} {}}
sl@0
   138
do_test check-2.4 {
sl@0
   139
  catchsql {
sl@0
   140
    INSERT INTO t2 VALUES(1.1, NULL, NULL);
sl@0
   141
  }
sl@0
   142
} {1 {constraint failed}}
sl@0
   143
do_test check-2.5 {
sl@0
   144
  catchsql {
sl@0
   145
    INSERT INTO t2 VALUES(NULL, 5, NULL);
sl@0
   146
  }
sl@0
   147
} {1 {constraint failed}}
sl@0
   148
do_test check-2.6 {
sl@0
   149
  catchsql {
sl@0
   150
    INSERT INTO t2 VALUES(NULL, NULL, 3.14159);
sl@0
   151
  }
sl@0
   152
} {1 {constraint failed}}
sl@0
   153
sl@0
   154
ifcapable subquery {
sl@0
   155
  do_test check-3.1 {
sl@0
   156
    catchsql {
sl@0
   157
      CREATE TABLE t3(
sl@0
   158
        x, y, z,
sl@0
   159
        CHECK( x<(SELECT min(x) FROM t1) )
sl@0
   160
      );
sl@0
   161
    }
sl@0
   162
  } {1 {subqueries prohibited in CHECK constraints}}
sl@0
   163
}
sl@0
   164
sl@0
   165
do_test check-3.2 {
sl@0
   166
  execsql {
sl@0
   167
    SELECT name FROM sqlite_master ORDER BY name
sl@0
   168
  }
sl@0
   169
} {t1 t2}
sl@0
   170
do_test check-3.3 {
sl@0
   171
  catchsql {
sl@0
   172
    CREATE TABLE t3(
sl@0
   173
      x, y, z,
sl@0
   174
      CHECK( q<x )
sl@0
   175
    );
sl@0
   176
  }
sl@0
   177
} {1 {no such column: q}}
sl@0
   178
do_test check-3.4 {
sl@0
   179
  execsql {
sl@0
   180
    SELECT name FROM sqlite_master ORDER BY name
sl@0
   181
  }
sl@0
   182
} {t1 t2}
sl@0
   183
do_test check-3.5 {
sl@0
   184
  catchsql {
sl@0
   185
    CREATE TABLE t3(
sl@0
   186
      x, y, z,
sl@0
   187
      CHECK( t2.x<x )
sl@0
   188
    );
sl@0
   189
  }
sl@0
   190
} {1 {no such column: t2.x}}
sl@0
   191
do_test check-3.6 {
sl@0
   192
  execsql {
sl@0
   193
    SELECT name FROM sqlite_master ORDER BY name
sl@0
   194
  }
sl@0
   195
} {t1 t2}
sl@0
   196
do_test check-3.7 {
sl@0
   197
  catchsql {
sl@0
   198
    CREATE TABLE t3(
sl@0
   199
      x, y, z,
sl@0
   200
      CHECK( t3.x<25 )
sl@0
   201
    );
sl@0
   202
  }
sl@0
   203
} {0 {}}
sl@0
   204
do_test check-3.8 {
sl@0
   205
  execsql {
sl@0
   206
    INSERT INTO t3 VALUES(1,2,3);
sl@0
   207
    SELECT * FROM t3;
sl@0
   208
  }
sl@0
   209
} {1 2 3}
sl@0
   210
do_test check-3.9 {
sl@0
   211
  catchsql {
sl@0
   212
    INSERT INTO t3 VALUES(111,222,333);
sl@0
   213
  }
sl@0
   214
} {1 {constraint failed}}
sl@0
   215
sl@0
   216
do_test check-4.1 {
sl@0
   217
  execsql {
sl@0
   218
    CREATE TABLE t4(x, y,
sl@0
   219
      CHECK (
sl@0
   220
           x+y==11
sl@0
   221
        OR x*y==12
sl@0
   222
        OR x/y BETWEEN 5 AND 8
sl@0
   223
        OR -x==y+10
sl@0
   224
      )
sl@0
   225
    );
sl@0
   226
  }
sl@0
   227
} {}
sl@0
   228
do_test check-4.2 {
sl@0
   229
  execsql {
sl@0
   230
    INSERT INTO t4 VALUES(1,10);
sl@0
   231
    SELECT * FROM t4
sl@0
   232
  }
sl@0
   233
} {1 10}
sl@0
   234
do_test check-4.3 {
sl@0
   235
  execsql {
sl@0
   236
    UPDATE t4 SET x=4, y=3;
sl@0
   237
    SELECT * FROM t4
sl@0
   238
  }
sl@0
   239
} {4 3}
sl@0
   240
do_test check-4.3 {
sl@0
   241
  execsql {
sl@0
   242
    UPDATE t4 SET x=12, y=2;
sl@0
   243
    SELECT * FROM t4
sl@0
   244
  }
sl@0
   245
} {12 2}
sl@0
   246
do_test check-4.4 {
sl@0
   247
  execsql {
sl@0
   248
    UPDATE t4 SET x=12, y=-22;
sl@0
   249
    SELECT * FROM t4
sl@0
   250
  }
sl@0
   251
} {12 -22}
sl@0
   252
do_test check-4.5 {
sl@0
   253
  catchsql {
sl@0
   254
    UPDATE t4 SET x=0, y=1;
sl@0
   255
  }
sl@0
   256
} {1 {constraint failed}}
sl@0
   257
do_test check-4.6 {
sl@0
   258
  execsql {
sl@0
   259
    SELECT * FROM t4;
sl@0
   260
  }
sl@0
   261
} {12 -22}
sl@0
   262
do_test check-4.7 {
sl@0
   263
  execsql {
sl@0
   264
    PRAGMA ignore_check_constraints=ON;
sl@0
   265
    UPDATE t4 SET x=0, y=1;
sl@0
   266
    SELECT * FROM t4;
sl@0
   267
  }
sl@0
   268
} {0 1}
sl@0
   269
do_test check-4.8 {
sl@0
   270
  catchsql {
sl@0
   271
    PRAGMA ignore_check_constraints=OFF;
sl@0
   272
    UPDATE t4 SET x=0, y=2;
sl@0
   273
  }
sl@0
   274
} {1 {constraint failed}}
sl@0
   275
ifcapable vacuum {
sl@0
   276
  do_test check_4.9 {
sl@0
   277
    catchsql {
sl@0
   278
      VACUUM
sl@0
   279
    }
sl@0
   280
  } {0 {}}
sl@0
   281
}
sl@0
   282
sl@0
   283
do_test check-5.1 {
sl@0
   284
  catchsql {
sl@0
   285
    CREATE TABLE t5(x, y,
sl@0
   286
      CHECK( x*y<:abc )
sl@0
   287
    );
sl@0
   288
  }
sl@0
   289
} {1 {parameters prohibited in CHECK constraints}}
sl@0
   290
do_test check-5.2 {
sl@0
   291
  catchsql {
sl@0
   292
    CREATE TABLE t5(x, y,
sl@0
   293
      CHECK( x*y<? )
sl@0
   294
    );
sl@0
   295
  }
sl@0
   296
} {1 {parameters prohibited in CHECK constraints}}
sl@0
   297
sl@0
   298
ifcapable conflict {
sl@0
   299
sl@0
   300
do_test check-6.1 {
sl@0
   301
  execsql {SELECT * FROM t1}
sl@0
   302
} {4 11.0}
sl@0
   303
do_test check-6.2 {
sl@0
   304
  execsql {
sl@0
   305
    UPDATE OR IGNORE t1 SET x=5;
sl@0
   306
    SELECT * FROM t1;
sl@0
   307
  }
sl@0
   308
} {4 11.0}
sl@0
   309
do_test check-6.3 {
sl@0
   310
  execsql {
sl@0
   311
    INSERT OR IGNORE INTO t1 VALUES(5,4.0);
sl@0
   312
    SELECT * FROM t1;
sl@0
   313
  }
sl@0
   314
} {4 11.0}
sl@0
   315
do_test check-6.4 {
sl@0
   316
  execsql {
sl@0
   317
    INSERT OR IGNORE INTO t1 VALUES(2,20.0);
sl@0
   318
    SELECT * FROM t1;
sl@0
   319
  }
sl@0
   320
} {4 11.0 2 20.0}
sl@0
   321
do_test check-6.5 {
sl@0
   322
  catchsql {
sl@0
   323
    UPDATE OR FAIL t1 SET x=7-x, y=y+1;
sl@0
   324
  }
sl@0
   325
} {1 {constraint failed}}
sl@0
   326
do_test check-6.6 {
sl@0
   327
  execsql {
sl@0
   328
    SELECT * FROM t1;
sl@0
   329
  }
sl@0
   330
} {3 12.0 2 20.0}
sl@0
   331
do_test check-6.7 {
sl@0
   332
  catchsql {
sl@0
   333
    BEGIN;
sl@0
   334
    INSERT INTO t1 VALUES(1,30.0);
sl@0
   335
    INSERT OR ROLLBACK INTO t1 VALUES(8,40.0);
sl@0
   336
  }
sl@0
   337
} {1 {constraint failed}}
sl@0
   338
do_test check-6.8 {
sl@0
   339
  catchsql {
sl@0
   340
    COMMIT;
sl@0
   341
  }
sl@0
   342
} {1 {cannot commit - no transaction is active}}
sl@0
   343
do_test check-6.9 {
sl@0
   344
  execsql {
sl@0
   345
    SELECT * FROM t1
sl@0
   346
  }
sl@0
   347
} {3 12.0 2 20.0}
sl@0
   348
sl@0
   349
do_test check-6.11 {
sl@0
   350
  execsql {SELECT * FROM t1}
sl@0
   351
} {3 12.0 2 20.0}
sl@0
   352
do_test check-6.12 {
sl@0
   353
  catchsql {
sl@0
   354
    REPLACE INTO t1 VALUES(6,7);
sl@0
   355
  }
sl@0
   356
} {1 {constraint failed}}
sl@0
   357
do_test check-6.13 {
sl@0
   358
  execsql {SELECT * FROM t1}
sl@0
   359
} {3 12.0 2 20.0}
sl@0
   360
do_test check-6.14 {
sl@0
   361
  catchsql {
sl@0
   362
    INSERT OR IGNORE INTO t1 VALUES(6,7);
sl@0
   363
  }
sl@0
   364
} {0 {}}
sl@0
   365
do_test check-6.15 {
sl@0
   366
  execsql {SELECT * FROM t1}
sl@0
   367
} {3 12.0 2 20.0}
sl@0
   368
sl@0
   369
sl@0
   370
}
sl@0
   371
sl@0
   372
finish_test