Skip to content

Commit db61f02

Browse files
committed
Moved tests
1 parent a4e3217 commit db61f02

File tree

2 files changed

+71
-102
lines changed

2 files changed

+71
-102
lines changed

test/cases/fetch_test_sqlserver.rb

+71
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
require "cases/helper_sqlserver"
44
require "models/book"
5+
require "models/post"
56

67
class FetchTestSqlserver < ActiveRecord::TestCase
8+
fixtures :posts
9+
710
let(:books) { @books }
811

912
before { create_10_books }
@@ -59,6 +62,74 @@ class FetchTestSqlserver < ActiveRecord::TestCase
5962
query.to_sql
6063
end
6164
end
65+
66+
it "order by the provided orderings" do
67+
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"
68+
69+
assert_queries_match(/#{Regexp.escape(sql)}/) do
70+
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)"))
71+
assert_equal result, [11, 5, 1]
72+
end
73+
end
74+
75+
it "in subquery order by first projection if no order provided" do
76+
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"
77+
78+
assert_queries_match(/#{Regexp.escape(sql)}/) do
79+
result = Post.from(Post.limit(5).select(:legacy_comments_count, :tags_count)).pick(Arel.sql("sum(legacy_comments_count), count(*), min(legacy_comments_count)"))
80+
assert_equal result, [10, 5, 0]
81+
end
82+
end
83+
84+
it "in subquery order by primary key if no projections and order provided" do
85+
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"
86+
87+
assert_queries_match(/#{Regexp.escape(sql)}/) do
88+
result = Post.from(Post.limit(5)).pick(Arel.sql("sum(legacy_comments_count), count(*), min(legacy_comments_count)"))
89+
assert_equal result, [10, 5, 0]
90+
end
91+
end
92+
end
93+
94+
describe "GROUP query" do
95+
it "order by primary key if not a GROUP query" do
96+
assert_queries_match(/#{Regexp.escape("ORDER BY [posts].[id] ASC")}/i) do
97+
Post.pick(:title)
98+
end
99+
end
100+
101+
it "invalid SQL generated by `find_nth_with_limit` adding primary key ordering" do
102+
error = assert_raises(ActiveRecord::StatementInvalid) do
103+
Post.select(:title, "count(*)").group(:title).first(2)
104+
end
105+
assert_match(/Column "posts\.id" is invalid in the ORDER BY clause/, error.message)
106+
end
107+
108+
it "valid SQL generated if order specified" do
109+
assert_queries_match(/#{Regexp.escape("SELECT [posts].[title], count(*) FROM [posts] GROUP BY [posts].[title] ORDER BY [posts].[title]")}/i) do
110+
Post.select(:title, "count(*)").group(:title).order(:title).first(2)
111+
end
112+
end
113+
end
114+
115+
describe "LIMIT query" do
116+
it "order by primary key if no projections" do
117+
sql = Post.limit(5).to_sql
118+
119+
assert_equal "SELECT [posts].* FROM [posts] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY", sql
120+
end
121+
122+
it "order by primary key if projections and no order provided" do
123+
sql = Post.select(:legacy_comments_count).limit(5).to_sql
124+
125+
assert_equal "SELECT [posts].[legacy_comments_count] FROM [posts] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY", sql
126+
end
127+
128+
it "use order provided" do
129+
sql = Post.select(:legacy_comments_count).order(:tags_count).limit(5).to_sql
130+
131+
assert_equal "SELECT [posts].[legacy_comments_count] FROM [posts] ORDER BY [posts].[tags_count] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY", sql
132+
end
62133
end
63134

64135
protected

test/cases/implicit_order_test_sqlserver.rb

-102
This file was deleted.

0 commit comments

Comments
 (0)