Skip to content
This repository
Browse code

Merge pull request #4153 from alexeymuranov/my_fix_for_prefix_suffix_…

…fixtures_test

Fix a fixtures test case with table prefix/suffix
  • Loading branch information...
commit 4af5252d368cbc12d79f1a7ca87e7367f7daa40b 2 parents 1a77015 + 64f106d
Jon Leighton authored December 28, 2011
4  activerecord/lib/active_record/fixtures.rb
@@ -486,8 +486,8 @@ def self.create_fixtures(fixtures_directory, table_names, class_names = {})
486 486
 
487 487
             # Cap primary key sequences to max(pk).
488 488
             if connection.respond_to?(:reset_pk_sequence!)
489  
-              table_names.each do |table_name|
490  
-                connection.reset_pk_sequence!(table_name.tr('/', '_'))
  489
+              fixture_files.each do |ff|
  490
+                connection.reset_pk_sequence!(ff.table_name)
491 491
               end
492 492
             end
493 493
           end
60  activerecord/test/cases/fixtures_test.rb
... ...
@@ -1,31 +1,32 @@
1  
-require "cases/helper"
2  
-require 'models/post'
  1
+require 'cases/helper'
  2
+require 'models/admin'
  3
+require 'models/admin/account'
  4
+require 'models/admin/user'
3 5
 require 'models/binary'
4  
-require 'models/topic'
  6
+require 'models/book'
  7
+require 'models/category'
  8
+require 'models/company'
5 9
 require 'models/computer'
  10
+require 'models/course'
6 11
 require 'models/developer'
7  
-require 'models/company'
8  
-require 'models/task'
9  
-require 'models/reply'
10 12
 require 'models/joke'
11  
-require 'models/course'
12  
-require 'models/category'
  13
+require 'models/matey'
13 14
 require 'models/parrot'
14 15
 require 'models/pirate'
15  
-require 'models/treasure'
16  
-require 'models/traffic_light'
17  
-require 'models/matey'
  16
+require 'models/post'
  17
+require 'models/reply'
18 18
 require 'models/ship'
19  
-require 'models/book'
20  
-require 'models/admin'
21  
-require 'models/admin/account'
22  
-require 'models/admin/user'
  19
+require 'models/task'
  20
+require 'models/topic'
  21
+require 'models/traffic_light'
  22
+require 'models/treasure'
23 23
 require 'tempfile'
24 24
 
25 25
 class FixturesTest < ActiveRecord::TestCase
26 26
   self.use_instantiated_fixtures = true
27 27
   self.use_transactional_fixtures = false
28 28
 
  29
+  # other_topics fixture should not be included here
29 30
   fixtures :topics, :developers, :accounts, :tasks, :categories, :funny_jokes, :binaries, :traffic_lights
30 31
 
31 32
   FIXTURES = %w( accounts binaries companies customers
@@ -93,7 +94,7 @@ def test_inserts_with_pre_and_suffix
93 94
       # Reset cache to make finds on the new table work
94 95
       ActiveRecord::Fixtures.reset_cache
95 96
 
96  
-      ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t|
  97
+      ActiveRecord::Base.connection.create_table :prefix_other_topics_suffix do |t|
97 98
         t.column :title, :string
98 99
         t.column :author_name, :string
99 100
         t.column :author_email_address, :string
@@ -115,23 +116,36 @@ def test_inserts_with_pre_and_suffix
115 116
       ActiveRecord::Base.table_name_prefix = 'prefix_'
116 117
       ActiveRecord::Base.table_name_suffix = '_suffix'
117 118
 
118  
-      topics = create_fixtures("topics")
119  
-
120  
-      first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'David'")
121  
-      assert_equal("The First Topic", first_row["title"])
  119
+      other_topic_klass = Class.new(ActiveRecord::Base) do
  120
+        def self.name
  121
+          "OtherTopic"
  122
+        end
  123
+      end
122 124
 
123  
-      second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
124  
-      assert_nil(second_row["author_email_address"])
  125
+      topics = [create_fixtures("other_topics")].flatten.first
125 126
 
126 127
       # This checks for a caching problem which causes a bug in the fixtures
127 128
       # class-level configuration helper.
128 129
       assert_not_nil topics, "Fixture data inserted, but fixture objects not returned from create"
  130
+
  131
+      first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'David'")
  132
+      assert_not_nil first_row, "The prefix_other_topics_suffix table appears to be empty despite create_fixtures: the row with author_name = 'David' was not found"
  133
+      assert_equal("The First Topic", first_row["title"])
  134
+
  135
+      second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'Mary'")
  136
+      assert_nil(second_row["author_email_address"])
  137
+
  138
+      assert_equal :prefix_other_topics_suffix, topics.table_name.to_sym
  139
+      # This assertion should preferably be the last in the list, because calling
  140
+      # other_topic_klass.table_name sets a class-level instance variable
  141
+      assert_equal :prefix_other_topics_suffix, other_topic_klass.table_name.to_sym
  142
+
129 143
     ensure
130 144
       # Restore prefix/suffix to its previous values
131 145
       ActiveRecord::Base.table_name_prefix = old_prefix
132 146
       ActiveRecord::Base.table_name_suffix = old_suffix
133 147
 
134  
-      ActiveRecord::Base.connection.drop_table :prefix_topics_suffix rescue nil
  148
+      ActiveRecord::Base.connection.drop_table :prefix_other_topics_suffix rescue nil
135 149
     end
136 150
   end
137 151
 
42  activerecord/test/fixtures/other_topics.yml
... ...
@@ -0,0 +1,42 @@
  1
+first:
  2
+  id: 1
  3
+  title: The First Topic
  4
+  author_name: David
  5
+  author_email_address: david@loudthinking.com
  6
+  written_on: 2003-07-16t15:28:11.2233+01:00
  7
+  last_read: 2004-04-15
  8
+  bonus_time: 2005-01-30t15:28:00.00+01:00
  9
+  content: Have a nice day
  10
+  approved: false
  11
+  replies_count: 1
  12
+
  13
+second:
  14
+  id: 2
  15
+  title: The Second Topic of the day
  16
+  author_name: Mary
  17
+  written_on: 2004-07-15t15:28:00.0099+01:00
  18
+  content: Have a nice day
  19
+  approved: true
  20
+  replies_count: 0
  21
+  parent_id: 1
  22
+  type: Reply
  23
+
  24
+third:
  25
+  id: 3
  26
+  title: The Third Topic of the day
  27
+  author_name: Carl
  28
+  written_on: 2005-07-15t15:28:00.0099+01:00
  29
+  content: I'm a troll
  30
+  approved: true
  31
+  replies_count: 1
  32
+
  33
+fourth:
  34
+  id: 4
  35
+  title: The Fourth Topic of the day
  36
+  author_name: Carl
  37
+  written_on: 2006-07-15t15:28:00.0099+01:00
  38
+  content: Why not?
  39
+  approved: true
  40
+  type: Reply
  41
+  parent_id: 3
  42
+

0 notes on commit 4af5252

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