Skip to content
This repository
Browse code

Add order to tests that rely on db ordering, to fix failing tests on pg

Also skip persistente tests related to UPDATE + ORDER BY for postgresql

PostgreSQL does not support updates with order by, and these tests are
failing randomly depending on the fixture loading order now.
  • Loading branch information...
commit b332891b2ab4188e9d39737f4d214812afa3ce2c 1 parent b714140
Carlos Antonio da Silva authored March 22, 2012
9  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -912,7 +912,7 @@ def test_clearing_an_association_collection
912 912
   end
913 913
 
914 914
   def test_clearing_updates_counter_cache
915  
-    topic = Topic.first
  915
+    topic = Topic.order(:id).first
916 916
 
917 917
     assert_difference 'topic.reload.replies_count', -1 do
918 918
       topic.replies.clear
@@ -1001,14 +1001,14 @@ def test_dependent_association_respects_optional_hash_conditions_on_delete
1001 1001
   end
1002 1002
 
1003 1003
   def test_delete_all_association_with_primary_key_deletes_correct_records
1004  
-    firm = Firm.find(:first)
  1004
+    firm = Firm.order(:id).first
1005 1005
     # break the vanilla firm_id foreign key
1006 1006
     assert_equal 2, firm.clients.count
1007 1007
     firm.clients.first.update_column(:firm_id, nil)
1008 1008
     assert_equal 1, firm.clients(true).count
1009 1009
     assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
1010 1010
     old_record = firm.clients_using_primary_key_with_delete_all.first
1011  
-    firm = Firm.find(:first)
  1011
+    firm = Firm.order(:id).first
1012 1012
     firm.destroy
1013 1013
     assert_nil Client.find_by_id(old_record.id)
1014 1014
   end
@@ -1168,13 +1168,12 @@ def test_depends_and_nullify
1168 1168
 
1169 1169
     core = companies(:rails_core)
1170 1170
     assert_equal accounts(:rails_core_account), core.account
1171  
-    assert_equal companies(:leetsoft, :jadedpixel), core.companies
  1171
+    assert_equal companies(:leetsoft, :jadedpixel), core.companies.order(:id)
1172 1172
     core.destroy
1173 1173
     assert_nil accounts(:rails_core_account).reload.firm_id
1174 1174
     assert_nil companies(:leetsoft).reload.client_of
1175 1175
     assert_nil companies(:jadedpixel).reload.client_of
1176 1176
 
1177  
-
1178 1177
     assert_equal num_accounts, Account.count
1179 1178
   end
1180 1179
 
6  activerecord/test/cases/associations/join_model_test.rb
@@ -403,7 +403,7 @@ def test_has_many_through_has_many_find_by_id
403 403
   end
404 404
 
405 405
   def test_has_many_through_polymorphic_has_one
406  
-    assert_equal Tagging.find(1,2).sort_by { |t| t.id }, authors(:david).tagging
  406
+    assert_equal Tagging.find(1,2).sort_by { |t| t.id }, authors(:david).tagging.order(:id)
407 407
   end
408 408
 
409 409
   def test_has_many_through_polymorphic_has_many
@@ -452,7 +452,7 @@ def test_add_to_self_referential_has_many_through
452 452
   end
453 453
 
454 454
   def test_has_many_through_uses_conditions_specified_on_the_has_many_association
455  
-    author = Author.find(:first)
  455
+    author = Author.order(:id).first
456 456
     assert_present author.comments
457 457
     assert_blank author.nonexistant_comments
458 458
   end
@@ -649,7 +649,7 @@ def test_preload_polymorphic_has_many_through
649 649
   end
650 650
 
651 651
   def test_preload_polymorph_many_types
652  
-    taggings = Tagging.find :all, :include => :taggable, :conditions => ['taggable_type != ?', 'FakeModel']
  652
+    taggings = Tagging.find :all, :include => :taggable, :conditions => ['taggable_type != ?', 'FakeModel'], :order => 'id'
653 653
     assert_no_queries do
654 654
       taggings.first.taggable.id
655 655
       taggings[1].taggable.id
18  activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -55,7 +55,7 @@ def test_has_many_through_has_many_with_has_many_through_source_reflection
55 55
   end
56 56
 
57 57
   def test_has_many_through_has_many_with_has_many_through_source_reflection_preload
58  
-    authors = assert_queries(5) { Author.includes(:tags).to_a }
  58
+    authors = assert_queries(5) { Author.includes(:tags).order(:id).to_a }
59 59
     general = tags(:general)
60 60
 
