xref: /sqlite-3.40.0/ext/session/session6.test (revision a32536b4)
1e437ca5eSdan# 2011 July 11
2e437ca5eSdan#
3e437ca5eSdan# The author disclaims copyright to this source code.  In place of
4e437ca5eSdan# a legal notice, here is a blessing:
5e437ca5eSdan#
6e437ca5eSdan#    May you do good and not evil.
7e437ca5eSdan#    May you find forgiveness for yourself and forgive others.
8e437ca5eSdan#    May you share freely, never taking more than you give.
9e437ca5eSdan#
10e437ca5eSdan#***********************************************************************
11e437ca5eSdan# This file implements regression tests for SQLite sessions extension.
12e437ca5eSdan# Specifically, it tests that sessions work when the database is modified
13e437ca5eSdan# using incremental blob handles.
14e437ca5eSdan#
15e437ca5eSdan
16e437ca5eSdanif {![info exists testdir]} {
17e437ca5eSdan  set testdir [file join [file dirname [info script]] .. .. test]
18e437ca5eSdan}
19e437ca5eSdansource [file join [file dirname [info script]] session_common.tcl]
20e437ca5eSdansource $testdir/tester.tcl
21e437ca5eSdanifcapable !session {finish_test; return}
22*a32536b4Sdanifcapable !incrblob {finish_test; return}
23e437ca5eSdan
24e437ca5eSdanset testprefix session6
25e437ca5eSdan
26e437ca5eSdanproc do_then_apply_tcl {tcl {dbname main}} {
27e437ca5eSdan  proc xConflict args { return "OMIT" }
28e437ca5eSdan  set rc [catch {
29e437ca5eSdan    sqlite3session S db $dbname
30e437ca5eSdan    db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" {
31e437ca5eSdan      S attach $name
32e437ca5eSdan    }
33e437ca5eSdan    eval $tcl
34e437ca5eSdan    sqlite3changeset_apply db2 [S changeset] xConflict
35e437ca5eSdan  } msg]
36e437ca5eSdan
37e437ca5eSdan  catch { S delete }
38e437ca5eSdan  if {$rc} {error $msg}
39e437ca5eSdan}
40e437ca5eSdan
41e437ca5eSdantest_sqlite3_log x
42e437ca5eSdanproc x {args} {puts $args}
43e437ca5eSdan
44e437ca5eSdanforcedelete test.db2
45e437ca5eSdansqlite3 db2 test.db2
46e437ca5eSdan
47e437ca5eSdando_common_sql {
48e437ca5eSdan  CREATE TABLE t1(a PRIMARY KEY, b);
49e437ca5eSdan  CREATE TABLE t2(c PRIMARY KEY, d);
50e437ca5eSdan}
51e437ca5eSdan
52e437ca5eSdan# Test a blob update.
53e437ca5eSdan#
54e437ca5eSdando_test 1.1 {
55e437ca5eSdan  do_then_apply_tcl {
56e437ca5eSdan    db eval { INSERT INTO t1 VALUES(1, 'helloworld') }
57e437ca5eSdan    db eval { INSERT INTO t2 VALUES(2, 'onetwothree') }
58e437ca5eSdan  }
59e437ca5eSdan  compare_db db db2
60e437ca5eSdan} {}
61e437ca5eSdando_test 1.2 {
62e437ca5eSdan  do_then_apply_tcl {
63e437ca5eSdan    set fd [db incrblob t1 b 1]
64e437ca5eSdan    puts -nonewline $fd 1234567890
65e437ca5eSdan    close $fd
66e437ca5eSdan  }
67e437ca5eSdan  compare_db db db2
68e437ca5eSdan} {}
69e437ca5eSdan
70e437ca5eSdan# Test an attached database.
71e437ca5eSdan#
72e437ca5eSdando_test 2.1 {
73e437ca5eSdan  forcedelete test.db3
74e437ca5eSdan  file copy test.db2 test.db3
75e437ca5eSdan  execsql { ATTACH 'test.db3' AS aux; }
76e437ca5eSdan
77e437ca5eSdan  do_then_apply_tcl {
78e437ca5eSdan    set fd [db incrblob aux t2 d 1]
79e437ca5eSdan    puts -nonewline $fd fourfivesix
80e437ca5eSdan    close $fd
81e437ca5eSdan  } aux
82e437ca5eSdan
83e437ca5eSdan  sqlite3 db3 test.db3
84e437ca5eSdan  compare_db db2 db3
85e437ca5eSdan} {}
86e437ca5eSdan
87e437ca5eSdan
88e437ca5eSdandb3 close
89e437ca5eSdandb2 close
90e437ca5eSdan
91e437ca5eSdanfinish_test
92