This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
/
test_database_queries_utf_16.rb
82 lines (73 loc) · 3.16 KB
/
test_database_queries_utf_16.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
if RUBY_VERSION >= '1.9.1'
require "helper"
class TestDatabaseQueriesUtf16 < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new(":memory:", :encoding => "utf-16")
@db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu1 NUMERIC, i1 INTEGER, i2 INTEGER, no BLOB)")
end
def teardown
@db.close
end
def test_tables_empty
assert_equal [], @db.execute("SELECT * FROM t1")
end
def test_execute
@db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)")
rows = @db.execute("SELECT * FROM t1")
assert_equal 1, rows.size
row = rows[0]
assert_equal "text1".encode(Encoding::UTF_16LE), row[1]
assert_equal Encoding::UTF_16LE, row[1].encoding
assert_equal 1.22, row[2]
assert_equal 42, row[3]
assert_equal 4294967296, row[4]
assert_nil row[5]
end
def test_execute_with_bindings
blob = open("test/fixtures/SQLite.gif", "rb").read
@db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob)
rows = @db.execute("SELECT * FROM t1")
assert_equal 1, rows.size
row = rows[0]
assert_equal "text1".encode(Encoding::UTF_16LE), row[1]
assert_equal Encoding::UTF_16LE, row[1].encoding
assert_equal 1.22, row[2]
assert_equal 42, row[3]
assert_equal 4294967296, row[4]
assert_equal blob, row[5]
assert_equal Encoding::ASCII_8BIT, row[5].encoding
end
def test_execute_with_different_encodings
expected_string = "text1"
@db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT))
@db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8))
@db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE))
@db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE))
rows = @db.execute("SELECT * FROM t1")
assert_equal 4, rows.size
assert_equal expected_string, rows[0][1]
assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][1]
assert_equal expected_string.encode(Encoding::UTF_16LE), rows[2][1]
assert_equal expected_string.encode(Encoding::UTF_16LE), rows[3][1]
assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding
assert_equal Encoding::UTF_16LE, rows[1][1].encoding
assert_equal Encoding::UTF_16LE, rows[2][1].encoding
assert_equal Encoding::UTF_16LE, rows[3][1].encoding
end
def test_execute_with_bad_query
assert_raise(SQLite3::SQLException) { @db.execute("bad query") }
assert_equal %Q{near "bad": syntax error}, @db.errmsg
assert_equal 1, @db.errcode
end
def test_last_insert_row_id
@db.execute("INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL)")
id = @db.last_insert_row_id
rows = @db.execute("SELECT * FROM t1 WHERE id = #{id}")
assert_equal 1, rows.size
end
# def test_execute_with_closed_database
# @db.close
# @db.execute("SELECT * FROM t1")
# end
end
end