61 61
     assert_no_queries do
@@ -84,7 +84,7 @@ def test_has_many_through_has_many_through_with_has_many_source_reflection
84 84
 
85 85
   def test_has_many_through_has_many_through_with_has_many_source_reflection_preload
86 86
     luke, david = subscribers(:first), subscribers(:second)
87  
-    authors = assert_queries(4) { Author.includes(:subscribers).to_a }
  87
+    authors = assert_queries(4) { Author.includes(:subscribers).order(:id).to_a }
88 88
     assert_no_queries do
89 89
       assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick)
90 90
     end
@@ -106,10 +106,10 @@ def test_has_many_through_has_one_with_has_one_through_source_reflection
106 106
   end
107 107
 
108 108
   def test_has_many_through_has_one_with_has_one_through_source_reflection_preload
109  
-    members = assert_queries(4) { Member.includes(:nested_member_types).to_a }
  109
+    members = assert_queries(4) { Member.includes(:nested_member_types).order(:id).to_a }
110 110
     founding = member_types(:founding)
111 111
     assert_no_queries do
112  
-      assert_equal [founding], members.first.nested_member_types
  112
+      assert_equal [founding], members.first.nested_member_types.to_a
113 113
     end
114 114
   end
115 115
 
@@ -128,10 +128,10 @@ def test_has_many_through_has_one_through_with_has_one_source_reflection
128 128
   end
129 129
 
130 130
   def test_has_many_through_has_one_through_with_has_one_source_reflection_preload
131  
-    members = assert_queries(4) { Member.includes(:nested_sponsors).to_a }
  131
+    members = assert_queries(4) { Member.includes(:nested_sponsors).order(:id).to_a }
132 132
     mustache = sponsors(:moustache_club_sponsor_for_groucho)
133 133
     assert_no_queries do
134  
-      assert_equal [mustache], members.first.nested_sponsors
  134
+      assert_equal [mustache], members.first.nested_sponsors.to_a
135 135
     end
136 136
   end
137 137
 
@@ -163,7 +163,7 @@ def test_has_many_through_has_one_with_has_many_through_source_reflection_preloa
163 163
 
164 164
   def test_has_many_through_has_one_with_has_many_through_source_reflection_preload_via_joins
165 165
     assert_includes_and_joins_equal(
166  
-      Member.where('member_details.id' => member_details(:groucho).id).order('member_details.id'),
  166
+      Member.where('member_details.id' => member_details(:groucho).id).order('members.id'),
167 167
       [members(:groucho), members(:some_other_guy)], :organization_member_details
168 168
     )
169 169
 
@@ -193,7 +193,7 @@ def test_has_many_through_has_one_through_with_has_many_source_reflection_preloa
193 193
 
194 194
   def test_has_many_through_has_one_through_with_has_many_source_reflection_preload_via_joins
195 195
     assert_includes_and_joins_equal(
196  
-      Member.where('member_details.id' => member_details(:groucho).id).order('member_details.id'),
  196
+      Member.where('member_details.id' => member_details(:groucho).id).order('members.id'),
197 197
       [members(:groucho), members(:some_other_guy)], :organization_member_details_2
198 198
     )
199 199
 
@@ -285,7 +285,7 @@ def test_has_many_through_has_many_through_with_belongs_to_source_reflection
285 285
   end
286 286
 
287 287
   def test_has_many_through_has_many_through_with_belongs_to_source_reflection_preload
288  
-    authors = assert_queries(5) { Author.includes(:tagging_tags).to_a }
  288
+    authors = assert_queries(5) { Author.includes(:tagging_tags).order(:id).to_a }
289 289
     general = tags(:general)
290 290
 
291 291
     assert_no_queries do
6  activerecord/test/cases/clone_test.rb
@@ -6,7 +6,7 @@ class CloneTest < ActiveRecord::TestCase
6 6
     fixtures :topics
7 7
 
8 8
     def test_persisted
9  
-      topic = Topic.first
  9
+      topic = Topic.order(:id).first
10 10
       cloned = topic.clone
11 11
       assert topic.persisted?, 'topic persisted'
12 12
       assert cloned.persisted?, 'topic persisted'
@@ -14,7 +14,7 @@ def test_persisted
14 14
     end
15 15
 
16 16
     def test_stays_frozen
17  
-      topic = Topic.first
  17
+      topic = Topic.order(:id).first
18 18
       topic.freeze
19 19
 
20 20
       cloned = topic.clone
@@ -24,7 +24,7 @@ def test_stays_frozen
24 24
     end
25 25
 
