test_collation.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. require 'helper'
  3. module SQLite3
  4. class TestCollation < SQLite3::TestCase
  5. class Comparator
  6. attr_reader :calls
  7. def initialize
  8. @calls = []
  9. end
  10. def compare left, right
  11. @calls << [left, right]
  12. left <=> right
  13. end
  14. end
  15. def setup
  16. @db = SQLite3::Database.new(':memory:')
  17. @create = "create table ex(id int, data string)"
  18. @db.execute(@create);
  19. [ [1, 'hello'], [2, 'world'] ].each do |vals|
  20. @db.execute('insert into ex (id, data) VALUES (?, ?)', vals)
  21. end
  22. end
  23. def test_custom_collation
  24. comparator = Comparator.new
  25. @db.collation 'foo', comparator
  26. assert_equal comparator, @db.collations['foo']
  27. @db.execute('select data from ex order by 1 collate foo')
  28. assert_equal 1, comparator.calls.length
  29. end
  30. def test_remove_collation
  31. comparator = Comparator.new
  32. @db.collation 'foo', comparator
  33. @db.collation 'foo', nil
  34. assert_nil @db.collations['foo']
  35. assert_raises(SQLite3::SQLException) do
  36. @db.execute('select data from ex order by 1 collate foo')
  37. end
  38. end
  39. if RUBY_VERSION >= '1.9.1'
  40. def test_encoding
  41. comparator = Comparator.new
  42. @db.collation 'foo', comparator
  43. @db.execute('select data from ex order by 1 collate foo')
  44. a, b = *comparator.calls.first
  45. assert_equal Encoding.find('UTF-8'), a.encoding
  46. assert_equal Encoding.find('UTF-8'), b.encoding
  47. end
  48. def test_encoding_default_internal
  49. warn_before = $-w
  50. $-w = false
  51. before_enc = Encoding.default_internal
  52. Encoding.default_internal = 'EUC-JP'
  53. comparator = Comparator.new
  54. @db.collation 'foo', comparator
  55. @db.execute('select data from ex order by 1 collate foo')
  56. a, b = *comparator.calls.first
  57. assert_equal Encoding.find('EUC-JP'), a.encoding
  58. assert_equal Encoding.find('EUC-JP'), b.encoding
  59. ensure
  60. Encoding.default_internal = before_enc
  61. $-w = warn_before
  62. end
  63. end
  64. end
  65. end