Skip to content

Commit 59e3e06

Browse files
committed
Order all green.
1 parent ad66f64 commit 59e3e06

File tree

1 file changed

+138
-139
lines changed

1 file changed

+138
-139
lines changed

test/cases/order_test_sqlserver.rb

Lines changed: 138 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -5,144 +5,143 @@ class OrderTestSQLServer < ActiveRecord::TestCase
55

66
fixtures :posts
77

8-
context 'Order by' do
9-
10-
should 'not mangel complex order clauses' do
11-
xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END"
12-
xyz_post = Post.create title: 'XYZ Post', body: 'Test cased orders.'
13-
assert_equal xyz_post, Post.order(Arel::Nodes::Ordering.new(Arel.sql(xyz_order))).first
14-
end
15-
16-
should 'support column' do
17-
order = "title"
18-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
19-
assert_equal post1, Post.order(order).first
20-
end
21-
22-
should 'support column ASC' do
23-
order = "title ASC"
24-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
25-
assert_equal post1, Post.order(order).first
26-
end
27-
28-
should 'support column DESC' do
29-
order = "title DESC"
30-
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
31-
assert_equal post1, Post.order(order).first
32-
end
33-
34-
should 'support column as symbol' do
35-
order = :title
36-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
37-
assert_equal post1, Post.order(order).first
38-
end
39-
40-
should 'support table and column' do
41-
order = "posts.title"
42-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
43-
assert_equal post1, Post.order(order).first
44-
end
45-
46-
should 'support quoted column' do
47-
order = "[title]"
48-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
49-
assert_equal post1, Post.order(order).first
50-
end
51-
52-
should 'support quoted table and column' do
53-
order = "[posts].[title]"
54-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
55-
assert_equal post1, Post.order(order).first
56-
end
57-
58-
should 'support primary: column, secondary: column' do
59-
order = "title DESC, body"
60-
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
61-
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
62-
assert_equal post1, Post.order(order).first
63-
assert_equal post2, Post.order(order).second
64-
end
65-
66-
should 'support primary: table and column, secondary: column' do
67-
order = "posts.title DESC, body"
68-
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
69-
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
70-
assert_equal post1, Post.order(order).first
71-
assert_equal post2, Post.order(order).second
72-
end
73-
74-
should 'support primary: case expression, secondary: column' do
75-
order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
76-
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
77-
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
78-
assert_equal post1, Post.order(order).first
79-
assert_equal post2, Post.order(order).second
80-
end
81-
82-
should 'support primary: quoted table and column, secondary: case expresion' do
83-
order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
84-
post1 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
85-
post2 = Post.create title: 'ZZY Post', body: 'ZZZ Test cased orders.'
86-
assert_equal post1, Post.order(order).first
87-
assert_equal post2, Post.order(order).second
88-
end
89-
90-
should 'support inline function' do
91-
order = "LEN(title)"
92-
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
93-
assert_equal post1, Post.order(order).first
94-
end
95-
96-
should 'support inline function with parameters' do
97-
order = "SUBSTRING(title, 1, 3)"
98-
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
99-
assert_equal post1, Post.order(order).first
100-
end
101-
102-
should 'support inline function with parameters DESC' do
103-
order = "SUBSTRING(title, 1, 3) DESC"
104-
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
105-
assert_equal post1, Post.order(order).first
106-
end
107-
108-
should 'support primary: inline function, secondary: column' do
109-
order = "LEN(title), body"
110-
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
111-
post2 = Post.create title: 'A', body: 'Test cased orders.'
112-
assert_equal post1, Post.order(order).first
113-
assert_equal post2, Post.order(order).second
114-
end
115-
116-
should 'support primary: inline function, secondary: column with direction' do
117-
order = "LEN(title) ASC, body DESC"
118-
post1 = Post.create title: 'A', body: 'ZZZ Test cased orders.'
119-
post2 = Post.create title: 'A', body: 'Test cased orders.'
120-
assert_equal post1, Post.order(order).first
121-
assert_equal post2, Post.order(order).second
122-
end
123-
124-
should 'support primary: column, secondary: inline function' do
125-
order = "body DESC, LEN(title)"
126-
post1 = Post.create title: 'Post', body: 'ZZZ Test cased orders.'
127-
post2 = Post.create title: 'Longer Post', body: 'ZZZ Test cased orders.'
128-
assert_equal post1, Post.order(order).first
129-
assert_equal post2, Post.order(order).second
130-
end
131-
132-
should 'support primary: case expression, secondary: inline function' do
133-
order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
134-
post1 = Post.create title: 'ZZZ Post', body: 'Z'
135-
post2 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
136-
assert_equal post1, Post.order(order).first
137-
assert_equal post2, Post.order(order).second
138-
end
139-
140-
should 'support primary: inline function, secondary: case expression' do
141-
order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
142-
post1 = Post.create title: 'ZZZ Post', body: 'Z'
143-
post2 = Post.create title: 'Post', body: 'Z'
144-
assert_equal post1, Post.order(order).first
145-
assert_equal post2, Post.order(order).second
146-
end
8+
it 'not mangel complex order clauses' do
9+
xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END"
10+
xyz_post = Post.create title: 'XYZ Post', body: 'Test cased orders.'
11+
assert_equal xyz_post, Post.order(Arel.sql(xyz_order)).first
14712
end
13+
14+
it 'support column' do
15+
order = "title"
16+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
17+
assert_equal post1, Post.order(order).first
18+
end
19+
20+
it 'support column ASC' do
21+
order = "title ASC"
22+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
23+
assert_equal post1, Post.order(order).first
24+
end
25+
26+
it 'support column DESC' do
27+
order = "title DESC"
28+
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
29+
assert_equal post1, Post.order(order).first
30+
end
31+
32+
it 'support column as symbol' do
33+
order = :title
34+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
35+
assert_equal post1, Post.order(order).first
36+
end
37+
38+
it 'support table and column' do
39+
order = "posts.title"
40+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
41+
assert_equal post1, Post.order(order).first
42+
end
43+
44+
it 'support quoted column' do
45+
order = "[title]"
46+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
47+
assert_equal post1, Post.order(order).first
48+
end
49+
50+
it 'support quoted table and column' do
51+
order = "[posts].[title]"
52+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
53+
assert_equal post1, Post.order(order).first
54+
end
55+
56+
it 'support primary: column, secondary: column' do
57+
order = "title DESC, body"
58+
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
59+
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
60+
assert_equal post1, Post.order(order).first
61+
assert_equal post2, Post.order(order).second
62+
end
63+
64+
it 'support primary: table and column, secondary: column' do
65+
order = "posts.title DESC, body"
66+
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
67+
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
68+
assert_equal post1, Post.order(order).first
69+
assert_equal post2, Post.order(order).second
70+
end
71+
72+
it 'support primary: case expression, secondary: column' do
73+
order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
74+
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
75+
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
76+
assert_equal post1, Post.order(order).first
77+
assert_equal post2, Post.order(order).second
78+
end
79+
80+
it 'support primary: quoted table and column, secondary: case expresion' do
81+
order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
82+
post1 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
83+
post2 = Post.create title: 'ZZY Post', body: 'ZZZ Test cased orders.'
84+
assert_equal post1, Post.order(order).first
85+
assert_equal post2, Post.order(order).second
86+
end
87+
88+
it 'support inline function' do
89+
order = "LEN(title)"
90+
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
91+
assert_equal post1, Post.order(order).first
92+
end
93+
94+
it 'support inline function with parameters' do
95+
order = "SUBSTRING(title, 1, 3)"
96+
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
97+
assert_equal post1, Post.order(order).first
98+
end
99+
100+
it 'support inline function with parameters DESC' do
101+
order = "SUBSTRING(title, 1, 3) DESC"
102+
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
103+
assert_equal post1, Post.order(order).first
104+
end
105+
106+
it 'support primary: inline function, secondary: column' do
107+
order = "LEN(title), body"
108+
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
109+
post2 = Post.create title: 'A', body: 'Test cased orders.'
110+
assert_equal post1, Post.order(order).first
111+
assert_equal post2, Post.order(order).second
112+
end
113+
114+
it 'support primary: inline function, secondary: column with direction' do
115+
order = "LEN(title) ASC, body DESC"
116+
post1 = Post.create title: 'A', body: 'ZZZ Test cased orders.'
117+
post2 = Post.create title: 'A', body: 'Test cased orders.'
118+
assert_equal post1, Post.order(order).first
119+
assert_equal post2, Post.order(order).second
120+
end
121+
122+
it 'support primary: column, secondary: inline function' do
123+
order = "body DESC, LEN(title)"
124+
post1 = Post.create title: 'Post', body: 'ZZZ Test cased orders.'
125+
post2 = Post.create title: 'Longer Post', body: 'ZZZ Test cased orders.'
126+
assert_equal post1, Post.order(order).first
127+
assert_equal post2, Post.order(order).second
128+
end
129+
130+
it 'support primary: case expression, secondary: inline function' do
131+
order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
132+
post1 = Post.create title: 'ZZZ Post', body: 'Z'
133+
post2 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
134+
assert_equal post1, Post.order(order).first
135+
assert_equal post2, Post.order(order).second
136+
end
137+
138+
it 'support primary: inline function, secondary: case expression' do
139+
order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
140+
post1 = Post.create title: 'ZZZ Post', body: 'Z'
141+
post2 = Post.create title: 'Post', body: 'Z'
142+
assert_equal post1, Post.order(order).first
143+
assert_equal post2, Post.order(order).second
144+
end
145+
146+
148147
end

0 commit comments

Comments
 (0)