package require Itcl
package require sqlite3
package require tcltest

source /home/nagu/Downloads/dio/dio.tcl
source /home/nagu/Downloads/dio/dio_Sqlite.tcl

set testdb "/home/nagu/Downloads/dio/test/test.db"
set test2db "/home/nagu/Downloads/dio/test/test2.db"

set interface "Sqlite"

# handle-01.01

	::tcltest::test handle-01.01 {
		Test handle command
	} -body {
		::DIO::handle $interface dbcmd -db $testdb
	} -cleanup {
		dbcmd close
		dbcmd destroy
	} -result {dbcmd} 

# interface-01.01

	::tcltest::test interface-01.01 {
		Test interface command
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
	} -body {
		dbcmd interface
	} -cleanup {
		dbcmd close
		dbcmd destroy
	} -result {Sqlite} 

# db_handle-01.01

	::tcltest::test db_handle-01.01 {
		Check the value of db that was configured through handle command
	} -setup {
		set dbcmd [::DIO::handle $interface -db $testdb]
	} -body {
		string equal [$dbcmd db] $testdb
	} -cleanup {
		$dbcmd destroy
	} -result {1}

# db_db-01.01

	::tcltest::test db_db-01.01 {
		Check the value of db that was configured through db command
	} -setup {
		::DIO::handle $interface dbcmd -db $test2db
	} -body {
		dbcmd db $testdb
		string equal [dbcmd db] $testdb
	} -cleanup {
		dbcmd destroy
	} -result {1}

# exec01-01.01

	::tcltest::test exec01-01.01 {
		Test exec command by executing various sql statements
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
	} -body {
		set result [dbcmd exec {pragma table_info(t1)}]
		$result numrows
	} -cleanup {
		dbcmd destroy
	} -result {2}

# exec02-01.01

	::tcltest::test exec02-01.01 {
		Test exec command by executing various sql statements
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
		dbcmd table t1
	} -body {
		dbcmd exec {insert into t1 (a, b) values ('1', 'hello')}
		dbcmd count
	} -cleanup {
		dbcmd destroy
	} -result {1}

# table-01.01

	::tcltest::test table-01.01 {
		Configure table option and check the return value
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
		dbcmd table t1
	} -body {
		dbcmd table
	} -cleanup {
		dbcmd destroy
	} -result {t1}

# keyfield-01.01

	::tcltest::test keyfield-01.01 {
		Configure keyfield option and check the return value
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
		dbcmd table t1
		dbcmd keyfield a
	} -body {
		dbcmd keyfield
	} -cleanup {
		dbcmd destroy
	} -result {a}

# insert-01.01

	::tcltest::test insert-01.01 {
		Test insert command
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
		dbcmd configure -table t1 -keyfield a
		array set irec [list a 1 b hello]
	} -body {
		dbcmd insert irec
		dbcmd count
	} -cleanup {
		dbcmd destroy
	} -output {1}

# fetch-01.01

	::tcltest::test fetch-01.01 {
		Test fetch command
	} -setup {
		::DIO::handle $interface dbcmd -db $testdb
		dbcmd exec {drop table if exists t1; create table t1(a int primary key, b text)}
		dbcmd configure -table t1 -keyfield a
		array set irec1 [list a 1 b hello]
		array set irec2 [list a 2 b world]
		array set orec {}
	} -body {
		dbcmd insert irec1
		dbcmd insert irec2
		dbcmd fetch 2 orec
		array get orec
	} -cleanup {
		dbcmd destroy
	} -output {a 2 b world}


::tcltest::runAllTests
