Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix a fixtures test case with table prefix/suffix #4114

Merged
merged 1 commit into from

3 participants

@alexeymuranov

Make sure the table name of a model is reset in a test case after assigning ActiveRecord::Base.table_name_prefix and ActiveRecord::Base.table_name_suffix. Before this change, Topic.table_name would use the "memoized" value without prefix and suffix.

This change does not currently fix anything, but make the test case to test the right thing. I plan to submit a patch to make fixtures use the table name defined in the associated model, and there this test would fail without this fix.

This was somebody else's test case, so an independent opinion on the change can be helpful.

@jonleighton
Collaborator

I am not sure about this. Can you rewrite the test to use its own model class rather than modifying Topic?

@alexeymuranov

The problem is that changing ActiveRecord::Base.table_name_prefix will only affect Topic.table_name if Topic.table_name was never called before, otherwise the instance variable @table_name with the old table name will be used. So, i think i am fixing a bug in the test case here.

@alexeymuranov

Do you propose that i do not use an existing fixture/model and add a new one? What difference will it make?

@jonleighton
Collaborator

Yes, I am proposing that. Just feels less hackish then this weird way of resetting the table name.

@alexeymuranov

If this means to replace Topic with AnotherTopic, i can do this, but i do not see the difference with the present situation. There will be the same weird way to reset the table name, and somebody might still use AnotherTopic in their tests.

@alexeymuranov

There should be some way to reload the class and reset all class instance variables, but i do not know, maybe you can suggest something along this line?

@alexeymuranov

Ok, i think i've understood what you meant: that the fixture/model be unique in the FixturesTest class.

@alexeymuranov

How about this? But i do not understand very well what this test is doing, i was just fixing a small bug.

@jonleighton
Collaborator

Yes, that looks good, but you could actually just create the class instance within the test, which will ensure that it is definitely not used elsewhere:

[1] pry(main)> require 'active_record'
=> true
[2] pry(main)> klass = Class.new(ActiveRecord::Base) do
[2] pry(main)*   def self.name
[2] pry(main)*     "Topic"
[2] pry(main)*   end  
[2] pry(main)* end  
=> #<ActiveRecord::Aggregations::ClassMethods:0x10f0810>
[3] pry(main)> klass
=> #<ActiveRecord::Aggregations::ClassMethods:0x10f0810>
[4] pry(main)> klass.table_name
=> "topics"
@alexeymuranov

Ok, i'll try to do like this. I still think to use the same commit to order the require at the top alphabetically.

@alexeymuranov alexeymuranov Fix a fixtures test case with table prefix/suffix
Make sure the table name of a model is reset in a test case after assigning ActiveRecord::Base.table_name_prefix and ActiveRecord::Base.table_name_suffix.  This was somebody else's test case, so an independent opinion on the change can be helpful.
adb917a
@alexeymuranov

I think i've finished.

@alexeymuranov alexeymuranov commented on the diff
activerecord/test/cases/fixtures_test.rb
((29 lines not shown))
require 'models/ship'
-require 'models/book'
-require 'models/admin'
-require 'models/admin/account'
-require 'models/admin/user'
+require 'models/task'
+require 'models/topic'
+require 'models/traffic_light'
+require 'models/treasure'
require 'tempfile'

Sorted alphabetically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jonleighton jonleighton merged commit f8e484d into from
@jonleighton jonleighton referenced this pull request from a commit
@jonleighton jonleighton Revert "Merge pull request #4114 from alexeymuranov/my_fix_for_prefix…
…_suffix_fixtures_test"

This reverts commit f8e484d, reversing
changes made to fa5adfb.

Reason: broke the postgres tests.
91681c2
@jonleighton
Collaborator

This broke the postgres tests: http://travis-ci.org/#!/rails/rails/jobs/437615

I have reverted - please submit a new PR with a fix for that. Thanks.

@alexeymuranov

I do not know when i will have time to look into it, i haven't figured out how to use PostgreSQL on my Mac OS.

All errors are in the same line where database table rows are first used. It looks like the table must have been empty, but strange that this only happens with PostgreSQL. The only thing that i've changed that i can think of is that i do not have fixture :other_topics anymore, but isn't create_fixtures("other_topics") supposed to load the fixture data into the table?

@alexeymuranov

I've installed PostgreSQL! I'll try to debug this.

@alexeymuranov

How do i run a single test with postgresql?

@jonleighton
Collaborator

ARCONN=postgresql ruby -Itest path/to/test.rb

@alexeymuranov

Thanks!

@alexeymuranov

I have no idea what it was. I went back to master and started slowly applying patches. I created OtherTopic model identical to Topic, added table creation to schema.rb, did other things, then removed them, and i ended up with my original commit that now passes the postgresql test, but was not passing it before. What should i do?

@alexeymuranov

$ bundle exec rake test gave me

...
3302 tests, 10188 assertions, 0 failures, 0 errors, 10 skips
@alexeymuranov

