# 2017 April 25
#
# 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.  The
# focus of this script is testing the server mode of SQLite.
#


set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix server2

#-------------------------------------------------------------------------
# Check that the *-hma file is deleted correctly.
#
do_execsql_test 1.0 {
  CREATE TABLE t1(a, b);
} {}
do_test 1.1 {
  file exists test.db-hma
} {1}
do_test 1.2 {
  db close
  file exists test.db-hma
} {0}
do_test 1.3 {
  sqlite3 db test.db
  db eval { CREATE TABLE t2(a, b) }
  sqlite3 db2 test.db
  db2 eval { CREATE TABLE t3(a, b) }
  file exists test.db-hma
} {1}
do_test 1.4 {
  db2 close
  file exists test.db-hma
} {1}
do_test 1.5 {
  db close
  file exists test.db-hma
} {0}


#-------------------------------------------------------------------------
#
reset_db
sqlite3 db2 test.db

do_execsql_test 2.0 {
  CREATE TABLE t1(a, b);
  CREATE TABLE t2(c, d);
}

# Two concurrent transactions committed.
#
do_test 2.1 {
  db eval {
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  db2 eval {
    BEGIN;
      INSERT INTO t2 VALUES(3, 4);
  }
} {}
do_test 2.2 {
  lsort [glob test.db*]
} {test.db test.db-hma test.db-journal0 test.db-journal1}
do_test 2.3.1 { db eval COMMIT  } {}
do_test 2.3.2 { db2 eval COMMIT } {}
do_execsql_test 2.4 {SELECT * FROM t1, t2} {1 2 3 4}
do_test 2.5 {
  lsort [glob test.db*]
} {test.db test.db-hma test.db-journal0 test.db-journal1}

do_test 2.6 {
  execsql {BEGIN}
  execsql {INSERT INTO t1 VALUES(5, 6)}

  execsql {BEGIN} db2
  catchsql {INSERT INTO t1 VALUES(7, 8)} db2
} {1 {database is locked}}
do_test 2.7 {
  # Transaction is automatically rolled back in this case.
  sqlite3_get_autocommit db2
} {1}
do_test 2.8 {
  execsql COMMIT
  execsql { SELECT * FROM t1 } db2
} {1 2 5 6}



finish_test

