###############################################################################
#
# progress.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

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

runTest {test progress-1.1 {no progress without ProgressOps} -setup {
  setupDb [set fileName progress-1.1.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  set sql { \
    CREATE TABLE t1(x INTEGER); \
    INSERT INTO t1 (x) VALUES(1); \
    INSERT INTO t1 (x) VALUES(2); \
    INSERT INTO t1 (x) VALUES(3); \
    INSERT INTO t1 (x) VALUES(4); \
    SELECT x FROM t1 ORDER BY x; \
  }

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        private static int count = 0;

        ///////////////////////////////////////////////////////////////////////

        public static void MyProgressHandler(
          object sender,
          ProgressEventArgs e
          )
        {
          count++;
        }

        ///////////////////////////////////////////////////////////////////////

        public static int Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};[getTestProperties]"))
          {
            connection.Progress += MyProgressHandler;
            connection.Open();

            using (SQLiteCommand command = new SQLiteCommand("${sql}",
                connection))
            {
              command.ExecuteNonQuery();
            }
          }

          return count;
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result \
      [expr {[string is integer -strict $result] && $result == 0 ? 1 : 0}]
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 0 1$}}

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

runTest {test progress-1.2 {simple progress counter} -setup {
  setupDb [set fileName progress-1.2.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  set sql { \
    CREATE TABLE t1(x INTEGER); \
    INSERT INTO t1 (x) VALUES(1); \
    INSERT INTO t1 (x) VALUES(2); \
    INSERT INTO t1 (x) VALUES(3); \
    INSERT INTO t1 (x) VALUES(4); \
    SELECT x FROM t1 ORDER BY x; \
  }

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        private static int count = 0;

        ///////////////////////////////////////////////////////////////////////

        public static void MyProgressHandler(
          object sender,
          ProgressEventArgs e
          )
        {
          count++;
        }

        ///////////////////////////////////////////////////////////////////////

        public static int Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};ProgressOps=1;[getTestProperties]"))
          {
            connection.Progress += MyProgressHandler;
            connection.Open();

            using (SQLiteCommand command = new SQLiteCommand("${sql}",
                connection))
            {
              command.ExecuteNonQuery();
            }
          }

          return count;
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result \
      [expr {[string is integer -strict $result] && $result > 0 ? 1 : 0}]
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \d+ 1$}}

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

runTest {test progress-1.3 {progress with interrupt} -setup {
  setupDb [set fileName progress-1.3.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  set sql { \
    CREATE TABLE t1(x INTEGER); \
    INSERT INTO t1 (x) VALUES(1); \
    INSERT INTO t1 (x) VALUES(2); \
    INSERT INTO t1 (x) VALUES(3); \
    INSERT INTO t1 (x) VALUES(4); \
    SELECT x FROM t1 ORDER BY x; \
  }

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        private static int count = 0;

        ///////////////////////////////////////////////////////////////////////

        public static void MyProgressHandler(
          object sender,
          ProgressEventArgs e
          )
        {
          count++;
          e.ReturnCode = SQLiteProgressReturnCode.Interrupt;
        }

        ///////////////////////////////////////////////////////////////////////

        public static int Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};ProgressOps=1;[getTestProperties]"))
          {
            connection.Progress += MyProgressHandler;
            connection.Open();

            using (SQLiteCommand command = new SQLiteCommand("${sql}",
                connection))
            {
              command.ExecuteNonQuery();
            }
          }

          return count;
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result \
      [expr {[string is integer -strict $result] && $result > 0 ? 1 : 0}]
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \d+ 1$}}

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

runTest {test progress-1.4 {progress with exception} -setup {
  setupDb [set fileName progress-1.4.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  set sql { \
    CREATE TABLE t1(x INTEGER); \
    INSERT INTO t1 (x) VALUES(1); \
    INSERT INTO t1 (x) VALUES(2); \
    INSERT INTO t1 (x) VALUES(3); \
    INSERT INTO t1 (x) VALUES(4); \
    SELECT x FROM t1 ORDER BY x; \
  }

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System;
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        private static int count = 0;

        ///////////////////////////////////////////////////////////////////////

        public static void MyProgressHandler(
          object sender,
          ProgressEventArgs e
          )
        {
          count++;
          throw new Exception();
        }

        ///////////////////////////////////////////////////////////////////////

        public static int Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};ProgressOps=1;[getTestProperties]"))
          {
            connection.Progress += MyProgressHandler;
            connection.Open();

            using (SQLiteCommand command = new SQLiteCommand("${sql}",
                connection))
            {
              command.ExecuteNonQuery();
            }
          }

          return count;
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result \
      [expr {[string is integer -strict $result] && $result > 0 ? 1 : 0}]
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \d+ 1$}}

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

runSQLiteTestEpilogue
runTestEpilogue
