ambition 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README +37 -33
- data/Rakefile +1 -1
- data/lib/ambition/processor.rb +4 -1
- data/lib/ambition/where.rb +2 -2
- data/test/chaining_test.rb +4 -4
- data/test/count_test.rb +1 -1
- data/test/enumerable_test.rb +9 -9
- data/test/join_test.rb +1 -1
- data/test/limit_test.rb +6 -6
- data/test/order_test.rb +7 -7
- data/test/types_test.rb +7 -7
- data/test/where_test.rb +29 -26
- metadata +1 -1
data/README
CHANGED
@@ -49,11 +49,11 @@ would.
|
|
49
49
|
|
50
50
|
See, +to_sql+:
|
51
51
|
>> User.select { |m| m.name == 'jon' }.to_sql
|
52
|
-
=> "SELECT * FROM users WHERE users
|
52
|
+
=> "SELECT * FROM users WHERE users.name = 'jon'"
|
53
53
|
|
54
54
|
See, +to_hash+:
|
55
55
|
>> User.select { |m| m.name == 'jon' }.to_hash
|
56
|
-
=> {:conditions=>"users
|
56
|
+
=> {:conditions=>"users.name = 'jon'"}
|
57
57
|
|
58
58
|
== Limitations
|
59
59
|
|
@@ -72,22 +72,22 @@ Instance variables and globals work, too. Same with method calls.
|
|
72
72
|
== Equality -- select { |u| u.field == 'bob' }
|
73
73
|
|
74
74
|
User.select { |m| m.name == 'jon' }
|
75
|
-
"SELECT * FROM users WHERE users
|
75
|
+
"SELECT * FROM users WHERE users.name = 'jon'"
|
76
76
|
|
77
77
|
User.select { |m| m.name != 'jon' }
|
78
|
-
"SELECT * FROM users WHERE users
|
78
|
+
"SELECT * FROM users WHERE users.name <> 'jon'"
|
79
79
|
|
80
80
|
User.select { |m| m.name == 'jon' && m.age == 21 }
|
81
|
-
"SELECT * FROM users WHERE (users
|
81
|
+
"SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
|
82
82
|
|
83
83
|
User.select { |m| m.name == 'jon' || m.age == 21 }
|
84
|
-
"SELECT * FROM users WHERE (users
|
84
|
+
"SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
|
85
85
|
|
86
86
|
User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }
|
87
|
-
"SELECT * FROM users WHERE (users
|
87
|
+
"SELECT * FROM users WHERE (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
|
88
88
|
|
89
89
|
User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }
|
90
|
-
"SELECT * FROM users WHERE ((users
|
90
|
+
"SELECT * FROM users WHERE ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
|
91
91
|
|
92
92
|
== Associations -- select { |u| u.field == 'bob' && u.association.field == 'bob@bob.com' }
|
93
93
|
|
@@ -95,83 +95,83 @@ The +to_sql+ method doesn't work on associations yet, but that's okay: they can
|
|
95
95
|
still query through ActiveRecord just fine.
|
96
96
|
|
97
97
|
User.select { |u| u.email == 'chris@ozmm.org' && u.profile.name == 'chris wanstrath' }.map(&:title)
|
98
|
-
SELECT users
|
98
|
+
SELECT users.id AS t0_r0, ... FROM users LEFT OUTER JOIN profiles ON profiles.user_id = users.id WHERE ((users.email = 'chris@ozmm.org' AND profiles.name = 'chris wanstrath'))
|
99
99
|
|
100
100
|
== Comparisons -- select { |u| u.age > 21 }
|
101
101
|
|
102
102
|
User.select { |m| m.age > 21 }
|
103
|
-
"SELECT * FROM users WHERE users
|
103
|
+
"SELECT * FROM users WHERE users.age > 21"
|
104
104
|
|
105
105
|
User.select { |m| m.age < 21 }.to_sql
|
106
|
-
"SELECT * FROM users WHERE users
|
106
|
+
"SELECT * FROM users WHERE users.age < 21"
|
107
107
|
|
108
108
|
User.select { |m| [1, 2, 3, 4].include? m.id }
|
109
|
-
"SELECT * FROM users WHERE users
|
109
|
+
"SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
|
110
110
|
|
111
111
|
== LIKE and REGEXP (RLIKE) -- select { |m| m.name =~ 'chris' }
|
112
112
|
|
113
113
|
User.select { |m| m.name =~ 'chris' }
|
114
|
-
"SELECT * FROM users WHERE users
|
114
|
+
"SELECT * FROM users WHERE users.name LIKE 'chris'"
|
115
115
|
|
116
116
|
User.select { |m| m.name =~ 'chri%' }
|
117
|
-
"SELECT * FROM users WHERE users
|
117
|
+
"SELECT * FROM users WHERE users.name LIKE 'chri%'"
|
118
118
|
|
119
119
|
User.select { |m| m.name !~ 'chris' }
|
120
|
-
"SELECT * FROM users WHERE users
|
120
|
+
"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
|
121
121
|
|
122
122
|
User.select { |m| !(m.name =~ 'chris') }
|
123
|
-
"SELECT * FROM users WHERE users
|
123
|
+
"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
|
124
124
|
|
125
125
|
User.select { |m| m.name =~ /chris/ }
|
126
|
-
"SELECT * FROM users WHERE users
|
126
|
+
"SELECT * FROM users WHERE users.name REGEXP 'chris'"
|
127
127
|
|
128
128
|
== #detect
|
129
129
|
|
130
130
|
User.detect { |m| m.name == 'chris' }
|
131
|
-
"SELECT * FROM users WHERE users
|
131
|
+
"SELECT * FROM users WHERE users.name = 'chris' LIMIT 1"
|
132
132
|
|
133
133
|
== LIMITs -- first, first(x), [offset, limit], [range], slice
|
134
134
|
|
135
135
|
User.select { |m| m.name == 'jon' }.first
|
136
|
-
"SELECT * FROM users WHERE users
|
136
|
+
"SELECT * FROM users WHERE users.name = 'jon' LIMIT 1"
|
137
137
|
|
138
138
|
User.select { |m| m.name == 'jon' }.first(5)
|
139
|
-
"SELECT * FROM users WHERE users
|
139
|
+
"SELECT * FROM users WHERE users.name = 'jon' LIMIT 5"
|
140
140
|
|
141
141
|
User.select { |m| m.name == 'jon' }[10, 20]
|
142
|
-
"SELECT * FROM users WHERE users
|
142
|
+
"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 20"
|
143
143
|
|
144
144
|
User.select { |m| m.name == 'jon' }[10..20]
|
145
|
-
"SELECT * FROM users WHERE users
|
145
|
+
"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 10"
|
146
146
|
|
147
147
|
== ORDER -- sort_by { |u| u.field }
|
148
148
|
|
149
149
|
User.select { |m| m.name == 'jon' }.sort_by { |m| m.name }
|
150
|
-
"SELECT * FROM users WHERE users
|
150
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
|
151
151
|
|
152
152
|
User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, m.age ] }
|
153
|
-
"SELECT * FROM users WHERE users
|
153
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
|
154
154
|
|
155
155
|
User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, -m.age ] }
|
156
|
-
"SELECT * FROM users WHERE users
|
156
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age DESC"
|
157
157
|
|
158
158
|
User.select { |m| m.name == 'jon' }.sort_by { |m| [ -m.name, -m.age ] }
|
159
|
-
"SELECT * FROM users WHERE users
|
159
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name DESC, users.age DESC"
|
160
160
|
|
161
161
|
User.select { |m| m.name == 'jon' }.sort_by { |m| -m.age }
|
162
|
-
"SELECT * FROM users WHERE users
|
162
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
|
163
163
|
|
164
164
|
User.select { |m| m.name == 'jon' }.sort_by { |m| -m.profiles.title }
|
165
|
-
"SELECT users
|
166
|
-
WHERE (users
|
165
|
+
"SELECT users.id AS t0_r0, ... FROM users LEFT OUTER JOIN profiles ON profiles.user_id = users.id
|
166
|
+
WHERE (users.name = 'jon') ORDER BY profiles.title DESC"
|
167
167
|
|
168
168
|
User.select { |m| m.name == 'jon' }.sort_by { rand }
|
169
|
-
"SELECT * FROM users WHERE users
|
169
|
+
"SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
|
170
170
|
|
171
171
|
== COUNT -- select { |u| u.name == 'jon' }.size
|
172
172
|
|
173
173
|
User.select { |m| m.name == 'jon' }.size
|
174
|
-
SELECT count(*) AS count_all FROM users WHERE (users
|
174
|
+
SELECT count(*) AS count_all FROM users WHERE (users.name = 'jon')
|
175
175
|
|
176
176
|
>> User.select { |m| m.name == 'jon' }.size
|
177
177
|
=> 21
|
@@ -190,8 +190,12 @@ kickers.
|
|
190
190
|
The +downcase+ and +upcase+ methods will map to LOWER() and UPPER(), respectively.
|
191
191
|
|
192
192
|
>> User.select { |m| m.name.downcase =~ 'jon%' }.to_sql
|
193
|
-
=> "SELECT * FROM users WHERE LOWER(users
|
194
|
-
|
193
|
+
=> "SELECT * FROM users WHERE LOWER(users.name) LIKE 'jon%'"
|
194
|
+
|
195
|
+
== Quoting
|
196
|
+
|
197
|
+
Columns and values will be quoted using ActiveRecord's quote_column_name and quote methods, if
|
198
|
+
possible.
|
195
199
|
|
196
200
|
== SELECT * FROM bugs
|
197
201
|
|
data/Rakefile
CHANGED
data/lib/ambition/processor.rb
CHANGED
@@ -6,7 +6,6 @@ module Ambition
|
|
6
6
|
|
7
7
|
attr_reader :key, :join_string, :prefix, :includes
|
8
8
|
|
9
|
-
|
10
9
|
def initialize
|
11
10
|
super()
|
12
11
|
@strict = false
|
@@ -52,6 +51,10 @@ module Ambition
|
|
52
51
|
else ActiveRecord::Base.connection.quote(value) rescue quote(value)
|
53
52
|
end
|
54
53
|
end
|
54
|
+
|
55
|
+
def quote_column_name(value)
|
56
|
+
ActiveRecord::Base.connection.quote_column_name(value) rescue value.to_s
|
57
|
+
end
|
55
58
|
|
56
59
|
def extract_includes(receiver, method)
|
57
60
|
return unless receiver.first == :call && receiver[1].last == @receiver
|
data/lib/ambition/where.rb
CHANGED
@@ -131,7 +131,7 @@ module Ambition
|
|
131
131
|
else
|
132
132
|
"#{process(receiver)} = #{other_value}"
|
133
133
|
end
|
134
|
-
when '<>', '>', '<'
|
134
|
+
when '<>', '>', '<', '>=', '<='
|
135
135
|
"#{process(receiver)} #{method} #{process(other)}"
|
136
136
|
when 'include?'
|
137
137
|
"#{process(other)} IN (#{process(receiver)})"
|
@@ -144,7 +144,7 @@ module Ambition
|
|
144
144
|
when 'downcase'
|
145
145
|
"LOWER(#{process(receiver)})"
|
146
146
|
else
|
147
|
-
extract_includes(receiver, method) || "#{process(receiver)}
|
147
|
+
extract_includes(receiver, method) || "#{process(receiver)}.#{quote_column_name(method)}"
|
148
148
|
end
|
149
149
|
end
|
150
150
|
end
|
data/test/chaining_test.rb
CHANGED
@@ -4,14 +4,14 @@ context "Chaining" do
|
|
4
4
|
specify "should join selects with AND" do
|
5
5
|
sql = User.select { |m| m.name == 'jon' }
|
6
6
|
sql = sql.select { |m| m.age == 22 }
|
7
|
-
sql.to_sql.should == "SELECT * FROM users WHERE users
|
7
|
+
sql.to_sql.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22"
|
8
8
|
end
|
9
9
|
|
10
10
|
specify "should join sort_bys with a comma" do
|
11
11
|
sql = User.select { |m| m.name == 'jon' }
|
12
12
|
sql = sql.sort_by { |m| m.name }
|
13
13
|
sql = sql.sort_by { |m| m.age }
|
14
|
-
sql.to_sql.should == "SELECT * FROM users WHERE users
|
14
|
+
sql.to_sql.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
|
15
15
|
end
|
16
16
|
|
17
17
|
specify "should join selects and sorts intelligently" do
|
@@ -19,7 +19,7 @@ context "Chaining" do
|
|
19
19
|
sql = sql.select { |m| m.age == 22 }
|
20
20
|
sql = sql.sort_by { |m| -m.name }
|
21
21
|
sql = sql.sort_by { |m| m.age }
|
22
|
-
sql.to_sql.should == "SELECT * FROM users WHERE users
|
22
|
+
sql.to_sql.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 ORDER BY users.name DESC, users.age"
|
23
23
|
end
|
24
24
|
|
25
25
|
specify "should join lots of selects and sorts intelligently" do
|
@@ -29,6 +29,6 @@ context "Chaining" do
|
|
29
29
|
sql = sql.select { |m| m.power == true }
|
30
30
|
sql = sql.sort_by { |m| m.email }
|
31
31
|
sql = sql.select { |m| m.admin == true && m.email == 'chris@ozmm.org' }
|
32
|
-
sql.to_sql.should == "SELECT * FROM users WHERE users
|
32
|
+
sql.to_sql.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 AND users.power = 1 AND (users.admin = 1 AND users.email = 'chris@ozmm.org') ORDER BY users.name, users.email"
|
33
33
|
end
|
34
34
|
end
|
data/test/count_test.rb
CHANGED
data/test/enumerable_test.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/helper'
|
|
2
2
|
|
3
3
|
context "Each" do
|
4
4
|
specify "simple ==" do
|
5
|
-
hash = { :conditions => "users
|
5
|
+
hash = { :conditions => "users.age = 21" }
|
6
6
|
User.expects(:find).with(:all, hash).returns([])
|
7
7
|
User.select { |m| m.age == 21 }.each do |user|
|
8
8
|
puts user.name
|
@@ -10,7 +10,7 @@ context "Each" do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
specify "limit and conditions" do
|
13
|
-
hash = { :limit => '5', :conditions => "users
|
13
|
+
hash = { :limit => '5', :conditions => "users.age = 21" }
|
14
14
|
User.expects(:find).with(:all, hash).returns([])
|
15
15
|
User.select { |m| m.age == 21 }.first(5).each do |user|
|
16
16
|
puts user.name
|
@@ -18,7 +18,7 @@ context "Each" do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
specify "limit and conditions and order" do
|
21
|
-
hash = { :limit => '5', :conditions => "users
|
21
|
+
hash = { :limit => '5', :conditions => "users.age = 21", :order => 'users.name' }
|
22
22
|
User.expects(:find).with(:all, hash).returns([])
|
23
23
|
User.select { |m| m.age == 21 }.sort_by { |m| m.name }.first(5).each do |user|
|
24
24
|
puts user.name
|
@@ -36,13 +36,13 @@ end
|
|
36
36
|
|
37
37
|
context "Enumerable Methods" do
|
38
38
|
specify "map" do
|
39
|
-
hash = { :conditions => "users
|
39
|
+
hash = { :conditions => "users.age = 21" }
|
40
40
|
User.expects(:find).with(:all, hash).returns([])
|
41
41
|
User.select { |m| m.age == 21 }.map { |u| u.name }
|
42
42
|
end
|
43
43
|
|
44
44
|
specify "each_with_index" do
|
45
|
-
hash = { :conditions => "users
|
45
|
+
hash = { :conditions => "users.age = 21" }
|
46
46
|
User.expects(:find).with(:all, hash).returns([])
|
47
47
|
User.select { |m| m.age == 21 }.each_with_index do |user, i|
|
48
48
|
puts "#{i}: #{user.name}"
|
@@ -50,7 +50,7 @@ context "Enumerable Methods" do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
specify "any?" do
|
53
|
-
User.expects(:count).with(:conditions => "users
|
53
|
+
User.expects(:count).with(:conditions => "users.age > 21").returns(1)
|
54
54
|
User.any? { |u| u.age > 21 }.should == true
|
55
55
|
end
|
56
56
|
|
@@ -63,10 +63,10 @@ context "Enumerable Methods" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
specify "empty?" do
|
66
|
-
User.expects(:count).with(:conditions => "users
|
66
|
+
User.expects(:count).with(:conditions => "users.age > 21").returns(1)
|
67
67
|
User.select { |u| u.age > 21 }.empty?.should.equal false
|
68
68
|
|
69
|
-
User.expects(:count).with(:conditions => "users
|
69
|
+
User.expects(:count).with(:conditions => "users.age > 21").returns(0)
|
70
70
|
User.select { |u| u.age > 21 }.empty?.should.equal true
|
71
71
|
end
|
72
72
|
|
@@ -74,7 +74,7 @@ context "Enumerable Methods" do
|
|
74
74
|
User.expects(:find).with(:all, {})
|
75
75
|
User.entries
|
76
76
|
|
77
|
-
hash = { :conditions => "users
|
77
|
+
hash = { :conditions => "users.age = 21" }
|
78
78
|
User.expects(:find).with(:all, hash).returns([])
|
79
79
|
User.select { |m| m.age == 21 }.entries
|
80
80
|
end
|
data/test/join_test.rb
CHANGED
@@ -12,7 +12,7 @@ context "Joins" do
|
|
12
12
|
specify "simple mixed == on an association" do
|
13
13
|
sql = User.select { |m| m.name == 'chris' && m.account.email == 'chris@ozmm.org' }
|
14
14
|
sql.to_hash.should == {
|
15
|
-
:conditions => "(users
|
15
|
+
:conditions => "(users.name = 'chris' AND accounts.email = 'chris@ozmm.org')",
|
16
16
|
:include => [:account]
|
17
17
|
}
|
18
18
|
end
|
data/test/limit_test.rb
CHANGED
@@ -6,37 +6,37 @@ context "Limit" do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
specify "first" do
|
9
|
-
conditions = { :conditions => "users
|
9
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '1' }
|
10
10
|
User.expects(:find).with(:first, conditions)
|
11
11
|
@sql.first
|
12
12
|
end
|
13
13
|
|
14
14
|
specify "first with argument" do
|
15
|
-
conditions = { :conditions => "users
|
15
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '5' }
|
16
16
|
User.expects(:find).with(:all, conditions)
|
17
17
|
@sql.first(5)
|
18
18
|
end
|
19
19
|
|
20
20
|
specify "[] with one element" do
|
21
|
-
conditions = { :conditions => "users
|
21
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '10, 1' }
|
22
22
|
User.expects(:find).with(:all, conditions)
|
23
23
|
@sql[10]
|
24
24
|
end
|
25
25
|
|
26
26
|
specify "[] with two elements" do
|
27
|
-
conditions = { :conditions => "users
|
27
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '10, 20' }
|
28
28
|
User.expects(:find).with(:all, conditions)
|
29
29
|
@sql[10, 20]
|
30
30
|
end
|
31
31
|
|
32
32
|
specify "slice is an alias of []" do
|
33
|
-
conditions = { :conditions => "users
|
33
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '10, 20' }
|
34
34
|
User.expects(:find).with(:all, conditions)
|
35
35
|
@sql.slice(10, 20)
|
36
36
|
end
|
37
37
|
|
38
38
|
specify "[] with range" do
|
39
|
-
conditions = { :conditions => "users
|
39
|
+
conditions = { :conditions => "users.name = 'jon'", :limit => '10, 10' }
|
40
40
|
User.expects(:find).with(:all, conditions)
|
41
41
|
@sql[10..20]
|
42
42
|
end
|
data/test/order_test.rb
CHANGED
@@ -7,38 +7,38 @@ context "Order" do
|
|
7
7
|
|
8
8
|
specify "simple order" do
|
9
9
|
string = @sql.sort_by { |m| m.name }.to_sql
|
10
|
-
string.should == "SELECT * FROM users WHERE users
|
10
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
|
11
11
|
end
|
12
12
|
|
13
13
|
specify "simple combined order" do
|
14
14
|
string = @sql.sort_by { |m| [ m.name, m.age ] }.to_sql
|
15
|
-
string.should == "SELECT * FROM users WHERE users
|
15
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
|
16
16
|
end
|
17
17
|
|
18
18
|
specify "simple combined order with single reverse" do
|
19
19
|
string = @sql.sort_by { |m| [ m.name, -m.age ] }.to_sql
|
20
|
-
string.should == "SELECT * FROM users WHERE users
|
20
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age DESC"
|
21
21
|
end
|
22
22
|
|
23
23
|
specify "simple combined order with two reverses" do
|
24
24
|
string = @sql.sort_by { |m| [ -m.name, -m.age ] }.to_sql
|
25
|
-
string.should == "SELECT * FROM users WHERE users
|
25
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name DESC, users.age DESC"
|
26
26
|
end
|
27
27
|
|
28
28
|
specify "reverse order with -" do
|
29
29
|
string = @sql.sort_by { |m| -m.age }.to_sql
|
30
|
-
string.should == "SELECT * FROM users WHERE users
|
30
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
|
31
31
|
end
|
32
32
|
|
33
33
|
xspecify "reverse order with #reverse" do
|
34
34
|
# TODO: not implemented
|
35
35
|
string = @sql.sort_by { |m| m.age }.reverse.to_sql
|
36
|
-
string.should == "SELECT * FROM users WHERE users
|
36
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
|
37
37
|
end
|
38
38
|
|
39
39
|
specify "random order" do
|
40
40
|
string = @sql.sort_by { rand }.to_sql
|
41
|
-
string.should == "SELECT * FROM users WHERE users
|
41
|
+
string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
|
42
42
|
end
|
43
43
|
|
44
44
|
specify "non-existent method to sort by" do
|
data/test/types_test.rb
CHANGED
@@ -18,38 +18,38 @@ context "Different types" do
|
|
18
18
|
types_hash.each do |type, translation|
|
19
19
|
specify "simple using #{type}" do
|
20
20
|
sql = User.select { |m| m.name == type }.to_sql
|
21
|
-
sql.should == "SELECT * FROM users WHERE users
|
21
|
+
sql.should == "SELECT * FROM users WHERE users.name = #{translation}"
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
specify "float" do
|
26
26
|
sql = User.select { |m| m.name == 1.2 }.to_sql
|
27
|
-
sql.should == "SELECT * FROM users WHERE users
|
27
|
+
sql.should == "SELECT * FROM users WHERE users.name = 1.2"
|
28
28
|
end
|
29
29
|
|
30
30
|
specify "integer" do
|
31
31
|
sql = User.select { |m| m.name == 1 }.to_sql
|
32
|
-
sql.should == "SELECT * FROM users WHERE users
|
32
|
+
sql.should == "SELECT * FROM users WHERE users.name = 1"
|
33
33
|
end
|
34
34
|
|
35
35
|
specify "true" do
|
36
36
|
sql = User.select { |m| m.name == true }.to_sql
|
37
|
-
sql.should == "SELECT * FROM users WHERE users
|
37
|
+
sql.should == "SELECT * FROM users WHERE users.name = 1"
|
38
38
|
end
|
39
39
|
|
40
40
|
specify "false" do
|
41
41
|
sql = User.select { |m| m.name == false }.to_sql
|
42
|
-
sql.should == "SELECT * FROM users WHERE users
|
42
|
+
sql.should == "SELECT * FROM users WHERE users.name = 0"
|
43
43
|
end
|
44
44
|
|
45
45
|
specify "nil" do
|
46
46
|
sql = User.select { |m| m.name == nil }.to_sql
|
47
|
-
sql.should == "SELECT * FROM users WHERE users
|
47
|
+
sql.should == "SELECT * FROM users WHERE users.name is NULL"
|
48
48
|
end
|
49
49
|
|
50
50
|
xspecify "Time" do
|
51
51
|
# TODO: nothing but variables inside blocks for now
|
52
52
|
sql = User.select { |m| m.name == Time.now }.to_sql
|
53
|
-
sql.should == "SELECT * FROM users WHERE users
|
53
|
+
sql.should == "SELECT * FROM users WHERE users.name = NULL"
|
54
54
|
end
|
55
55
|
end
|
data/test/where_test.rb
CHANGED
@@ -3,57 +3,60 @@ require File.dirname(__FILE__) + '/helper'
|
|
3
3
|
context "Where (using select)" do
|
4
4
|
specify "simple ==" do
|
5
5
|
sql = User.select { |m| m.name == 'jon' }.to_sql
|
6
|
-
sql.should == "SELECT * FROM users WHERE users
|
6
|
+
sql.should == "SELECT * FROM users WHERE users.name = 'jon'"
|
7
7
|
end
|
8
8
|
|
9
9
|
specify "simple !=" do
|
10
10
|
sql = User.select { |m| m.name != 'jon' }.to_sql
|
11
|
-
sql.should == "SELECT * FROM users WHERE users
|
11
|
+
sql.should == "SELECT * FROM users WHERE users.name <> 'jon'"
|
12
12
|
end
|
13
13
|
|
14
14
|
specify "simple == && ==" do
|
15
15
|
sql = User.select { |m| m.name == 'jon' && m.age == 21 }.to_sql
|
16
|
-
sql.should == "SELECT * FROM users WHERE (users
|
16
|
+
sql.should == "SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
|
17
17
|
end
|
18
18
|
|
19
19
|
specify "simple == || ==" do
|
20
20
|
sql = User.select { |m| m.name == 'jon' || m.age == 21 }.to_sql
|
21
|
-
sql.should == "SELECT * FROM users WHERE (users
|
21
|
+
sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
|
22
22
|
end
|
23
23
|
|
24
24
|
specify "mixed && and ||" do
|
25
25
|
sql = User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }.to_sql
|
26
|
-
sql.should == "SELECT * FROM users WHERE (users
|
26
|
+
sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
|
27
27
|
end
|
28
28
|
|
29
29
|
specify "grouped && and ||" do
|
30
30
|
sql = User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }.to_sql
|
31
|
-
sql.should == "SELECT * FROM users WHERE ((users
|
31
|
+
sql.should == "SELECT * FROM users WHERE ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
|
32
32
|
end
|
33
33
|
|
34
34
|
specify "simple >/<" do
|
35
35
|
sql = User.select { |m| m.age > 21 }.to_sql
|
36
|
-
sql.should == "SELECT * FROM users WHERE users
|
36
|
+
sql.should == "SELECT * FROM users WHERE users.age > 21"
|
37
|
+
|
38
|
+
sql = User.select { |m| m.age >= 21 }.to_sql
|
39
|
+
sql.should == "SELECT * FROM users WHERE users.age >= 21"
|
37
40
|
|
38
41
|
sql = User.select { |m| m.age < 21 }.to_sql
|
39
|
-
sql.should == "SELECT * FROM users WHERE users
|
42
|
+
sql.should == "SELECT * FROM users WHERE users.age < 21"
|
40
43
|
end
|
41
44
|
|
42
45
|
specify "array.include? item" do
|
43
46
|
sql = User.select { |m| [1, 2, 3, 4].include? m.id }.to_sql
|
44
|
-
sql.should == "SELECT * FROM users WHERE users
|
47
|
+
sql.should == "SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
|
45
48
|
end
|
46
49
|
|
47
50
|
specify "simple == with variables" do
|
48
51
|
me = 'chris'
|
49
52
|
sql = User.select { |m| m.name == me }.to_sql
|
50
|
-
sql.should == "SELECT * FROM users WHERE users
|
53
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{me}'"
|
51
54
|
end
|
52
55
|
|
53
56
|
specify "simple == with method arguments" do
|
54
57
|
def test_it(name)
|
55
58
|
sql = User.select { |m| m.name == name }.to_sql
|
56
|
-
sql.should == "SELECT * FROM users WHERE users
|
59
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{name}'"
|
57
60
|
end
|
58
61
|
|
59
62
|
test_it('chris')
|
@@ -62,7 +65,7 @@ context "Where (using select)" do
|
|
62
65
|
specify "simple == with instance variables" do
|
63
66
|
@me = 'chris'
|
64
67
|
sql = User.select { |m| m.name == @me }.to_sql
|
65
|
-
sql.should == "SELECT * FROM users WHERE users
|
68
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{@me}'"
|
66
69
|
end
|
67
70
|
|
68
71
|
xspecify "simple == with instance variable method call" do
|
@@ -70,13 +73,13 @@ context "Where (using select)" do
|
|
70
73
|
@person = OpenStruct.new(:name => 'chris')
|
71
74
|
|
72
75
|
sql = User.select { |m| m.name == @person.name }.to_sql
|
73
|
-
sql.should == "SELECT * FROM users WHERE users
|
76
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{@person.name}'"
|
74
77
|
end
|
75
78
|
|
76
79
|
specify "simple == with global variables" do
|
77
80
|
$my_name = 'boston'
|
78
81
|
sql = User.select { |m| m.name == $my_name }.to_sql
|
79
|
-
sql.should == "SELECT * FROM users WHERE users
|
82
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{$my_name}'"
|
80
83
|
end
|
81
84
|
|
82
85
|
specify "simple == with method call" do
|
@@ -85,43 +88,43 @@ context "Where (using select)" do
|
|
85
88
|
end
|
86
89
|
|
87
90
|
sql = User.select { |m| m.name == band }.to_sql
|
88
|
-
sql.should == "SELECT * FROM users WHERE users
|
91
|
+
sql.should == "SELECT * FROM users WHERE users.name = '#{band}'"
|
89
92
|
end
|
90
93
|
|
91
94
|
specify "simple =~ with string" do
|
92
95
|
sql = User.select { |m| m.name =~ 'chris' }.to_sql
|
93
|
-
sql.should == "SELECT * FROM users WHERE users
|
96
|
+
sql.should == "SELECT * FROM users WHERE users.name LIKE 'chris'"
|
94
97
|
|
95
98
|
sql = User.select { |m| m.name =~ 'chri%' }.to_sql
|
96
|
-
sql.should == "SELECT * FROM users WHERE users
|
99
|
+
sql.should == "SELECT * FROM users WHERE users.name LIKE 'chri%'"
|
97
100
|
end
|
98
101
|
|
99
102
|
specify "simple !~ with string" do
|
100
103
|
sql = User.select { |m| m.name !~ 'chris' }.to_sql
|
101
|
-
sql.should == "SELECT * FROM users WHERE users
|
104
|
+
sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
|
102
105
|
|
103
106
|
sql = User.select { |m| !(m.name =~ 'chris') }.to_sql
|
104
|
-
sql.should == "SELECT * FROM users WHERE users
|
107
|
+
sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
|
105
108
|
end
|
106
109
|
|
107
110
|
specify "simple =~ with regexp" do
|
108
111
|
sql = User.select { |m| m.name =~ /chris/ }.to_sql
|
109
|
-
sql.should == "SELECT * FROM users WHERE users
|
112
|
+
sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
|
110
113
|
end
|
111
114
|
|
112
115
|
specify "simple =~ with regexp flags" do
|
113
116
|
sql = User.select { |m| m.name =~ /chris/i }.to_sql
|
114
|
-
sql.should == "SELECT * FROM users WHERE users
|
117
|
+
sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
|
115
118
|
end
|
116
119
|
|
117
120
|
specify "simple LOWER()" do
|
118
121
|
sql = User.select { |m| m.name.downcase =~ 'chris%' }.to_sql
|
119
|
-
sql.should == "SELECT * FROM users WHERE LOWER(users
|
122
|
+
sql.should == "SELECT * FROM users WHERE LOWER(users.name) LIKE 'chris%'"
|
120
123
|
end
|
121
124
|
|
122
125
|
specify "simple UPPER()" do
|
123
126
|
sql = User.select { |m| m.name.upcase =~ 'chris%' }.to_sql
|
124
|
-
sql.should == "SELECT * FROM users WHERE UPPER(users
|
127
|
+
sql.should == "SELECT * FROM users WHERE UPPER(users.name) LIKE 'chris%'"
|
125
128
|
end
|
126
129
|
|
127
130
|
specify "undefined equality symbol" do
|
@@ -131,7 +134,7 @@ context "Where (using select)" do
|
|
131
134
|
xspecify "simple == with inline ruby" do
|
132
135
|
# TODO: implement this
|
133
136
|
sql = User.select { |m| m.created_at == 2.days.ago.to_s(:db) }.to_sql
|
134
|
-
sql.should == "SELECT * FROM users WHERE users
|
137
|
+
sql.should == "SELECT * FROM users WHERE users.created_at = #{2.days.ago.to_s(:db)}"
|
135
138
|
end
|
136
139
|
|
137
140
|
specify "inspect" do
|
@@ -141,13 +144,13 @@ end
|
|
141
144
|
|
142
145
|
context "Where (using detect)" do
|
143
146
|
specify "simple ==" do
|
144
|
-
conditions = { :conditions => "users
|
147
|
+
conditions = { :conditions => "users.name = 'chris'", :limit => '1' }
|
145
148
|
User.expects(:find).with(:first, conditions)
|
146
149
|
User.detect { |m| m.name == 'chris' }
|
147
150
|
end
|
148
151
|
|
149
152
|
specify "nothing found" do
|
150
|
-
conditions = { :conditions => "users
|
153
|
+
conditions = { :conditions => "users.name = 'chris'", :limit => '1' }
|
151
154
|
User.expects(:find).with(:first, conditions).returns(nil)
|
152
155
|
User.detect { |m| m.name == 'chris' }.should.be.nil
|
153
156
|
end
|