26 26
     def test_shallow
27  
-      topic = Topic.first
  27
+      topic = Topic.order(:id).first
28 28
       cloned = topic.clone
29 29
       topic.author_name = 'Aaron'
30 30
       assert_equal 'Aaron', cloned.author_name
19  activerecord/test/cases/dup_test.rb
@@ -10,14 +10,14 @@ def test_dup
10 10
     end
11 11
 
12 12
     def test_not_readonly
13  
-      topic = Topic.first
  13
+      topic = Topic.order(:id).first
14 14
 
15 15
       duped = topic.dup
16 16
       assert !duped.readonly?, 'should not be readonly'
17 17
     end
18 18
 
19 19
     def test_is_readonly
20  
-      topic = Topic.first
  20
+      topic = Topic.order(:id).first
21 21
       topic.readonly!
22 22
 
23 23
       duped = topic.dup
@@ -25,7 +25,7 @@ def test_is_readonly
25 25
     end
26 26
 
27 27
     def test_dup_not_persisted
28  
-      topic = Topic.first
  28
+      topic = Topic.order(:id).first
29 29
       duped = topic.dup
30 30
 
31 31
       assert !duped.persisted?, 'topic not persisted'
@@ -33,20 +33,20 @@ def test_dup_not_persisted
33 33
     end
34 34
 
35 35
     def test_dup_has_no_id
36  
-      topic = Topic.first
  36
+      topic = Topic.order(:id).first
37 37
       duped = topic.dup
38 38
       assert_nil duped.id
39 39
     end
40 40
 
41 41
     def test_dup_with_modified_attributes
42  
-      topic = Topic.first
  42
+      topic = Topic.order(:id).first
43 43
       topic.author_name = 'Aaron'
44 44
       duped = topic.dup
45 45
       assert_equal 'Aaron', duped.author_name
46 46
     end
47 47
 
48 48
     def test_dup_with_changes
49  
-      dbtopic = Topic.first
  49
+      dbtopic = Topic.order(:id).first
50 50
       topic = Topic.new
51 51
 
52 52
       topic.attributes = dbtopic.attributes
@@ -61,7 +61,7 @@ def test_dup_with_changes
61 61
     end
62 62
 
63 63
     def test_dup_topics_are_independent
64  
-      topic = Topic.first
  64
+      topic = Topic.order(:id).first
65 65
       topic.author_name = 'Aaron'
66 66
       duped = topic.dup
67 67
 
@@ -71,7 +71,7 @@ def test_dup_topics_are_independent
71 71
     end
72 72
 
73 73
     def test_dup_attributes_are_independent
74  
-      topic = Topic.first
  74
+      topic = Topic.order(:id).first
75 75
       duped = topic.dup
76 76
 
77 77
       duped.author_name = 'meow'
@@ -82,7 +82,7 @@ def test_dup_attributes_are_independent
82 82
     end
83 83
 
84 84
     def test_dup_timestamps_are_cleared
85  
-      topic = Topic.first
  85
+      topic = Topic.order(:id).first
86 86
       assert_not_nil topic.updated_at
87 87
       assert_not_nil topic.created_at
88 88
 
@@ -98,6 +98,5 @@ def test_dup_timestamps_are_cleared
98 98
       assert_not_nil new_topic.updated_at
99 99
       assert_not_nil new_topic.created_at
100 100
     end
101  
-
102 101
   end
103 102
 end
4  activerecord/test/cases/finder_test.rb
@@ -53,8 +53,8 @@ def test_exists_returns_true_with_one_record_and_no_args
53 53
   def test_exists_with_nil_arg
54 54
     assert !Topic.exists?(nil)
55 55
     assert Topic.exists?
56  
-    assert !Topic.first.replies.exists?(nil)
57  
-    assert Topic.first.replies.exists?
  56
+    assert !Topic.order(:id).first.replies.exists?(nil)
  57
+    assert Topic.order(:id).first.replies.exists?
58 58
   end
59 59
 
60 60
   # ensures +exists?+ runs valid SQL by excluding order value
4  activerecord/test/cases/json_serialization_test.rb
@@ -211,7 +211,7 @@ def test_should_allow_includes_for_list_of_authors
211 211
     ['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
212 212
      '{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[', '{"id":7}', '{"id":9}'].each do |fragment|
213 213
       assert json.include?(fragment), json
214  
-     end
  214
+    end
215 215
   end
216 216
 
217 217
   def test_should_allow_options_for_hash_of_authors
@@ -223,7 +223,7 @@ def test_should_allow_options_for_hash_of_authors
223 223
   end
