/
query_builder_spec.rb
175 lines (124 loc) · 4.6 KB
/
query_builder_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Yql::QueryBuilder do
describe ".show_tables" do
it "should return show tables string" do
Yql::QueryBuilder.show_tables.should eql('show tables;')
end
end
describe ".describe_table" do
it "should return the describe tables query" do
Yql::QueryBuilder.describe_table('yql.table.name').should eql('desc yql.table.name;')
end
end
describe "#describe_table" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
end
it "should describe the current table using class method" do
Yql::QueryBuilder.should_receive(:describe_table).with('yql.table.name')
@query_builder.describe_table
end
it "should return the result from the class method" do
Yql::QueryBuilder.stub!(:describe_table).and_return('table description')
@query_builder.describe_table.should eql('table description')
end
end
describe "#find" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
@query_builder.stub!(:construct_query).and_return('The yql query')
end
it "should set the limit of records to 1 by default" do
@query_builder.should_receive(:limit=).with(1)
@query_builder.find
end
it "should construct the query" do
@query_builder.should_receive(:construct_query)
@query_builder.find
end
it "should return the constructed query" do
@query_builder.find.should eql('The yql query')
end
end
describe "#find_all" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
@query_builder.stub!(:construct_query).and_return('The yql query')
end
it "should set the limit of records to nil" do
@query_builder.should_receive(:limit=).with(nil)
@query_builder.find_all
end
it "should set the with provided hash" do
@query_builder.should_receive(:limit=).with(10)
@query_builder.find_all({:limit => 10})
end
it "should construct the query" do
@query_builder.should_receive(:construct_query)
@query_builder.find_all
end
it "should return the constructed query" do
@query_builder.find_all.should eql('The yql query')
end
end
describe "#to_s" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
@query_builder.stub!(:construct_query).and_return('The yql query')
end
it "should construct the query" do
@query_builder.should_receive(:construct_query)
@query_builder.to_s
end
it "should return the constructed query" do
@query_builder.to_s.should eql('The yql query')
end
end
describe "#limit" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
end
it "should return nil if @limit instance is not set" do
@query_builder.limit.should be_nil
end
it "should return the limit text with limit number" do
@query_builder.limit = 3
@query_builder.limit.should eql('limit 3')
end
end
describe "#conditions" do
before(:each) do
@query_builder = Yql::QueryBuilder.new('yql.table.name')
end
context "when condition is set as string" do
it "should return the condition string with where clause" do
@query_builder.conditions = "some sql like yql conditions"
@query_builder.conditions.should eql('where some sql like yql conditions')
end
end
context "when condition is passed as a hash" do
before(:each) do
@query_builder.conditions = {:attr1 => 'val1', :attr2 => 'val2', :attr3 => 4, :attr4 => '5'}
end
it "should 'where' text in the beginning of the return value" do
@query_builder.conditions.should match(/^where attr/)
# @query_builder.conditions.should match("where key1='val1' and key2='val2' and key3=4 and key4='5'")
end
it "should have the attr1 key value pair" do
@query_builder.conditions.should match(/attr1='val1'/)
end
it "should have the attr2 key value pair" do
@query_builder.conditions.should match(/attr2='val2'/)
end
it "should have the attr3 key value pair as integer" do
@query_builder.conditions.should match(/attr3=4/)
end
it "should have the attr3 key value pair integer as string" do
@query_builder.conditions.should match(/attr4='5'/)
end
it "should have 'and' only thrice in the returned value as there are 4 key values passed to condition" do
@query_builder.conditions.scan('and').size.should eql(3)
end
end
end
end