~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to spec/extensions/pg_hstore_ops_spec.rb

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
2
2
 
 
3
Sequel.extension :pg_array, :pg_array_ops, :pg_hstore, :pg_hstore_ops
 
4
 
3
5
describe "Sequel::Postgres::HStoreOp" do
4
6
  before do
5
7
    @ds = Sequel.connect('mock://postgres', :quote_identifiers=>false).dataset
6
 
    @h = :h.hstore
 
8
    @h = Sequel.hstore_op(:h)
7
9
  end
8
10
 
9
11
  it "#- should use the - operator" do
10
 
    @ds.literal(@h - 'a').should == "(h - 'a')"
 
12
    @ds.literal(@h - :a).should == "(h - a)"
 
13
  end
 
14
 
 
15
  it "#- should cast String argument to text when using - operator" do
 
16
    @ds.literal(@h - 'a').should == "(h - CAST('a' AS text))"
 
17
  end
 
18
 
 
19
  it "#- should not cast LiteralString argument to text when using - operator" do
 
20
    @ds.literal(@h - Sequel.lit('a')).should == "(h - a)"
 
21
  end
 
22
 
 
23
  it "#- should handle arrays" do
 
24
    @ds.literal(@h - %w'a').should == "(h - ARRAY['a'])"
 
25
  end
 
26
 
 
27
  it "#- should handle hashes" do
 
28
    @ds.literal(@h - {'a'=>'b'}).should == "(h - '\"a\"=>\"b\"'::hstore)"
11
29
  end
12
30
 
13
31
  it "#- should return an HStoreOp" do
14
 
    @ds.literal((@h - 'a')['a']).should == "((h - 'a') -> 'a')"
 
32
    @ds.literal((@h - :a)['a']).should == "((h - a) -> 'a')"
15
33
  end
16
34
 
17
35
  it "#[] should use the -> operator" do
18
36
    @ds.literal(@h['a']).should == "(h -> 'a')"
19
37
  end
20
38
 
 
39
  it "#[] should handle arrays" do
 
40
    @ds.literal(@h[%w'a']).should == "(h -> ARRAY['a'])"
 
41
  end
 
42
 
 
43
  it "#[] should return a PGArrayOp if given an array" do
 
44
    @ds.literal(@h[%w'a'][0]).should == "(h -> ARRAY['a'])[0]"
 
45
  end
 
46
 
 
47
  it "#[] should not return a PGArrayOp if given an array but pg_array_op is not supported" do
 
48
    begin
 
49
      module Sequel::Postgres::HStoreOp::Sequel
 
50
        SQL = ::Sequel::SQL
 
51
      end
 
52
      @ds.literal(@h[%w'a']).should_not be_a_kind_of(Sequel::Postgres::ArrayOp)
 
53
    ensure
 
54
      Sequel::Postgres::HStoreOp.send(:remove_const, :Sequel)
 
55
    end
 
56
  end
 
57
 
 
58
  it "#[] should return a PGArrayOp if given a PGArray" do
 
59
    @ds.literal(@h[Sequel.pg_array(%w'a')][0]).should == "(h -> ARRAY['a'])[0]"
 
60
  end
 
61
 
 
62
  it "#[] should return a PGArrayOp if given a PGArrayOp" do
 
63
    @ds.literal(@h[Sequel.pg_array_op(:a)][0]).should == "(h -> a)[0]"
 
64
  end
 
65
 
21
66
  it "#[] should return a string expression" do
22
67
    @ds.literal(@h['a'] + 'b').should == "((h -> 'a') || 'b')"
23
68
  end
27
72
    @ds.literal(@h.merge(:h1)).should == "(h || h1)"
28
73
  end
29
74
 
 
75
  it "#concat and #merge should handle hashes" do
 
76
    @ds.literal(@h.concat('a'=>'b')).should == "(h || '\"a\"=>\"b\"'::hstore)"
 
77
    @ds.literal(@h.merge('a'=>'b')).should == "(h || '\"a\"=>\"b\"'::hstore)"
 
78
  end
 
79
 
30
80
  it "#concat should return an HStoreOp" do
31
81
    @ds.literal(@h.concat(:h1)['a']).should == "((h || h1) -> 'a')"
32
82
  end
35
85
    @ds.literal(@h.contain_all(:h1)).should == "(h ?& h1)"
36
86
  end
37
87
 
 
88
  it "#contain_all handle arrays" do
 
89
    @ds.literal(@h.contain_all(%w'h1')).should == "(h ?& ARRAY['h1'])"
 
90
  end
 
91
 
38
92
  it "#contain_any should use the ?| operator" do
39
93
    @ds.literal(@h.contain_any(:h1)).should == "(h ?| h1)"
40
94
  end
41
95
 
 
96
  it "#contain_any should handle arrays" do
 
97
    @ds.literal(@h.contain_any(%w'h1')).should == "(h ?| ARRAY['h1'])"
 
98
  end
 
99
 
42
100
  it "#contains should use the @> operator" do
43
101
    @ds.literal(@h.contains(:h1)).should == "(h @> h1)"
44
102
  end
45
103
 
 
104
  it "#contains should handle hashes" do
 
105
    @ds.literal(@h.contains('a'=>'b')).should == "(h @> '\"a\"=>\"b\"'::hstore)"
 
106
  end
 
107
 