224 224
 
225 225
   def test_should_be_able_to_encode_relation
226  
-    authors_relation = Author.where(:id => [@david.id, @mary.id])
  226
+    authors_relation = Author.where(:id => [@david.id, @mary.id]).order(:id)
227 227
 
228 228
     json = ActiveSupport::JSON.encode authors_relation, :only => :name
229 229
     assert_equal '[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]', json
2  activerecord/test/cases/nested_attributes_test.rb
@@ -876,7 +876,7 @@ def setup
876 876
 
877 877
   def test_should_update_existing_records_with_non_standard_primary_key
878 878
     @owner.update_attributes(@params)
879  
-    assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name)
  879
+    assert_equal %w(Bar Foo), @owner.pets.map(&:name).sort
880 880
   end
881 881
 
882 882
   def test_attr_accessor_of_child_should_be_value_provided_during_update_attributes
11  activerecord/test/cases/persistence_test.rb
@@ -17,15 +17,16 @@
17 17
 require 'active_support/core_ext/exception'
18 18
 
19 19
 class PersistencesTest < ActiveRecord::TestCase
  20
+  fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics,
  21
+    'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans
20 22
 
21  
-  fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans
22  
-
23  
-  # Oracle UPDATE does not support ORDER BY
24  
-  unless current_adapter?(:OracleAdapter)
  23
+  # Skip databases that don't support UPDATE + ORDER BY
  24
+  unless current_adapter?(:OracleAdapter, :PostgreSQLAdapter)
25 25
     def test_update_all_ignores_order_without_limit_from_association
26 26
       author = authors(:david)
27 27
       assert_nothing_raised do
28  
-        assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
  28
+        assert_equal author.posts_with_comments_and_categories.length,
  29
+          author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
29 30
       end
30 31
     end
31 32
 
2  activerecord/test/cases/relations_test.rb
@@ -674,7 +674,7 @@ def test_relation_merging_with_locks
674 674
   def test_relation_merging_with_preload
675 675
     ActiveRecord::IdentityMap.without do
676 676
       [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts|
677  
-        assert_queries(2) { assert posts.first.author }
  677
+        assert_queries(2) { assert posts.order(:id).first.author }
678 678
       end
679 679
     end
680 680
   end
2  activerecord/test/cases/timestamp_test.rb
@@ -10,7 +10,7 @@ class TimestampTest < ActiveRecord::TestCase
10 10
   fixtures :developers, :owners, :pets, :toys, :cars, :tasks
11 11
 
12 12
   def setup
13  
-    @developer = Developer.first
  13
+    @developer = Developer.order(:id).first
14 14
     @developer.update_attribute(:updated_at, Time.now.prev_month)
15 15
     @previously_updated_at = @developer.updated_at
16 16
   end
6  activerecord/test/cases/yaml_serialization_test.rb
@@ -12,7 +12,7 @@ def test_to_yaml_with_time_with_zone_should_not_raise_exception
12 12
   end
13 13
 
14 14
   def test_roundtrip
15  
-    topic = Topic.first
  15
+    topic = Topic.order(:id).first
16 16
     assert topic
17 17
     t = YAML.load YAML.dump topic
18 18
     assert_equal topic, t
@@ -24,7 +24,7 @@ def test_roundtrip_serialized_column
24 24
   end
25 25
 
26 26
   def test_encode_with_coder
27  
-    topic = Topic.first
  27
+    topic = Topic.order(:id).first
28 28
     coder = {}
29 29
     topic.encode_with coder
30 30
     assert_equal({'attributes' => topic.attributes}, coder)
@@ -34,7 +34,7 @@ def test_encode_with_coder
34 34
     require 'psych'
35 35
 
36 36
     def test_psych_roundtrip
37  
-      topic = Topic.first
  37
+      topic = Topic.order(:id).first
38 38
       assert topic
39 39
       t = Psych.load Psych.dump topic
40 40
       assert_equal topic, t
2  activerecord/test/models/company.rb
@@ -104,7 +104,7 @@ def log_after_remove(record)
104 104
 end
105 105
 
106 106
 class DependentFirm < Company
107  
-  has_one :account, :foreign_key => "firm_id", :dependent => :nullify
  107
+  has_one :account, :foreign_key => "firm_id", :dependent => :nullify, :order => "accounts.id"
108 108
   has_many :companies, :foreign_key => 'client_of', :dependent => :nullify
109 109
 end
110 110
 

0 notes on commit b332891

Please sign in to comment.
Something went wrong with that request. Please try again.