Permalink
Browse files

Add set_fixture_class to allow the use of table name accessors with m…

…odels which use set_default_name. Closes #3935

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3690 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent d0bd952 commit ada11d6313bd1f74c6a229caca36ca063b38da4e @seckar seckar committed Feb 27, 2006
View
@@ -1,5 +1,7 @@
*SVN*
+* Add set_fixture_class to allow the use of table name accessors with models which use set_default_name. [Kevin Clark]
+
* Added that fixtures to placed in subdirectories of the main fixture files are also loaded #3937 [dblack@wobblini.net]
* Define attribute query methods to avoid method_missing calls. #3677 [jonathan@bluewire.net.nz]
@@ -237,14 +237,13 @@ def self.instantiate_all_loaded_fixtures(object, load_instances=true)
cattr_accessor :all_loaded_fixtures
self.all_loaded_fixtures = {}
- def self.create_fixtures(fixtures_directory, *table_names)
+ def self.create_fixtures(fixtures_directory, table_names, class_names)
table_names = table_names.flatten.map { |n| n.to_s }
connection = block_given? ? yield : ActiveRecord::Base.connection
-
ActiveRecord::Base.silence do
fixtures_map = {}
fixtures = table_names.map do |table_name|
- fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, File.join(fixtures_directory, table_name.to_s))
+ fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s))
end
all_loaded_fixtures.merge! fixtures_map
@@ -267,10 +266,10 @@ def self.create_fixtures(fixtures_directory, *table_names)
attr_reader :table_name
- def initialize(connection, table_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
+ def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
-
- @class_name = ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize
+ @class_name = class_name ||
+ (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
read_fixture_files
end
@@ -423,6 +422,7 @@ module Unit #:nodoc:
class TestCase #:nodoc:
cattr_accessor :fixture_path
class_inheritable_accessor :fixture_table_names
+ class_inheritable_accessor :fixture_class_names
class_inheritable_accessor :use_transactional_fixtures
class_inheritable_accessor :use_instantiated_fixtures # true, false, or :no_instances
class_inheritable_accessor :pre_loaded_fixtures
@@ -431,9 +431,16 @@ class TestCase #:nodoc:
self.use_transactional_fixtures = false
self.use_instantiated_fixtures = true
self.pre_loaded_fixtures = false
-
+
+ self.fixture_class_names = {}
+
@@already_loaded_fixtures = {}
-
+ self.fixture_class_names = {}
+
+ def self.set_fixture_class(class_names = {})
+ self.fixture_class_names = self.fixture_class_names.merge(class_names)
+ end
+
def self.fixtures(*table_names)
table_names = table_names.flatten.map { |n| n.to_s }
self.fixture_table_names |= table_names
@@ -548,7 +555,7 @@ def self.method_added(method)
private
def load_fixtures
@loaded_fixtures = {}
- fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names)
+ fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names)
unless fixtures.nil?
if fixtures.instance_of?(Fixtures)
@loaded_fixtures[fixtures.table_name] = fixtures
@@ -16,7 +16,7 @@ class Test::Unit::TestCase #:nodoc:
self.use_transactional_fixtures = (ENV['AR_NO_TX_FIXTURES'] != "yes")
def create_fixtures(*table_names, &block)
- Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, &block)
+ Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block)
end
def assert_date_from_db(expected, actual, message = nil)
@@ -4,6 +4,11 @@ CREATE TABLE 'accounts' (
'credit_limit' INTEGER DEFAULT NULL
);
+CREATE TABLE 'funny_jokes' (
+ 'id' INTEGER PRIMARY KEY NOT NULL,
+ 'name' TEXT DEFAULT NULL
+);
+
CREATE TABLE 'companies' (
'id' INTEGER PRIMARY KEY NOT NULL,
'type' VARCHAR(255) DEFAULT NULL,
@@ -0,0 +1,14 @@
+a_joke:
+ id: 1
+ name: Knock knock
+
+another_joke:
+ id: 2
+ name: The Aristocrats
+a_joke:
+ id: 1
+ name: Knock knock
+
+another_joke:
+ id: 2
+ name: The Aristocrats
@@ -0,0 +1,6 @@
+class Joke < ActiveRecord::Base
+ set_table_name 'funny_jokes'
+end
+class Joke < ActiveRecord::Base
+ set_table_name 'funny_jokes'
+end
@@ -4,6 +4,7 @@
require 'fixtures/company'
require 'fixtures/task'
require 'fixtures/reply'
+require 'fixtures/joke'
class FixturesTest < Test::Unit::TestCase
self.use_instantiated_fixtures = true
@@ -110,7 +111,7 @@ def test_bad_format
def test_deprecated_yaml_extension
assert_raise(Fixture::FormatError) {
- Fixtures.new(nil, 'bad_extension', File.join(File.dirname(__FILE__), 'fixtures'))
+ Fixtures.new(nil, 'bad_extension', 'BadExtension', File.join(File.dirname(__FILE__), 'fixtures'))
}
end
@@ -141,26 +142,26 @@ def test_erb_in_fixtures
end
def test_empty_yaml_fixture
- assert_not_nil Fixtures.new( Account.connection, "accounts", File.dirname(__FILE__) + "/fixtures/naked/yml/accounts")
+ assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/yml/accounts")
end
def test_empty_yaml_fixture_with_a_comment_in_it
- assert_not_nil Fixtures.new( Account.connection, "companies", File.dirname(__FILE__) + "/fixtures/naked/yml/companies")
+ assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', File.dirname(__FILE__) + "/fixtures/naked/yml/companies")
end
def test_dirty_dirty_yaml_file
assert_raises(Fixture::FormatError) do
- Fixtures.new( Account.connection, "courses", File.dirname(__FILE__) + "/fixtures/naked/yml/courses")
+ Fixtures.new( Account.connection, "courses", 'Course', File.dirname(__FILE__) + "/fixtures/naked/yml/courses")
end
end
def test_empty_csv_fixtures
- assert_not_nil Fixtures.new( Account.connection, "accounts", File.dirname(__FILE__) + "/fixtures/naked/csv/accounts")
+ assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/csv/accounts")
end
def test_omap_fixtures
assert_nothing_raised do
- fixtures = Fixtures.new(Account.connection, 'categories', File.dirname(__FILE__) + '/fixtures/categories_ordered')
+ fixtures = Fixtures.new(Account.connection, 'categories', 'Category', File.dirname(__FILE__) + '/fixtures/categories_ordered')
i = 0
fixtures.each do |name, fixture|
@@ -326,3 +327,18 @@ def test_number2
assert true
end
end
+
+class SetTableNameFixturesTest < Test::Unit::TestCase
+ set_fixture_class :funny_jokes => 'Joke'
+ fixtures :funny_jokes
+
+ def test_table_method
+ assert_kind_of Joke, funny_jokes(:a_joke)
+ end
+end
+
+
+
+
+
+

0 comments on commit ada11d6

Please sign in to comment.