class TC_MysqlRes

Public Instance Methods

setup() click to toggle source
# File test.rb, line 213
def setup()
  @host, @user, @pass, db, port, sock, flag = ARGV
  @db = db || "test"
  @port = port.to_i
  @sock = sock.nil? || sock.empty? ? nil : sock
  @flag = flag.to_i
  @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  @m.query("create temporary table t (id int, str char(10), primary key (id))")
  @m.query("insert into t values (1, 'abc'), (2, 'defg'), (3, 'hi'), (4, null)")
  @res = @m.query("select * from t")
end
teardown() click to toggle source
# File test.rb, line 224
def teardown()
  @res.free
  @m.close
end
test_data_seek() click to toggle source
# File test.rb, line 275
def test_data_seek()
  assert_equal(["1","abc"], @res.fetch_row)
  assert_equal(["2","defg"], @res.fetch_row)
  assert_equal(["3","hi"], @res.fetch_row)
  @res.data_seek(1)
  assert_equal(["2","defg"], @res.fetch_row)
end
test_each() click to toggle source
# File test.rb, line 261
def test_each()
  ary = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
  @res.each do |a|
    assert_equal(ary.shift, a)
  end
end
test_each_hash() click to toggle source
# File test.rb, line 268
def test_each_hash()
  hash = [{"id"=>"1","str"=>"abc"}, {"id"=>"2","str"=>"defg"}, {"id"=>"3","str"=>"hi"}, {"id"=>"4","str"=>nil}]
  @res.each_hash do |h|
    assert_equal(hash.shift, h)
  end
end
test_fetch_field() click to toggle source
# File test.rb, line 302
def test_fetch_field()
  f = @res.fetch_field
  assert_equal("id", f.name)
  assert_equal("t", f.table)
  assert_equal(nil, f.def)
  assert_equal(Mysql::Field::TYPE_LONG, f.type)
  assert_equal(11, f.length)
  assert_equal(1, f.max_length)
  assert_equal(Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG, f.flags)
  assert_equal(0, f.decimals)
  f = @res.fetch_field
  assert_equal("str", f.name)
  assert_equal("t", f.table)
  assert_equal(nil, f.def)
  assert_equal(Mysql::Field::TYPE_STRING, f.type)
  assert_equal(10, f.length)
  assert_equal(4, f.max_length)
  assert_equal(0, f.flags)
  assert_equal(0, f.decimals)
  f = @res.fetch_field
  assert_equal(nil, f)
end
test_fetch_field_direct() click to toggle source
# File test.rb, line 332
def test_fetch_field_direct()
  f = @res.fetch_field_direct(0)
  assert_equal("id", f.name)
  f = @res.fetch_field_direct(1)
  assert_equal("str", f.name)
  assert_raises(Mysql::Error){@res.fetch_field_direct(-1)}
  assert_raises(Mysql::Error){@res.fetch_field_direct(2)}
end
test_fetch_fields() click to toggle source
# File test.rb, line 325
def test_fetch_fields()
  a = @res.fetch_fields
  assert_equal(2, a.size)
  assert_equal("id", a[0].name)
  assert_equal("str", a[1].name)
end
test_fetch_hash() click to toggle source
# File test.rb, line 245
def test_fetch_hash()
  assert_equal({"id"=>"1", "str"=>"abc"}, @res.fetch_hash)
  assert_equal({"id"=>"2", "str"=>"defg"}, @res.fetch_hash)
  assert_equal({"id"=>"3", "str"=>"hi"}, @res.fetch_hash)
  assert_equal({"id"=>"4", "str"=>nil}, @res.fetch_hash)
  assert_equal(nil, @res.fetch_hash)
end
test_fetch_hash2() click to toggle source
# File test.rb, line 253
def test_fetch_hash2()
  assert_equal({"t.id"=>"1", "t.str"=>"abc"}, @res.fetch_hash(true))
  assert_equal({"t.id"=>"2", "t.str"=>"defg"}, @res.fetch_hash(true))
  assert_equal({"t.id"=>"3", "t.str"=>"hi"}, @res.fetch_hash(true))
  assert_equal({"t.id"=>"4", "t.str"=>nil}, @res.fetch_hash(true))
  assert_equal(nil, @res.fetch_hash)
