# 2018 Feb 11
#
# 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.
#
#***********************************************************************
#
# The focus of this file is testing the zonefile extension.
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
}
source [file join $testdir tester.tcl]
set testprefix zonefile1
load_static_extension db zonefile

do_execsql_test 1.0 {
  CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB);
  INSERT INTO zz VALUES(1, -1, -1, randomblob(100));
  INSERT INTO zz VALUES(2, -1, -1, randomblob(100));
  INSERT INTO zz VALUES(3, -1, -1, randomblob(100));
}

do_execsql_test 1.1 {
  SELECT zonefile_write('test.zonefile', 'zz');
} {{}}

do_execsql_test 1.2 {
  CREATE VIRTUAL TABLE z1 USING zonefile;
  SELECT name FROM sqlite_master WHERE name LIKE 'z1%' ORDER BY 1;
} {z1 z1_files z1_shadow_file z1_shadow_idx}

do_execsql_test 1.3 {
  INSERT INTO z1_files(filename) VALUES('test.zonefile');
  SELECT filename, 
         json_extract(header, '$.magicNumber'),
         json_extract(header, '$.numFrames'),
         json_extract(header, '$.numKeys')
         FROM z1_files;
} {test.zonefile 1179332920 1 3}

do_execsql_test 1.4 { SELECT count(*) FROM z1_shadow_idx } 3

do_execsql_test 1.5.1 { SELECT k FROM z1 } {1 2 3}
do_execsql_test 1.5.2 { SELECT fileid FROM z1 } {1 1 1}
do_execsql_test 1.5.3 { SELECT frame FROM z1 } {0 0 0}
do_execsql_test 1.5.4 { SELECT sz FROM z1 } {100 100 100}

do_execsql_test 1.5.5 {
  SELECT zz.v==z1.v FROM zz, z1 WHERE zz.k=z1.k
} {1 1 1}

do_execsql_test 1.5 {
  DELETE FROM z1_files;
  SELECT * FROM z1_files;
} {}

do_execsql_test 1.6 { SELECT count(*) FROM z1_shadow_idx } 0

do_execsql_test 1.7 { DROP TABLE z1 }

finish_test

