Class: SQLite3::TestCollation

Inherits:
TestCase
  • Object
show all
Defined in:
lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb,
lib/sqlite3-1.5.3-x86_64-darwin/test/test_collation.rb

Defined Under Namespace

Classes: Comparator

Instance Method Summary collapse

Methods inherited from TestCase

#assert_nothing_raised

Instance Method Details

#setupObject



19
20
21
22
23
24
25
26
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb', line 19

def setup
  @db = SQLite3::Database.new(':memory:')
  @create = "create table ex(id int, data string)"
  @db.execute(@create);
  [ [1, 'hello'], [2, 'world'] ].each do |vals|
    @db.execute('insert into ex (id, data) VALUES (?, ?)', vals)
  end
end

#test_custom_collationObject



28
29
30
31
32
33
34
35
36
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb', line 28

def test_custom_collation
  comparator = Comparator.new

  @db.collation 'foo', comparator

  assert_equal comparator, @db.collations['foo']
  @db.execute('select data from ex order by 1 collate foo')
  assert_equal 1, comparator.calls.length
end

#test_encodingObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb', line 51

def test_encoding
  comparator = Comparator.new
  @db.collation 'foo', comparator
  @db.execute('select data from ex order by 1 collate foo')

  a, b = *comparator.calls.first

  assert_equal Encoding.find('UTF-8'), a.encoding
  assert_equal Encoding.find('UTF-8'), b.encoding
end

#test_encoding_default_internalObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb', line 62

def test_encoding_default_internal
  warn_before = $-w
  $-w = false
  before_enc = Encoding.default_internal

  Encoding.default_internal = 'EUC-JP'
  comparator = Comparator.new
  @db.collation 'foo', comparator
  @db.execute('select data from ex order by 1 collate foo')

  a, b = *comparator.calls.first

  assert_equal Encoding.find('EUC-JP'), a.encoding
  assert_equal Encoding.find('EUC-JP'), b.encoding
ensure
  Encoding.default_internal = before_enc
  $-w = warn_before
end

#test_remove_collationObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_collation.rb', line 38

def test_remove_collation
  comparator = Comparator.new

  @db.collation 'foo', comparator
  @db.collation 'foo', nil

  assert_nil @db.collations['foo']
  assert_raises(SQLite3::SQLException) do
    @db.execute('select data from ex order by 1 collate foo')
  end
end