1
1
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
3
Sequel.extension :pg_array, :pg_array_ops, :pg_hstore, :pg_hstore_ops
3
5
describe "Sequel::Postgres::HStoreOp" do
5
7
@ds = Sequel.connect('mock://postgres', :quote_identifiers=>false).dataset
8
@h = Sequel.hstore_op(:h)
9
11
it "#- should use the - operator" do
10
@ds.literal(@h - 'a').should == "(h - 'a')"
12
@ds.literal(@h - :a).should == "(h - a)"
15
it "#- should cast String argument to text when using - operator" do
16
@ds.literal(@h - 'a').should == "(h - CAST('a' AS text))"
19
it "#- should not cast LiteralString argument to text when using - operator" do
20
@ds.literal(@h - Sequel.lit('a')).should == "(h - a)"
23
it "#- should handle arrays" do
24
@ds.literal(@h - %w'a').should == "(h - ARRAY['a'])"
27
it "#- should handle hashes" do
28
@ds.literal(@h - {'a'=>'b'}).should == "(h - '\"a\"=>\"b\"'::hstore)"
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')"
17
35
it "#[] should use the -> operator" do
18
36
@ds.literal(@h['a']).should == "(h -> 'a')"
39
it "#[] should handle arrays" do
40
@ds.literal(@h[%w'a']).should == "(h -> ARRAY['a'])"
43
it "#[] should return a PGArrayOp if given an array" do
44
@ds.literal(@h[%w'a'][0]).should == "(h -> ARRAY['a'])[0]"
47
it "#[] should not return a PGArrayOp if given an array but pg_array_op is not supported" do
49
module Sequel::Postgres::HStoreOp::Sequel
52
@ds.literal(@h[%w'a']).should_not be_a_kind_of(Sequel::Postgres::ArrayOp)
54
Sequel::Postgres::HStoreOp.send(:remove_const, :Sequel)
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]"
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]"
21
66
it "#[] should return a string expression" do
22
67
@ds.literal(@h['a'] + 'b').should == "((h -> 'a') || 'b')"
27
72
@ds.literal(@h.merge(:h1)).should == "(h || h1)"
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)"
30
80
it "#concat should return an HStoreOp" do
31
81
@ds.literal(@h.concat(:h1)['a']).should == "((h || h1) -> 'a')"
35
85
@ds.literal(@h.contain_all(:h1)).should == "(h ?& h1)"
88
it "#contain_all handle arrays" do
89
@ds.literal(@h.contain_all(%w'h1')).should == "(h ?& ARRAY['h1'])"
38
92
it "#contain_any should use the ?| operator" do
39
93
@ds.literal(@h.contain_any(:h1)).should == "(h ?| h1)"
96
it "#contain_any should handle arrays" do
97
@ds.literal(@h.contain_any(%w'h1')).should == "(h ?| ARRAY['h1'])"
42
100
it "#contains should use the @> operator" do
43
101
@ds.literal(@h.contains(:h1)).should == "(h @> h1)"
104
it "#contains should handle hashes" do
105
@ds.literal(@h.contains('a'=>'b')).should == "(h @> '\"a\"=>\"b\"'::hstore)"
46
108
it "#contained_by should use the <@ operator" do
47
109
@ds.literal(@h.contained_by(:h1)).should == "(h <@ h1)"
112
it "#contained_by should handle hashes" do
113
@ds.literal(@h.contained_by('a'=>'b')).should == "(h <@ '\"a\"=>\"b\"'::hstore)"
50
116
it "#defined should use the defined function" do
51
117
@ds.literal(@h.defined('a')).should == "defined(h, 'a')"
55
121
@ds.literal(@h.delete('a')).should == "delete(h, 'a')"
124
it "#delete should handle arrays" do
125
@ds.literal(@h.delete(%w'a')).should == "delete(h, ARRAY['a'])"
128
it "#delete should handle hashes" do
129
@ds.literal(@h.delete('a'=>'b')).should == "delete(h, '\"a\"=>\"b\"'::hstore)"
58
132
it "#delete should return an HStoreOp" do
59
133
@ds.literal(@h.delete('a')['a']).should == "(delete(h, 'a') -> 'a')"
80
154
@ds.literal(@h.akeys).should == "akeys(h)"
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]"
83
162
it "#populate should use the populate_record function" do
84
163
@ds.literal(@h.populate(:a)).should == "populate_record(a, h)"
96
175
@ds.literal(@h.slice(:a)).should == "slice(h, a)"
178
it "#slice should handle arrays" do
179
@ds.literal(@h.slice(%w'a')).should == "slice(h, ARRAY['a'])"
99
182
it "#slice should return an HStoreOp" do
100
183
@ds.literal(@h.slice(:a)['a']).should == "(slice(h, a) -> 'a')"
108
191
@ds.literal(@h.to_array).should == "hstore_to_array(h)"
194
it "#to_array should return a PGArrayOp" do
195
@ds.literal(@h.to_array[0]).should == "hstore_to_array(h)[0]"
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)"
202
it "#to_matrix should return a PGArrayOp" do
203
@ds.literal(@h.to_matrix[0]).should == "hstore_to_matrix(h)[0]"
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)"
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]"
216
it "should have Sequel.hstore_op return HStoreOp instances as-is" do
217
Sequel.hstore_op(@h).should equal(@h)
220
it "should have Sequel.hstore return HStoreOp instances" do
221
Sequel.hstore(:h).should == @h
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')"
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')"
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')"
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')"