end
test_fetch_lengths() click to toggle source
# File test.rb, line 341
def test_fetch_lengths()
  assert_equal(nil,  @res.fetch_lengths())
  @res.fetch_row
  assert_equal([1, 3],  @res.fetch_lengths())
  @res.fetch_row
  assert_equal([1, 4],  @res.fetch_lengths())
  @res.fetch_row
  assert_equal([1, 2],  @res.fetch_lengths())
  @res.fetch_row
  assert_equal([1, 0],  @res.fetch_lengths())
  @res.fetch_row
  assert_equal(nil,  @res.fetch_lengths())
end
test_fetch_row() click to toggle source
# File test.rb, line 237
def test_fetch_row()
  assert_equal(["1","abc"], @res.fetch_row)
  assert_equal(["2","defg"], @res.fetch_row)
  assert_equal(["3","hi"], @res.fetch_row)
  assert_equal(["4",nil], @res.fetch_row)
  assert_equal(nil, @res.fetch_row)
end
test_field_hash() click to toggle source
# File test.rb, line 355
def test_field_hash()
  f = @res.fetch_field
  h = {
    "name" => "id",
    "table" => "t",
    "def" => nil,
    "type" => Mysql::Field::TYPE_LONG,
    "length" => 11,
    "max_length" => 1,
    "flags" => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
    "decimals" => 0,
  }
  assert_equal(h, f.hash)
  f = @res.fetch_field
  h = {
    "name" => "str",
    "table" => "t",
    "def" => nil,
    "type" => Mysql::Field::TYPE_STRING,
    "length" => 10,
    "max_length" => 4,
    "flags" => 0,
    "decimals" => 0,
  }
  assert_equal(h, f.hash)
end
test_field_inspect() click to toggle source
# File test.rb, line 382
def test_field_inspect()
  f = @res.fetch_field
  assert_equal("#<Mysql::Field:id>", f.inspect)
  f = @res.fetch_field
  assert_equal("#<Mysql::Field:str>", f.inspect)
end
test_field_seek() click to toggle source
# File test.rb, line 292
def test_field_seek()
  assert_equal(0, @res.field_tell)
  @res.fetch_field
  assert_equal(1, @res.field_tell)
  @res.fetch_field
  assert_equal(2, @res.field_tell)
  @res.field_seek(1)
  assert_equal(1, @res.field_tell)
end
test_is_not_null() click to toggle source
# File test.rb, line 396
def test_is_not_null()
  f = @res.fetch_field
  assert_equal(true, f.is_not_null?)
  f = @res.fetch_field
  assert_equal(false, f.is_not_null?)
end
test_is_num() click to toggle source
# File test.rb, line 389
def test_is_num()
  f = @res.fetch_field
  assert_equal(true, f.is_num?)
  f = @res.fetch_field
  assert_equal(false, f.is_num?)
end
test_is_pri_key() click to toggle source
# File test.rb, line 403
def test_is_pri_key()
  f = @res.fetch_field
  assert_equal(true, f.is_pri_key?)
  f = @res.fetch_field
  assert_equal(false, f.is_pri_key?)
end
test_num_fields() click to toggle source
# File test.rb, line 229
def test_num_fields()
  assert_equal(2, @res.num_fields)
end
test_num_rows() click to toggle source
# File test.rb, line 233
def test_num_rows()
  assert_equal(4, @res.num_rows)
end
test_row_seek() click to toggle source
# File test.rb, line 283
def test_row_seek()
  assert_equal(["1","abc"], @res.fetch_row)
  pos = @res.row_tell
  assert_equal(["2","defg"], @res.fetch_row)
  assert_equal(["3","hi"], @res.fetch_row)
  @res.row_seek(pos)
  assert_equal(["2","defg"], @res.fetch_row)
end