/ Check-in [82fdb197]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add tests for sessions module.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 82fdb1975f5b29a751089a8582713372999ae56e
User & Date: dan 2014-08-18 08:42:36
Context
2014-08-18
13:48
Merge the latest trunk changes, and in particular the refactoring of the object names in the command-line shell. check-in: 419d286a user: drh tags: sessions
08:42
Add tests for sessions module. check-in: 82fdb197 user: dan tags: sessions
2014-08-16
19:01
Fix some missing and out-of-date comments in the sessions module. check-in: 05c1d914 user: dan tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/session/sessionB.test.

   323    323   } {
   324    324     DELETE FROM t2 WHERE b=1;
   325    325   } {
   326    326     {DELETE t2 0 .XX. {{} {} i 1 i 1 {} {}} {}} 
   327    327     {DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}} 
   328    328   }
   329    329   
   330         -finish_test
          330  +#-------------------------------------------------------------------------
          331  +# More rigorous testing of the _patchset(), _apply and _concat() APIs.
          332  +#
          333  +# The inputs to each test are a populate database and a list of DML 
          334  +# statements. This test determines that the final database is the same
          335  +# if:
          336  +# 
          337  +#   1) the statements are executed directly on the database.
          338  +#
          339  +#   2) a single patchset is collected while executing the statements and
          340  +#      then applied to a copy of the original database file.
          341  +#
          342  +#   3) individual patchsets are collected for statement while executing
          343  +#      them and concatenated together before being applied to a copy of
          344  +#      the original database. The concatenation is done in a couple of
          345  +#      different ways - linear, pairwise etc.
          346  +#
          347  +# All tests, as it happens, are run with both changesets and patchsets.
          348  +# But the focus is on patchset capabilities.
          349  +#
          350  +
          351  +# Return a checksum of the contents of the database file. Implicit IPK
          352  +# columns are not included in the checksum - just modifying rowids does
          353  +# not change the database checksum.
          354  +#
          355  +proc databasecksum {db} {
          356  +  set alltab [$db eval {SELECT name FROM sqlite_master WHERE type='table'}]
          357  +  foreach tab $alltab {
          358  +    $db eval "SELECT * FROM $tab LIMIT 1" res { }
          359  +    set slist [list]
          360  +    foreach col [lsort $res(*)] {
          361  +      lappend slist "quote($col)"
          362  +    }
          363  +    set sql "SELECT [join $slist ,] FROM $tab"
          364  +    append txt "[lsort [$db eval $sql]]\n"
          365  +  }
          366  +  return [md5 $txt]
          367  +}
          368  +
          369  +proc do_patchset_test {tn tstcmd lSql} {
          370  +  if {$tstcmd != "patchset" && $tstcmd != "changeset"} {
          371  +    error "have $tstcmd: must be patchset or changeset"
          372  +  }
          373  +
          374  +  foreach fname {test.db2 test.db3 test.db4 test.db5} {
          375  +    forcedelete $fname
          376  +    forcecopy test.db $fname
          377  +  }
          378  +
          379  +  # Execute the SQL statements on [db]. Collect a patchset for each 
          380  +  # individual statement, as well as a single patchset for the entire 
          381  +  # operation.
          382  +  sqlite3session S db main
          383  +  S attach *
          384  +  foreach sql $lSql { 
          385  +    sqlite3session T db main
          386  +    T attach *
          387  +    db eval $sql 
          388  +    lappend lPatch [T patchset]
          389  +    T delete
          390  +  }
          391  +  set patchset [S patchset]
          392  +  S delete
          393  +
          394  +  # Calculate a checksum for the final database.
          395  +  set cksum [databasecksum db]
          396  +
          397  +  # 1. Apply the single large patchset to test.db2
          398  +  sqlite3 db2 test.db2
          399  +  sqlite3changeset_apply db2 $patchset noop
          400  +  uplevel [list do_test $tn.1 { databasecksum db2 } $cksum ]
          401  +  db2 close
          402  +  
          403  +  # 2. Apply each of the single-statement patchsets to test.db3
          404  +  sqlite3 db2 test.db3
          405  +  foreach p $lPatch {
          406  +    sqlite3changeset_apply db2 $p noop
          407  +  }
          408  +  uplevel [list do_test $tn.2 { databasecksum db2 } $cksum ]
          409  +  db2 close
          410  +
          411  +  # 3. Concatenate all single-statement patchsets into a single large
          412  +  #    patchset, then apply it to test.db4.
          413  +  #
          414  +  sqlite3 db2 test.db4
          415  +  set big ""
          416  +  foreach p $lPatch {
          417  +    set big [sqlite3changeset_concat $big $p]
          418  +  }
          419  +  sqlite3changeset_apply db2 $big noop
          420  +  uplevel [list do_test $tn.3 { databasecksum db2 } $cksum ]
          421  +  db2 close
          422  +
          423  +  # 4. Concatenate all single-statement patchsets pairwise into a single
          424  +  #    large patchset, then apply it to test.db5. Pairwise concatenation:
          425  +  #
          426  +  #         a b c d e f g h i j k
          427  +  #      -> {a b} {c d} {e f} {g h} {i j} k
          428  +  #      -> {a b c d} {e f g h} {i j k}
          429  +  #      -> {a b c d e f g h} {i j k}
          430  +  #      -> {a b c d e f g h i j k}
          431  +  #      -> APPLY!
          432  +  #
          433  +  sqlite3 db2 test.db5
          434  +  set L $lPatch
          435  +  while {[llength $L] > 1} {
          436  +    set O [list]
          437  +    for {set i 0} {$i < [llength $L]} {incr i 2} {
          438  +      if {$i==[llength $L]-1} {
          439  +        lappend O [lindex $L $i]
          440  +      } else {
          441  +        set i1 [expr $i+1]
          442  +        lappend O [sqlite3changeset_concat [lindex $L $i] [lindex $L $i1]]
          443  +      }
          444  +    }
          445  +    set L $O
          446  +  }
          447  +  sqlite3changeset_apply db2 [lindex $L 0] noop
          448  +  uplevel [list do_test $tn.4 { databasecksum db2 } $cksum ]
          449  +  db2 close
          450  +}
          451  +
          452  +proc do_patchset_changeset_test {tn initsql args} {
          453  +  foreach tstcmd {patchset changeset} {
          454  +    reset_db
          455  +    execsql $initsql
          456  +    foreach sql $args {
          457  +      set lSql [split $sql ";"]
          458  +      uplevel [list do_patchset_test $tn.$tstcmd $tstcmd $lSql]
          459  +    }
          460  +  }
          461  +}
          462  +
          463  +do_patchset_changeset_test 5.1 {
          464  +  CREATE TABLE t1(a PRIMARY KEY, b, c);
          465  +  INSERT INTO t1 VALUES(1, 2, 3);
          466  +} {
          467  +  INSERT INTO t1 VALUES(4, 5, 6);
          468  +  DELETE FROM t1 WHERE a=1;
          469  +} {
          470  +  INSERT INTO t1 VALUES(7, 8, 9);
          471  +  UPDATE t1 SET c = 5;
          472  +  INSERT INTO t1 VALUES(10, 11, 12);
          473  +  UPDATE t1 SET c = 6;
          474  +  INSERT INTO t1 VALUES(13, 14, 15);
          475  +} {
          476  +  UPDATE t1 SET c=c+1;
          477  +  DELETE FROM t1 WHERE (a%2);
          478  +} 
          479  +
          480  +do_patchset_changeset_test 5.2 {
          481  +  CREATE TABLE t1(a PRIMARY KEY, b, c);
          482  +  CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b));
          483  +} {
          484  +  INSERT INTO t1 VALUES(x'00', 0, 'zero');
          485  +  INSERT INTO t1 VALUES(x'01', 1, 'one');
          486  +  INSERT INTO t1 VALUES(x'02', 4, 'four');
          487  +  INSERT INTO t1 VALUES(x'03', 9, 'nine');
          488  +  INSERT INTO t1 VALUES(x'04', 16, 'sixteen');
          489  +  INSERT INTO t1 VALUES(x'05', 25, 'twenty-five');
          490  +} {
          491  +  UPDATE t1 SET a = b WHERE b<=4;
          492  +  INSERT INTO t2 SELECT NULL, * FROM t1;
          493  +  DELETE FROM t1 WHERE b=25;
          494  +} {
          495  +  DELETE FROM t2;
          496  +  INSERT INTO t2 SELECT NULL, * FROM t1;
          497  +  DELETE FROM t1;
          498  +  INSERT INTO t1 SELECT b, c, d FROM t2;
          499  +  UPDATE t1 SET b = b+1;
          500  +  UPDATE t1 SET b = b+1;
          501  +  UPDATE t1 SET b = b+1;
          502  +}
          503  +
   331    504   
          505  +finish_test
   332    506