|
2 | 2 |
|
3 | 3 | require "cases/helper_sqlserver"
|
4 | 4 | require "models/book"
|
| 5 | +require "models/post" |
5 | 6 |
|
6 | 7 | class FetchTestSqlserver < ActiveRecord::TestCase
|
| 8 | + fixtures :posts |
| 9 | + |
7 | 10 | let(:books) { @books }
|
8 | 11 |
|
9 | 12 | before { create_10_books }
|
@@ -59,6 +62,74 @@ class FetchTestSqlserver < ActiveRecord::TestCase
|
59 | 62 | query.to_sql
|
60 | 63 | end
|
61 | 64 | 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 |
62 | 133 | end
|
63 | 134 |
|
64 | 135 | protected
|
|
0 commit comments