File: test_arrays.rb

package info (click to toggle)
ruby-dbd-pg 0.3.9%2Bgem2deb-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 348 kB
  • ctags: 231
  • sloc: ruby: 2,214; sql: 91; makefile: 2
file content (179 lines) | stat: -rw-r--r-- 6,201 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class TestPostgresArray < DBDConfig.testbase(:postgresql)
    def test_array_type
        assert_nothing_raised do
            cols = @dbh.columns("array_test")
            assert_equal(
                [
                    {
                        :name =>"foo", 
                        :default =>nil, 
                        :primary =>nil, 
                        :scale =>nil, 
                        :sql_type =>DBI::SQL_INTEGER, 
                        :nullable =>true, 
                        :indexed =>false, 
                        :precision =>-1, 
                        :type_name =>"integer", 
                        :unique =>nil,
                        :array_of_type  => true
                    }, 
                    {
                        :name =>"bar", 
                        :default =>nil, 
                        :primary =>nil, 
                        :scale =>nil, 
                        :sql_type =>DBI::SQL_INTEGER, 
                        :nullable =>true, 
                        :indexed =>false, 
                        :precision =>-1, 
                        :type_name =>"integer", 
                        :unique =>nil,
                        :array_of_type  => true
                    },
                    {
                        :name =>"baz", 
                        :default =>nil, 
                        :primary =>nil, 
                        :scale =>nil, 
                        :sql_type =>DBI::SQL_INTEGER, 
                        :nullable =>true, 
                        :indexed =>false, 
                        :precision =>-1, 
                        :type_name =>"integer", 
                        :unique =>nil,
                        :array_of_type  => true
                    },
                    {
                        :array_of_type=>true,
                        :unique=>nil,
                        :precision=>-1,
                        :name=>"quux",
                        :default=>nil,
                        :indexed=>false,
                        :scale=>nil,
                        :primary=>nil,
                        :sql_type=>12,
                        :nullable=>true,
                        :type_name=>"character varying"
                    }
                ], cols.collect { |x| x.reject { |key, value| key == :dbi_type } }
            )

            assert_equal(([DBI::DBD::Pg::Type::Array] * 4), cols.collect { |x| x["dbi_type"].class })
            assert_equal((([DBI::Type::Integer] * 3) + [DBI::Type::Varchar]), cols.collect { |x| x["dbi_type"].base_type })
        end
    end

    def test_array_parser
        # string representation
        assert_nothing_raised do
            sth = @dbh.prepare('insert into array_test (foo) values (?)')
            sth.execute('{1,2,3}')
            sth.finish
        end

        assert_nothing_raised do
            sth = @dbh.prepare('insert into array_test (foo) values (?)')
            sth.execute([1,2,3])
            sth.finish
        end

        assert_nothing_raised do
            sth = @dbh.prepare('insert into array_test (quux) values (?)')
            sth.execute(["Hello\\ World", "Again\\"])
            sth.finish
        end

        assert_nothing_raised do
            sth = @dbh.prepare('select foo from array_test where foo is not null')
            sth.execute
            assert_equal(
                [
                    [[1,2,3]],
                    [[1,2,3]],
                ], sth.fetch_all
            )
            sth.finish

            sth = @dbh.prepare('select quux from array_test where quux is not null')
            sth.execute

            assert_equal(
                [
                    [["Hello\\ World", "Again\\"]]
                ], sth.fetch_all
            )

            sth.finish
        end
    end

    def test_array_boundaries
        # bar has a max extents of 3
        sth = @dbh.prepare('insert into array_test (bar) values (?)')

        assert_nothing_raised do
            sth.execute('{1,2,3}')
        end

        # XXX postgresql does not enforce extents on single-dimension arrays 
        assert_nothing_raised do
            sth.execute('{1,2,3,4}')
        end

        sth.finish
        sth = @dbh.prepare('insert into array_test(baz) values (?)')

        assert_nothing_raised do
            sth.execute('{{1,2,3}, {1,2,3}}')
        end

        assert_nothing_raised do
            # XXX for the record, I have no fucking idea why this works, what
            # it's technically represented as and what backwards array
            # implementation would allow it to work.
            #
            # I'm hoping it'll break on some future version of postgresql so I
            # can fix it.
            sth.execute('{1,2,3}')
        end

        assert_raises(DBI::ProgrammingError) do
            sth.execute('{{1,2,3,4}, {1,2,3}}')
        end

        assert_raises(DBI::ProgrammingError) do
            sth.execute('{{1,2,3}, {1,2,3,4}}')
        end

        sth.finish
    end

    def test_array_type_parser
        pc = DBI::DBD::Pg::Type::Array

        assert_nothing_raised do
            po = pc.new(DBI::Type::Integer)
            assert_equal([1,2,3], po.parse("{1,2,3}"))
            assert_equal([[1,2,3],[4,5,6]], po.parse("{{1,2,3},{4,5,6}}"))
        end

        assert_nothing_raised do
            po = pc.new(DBI::Type::Varchar)
            assert_equal(["one", "two", "three"], po.parse("{\"one\",\"two\",\"three\"}"))
            assert_equal([["one"], ["two\\"]], po.parse("{{\"one\"},{\"two\\\\\"}}"))
            assert_equal([["one", "two\\"], ["three\\", "four"]], po.parse("{{\"one\",\"two\\\\\"},{\"three\\\\\",\"four\"}}"))
        end
    end

    def test_array_generator
        pg = DBI::DBD::Pg

        assert_nothing_raised do
            assert_equal("{1,2,3}", pg.generate_array([1,2,3]))
            assert_equal("{{1,2,3},{1,2,3}}", pg.generate_array([[1,2,3],[1,2,3]]))
            assert_equal("{\"one\",\"two\"}", pg.generate_array(["one", "two"]))
            assert_equal("{\"hello\\\\ world\",\"again\"}", pg.generate_array(["hello\\ world", "again"]))
        end
    end
end