|
23 | 23 | require "models/tyre"
|
24 | 24 | require "models/subscriber"
|
25 | 25 | require "models/non_primary_key"
|
| 26 | +require "models/clothing_item" |
26 | 27 | require "support/stubs/strong_parameters"
|
27 | 28 | require "support/async_helper"
|
28 | 29 |
|
29 | 30 | class FinderTest < ActiveRecord::TestCase
|
30 | 31 | include AsyncHelper
|
31 | 32 |
|
32 |
| - fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :author_addresses, :customers, :categories, :categorizations, :cars |
| 33 | + fixtures :companies, :topics, :entrants, :developers, :developers_projects, |
| 34 | + :posts, :comments, :accounts, :authors, :author_addresses, :customers, |
| 35 | + :categories, :categorizations, :cars, :clothing_items |
33 | 36 |
|
34 | 37 | def test_find_by_id_with_hash
|
35 | 38 | assert_nothing_raised do
|
@@ -1033,6 +1036,33 @@ def test_implicit_order_for_model_without_primary_key
|
1033 | 1036 | NonPrimaryKey.implicit_order_column = old_implicit_order_column
|
1034 | 1037 | end
|
1035 | 1038 |
|
| 1039 | + def test_implicit_order_column_reorders_query_constraints |
| 1040 | + c = ClothingItem.connection |
| 1041 | + ClothingItem.implicit_order_column = "color" |
| 1042 | + quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type")) |
| 1043 | + quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color")) |
| 1044 | + |
| 1045 | + assert_sql(/ORDER BY #{quoted_color} ASC, #{quoted_type} ASC LIMIT/i) do |
| 1046 | + assert_kind_of ClothingItem, ClothingItem.first |
| 1047 | + end |
| 1048 | + ensure |
| 1049 | + ClothingItem.implicit_order_column = nil |
| 1050 | + end |
| 1051 | + |
| 1052 | + def test_implicit_order_column_prepends_query_constraints |
| 1053 | + c = ClothingItem.connection |
| 1054 | + ClothingItem.implicit_order_column = "description" |
| 1055 | + quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type")) |
| 1056 | + quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color")) |
| 1057 | + quoted_descrption = Regexp.escape(c.quote_table_name("clothing_items.description")) |
| 1058 | + |
| 1059 | + assert_sql(/ORDER BY #{quoted_descrption} ASC, #{quoted_type} ASC, #{quoted_color} ASC LIMIT/i) do |
| 1060 | + assert_kind_of ClothingItem, ClothingItem.first |
| 1061 | + end |
| 1062 | + ensure |
| 1063 | + ClothingItem.implicit_order_column = nil |
| 1064 | + end |
| 1065 | + |
1036 | 1066 | def test_take_and_first_and_last_with_integer_should_return_an_array
|
1037 | 1067 | assert_kind_of Array, Topic.take(5)
|
1038 | 1068 | assert_kind_of Array, Topic.first(5)
|
@@ -1731,6 +1761,26 @@ def test_finder_with_offset_string
|
1731 | 1761 | end
|
1732 | 1762 | end
|
1733 | 1763 |
|
| 1764 | + test "#last for a model with composite query constraints" do |
| 1765 | + c = ClothingItem.connection |
| 1766 | + quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type")) |
| 1767 | + quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color")) |
| 1768 | + |
| 1769 | + assert_sql(/ORDER BY #{quoted_type} DESC, #{quoted_color} DESC LIMIT/i) do |
| 1770 | + assert_kind_of ClothingItem, ClothingItem.last |
| 1771 | + end |
| 1772 | + end |
| 1773 | + |
| 1774 | + test "#first for a model with composite query constraints" do |
| 1775 | + c = ClothingItem.connection |
| 1776 | + quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type")) |
| 1777 | + quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color")) |
| 1778 | + |
| 1779 | + assert_sql(/ORDER BY #{quoted_type} ASC, #{quoted_color} ASC LIMIT/i) do |
| 1780 | + assert_kind_of ClothingItem, ClothingItem.first |
| 1781 | + end |
| 1782 | + end |
| 1783 | + |
1734 | 1784 | private
|
1735 | 1785 | def table_with_custom_primary_key
|
1736 | 1786 | yield(Class.new(Toy) do
|
|
0 commit comments