Could have something to do with the state of the test database?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2011
  1. @alexeymuranov

    Fix a fixtures test case with table prefix/suffix

    alexeymuranov authored
    Make sure the table name of a model is reset in a test case after assigning ActiveRecord::Base.table_name_prefix and ActiveRecord::Base.table_name_suffix.  This was somebody else's test case, so an independent opinion on the change can be helpful.
This page is out of date. Refresh to see the latest.
View
59 activerecord/test/cases/fixtures_test.rb
@@ -1,31 +1,32 @@
-require "cases/helper"
-require 'models/post'
+require 'cases/helper'
+require 'models/admin'
+require 'models/admin/account'
+require 'models/admin/user'
require 'models/binary'
-require 'models/topic'
+require 'models/book'
+require 'models/category'
+require 'models/company'
require 'models/computer'
+require 'models/course'
require 'models/developer'
-require 'models/company'
-require 'models/task'
-require 'models/reply'
require 'models/joke'
-require 'models/course'
-require 'models/category'
+require 'models/matey'
require 'models/parrot'
require 'models/pirate'
-require 'models/treasure'
-require 'models/traffic_light'
-require 'models/matey'
+require 'models/post'
+require 'models/reply'
require 'models/ship'
-require 'models/book'
-require 'models/admin'
-require 'models/admin/account'
-require 'models/admin/user'
+require 'models/task'
+require 'models/topic'
+require 'models/traffic_light'
+require 'models/treasure'
require 'tempfile'

Sorted alphabetically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
class FixturesTest < ActiveRecord::TestCase
self.use_instantiated_fixtures = true
self.use_transactional_fixtures = false
+ # other_topics fixture should not be included here
fixtures :topics, :developers, :accounts, :tasks, :categories, :funny_jokes, :binaries, :traffic_lights
FIXTURES = %w( accounts binaries companies customers
@@ -93,7 +94,7 @@ def test_inserts_with_pre_and_suffix
# Reset cache to make finds on the new table work
ActiveRecord::Fixtures.reset_cache
- ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t|
+ ActiveRecord::Base.connection.create_table :prefix_other_topics_suffix do |t|
t.column :title, :string
t.column :author_name, :string
t.column :author_email_address, :string
@@ -115,23 +116,35 @@ def test_inserts_with_pre_and_suffix
ActiveRecord::Base.table_name_prefix = 'prefix_'
ActiveRecord::Base.table_name_suffix = '_suffix'
- topics = create_fixtures("topics")
-
- first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'David'")
- assert_equal("The First Topic", first_row["title"])
+ other_topic_klass = Class.new(ActiveRecord::Base) do
+ def self.name
+ "OtherTopic"
+ end
+ end
- second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
- assert_nil(second_row["author_email_address"])
+ topics = create_fixtures("other_topics")
# This checks for a caching problem which causes a bug in the fixtures
# class-level configuration helper.
assert_not_nil topics, "Fixture data inserted, but fixture objects not returned from create"
+
+ first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'David'")
+ assert_equal("The First Topic", first_row["title"])
+
+ second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'Mary'")
+ assert_nil(second_row["author_email_address"])
+
+ assert_equal "prefix_other_topics_suffix", ActiveRecord::Fixtures::all_loaded_fixtures["other_topics"].table_name
+ # This assertion should be the last in the list, because calling
+ # other_topic_klass.table_name sets a class-level instance variable
+ assert_equal "prefix_other_topics_suffix", other_topic_klass.table_name
+
ensure
# Restore prefix/suffix to its previous values
ActiveRecord::Base.table_name_prefix = old_prefix
ActiveRecord::Base.table_name_suffix = old_suffix
- ActiveRecord::Base.connection.drop_table :prefix_topics_suffix rescue nil
+ ActiveRecord::Base.connection.drop_table :prefix_other_topics_suffix rescue nil
end
end
View
42 activerecord/test/fixtures/other_topics.yml
@@ -0,0 +1,42 @@
+first:
+ id: 1
+ title: The First Topic
+ author_name: David
+ author_email_address: david@loudthinking.com
+ written_on: 2003-07-16t15:28:11.2233+01:00
+ last_read: 2004-04-15
+ bonus_time: 2005-01-30t15:28:00.00+01:00
+ content: Have a nice day
+ approved: false
+ replies_count: 1
+
+second:
+ id: 2
+ title: The Second Topic of the day
+ author_name: Mary
+ written_on: 2004-07-15t15:28:00.0099+01:00
+ content: Have a nice day
+ approved: true
+ replies_count: 0
+ parent_id: 1
+ type: Reply
+
+third:
+ id: 3
+ title: The Third Topic of the day
+ author_name: Carl
+ written_on: 2005-07-15t15:28:00.0099+01:00
+ content: I'm a troll
+ approved: true
+ replies_count: 1
+
+fourth:
+ id: 4
+ title: The Fourth Topic of the day
+ author_name: Carl
+ written_on: 2006-07-15t15:28:00.0099+01:00
+ content: Why not?
+ approved: true
+ type: Reply
+ parent_id: 3
+
Something went wrong with that request. Please try again.