Skip to content

Commit

Permalink
Merge pull request rails#442 from jasonnoble/namespace_fixtures
Browse files Browse the repository at this point in the history
Namespace Fixtures in ActiveRecord
  • Loading branch information
tenderlove committed May 7, 2011
2 parents 0ef978d + e5c18ff commit 820b6f3
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 318 deletions.
575 changes: 290 additions & 285 deletions activerecord/lib/active_record/fixtures.rb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ db_namespace = namespace :db do
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|
Fixtures.create_fixtures(fixtures_dir, fixture_file)
ActiveRecord::Fixtures.create_fixtures(fixtures_dir, fixture_file)
end
end

Expand All @@ -316,13 +316,13 @@ db_namespace = namespace :db do
label, id = ENV['LABEL'], ENV['ID']
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')
Dir["#{base_dir}/**/*.yml"].each do |file|
if data = YAML::load(ERB.new(IO.read(file)).result)
data.keys.each do |key|
key_id = Fixtures.identify(key)
key_id = ActiveRecord::Fixtures.identify(key)

if key == label || key_id == id.to_i
puts "#{file}: #{key} (#{key_id})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words
private
# custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
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

# custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_associations_work_with_reserved_words
private
# custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
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

# custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
Expand Down
42 changes: 21 additions & 21 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_clean_fixtures
FIXTURES.each do |name|
fixtures = nil
assert_nothing_raised { fixtures = create_fixtures(name).first }
assert_kind_of(Fixtures, fixtures)
assert_kind_of(ActiveRecord::Fixtures, fixtures)
fixtures.each { |_name, fixture|
fixture.each { |key, value|
assert_match(MATCH_ATTRIBUTE_NAME, key)
Expand All @@ -46,15 +46,15 @@ def test_clean_fixtures
end

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'
end

def test_multiple_clean_fixtures
fixtures_array = nil
assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) }
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

def test_attributes
Expand All @@ -75,7 +75,7 @@ def test_inserts
if ActiveRecord::Base.connection.supports_migrations?
def test_inserts_with_pre_and_suffix
# 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|
t.column :title, :string
Expand Down Expand Up @@ -150,11 +150,11 @@ def test_erb_in_fixtures
end

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

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

def test_nonexistent_fixture_file
Expand All @@ -164,23 +164,23 @@ def test_nonexistent_fixture_file
assert Dir[nonexistent_fixture_path+"*"].empty?

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

def test_dirty_dirty_yaml_file
assert_raise(Fixture::FormatError) do
Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
assert_raise(ActiveRecord::Fixture::FormatError) do
ActiveRecord::Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
end
end

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

def test_omap_fixtures
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
fixtures.each do |name, fixture|
Expand Down Expand Up @@ -220,7 +220,7 @@ class FixturesResetPkSequenceTest < ActiveRecord::TestCase

def setup
@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

def test_resets_to_min_pk_with_specified_pk_and_sequence
Expand Down Expand Up @@ -524,21 +524,21 @@ class FasterFixturesTest < ActiveRecord::TestCase

def load_extra_fixture(name)
fixture = create_fixtures(name).first
assert fixture.is_a?(Fixtures)
assert fixture.is_a?(ActiveRecord::Fixtures)
@loaded_fixtures[fixture.table_name] = fixture
end

def test_cache
assert 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, 'categories')
assert ActiveRecord::Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'authors')

assert_no_queries do
create_fixtures('categories')
create_fixtures('authors')
end

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')
assert_equal 'Welcome to the weblog', posts(:welcome).title
end
Expand All @@ -548,17 +548,17 @@ class FoxyFixturesTest < ActiveRecord::TestCase
fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers, :"admin/accounts", :"admin/users"

def test_identifies_strings
assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo"))
assert_not_equal(Fixtures.identify("foo"), Fixtures.identify("FOO"))
assert_equal(ActiveRecord::Fixtures.identify("foo"), ActiveRecord::Fixtures.identify("foo"))
assert_not_equal(ActiveRecord::Fixtures.identify("foo"), ActiveRecord::Fixtures.identify("FOO"))
end

def test_identifies_symbols
assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo))
assert_equal(ActiveRecord::Fixtures.identify(:foo), ActiveRecord::Fixtures.identify(:foo))
end

def test_identifies_consistently
assert_equal 207281424, Fixtures.identify(:ruby)
assert_equal 1066363776, Fixtures.identify(:sapphire_2)
assert_equal 207281424, ActiveRecord::Fixtures.identify(:ruby)
assert_equal 1066363776, ActiveRecord::Fixtures.identify(:sapphire_2)
end

TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ActiveSupport::TestCase
self.use_transactional_fixtures = true

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

Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/fixtures/mateys.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
blackbeard_to_redbeard:
pirate_id: <%= Fixtures.identify(:blackbeard) %>
target_id: <%= Fixtures.identify(:redbeard) %>
pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>
target_id: <%= ActiveRecord::Fixtures.identify(:redbeard) %>
weight: 10
8 changes: 4 additions & 4 deletions activerecord/test/fixtures/parrots_pirates.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
george_blackbeard:
parrot_id: <%= Fixtures.identify(:george) %>
pirate_id: <%= Fixtures.identify(:blackbeard) %>
parrot_id: <%= ActiveRecord::Fixtures.identify(:george) %>
pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>

louis_blackbeard:
parrot_id: <%= Fixtures.identify(:louis) %>
pirate_id: <%= Fixtures.identify(:blackbeard) %>
parrot_id: <%= ActiveRecord::Fixtures.identify(:louis) %>
pirate_id: <%= ActiveRecord::Fixtures.identify(:blackbeard) %>

0 comments on commit 820b6f3

Please sign in to comment.