|
6 | 6 | class OrderTestSQLServer < ActiveRecord::TestCase |
7 | 7 | fixtures :posts |
8 | 8 |
|
9 | | - |
10 | | - |
11 | 9 | it "not mangel complex order clauses" do |
12 | 10 | xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END" |
13 | 11 | xyz_post = Post.create title: "XYZ Post", body: "Test cased orders." |
@@ -152,123 +150,4 @@ class OrderTestSQLServer < ActiveRecord::TestCase |
152 | 150 | sql = Post.order(:id).order("posts.id ASC").to_sql |
153 | 151 | assert_equal "SELECT [posts].* FROM [posts] ORDER BY [posts].[id] ASC, posts.id ASC", sql |
154 | 152 | end |
155 | | - |
156 | | - |
157 | | - describe "grouping queries" do |
158 | | - |
159 | | - # it "order by primary key by default if not a grouping query" do |
160 | | - # |
161 | | - # $DEBUG = true |
162 | | - # |
163 | | - # assert_queries_match(/ORDER BY \[posts\]\.\[id\] ASC/i) do |
164 | | - # Post.pick(:title) |
165 | | - # end |
166 | | - # end |
167 | | - |
168 | | - |
169 | | - it "ordering not required if not using FETCH" do |
170 | | - # skip |
171 | | - |
172 | | - assert_queries_match(/^#{Regexp.escape("SELECT count(*) FROM [posts] GROUP BY [posts].[title]")}$/i) do |
173 | | - Post.group(:title).select("count(*)").load |
174 | | - end |
175 | | - end |
176 | | - |
177 | | - it "error thrown if using FETCH without ordering and column not in select" do |
178 | | - # skip |
179 | | - |
180 | | - error = assert_raises(ActiveRecord::StatementInvalid) do |
181 | | - Post.select(:title, "count(*)").group(:title).first(2) |
182 | | - end |
183 | | - assert_match(/Column "posts\.id" is invalid in the ORDER BY clause/, error.message) |
184 | | - end |
185 | | - |
186 | | - it "ordering required if using FETCH" do |
187 | | - |
188 | | - # ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| |
189 | | - # puts payload[:sql] |
190 | | - # end |
191 | | - |
192 | | - # $DEBUG = true |
193 | | - |
194 | | - # TODO: failing when it should not fail. The ordering should be taken from the projection. |
195 | | - |
196 | | - # `first` calls `find_nth_with_limit` which adds ordering. |
197 | | - |
198 | | - assert_queries_match(/#{Regexp.escape("SELECT [posts].[title], count(*) FROM [posts] GROUP BY [posts].[title] ORDER BY [posts].[title] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY")}/i) do |
199 | | - # error = assert_raises(ActiveRecord::StatementInvalid) do |
200 | | - Post.select(:title, "count(*)").group(:title).order(:title).first(2) |
201 | | - end |
202 | | - |
203 | | - # assert_match(/Column "posts\.id" is invalid in the ORDER BY clause because/, error.message) |
204 | | - |
205 | | - # Post.group(:title).select("count(*)").first(2).load |
206 | | - |
207 | | - # assert_match(/Column "posts\.id" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause/, error.message) |
208 | | - end |
209 | | - |
210 | | - |
211 | | - # |
212 | | - # it "order by primary key by default if not a grouping query" do |
213 | | - # $DEBUG = true |
214 | | - # |
215 | | - # ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| |
216 | | - # puts payload[:sql] |
217 | | - # end |
218 | | - # |
219 | | - # Post.group(:title).select("count(*)").first(2).load |
220 | | - # end |
221 | | - |
222 | | - end |
223 | | - |
224 | | - |
225 | | - |
226 | | - describe "simple query containing limit" do |
227 | | - it "order by primary key if no projections" do |
228 | | - sql = Post.limit(5).to_sql |
229 | | - |
230 | | - assert_equal "SELECT [posts].* FROM [posts] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY", sql |
231 | | - end |
232 | | - |
233 | | - it "use order provided" do |
234 | | - sql = Post.select(:legacy_comments_count).order(:tags_count).limit(5).to_sql |
235 | | - |
236 | | - assert_equal "SELECT [posts].[legacy_comments_count] FROM [posts] ORDER BY [posts].[tags_count] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY", sql |
237 | | - end |
238 | | - |
239 | | - |
240 | | - |
241 | | - |
242 | | - end |
243 | | - |
244 | | - describe "query containing FROM and limit" do |
245 | | - it "uses the provided orderings" do |
246 | | - sql = "SELECT sum(legacy_comments_count), count(*), min(legacy_comments_count) FROM (SELECT [posts].[legacy_comments_count] FROM [posts] ORDER BY [posts].[legacy_comments_count] DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY) subquery ORDER BY sum(legacy_comments_count) ASC OFFSET 0 ROWS FETCH NEXT @1 ROWS ONLY" |
247 | | - |
248 | | - assert_queries_match(/#{Regexp.escape(sql)}/) do |
249 | | - result = Post.from(Post.order(legacy_comments_count: :desc).limit(5).select(:legacy_comments_count)).pick(Arel.sql("sum(legacy_comments_count), count(*), min(legacy_comments_count)")) |
250 | | - assert_equal result, [11, 5, 1] |
251 | | - end |
252 | | - end |
253 | | - |
254 | | - it "in the subquery the first projection is used for ordering if none provided" do |
255 | | - sql = "SELECT sum(legacy_comments_count), count(*), min(legacy_comments_count) FROM (SELECT [posts].[legacy_comments_count], [posts].[tags_count] FROM [posts] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY) subquery ORDER BY sum(legacy_comments_count) ASC OFFSET 0 ROWS FETCH NEXT @1 ROWS ONLY" |
256 | | - |
257 | | - # $DEBUG = true |
258 | | - |
259 | | - assert_queries_match(/#{Regexp.escape(sql)}/) do |
260 | | - result = Post.from(Post.limit(5).select(:legacy_comments_count, :tags_count)).pick(Arel.sql("sum(legacy_comments_count), count(*), min(legacy_comments_count)")) |
261 | | - assert_equal result, [10, 5, 0] |
262 | | - end |
263 | | - end |
264 | | - |
265 | | - it "in the subquery the primary key is used for ordering if none provided" do |
266 | | - sql = "SELECT sum(legacy_comments_count), count(*), min(legacy_comments_count) FROM (SELECT [posts].* FROM [posts] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY) subquery ORDER BY sum(legacy_comments_count) ASC OFFSET 0 ROWS FETCH NEXT @1 ROWS ONLY" |
267 | | - |
268 | | - assert_queries_match(/#{Regexp.escape(sql)}/) do |
269 | | - result = Post.from(Post.limit(5)).pick(Arel.sql("sum(legacy_comments_count), count(*), min(legacy_comments_count)")) |
270 | | - assert_equal result, [10, 5, 0] |
271 | | - end |
272 | | - end |
273 | | - end |
274 | 153 | end |
0 commit comments