-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathorder_test_sqlserver.rb
147 lines (123 loc) · 5.41 KB
/
order_test_sqlserver.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
require 'cases/helper_sqlserver'
require 'models/post'
class OrderTestSQLServer < ActiveRecord::TestCase
fixtures :posts
it 'not mangel complex order clauses' do
xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END"
xyz_post = Post.create title: 'XYZ Post', body: 'Test cased orders.'
assert_equal xyz_post, Post.order(Arel.sql(xyz_order)).first
end
it 'support column' do
order = "title"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support column ASC' do
order = "title ASC"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support column DESC' do
order = "title DESC"
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support column as symbol' do
order = :title
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support table and column' do
order = "posts.title"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support quoted column' do
order = "[title]"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support quoted table and column' do
order = "[posts].[title]"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support primary: column, secondary: column' do
order = "title DESC, body"
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: table and column, secondary: column' do
order = "posts.title DESC, body"
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: case expression, secondary: column' do
order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: quoted table and column, secondary: case expresion' do
order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
post1 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
post2 = Post.create title: 'ZZY Post', body: 'ZZZ Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support inline function' do
order = "LEN(title)"
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support inline function with parameters' do
order = "SUBSTRING(title, 1, 3)"
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support inline function with parameters DESC' do
order = "SUBSTRING(title, 1, 3) DESC"
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
end
it 'support primary: inline function, secondary: column' do
order = "LEN(title), body"
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
post2 = Post.create title: 'A', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: inline function, secondary: column with direction' do
order = "LEN(title) ASC, body DESC"
post1 = Post.create title: 'A', body: 'ZZZ Test cased orders.'
post2 = Post.create title: 'A', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: column, secondary: inline function' do
order = "body DESC, LEN(title)"
post1 = Post.create title: 'Post', body: 'ZZZ Test cased orders.'
post2 = Post.create title: 'Longer Post', body: 'ZZZ Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: case expression, secondary: inline function' do
order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
post1 = Post.create title: 'ZZZ Post', body: 'Z'
post2 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
it 'support primary: inline function, secondary: case expression' do
order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
post1 = Post.create title: 'ZZZ Post', body: 'Z'
post2 = Post.create title: 'Post', body: 'Z'
assert_equal post1, Post.order(order).first
assert_equal post2, Post.order(order).second
end
end