require "test/unit"
require "phi"
require "rdb/rdb"
require "rdb/bde"
require "rdb/dbx"

# DBX
class TCSQLConnection < Test::Unit::TestCase
  def test_new
    connection = Phi::SQLConnection.new 'IBLocal'
  end
end

class TCSQLDataSet < Test::Unit::TestCase
  def test_new
    connection = Phi::SQLConnection.new 'IBLocal'
    data_set = Phi::SQLDataSet.new connection
  end
end

# RDB
class TCDataSetProvider < Test::Unit::TestCase
  def test_new
    connection = Phi::SQLConnection.new 'IBLocal'
    data_set = Phi::SQLDataSet.new connection
    provider = Phi::DataSetProvider.new data_set
  end
end

class TCDataSource < Test::Unit::TestCase
  def test_new
    table = Phi::Table.new
    data_source = Phi::DataSource.new table
  end
end

class TCClientDataSet < Test::Unit::TestCase
  def test_field_values
    connection = Phi::SQLConnection.new #'IBLocal'

    connection.load_params_on_connect = false
    connection.connection_name = 'IBLocal'
    ini_file = 'C:/home/moriq/wc/apollo/bin/dbxconnections.ini'.tr('/','\\')
    connection.load_params_from_ini_file(ini_file)

    connection.driver_name = 'Interbase' # connection.params['DriverName']
    connection.get_driver_func = 'getSQLDriverINTERBASE'
    connection.library_name = 'dbexpint.dll'
    connection.vendor_lib = 'GDS32.DLL'

    connection.login_prompt = false
    connection.params['Database'] = 'C:/home/moriq/wc/apollo/bin/database.gdb'.tr('/','\\')

    data_set = Phi::SQLDataSet.new connection
    data_set.command_text = 'select * from EMPLOYEE'
    provider = Phi::DataSetProvider.new data_set
    c = Phi::ClientDataSet.new
    c.set_provider provider
    c.open
    begin
      assert_equal 'Robert', c.field_values['First_Name']

      assert_equal 0, c.change_count
      c.apply_updates(-1)

      index_name = 'First_Name_Last_Name_Index'
      c.add_index index_name, 'First_Name;Last_Name', [Phi::IX_CASE_INSENSITIVE]
      assert_equal 3, c.index_names.count
      assert_equal index_name, c.index_names[2]
    ensure
      c.close
    end
  end
end
