Permalink
Browse files

support and merge legacy fixture sets

  • Loading branch information...
1 parent 42a7ec5 commit 36444445c059a1eab2885a0db3aa551e7563b031 Dennis Collinson & Thomas Bukowski committed Jul 7, 2011
View
@@ -1,3 +1,6 @@
pkg/
.pairs
.idea/*
+*.db
+tmp/fixture_builder.yml
+test/fixtures/magical_creatures.yml
View
@@ -3,20 +3,37 @@ PATH
specs:
fixture_builder (0.2.2)
active_support
+ activerecord
GEM
remote: http://rubygems.org/
specs:
active_support (3.0.0)
activesupport (= 3.0.0)
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
activesupport (3.0.0)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ builder (2.1.2)
+ i18n (0.4.2)
rake (0.8.7)
+ sqlite3 (1.3.3)
test-unit (2.3.0)
+ tzinfo (0.3.29)
PLATFORMS
ruby
DEPENDENCIES
fixture_builder!
rake (= 0.8.7)
+ sqlite3
test-unit
@@ -18,12 +18,14 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ['lib']
-
+
s.homepage = %q{http://github.com/rdy/fixture_builder}
s.rubyforge_project = %q{fixture_builder}
s.summary = %q{Build YAML fixtures using object factories}
s.add_dependency(%q{active_support})
+ s.add_dependency(%q{activerecord})
s.add_development_dependency(%q{rake}, '0.8.7')
s.add_development_dependency(%q{test-unit})
+ s.add_development_dependency(%q{sqlite3})
end
@@ -2,12 +2,12 @@
module FixtureBuilder
class << self
- def configuration
- @configuration ||= FixtureBuilder::Configuration.new
+ def configuration(opts = {})
+ @configuration ||= FixtureBuilder::Configuration.new(opts)
end
- def configure
- yield configuration
+ def configure(opts = {})
+ yield configuration(opts)
end
end
@@ -8,7 +8,10 @@ class Configuration
SCHEMA_FILES = ['db/schema.rb', 'db/development_structure.sql', 'db/test_structure.sql', 'db/production_structure.sql']
- def initialize
+ def initialize(opts={})
+ @legacy_fixtures = Dir.glob(opts[:legacy_fixtures].to_a)
+ self.files_to_check += @legacy_fixtures
+
@custom_names = {}
@model_name_procs = {}
@file_hashes = file_hashes
@@ -64,6 +67,7 @@ def factory(&block)
say "Building fixtures"
delete_tables
delete_yml_files
+ load_legacy_fixtures if @legacy_fixtures.any?
surface_errors { instance_eval(&block) }
FileUtils.rm_rf(::Rails.root.join(spec_or_test_dir, 'fixtures', '*.yml'))
dump_empty_fixtures_for_all_tables
@@ -89,6 +93,23 @@ def name(custom_name, *model_objects)
private
+ def load_legacy_fixtures
+ @legacy_fixtures.each do |fixture_file|
+ fixtures = ::Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
+ populate_custom_names(fixtures)
+ end
+ end
+
+ def populate_custom_names(fixtures)
+ fixtures.each do |fixture|
+ name = fixture[0]
+ id = fixture[1]['id'].to_i
+ table_name = fixture[1].model_class.table_name
+ key = [table_name, id]
+ @custom_names[key] = name
+ end
+ end
+
def say(*messages)
puts messages.map { |message| "=> #{message}" }
end
@@ -158,7 +179,7 @@ def dump_tables
end
next files if rows.empty?
- @row_index = '000'
+ @row_index = '000'
@record_names = []
fixture_data = rows.inject({}) do |hash, record|
hash.merge(record_name(record, table_name) => record)
@@ -1,11 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
-class Rails
- def self.root
- File.join(File.dirname(__FILE__), '..')
- end
-end
-
class Model
def self.table_name
'models'
No changes.
@@ -0,0 +1,39 @@
+require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
+
+create_and_blow_away_old_db
+force_fixture_generation
+
+FixtureBuilder.configure :legacy_fixtures => [test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] do |fbuilder|
+ fbuilder.factory do
+ MagicalCreature.create(:name => "frank", :species => "unicorn")
+ end
+end
+
+class LegacyFixtureModeFixtureGenerationTest < Test::Unit::TestCase
+ @@magical_creatures = YAML.load(File.open(test_path("fixtures/magical_creatures.yml")))
+
+ def test_legacy_fixtures_created
+ alice = MagicalCreature.find_by_name("alice")
+ assert_equal "alice", alice.name
+ assert_equal "mermaid", alice.species
+ end
+
+ def test_invalid_legacy_fixtures_created
+ bigfoot = MagicalCreature.find(5)
+ assert_equal "bigfoot", bigfoot.name
+
+ assert_equal 5, @@magical_creatures['bigfoot']['id']
+ assert_equal "bigfoot", @@magical_creatures['bigfoot']['name']
+ assert_nil @@magical_creatures['bigfoot']['species']
+ end
+
+ def test_new_fixtures_are_created
+ assert_equal "frank", @@magical_creatures['frank']['name']
+ assert_equal "unicorn", @@magical_creatures['frank']['species']
+ end
+
+ def test_legacy_fixtures_retain_fixture_name
+ assert_equal "alice", @@magical_creatures['alice_the_mermaid']['name']
+ assert_equal "mermaid", @@magical_creatures['alice_the_mermaid']['species']
+ end
+end
@@ -0,0 +1,39 @@
+require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
+
+class LegacyFixtureModeTest < Test::Unit::TestCase
+ def setup
+ create_and_blow_away_old_db
+ force_fixture_generation
+ end
+
+ def teardown
+ FixtureBuilder.send(:remove_instance_variable, :@configuration)
+ end
+
+ def test_load_legacy_fixtures
+ FixtureBuilder.configure :legacy_fixtures => [test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] do |fbuilder|
+ fbuilder.factory do
+ end
+ end
+ assert_equal 3, MagicalCreature.all.size
+ end
+
+ def test_generate_new_fixtures_without_legacy
+ FixtureBuilder.configure do |fbuilder|
+ fbuilder.files_to_check += Dir[test_path("*.rb")]
+ fbuilder.factory do
+ MagicalCreature.create :name => "Melinda", :species => "Philanthropist"
+ end
+ end
+ assert_equal 1, MagicalCreature.all.size
+ end
+
+ def test_new_and_old_fixtures
+ FixtureBuilder.configure :legacy_fixtures => [test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] do |fbuilder|
+ fbuilder.factory do
+ MagicalCreature.create :name => "Barry", :species => "Party Guy"
+ end
+ end
+ assert_equal 4, MagicalCreature.all.size
+ end
+end
@@ -0,0 +1,12 @@
+bigfoot:
+ name: bigfoot
+ id: 5
+
+alice_the_mermaid:
+ name: alice
+ species: mermaid
+ id: 300
+
+xou_the_dragon:
+ name: xou
+ species: dragon
View
@@ -1,4 +1,43 @@
require 'rubygems'
require 'bundler/setup'
require 'test/unit'
-require 'fixture_builder'
+require 'fixture_builder'
+
+class Rails
+ def self.root
+ Pathname.new(File.join(File.dirname(__FILE__), '..'))
+ end
+end
+
+def test_path(glob)
+ File.join(Rails.root, 'test', glob)
+end
+
+require "active_support/concern"
+require "active_record"
+require "sqlite3"
+require "active_record/fixtures"
+
+class MagicalCreature < ActiveRecord::Base
+ validates_presence_of :name, :species
+end
+
+def create_and_blow_away_old_db
+ ActiveRecord::Base.configurations['test'] = {
+ 'adapter' => 'sqlite3',
+ 'database' => 'test.db'
+ }
+ ActiveRecord::Base.establish_connection(:test)
+
+ ActiveRecord::Base.connection.create_table(:magical_creatures, :force => true) do |t|
+ t.column :name, :string
+ t.column :species, :string
+ end
+end
+
+def force_fixture_generation
+ begin
+ FileUtils.rm(File.expand_path("../../tmp/fixture_builder.yml", __FILE__))
+ rescue
+ end
+end
View
No changes.

0 comments on commit 3644444

Please sign in to comment.