###############################################################################
#
# tkt-58ed318f2f.eagle --
#
# Written by Joe Mistachkin.
# Released to the public domain, use at your own risk!
#
###############################################################################

package require Eagle
package require Eagle.Library
package require Eagle.Test

runTestPrologue

###############################################################################

package require System.Data.SQLite.Test
runSQLiteTestPrologue

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.1 {standard GetDefaultDbType usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.1.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == 1}
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.2 {no property GetDefaultDbType usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.2.db] "" "" "" UseConnectionTypes
} -body {
  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == 1}
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.3 {no flag GetDefaultDbType usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.3.db] "" "" "" "" \
      "DefaultDbType=String;"
} -body {
  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == -1}
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.4 {zero GetDefaultDbType usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.4.db] "" "" "" UseConnectionTypes \
      "DefaultDbType=String;"
} -body {
  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == -1}
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.5 {standard GetDefaultTypeName usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.5.db]
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == 1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.6 {no property GetDefaultTypeName usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.6.db] "" "" "" UseConnectionTypes
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == 1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.7 {no flag GetDefaultTypeName usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.7.db] "" "" "" "" \
      "DefaultDbType=String;"
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == -1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.8 {zero GetDefaultTypeName usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.8.db] "" "" "" UseConnectionTypes \
      "DefaultTypeName=TEXT;"
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == -1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.9 {zero DefaultDbType settings read} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.9.db] "" "" "" NoConvertSettings
} -body {
  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == -1}
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.10 {zero DefaultTypeName settings read} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.10.db] "" "" "" NoConvertSettings
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == -1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.11 {normal SQLiteConvert settings usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.11.db]
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"
  set columns [$connection GetSchema COLUMNS]

  #
  # TODO: These counts may need to be updated in future versions.
  #
  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == 1 && \
      [getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == 1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.12 {zero SQLiteConvert settings usage} -setup {
  setupDb [set fileName tkt-58ed318f2f-1.12.db] "" "" "" NoConvertSettings
} -body {
  set connection [getDbConnection]

  sql execute $db {
    CREATE TABLE t1(x, y);
    INSERT INTO t1 (x, y) VALUES(0, 1);
    INSERT INTO t1 (x, y) VALUES('0', '1');
  }

  sql execute -execute reader -format list $db "SELECT x, y FROM t1;"
  set columns [$connection GetSchema COLUMNS]

  expr {[getSettingReadCount Use_SQLiteConvert_DefaultDbType] == -1 && \
      [getSettingReadCount Use_SQLiteConvert_DefaultTypeName] == -1}
} -cleanup {
  cleanupDb $fileName

  freeDbConnection

  unset -nocomplain columns connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.13 {zero settings usage} -setup {
  saveGetSettingValueEnvironment

  set env(No_SQLiteGetSettingValue) 1

  setupDb [set fileName tkt-58ed318f2f-1.13.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x);
    INSERT INTO t1 (x) VALUES(0);
  }

  sql execute -execute reader -format list $db "SELECT x FROM t1;"

  expr {[getSettingReadCount ""] eq "0 0"}
} -cleanup {
  cleanupDb $fileName
  restoreGetSettingValueEnvironment

  unset -nocomplain db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runTest {test tkt-58ed318f2f-1.14 {zero XML configuration file usage} -setup {
  saveGetSettingValueEnvironment

  set env(No_SQLiteXmlConfigFile) 1

  setupDb [set fileName tkt-58ed318f2f-1.14.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x);
    INSERT INTO t1 (x) VALUES(0);
  }

  sql execute -execute reader -format list $db "SELECT x FROM t1;"

  set counts [getSettingReadCount ""]

  expr {[lindex $counts 0] > 0 && [lindex $counts 1] > 0 && \
      [getSettingReadCount "" true] eq "0 0"}
} -cleanup {
  cleanupDb $fileName
  restoreGetSettingValueEnvironment

  unset -nocomplain counts db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite buildConfiguration.Debug} -result {True}}

###############################################################################

reportSQLiteResources $test_channel true

###############################################################################

runSQLiteTestEpilogue
runTestEpilogue
