File: test_collation.rb

package info (click to toggle)
ruby-sqlite3 1.7.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,772 kB
  • sloc: ruby: 3,839; ansic: 1,470; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 2,118 bytes parent folder | download | duplicates (8)
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
# -*- coding: utf-8 -*-

require 'helper'

module SQLite3
  class TestCollation < SQLite3::TestCase
    class Comparator
      attr_reader :calls
      def initialize
        @calls = []
      end

      def compare left, right
        @calls << [left, right]
        left <=> right
      end
    end

    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

    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

    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

    if RUBY_VERSION >= '1.9.1'
      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

      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
    end
  end
end