46
108
  it "#contained_by should use the <@ operator" do
47
109
    @ds.literal(@h.contained_by(:h1)).should == "(h <@ h1)"
48
110
  end
49
111
 
 
112
  it "#contained_by should handle hashes" do
 
113
    @ds.literal(@h.contained_by('a'=>'b')).should == "(h <@ '\"a\"=>\"b\"'::hstore)"
 
114
  end
 
115
 
50
116
  it "#defined should use the defined function" do
51
117
    @ds.literal(@h.defined('a')).should == "defined(h, 'a')"
52
118
  end
55
121
    @ds.literal(@h.delete('a')).should == "delete(h, 'a')"
56
122
  end
57
123
 
 
124
  it "#delete should handle arrays" do
 
125
    @ds.literal(@h.delete(%w'a')).should == "delete(h, ARRAY['a'])"
 
126
  end
 
127
 
 
128
  it "#delete should handle hashes" do
 
129
    @ds.literal(@h.delete('a'=>'b')).should == "delete(h, '\"a\"=>\"b\"'::hstore)"
 
130
  end
 
131
 
58
132
  it "#delete should return an HStoreOp" do
59
133
    @ds.literal(@h.delete('a')['a']).should == "(delete(h, 'a') -> 'a')"
60
134
  end
80
154
    @ds.literal(@h.akeys).should == "akeys(h)"
81
155
  end
82
156
 
 
157
  it "#keys and #akeys should return PGArrayOps" do
 
158
    @ds.literal(@h.keys[0]).should == "akeys(h)[0]"
 
159
    @ds.literal(@h.akeys[0]).should == "akeys(h)[0]"
 
160
  end
 
161
 
83
162
  it "#populate should use the populate_record function" do
84
163
    @ds.literal(@h.populate(:a)).should == "populate_record(a, h)"
85
164
  end
96
175
    @ds.literal(@h.slice(:a)).should == "slice(h, a)"
97
176
  end
98
177
 
 
178
  it "#slice should handle arrays" do
 
179
    @ds.literal(@h.slice(%w'a')).should == "slice(h, ARRAY['a'])"
 
180
  end
 
181
 
99
182
  it "#slice should return an HStoreOp" do
100
183
    @ds.literal(@h.slice(:a)['a']).should == "(slice(h, a) -> 'a')"
101
184
  end
108
191
    @ds.literal(@h.to_array).should == "hstore_to_array(h)"
109
192
  end
110
193
 
 
194
  it "#to_array should return a PGArrayOp" do
 
195
    @ds.literal(@h.to_array[0]).should == "hstore_to_array(h)[0]"
 
196
  end
 
197
 
111
198
  it "#to_matrix should use the hstore_to_matrix function" do
112
199
    @ds.literal(@h.to_matrix).should == "hstore_to_matrix(h)"
113
200
  end
114
201
 
 
202
  it "#to_matrix should return a PGArrayOp" do
 
203
    @ds.literal(@h.to_matrix[0]).should == "hstore_to_matrix(h)[0]"
 
204
  end
 
205
 
115
206
  it "#values and #avals should use the avals function" do
116
207
    @ds.literal(@h.values).should == "avals(h)"
117
208
    @ds.literal(@h.avals).should == "avals(h)"
118
209
  end
119
210
 
 
211
  it "#values and #avals should return PGArrayOps" do
 
212
    @ds.literal(@h.values[0]).should == "avals(h)[0]"
 
213
    @ds.literal(@h.avals[0]).should == "avals(h)[0]"
 
214
  end
 
215
 
 
216
  it "should have Sequel.hstore_op return HStoreOp instances as-is" do
 
217
    Sequel.hstore_op(@h).should equal(@h)
 
218
  end
 
219
 
 
220
  it "should have Sequel.hstore return HStoreOp instances" do
 
221
    Sequel.hstore(:h).should == @h
 
222
  end
 
223
 
120
224
  it "should be able to turn expressions into hstore ops using hstore" do
121
 
    @ds.literal(:a.qualify(:b).hstore['a']).should == "(b.a -> 'a')"
122
 
    @ds.literal(:a.sql_function(:b).hstore['a']).should == "(a(b) -> 'a')"
 
225
    @ds.literal(Sequel.qualify(:b, :a).hstore['a']).should == "(b.a -> 'a')"
 
226
    @ds.literal(Sequel.function(:a, :b).hstore['a']).should == "(a(b) -> 'a')"
123
227
  end
124
228
 
125
229
  it "should be able to turn literal strings into hstore ops using hstore" do
126
 
    @ds.literal('a'.lit.hstore['a']).should == "(a -> 'a')"
127
 
  end
128
 
 
129
 
  it "should be able to turn symbols into hstore ops using hstore" do
130
 
    @ds.literal(:a.hstore['a']).should == "(a -> 'a')"
 
230
    @ds.literal(Sequel.lit('a').hstore['a']).should == "(a -> 'a')"
131
231
  end
132
232
 
133
233
  it "should allow transforming HStore instances into HStoreOp instances" do
134
 
    @ds.literal({'a'=>'b'}.hstore.op['a']).should == "('\"a\"=>\"b\"'::hstore -> 'a')"
 
234
    @ds.literal(Sequel.hstore('a'=>'b').op['a']).should == "('\"a\"=>\"b\"'::hstore -> 'a')"
135
235
  end
136
236
end