File: encoding_spec.rb

package info (click to toggle)
ruby-dataobjects 0.10.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 396 kB
  • ctags: 193
  • sloc: ruby: 2,921; makefile: 4
file content (163 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (4)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
shared_examples_for 'a driver supporting different encodings' do

  describe 'character_set' do

    before do
      @connection = DataObjects::Connection.new(CONFIG.uri)
    end

    after do
      @connection.close
    end

    it { @connection.should respond_to(:character_set) }

    it 'uses utf8 by default' do
      @connection.character_set.should == 'UTF-8'
    end

    describe 'sets the character set through the URI' do
      before do
        # @latin1_connection = DataObjects::Connection.new("#{CONFIG.uri}?encoding=latin1")
        @latin1_connection = DataObjects::Connection.new("#{CONFIG.scheme}://#{CONFIG.user}:#{CONFIG.pass}@#{CONFIG.host}:#{CONFIG.port}#{CONFIG.database}?encoding=ISO-8859-1")
      end

      after { @latin1_connection.close }

      it { @latin1_connection.character_set.should == 'ISO-8859-1' }
    end

    describe 'uses UTF-8 when an invalid encoding is given' do
      before do
        @latin1_connection = DataObjects::Connection.new("#{CONFIG.scheme}://#{CONFIG.user}:#{CONFIG.pass}@#{CONFIG.host}:#{CONFIG.port}#{CONFIG.database}?encoding=ISO-INVALID")
      end

      after { @latin1_connection.close }

      it { @latin1_connection.character_set.should == 'UTF-8' }
    end
  end
end

shared_examples_for 'returning correctly encoded strings for the default database encoding' do

  if defined?(::Encoding)

    before :all do
    setup_test_environment
  end

    describe 'with encoded string support' do

      before do
        @connection = DataObjects::Connection.new(CONFIG.uri)
      end

      after do
        @connection.close
      end

      describe 'reading a String' do
        before do
          @reader = @connection.create_command("SELECT name, whitepaper_text FROM widgets WHERE ad_description = ?").execute_reader('Buy this product now!')
          @reader.next!
          @values = @reader.values
        end

        after do
          @reader.close
        end

        it 'should return UTF-8 encoded String' do
          @values.first.should be_kind_of(String)
          @values.first.encoding.name.should == 'UTF-8'
          @values.last.should be_kind_of(String)
          @values.last.encoding.name.should == 'UTF-8'
        end
      end

      describe 'reading a ByteArray' do
        before do
          @command = @connection.create_command("SELECT ad_image FROM widgets WHERE ad_description = ?")
          @command.set_types(Extlib::ByteArray)
          @reader = @command.execute_reader('Buy this product now!')
          @reader.next!
          @values = @reader.values
        end

        after do
          @reader.close
        end

        it 'should return ASCII-8BIT encoded ByteArray' do
          @values.first.should be_kind_of(::Extlib::ByteArray)
          @values.first.encoding.name.should == 'ASCII-8BIT'
        end
      end
    end
  end

end

shared_examples_for 'returning correctly encoded strings for the default internal encoding' do

  if defined?(::Encoding)

    before :all do
    setup_test_environment
  end

    describe 'with encoded string support' do

      before do
        @encoding_before = Encoding.default_internal
        Encoding.default_internal = 'ISO-8859-1'
        @connection = DataObjects::Connection.new(CONFIG.uri)
      end

      after do
        @connection.close
        Encoding.default_internal = @encoding_before
      end

      describe 'reading a String' do
        before do
          @reader = @connection.create_command("SELECT name, whitepaper_text FROM widgets WHERE ad_description = ?").execute_reader('Buy this product now!')
          @reader.next!
          @values = @reader.values
        end

        after do
          @reader.close
        end

        it 'should return ISO-8859-1 encoded String' do
          @values.first.should be_kind_of(String)
          @values.first.encoding.name.should == 'ISO-8859-1'
          @values.last.should be_kind_of(String)
          @values.last.encoding.name.should == 'ISO-8859-1'
        end
      end

      describe 'reading a ByteArray' do
        before do
          @command = @connection.create_command("SELECT ad_image FROM widgets WHERE ad_description = ?")
          @command.set_types(Extlib::ByteArray)
          @reader = @command.execute_reader('Buy this product now!')
          @reader.next!
          @values = @reader.values
        end

        after do
          @reader.close
        end

        it 'should return ASCII-8BIT encoded ByteArray' do
          @values.first.should be_kind_of(::Extlib::ByteArray)
          @values.first.encoding.name.should == 'ASCII-8BIT'
        end
      end
    end
  end

end