提交 820b6f3d 编写于 作者: A Aaron Patterson

Merge pull request #442 from jasonnoble/namespace_fixtures

Namespace Fixtures in ActiveRecord
...@@ -305,7 +305,7 @@ db_namespace = namespace :db do ...@@ -305,7 +305,7 @@ db_namespace = namespace :db do
fixtures_dir = File.join [base_dir, ENV['FIXTURES_DIR']].compact fixtures_dir = File.join [base_dir, ENV['FIXTURES_DIR']].compact
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir["#{fixtures_dir}/**/*.{yml,csv}"].map {|f| f[(fixtures_dir.size + 1)..-5] }).each do |fixture_file| (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir["#{fixtures_dir}/**/*.{yml,csv}"].map {|f| f[(fixtures_dir.size + 1)..-5] }).each do |fixture_file|
Fixtures.create_fixtures(fixtures_dir, fixture_file) ActiveRecord::Fixtures.create_fixtures(fixtures_dir, fixture_file)
end end
end end
...@@ -316,13 +316,13 @@ db_namespace = namespace :db do ...@@ -316,13 +316,13 @@ db_namespace = namespace :db do
label, id = ENV['LABEL'], ENV['ID'] label, id = ENV['LABEL'], ENV['ID']
raise 'LABEL or ID required' if label.blank? && id.blank? raise 'LABEL or ID required' if label.blank? && id.blank?
puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::Fixtures.identify(label)}.) if label
base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures') base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
Dir["#{base_dir}/**/*.yml"].each do |file| Dir["#{base_dir}/**/*.yml"].each do |file|
if data = YAML::load(ERB.new(IO.read(file)).result) if data = YAML::load(ERB.new(IO.read(file)).result)
data.keys.each do |key| data.keys.each do |key|
key_id = Fixtures.identify(key) key_id = ActiveRecord::Fixtures.identify(key)
if key == label || key_id == id.to_i if key == label || key_id == id.to_i
puts "#{file}: #{key} (#{key_id})" puts "#{file}: #{key} (#{key_id})"
......
...@@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words ...@@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words
private private
# custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path # custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
def create_test_fixtures(*fixture_names) def create_test_fixtures(*fixture_names)
Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names) ActiveRecord::Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names)
end end
# custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name # custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
......
...@@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words ...@@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words
private private
# custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path # custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
def create_test_fixtures(*fixture_names) def create_test_fixtures(*fixture_names)
Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names) ActiveRecord::Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names)
end end
# custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name # custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
......
...@@ -36,7 +36,7 @@ def test_clean_fixtures ...@@ -36,7 +36,7 @@ def test_clean_fixtures
FIXTURES.each do |name| FIXTURES.each do |name|
fixtures = nil fixtures = nil
assert_nothing_raised { fixtures = create_fixtures(name).first } assert_nothing_raised { fixtures = create_fixtures(name).first }
assert_kind_of(Fixtures, fixtures) assert_kind_of(ActiveRecord::Fixtures, fixtures)
fixtures.each { |_name, fixture| fixtures.each { |_name, fixture|
fixture.each { |key, value| fixture.each { |key, value|
assert_match(MATCH_ATTRIBUTE_NAME, key) assert_match(MATCH_ATTRIBUTE_NAME, key)
...@@ -46,7 +46,7 @@ def test_clean_fixtures ...@@ -46,7 +46,7 @@ def test_clean_fixtures
end end
def test_create_fixtures def test_create_fixtures
Fixtures.create_fixtures(FIXTURES_ROOT, "parrots") ActiveRecord::Fixtures.create_fixtures(FIXTURES_ROOT, "parrots")
assert Parrot.find_by_name('Curious George'), 'George is in the database' assert Parrot.find_by_name('Curious George'), 'George is in the database'
end end
...@@ -54,7 +54,7 @@ def test_multiple_clean_fixtures ...@@ -54,7 +54,7 @@ def test_multiple_clean_fixtures
fixtures_array = nil fixtures_array = nil
assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) } assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) }
assert_kind_of(Array, fixtures_array) assert_kind_of(Array, fixtures_array)
fixtures_array.each { |fixtures| assert_kind_of(Fixtures, fixtures) } fixtures_array.each { |fixtures| assert_kind_of(ActiveRecord::Fixtures, fixtures) }
end end
def test_attributes def test_attributes
...@@ -75,7 +75,7 @@ def test_inserts ...@@ -75,7 +75,7 @@ def test_inserts
if ActiveRecord::Base.connection.supports_migrations? if ActiveRecord::Base.connection.supports_migrations?
def test_inserts_with_pre_and_suffix def test_inserts_with_pre_and_suffix
# Reset cache to make finds on the new table work # Reset cache to make finds on the new table work
Fixtures.reset_cache ActiveRecord::Fixtures.reset_cache
ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t| ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t|
t.column :title, :string t.column :title, :string
...@@ -150,11 +150,11 @@ def test_erb_in_fixtures ...@@ -150,11 +150,11 @@ def test_erb_in_fixtures
end end
def test_empty_yaml_fixture def test_empty_yaml_fixture
assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/yml/accounts") assert_not_nil ActiveRecord::Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/yml/accounts")
end end
def test_empty_yaml_fixture_with_a_comment_in_it def test_empty_yaml_fixture_with_a_comment_in_it
assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', FIXTURES_ROOT + "/naked/yml/companies") assert_not_nil ActiveRecord::Fixtures.new( Account.connection, "companies", 'Company', FIXTURES_ROOT + "/naked/yml/companies")
end end
def test_nonexistent_fixture_file def test_nonexistent_fixture_file
...@@ -164,23 +164,23 @@ def test_nonexistent_fixture_file ...@@ -164,23 +164,23 @@ def test_nonexistent_fixture_file
assert Dir[nonexistent_fixture_path+"*"].empty? assert Dir[nonexistent_fixture_path+"*"].empty?
assert_raise(FixturesFileNotFound) do assert_raise(FixturesFileNotFound) do
Fixtures.new( Account.connection, "companies", 'Company', nonexistent_fixture_path) ActiveRecord::Fixtures.new( Account.connection, "companies", 'Company', nonexistent_fixture_path)
end end
end end
def test_dirty_dirty_yaml_file def test_dirty_dirty_yaml_file
assert_raise(Fixture::FormatError) do assert_raise(ActiveRecord::Fixture::FormatError) do
Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses") ActiveRecord::Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
end end
end end
def test_empty_csv_fixtures def test_empty_csv_fixtures
assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/csv/accounts") assert_not_nil ActiveRecord::Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/csv/accounts")
end end
def test_omap_fixtures def test_omap_fixtures
assert_nothing_raised do assert_nothing_raised do
fixtures = Fixtures.new(Account.connection, 'categories', 'Category', FIXTURES_ROOT + "/categories_ordered") fixtures = ActiveRecord::Fixtures.new(Account.connection, 'categories', 'Category', FIXTURES_ROOT + "/categories_ordered")
i = 0 i = 0
fixtures.each do |name, fixture| fixtures.each do |name, fixture|
...@@ -220,7 +220,7 @@ class FixturesResetPkSequenceTest < ActiveRecord::TestCase ...@@ -220,7 +220,7 @@ class FixturesResetPkSequenceTest < ActiveRecord::TestCase
def setup def setup
@instances = [Account.new(:credit_limit => 50), Company.new(:name => 'RoR Consulting')] @instances = [Account.new(:credit_limit => 50), Company.new(:name => 'RoR Consulting')]
Fixtures.reset_cache # make sure tables get reinitialized ActiveRecord::Fixtures.reset_cache # make sure tables get reinitialized
end end
def test_resets_to_min_pk_with_specified_pk_and_sequence def test_resets_to_min_pk_with_specified_pk_and_sequence
...@@ -524,13 +524,13 @@ class FasterFixturesTest < ActiveRecord::TestCase ...@@ -524,13 +524,13 @@ class FasterFixturesTest < ActiveRecord::TestCase
def load_extra_fixture(name) def load_extra_fixture(name)
fixture = create_fixtures(name).first fixture = create_fixtures(name).first
assert fixture.is_a?(Fixtures) assert fixture.is_a?(ActiveRecord::Fixtures)
@loaded_fixtures[fixture.table_name] = fixture @loaded_fixtures[fixture.table_name] = fixture
end end
def test_cache def test_cache
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'categories') assert ActiveRecord::Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'categories')
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'authors') assert ActiveRecord::Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'authors')
assert_no_queries do assert_no_queries do
create_fixtures('categories') create_fixtures('categories')
...@@ -538,7 +538,7 @@ def test_cache ...@@ -538,7 +538,7 @@ def test_cache
end end
load_extra_fixture('posts') load_extra_fixture('posts')
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'posts') assert ActiveRecord::Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'posts')
self.class.setup_fixture_accessors('posts') self.class.setup_fixture_accessors('posts')
assert_equal 'Welcome to the weblog', posts(:welcome).title assert_equal 'Welcome to the weblog', posts(:welcome).title
end end
...@@ -548,17 +548,17 @@ class FoxyFixturesTest < ActiveRecord::TestCase ...@@ -548,17 +548,17 @@ class FoxyFixturesTest < ActiveRecord::TestCase
fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers, :"admin/accounts", :"admin/users" fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers, :"admin/accounts", :"admin/users"
def test_identifies_strings def test_identifies_strings
assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo")) assert_equal(ActiveRecord::Fixtures.identify("foo"), ActiveRecord::Fixtures.identify("foo"))
assert_not_equal(Fixtures.identify("foo"), Fixtures.identify("FOO")) assert_not_equal(ActiveRecord::Fixtures.identify("foo"), ActiveRecord::Fixtures.identify("FOO"))
end end
def test_identifies_symbols def test_identifies_symbols
assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo)) assert_equal(ActiveRecord::Fixtures.identify(:foo), ActiveRecord::Fixtures.identify(:foo))
end end
def test_identifies_consistently def test_identifies_consistently
assert_equal 207281424, Fixtures.identify(:ruby) assert_equal 207281424, ActiveRecord::Fixtures.identify(:ruby)
assert_equal 1066363776, Fixtures.identify(:sapphire_2) assert_equal 1066363776, ActiveRecord::Fixtures.identify(:sapphire_2)
end end
TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on) TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
......
...@@ -104,7 +104,7 @@ class ActiveSupport::TestCase ...@@ -104,7 +104,7 @@ class ActiveSupport::TestCase
self.use_transactional_fixtures = true self.use_transactional_fixtures = true
def create_fixtures(*table_names, &block) def create_fixtures(*table_names, &block)
Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, fixture_class_names, &block) ActiveRecord::Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, fixture_class_names, &block)
end end
end end
......
blackbeard_to_redbeard: blackbeard_to_redbeard:
pirate_id: <%= Fixtures.identify(:blackbeard) %> pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>
target_id: <%= Fixtures.identify(:redbeard) %> target_id: <%= ActiveRecord::Fixtures.identify(:redbeard) %>
weight: 10 weight: 10
george_blackbeard: george_blackbeard:
parrot_id: <%= Fixtures.identify(:george) %> parrot_id: <%= ActiveRecord::Fixtures.identify(:george) %>
pirate_id: <%= Fixtures.identify(:blackbeard) %> pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>
louis_blackbeard: louis_blackbeard:
parrot_id: <%= Fixtures.identify(:louis) %> parrot_id: <%= ActiveRecord::Fixtures.identify(:louis) %>
pirate_id: <%= Fixtures.identify(:blackbeard) %> pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册