Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial release

  • Loading branch information...
commit bf57289999680396ab81a0a92e43153b257eba48 0 parents
Nando Vieira authored
4 .gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+pkg
+tmp
+*.log
1  .rspec
@@ -0,0 +1 @@
+--color --format documentation
2  Gemfile
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
89 Gemfile.lock
@@ -0,0 +1,89 @@
+PATH
+ remote: .
+ specs:
+ factory_girl-preload (0.1.0)
+ factory_girl (~> 1.3.3)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionpack (3.0.6)
+ activemodel (= 3.0.6)
+ activesupport (= 3.0.6)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.6)
+ activesupport (= 3.0.6)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.6)
+ activemodel (= 3.0.6)
+ activesupport (= 3.0.6)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activesupport (3.0.6)
+ archive-tar-minitar (0.5.2)
+ arel (2.0.9)
+ builder (2.1.2)
+ columnize (0.3.2)
+ diff-lcs (1.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ factory_girl (1.3.3)
+ i18n (0.5.0)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ mysql2 (0.2.7)
+ rack (1.2.2)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ railties (3.0.6)
+ actionpack (= 3.0.6)
+ activesupport (= 3.0.6)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+ rspec-rails (2.5.0)
+ actionpack (~> 3.0)
+ activesupport (~> 3.0)
+ railties (~> 3.0)
+ rspec (~> 2.5.0)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ thor (0.14.6)
+ tzinfo (0.3.26)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ actionpack (~> 3.0.6)
+ activerecord (~> 3.0.6)
+ factory_girl-preload!
+ mysql2 (~> 0.2.7)
+ rspec-rails (~> 2.5.0)
+ ruby-debug19
119 README.rdoc
@@ -0,0 +1,119 @@
+= factory_girl-preload
+
+We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).
+
+Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of specs.
+
+So here enters Factory Girl Preload (FGP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).
+
+== Installation
+
+ gem install factory_girl-preload
+
+== Usage
+
+I'm focusing Rails 3 + RSpec 2 stack, so I can't really guarantee that it will work on other setups. Here's how you get started:
+
+Add both FG and FGP to your Gemfile:
+
+ source :rubygems
+ gem "rails", "3.0.6"
+ gem "mysql2", "~> 0.2.7"
+
+ group :test, :development do
+ gem "rspec-rails", "~> 2.5.0"
+ gem "factory_girl", "~> 1.3.3"
+ gem "factory_girl-preload", "~> 0.1.0"
+ end
+
+On <tt>spec/spec_helper.rb</tt> file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:
+
+ ENV["RAILS_ENV"] ||= "test"
+ require File.expand_path("../../config/environment", __FILE__)
+ require "rspec/rails"
+
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+ RSpec.configure do |config|
+ config.use_transactional_fixtures = true
+ config.mock_with :rspec
+ end
+
+Create your fixtures on <tt>spec/support/factories.rb</tt>. You may have something like this:
+
+ Factory.sequence :email do |n|
+ "john#{n}@doe.com"
+ end
+
+ Factory.sequence :username do |n|
+ "john#{n}"
+ end
+
+ Factory.sequence :public_key do |n|
+ File.read(Rails.root.join("spec/fixtures/hash.rsa")) + n.to_s
+ end
+
+ Factory.define :user do |f|
+ f.name "John Doe"
+ f.email { Factory.next(:email) }
+ f.username { Factory.next(:username) }
+ f.password "test"
+ f.password_confirmation "test"
+ end
+
+ Factory.define :projects do |f|
+ f.name "My Project"
+ f.association :user
+ end
+
+Finally, you can create your preloadable factories. Put those on <tt>spec/support/preloadable_factories.rb</tt>:
+
+ Factory.preload do
+ factory(:john) { Factory(:user) }
+ factory(:myapp) { Factory(:project, :user => users(:john)) }
+ end
+
+Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.
+
+ require "spec_helper"
+
+ describe User do
+ let(:user) { users(:john) }
+
+ it "returns john's record" do
+ users(:john).should be_an(User)
+ end
+
+ it "returns myapp's record" do
+ projects(:myapp).user.should == users(:john)
+ end
+ end
+
+Easy and, probably, faster!
+
+== Maintainer
+
+* Nando Vieira (http://nandovieira.com.br)
+
+== License
+
+(The MIT License)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 Rakefile
@@ -0,0 +1,5 @@
+require "bundler"
+Bundler::GemHelper.install_tasks
+
+require "rspec/core/rake_task"
+RSpec::Core::RakeTask.new
26 factory_girl-preload.gemspec
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "factory_girl-preload"
+
+Gem::Specification.new do |s|
+ s.name = "factory_girl-preload"
+ s.version = Factory::Preload::Version::STRING
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Nando Vieira"]
+ s.email = ["fnando.vieira@gmail.com"]
+ s.homepage = "http://rubygems.org/gems/factory_girl-preload"
+ s.summary = "Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!"
+ s.description = s.summary
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "factory_girl", "~> 1.3.3"
+ s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
+ s.add_development_dependency "activerecord", "~> 3.0.6"
+ s.add_development_dependency "actionpack", "~> 3.0.6"
+ s.add_development_dependency "rspec-rails", "~> 2.5.0"
+ s.add_development_dependency "mysql2", "~> 0.2.7"
+end
1  lib/factory_girl-preload.rb
@@ -0,0 +1 @@
+require "factory_girl/preload"
42 lib/factory_girl/preload.rb
@@ -0,0 +1,42 @@
+class Factory
+ module Preload
+ autoload :Helpers, "factory_girl/preload/helpers"
+ autoload :Version, "factory_girl/preload/version"
+
+ require "factory_girl/preload/rspec2" if defined?(RSpec)
+
+ class << self
+ attr_accessor :preloaders
+ attr_accessor :factories
+ end
+
+ self.preloaders = []
+ self.factories = {}
+
+ def self.run
+ helper = Object.new.extend(Helpers)
+
+ ActiveRecord::Base.connection.transaction :requires_new => true do
+ preloaders.each do |block|
+ helper.instance_eval(&block)
+ end
+ end
+ end
+
+ def self.clean(*names)
+ query = ActiveRecord::Base.connection.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" ? "DELETE FROM `%s`" : "TRUNCATE TABLE `%s`"
+ names = ActiveRecord::Base.descendants.collect(&:table_name).uniq if names.empty?
+ names.each {|table| ActiveRecord::Base.connection.execute(query % table)}
+ end
+
+ def self.reload_factories
+ factories.each do |class_name, group|
+ group.values.each {|factory| factory.reload}
+ end
+ end
+ end
+
+ def self.preload(&block)
+ Preload.preloaders << block
+ end
+end
46 lib/factory_girl/preload/helpers.rb
@@ -0,0 +1,46 @@
+class Factory
+ module Preload
+ module Helpers
+ def self.extended(base)
+ included(base)
+ end
+
+ def self.included(base)
+ Dir[Rails.application.root.join("app/models/**/*.rb")].each do |file|
+ require_dependency file
+ end
+
+ ActiveRecord::Base.descendants.each do |model|
+ method_name = model.name.underscore.gsub("/", "_").pluralize
+
+ class_eval <<-RUBY, __FILE__, __LINE__
+ def #{method_name}(name)
+ factory(name, #{model})
+ end
+ RUBY
+ end
+ end
+
+ def factory(name, model = nil, &block)
+ if block_given?
+ factory_set(name, &block)
+ else
+ factory_get(name, model)
+ end
+ end
+
+ private
+ def factory_get(name, model)
+ factory = Factory::Preload.factories[model.name][name] rescue nil
+ raise "Couldn't find #{name.inspect} factory for #{model.name.inspect} model" unless factory
+ factory
+ end
+
+ def factory_set(name, &block)
+ record = block.call
+ Factory::Preload.factories[record.class.name] ||= {}
+ Factory::Preload.factories[record.class.name][name.to_sym] = record
+ end
+ end
+ end
+end
11 lib/factory_girl/preload/rspec2.rb
@@ -0,0 +1,11 @@
+RSpec.configure do |config|
+ config.include Factory::Preload::Helpers
+ config.before(:suite) do
+ Factory::Preload.clean
+ Factory::Preload.run
+ end
+
+ config.before(:each) do
+ Factory::Preload.reload_factories
+ end
+end
10 lib/factory_girl/preload/version.rb
@@ -0,0 +1,10 @@
+class Factory
+ module Preload
+ module Version
+ MAJOR = 0
+ MINOR = 1
+ PATCH = 0
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
+ end
+ end
+end
58 spec/factory_girl/preload_spec.rb
@@ -0,0 +1,58 @@
+require "spec_helper"
+
+describe Factory::Preload do
+ include Factory::Preload::Helpers
+
+ it "queues preloader block" do
+ block = proc {}
+ Factory.preload(&block)
+ Factory::Preload.preloaders.should include(block)
+ end
+
+ it "injects model methods" do
+ expect { users(:john) }.to_not raise_error
+ end
+
+ it "returns :john factory for User model" do
+ users(:john).should be_an(User)
+ end
+
+ it "returns :ruby factory for Skill model" do
+ skills(:ruby).should be_a(Skill)
+ end
+
+ it "reuses existing factories" do
+ skills(:ruby).user.should == users(:john)
+ end
+
+ it "raises error for missing factories" do
+ expect { users(:mary) }.to raise_error(%[Couldn't find :mary factory for "User" model])
+ end
+
+ it "removes records" do
+ User.count.should == 1
+ Factory::Preload.clean
+ User.count.should == 0
+ end
+
+ context "reloadable factories" do
+ before :all do
+ Factory::Preload.clean
+ Factory::Preload.run
+ end
+
+ before :each do
+ Factory::Preload.reload_factories
+ end
+
+ it "updates invitation count" do
+ users(:john).increment(:invitations)
+ users(:john).save
+ users(:john).invitations.should == 1
+ end
+
+ it "reloads factory" do
+ users(:john).invitations.should == 0
+ end
+ end
+end
34 spec/spec_helper.rb
@@ -0,0 +1,34 @@
+ENV["RAILS_ENV"] = "test"
+require "rails"
+
+ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../Gemfile"
+require "bundler"
+Bundler.setup
+require "rails/all"
+Bundler.require(:default)
+
+require "rspec/rails"
+require "factory_girl"
+require File.dirname(__FILE__) + "/support/factories"
+
+module RSpec
+ class Application < ::Rails::Application
+ config.root = File.dirname(__FILE__) + "/support/app"
+ config.active_support.deprecation = :log
+ # config.active_record.logger = Logger.new(STDOUT)
+ end
+end
+
+RSpec::Application.initialize!
+load File.dirname(__FILE__) + "/support/app/db/schema.rb"
+
+RSpec.configure do |config|
+ config.use_transactional_fixtures = true
+end
+
+Factory.preload do
+ factory(:john) { Factory(:user) }
+ factory(:ruby) { Factory(:skill, :user => users(:john)) }
+end
+
+Factory::Preload.run
3  spec/support/app/app/models/skill.rb
@@ -0,0 +1,3 @@
+class Skill < ActiveRecord::Base
+ belongs_to :user
+end
3  spec/support/app/app/models/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+ has_many :skills
+end
5 spec/support/app/config/database.yml
@@ -0,0 +1,5 @@
+test:
+ adapter: mysql2
+ database: test
+ username: root
+ host: localhost
19 spec/support/app/db/schema.rb
@@ -0,0 +1,19 @@
+ActiveRecord::Schema.define(:version => 0) do
+ begin
+ drop_table :users
+ drop_table :skills
+ rescue Exception => e
+ end
+
+ create_table :users do |t|
+ t.string :name, :null => false
+ t.string :email, :null => false
+ t.integer :invitations, :null => false, :default => 0
+ end
+
+ add_index :users, :email, :unique => true
+
+ create_table :skills do |t|
+ t.references :user
+ end
+end
12 spec/support/factories.rb
@@ -0,0 +1,12 @@
+Factory.define :user do |f|
+ f.name "John Doe"
+ f.email { Factory.next(:email) }
+end
+
+Factory.sequence :email do |i|
+ "john#{i}@doe.com"
+end
+
+Factory.define :skill do |f|
+ f.association :user
+end
Please sign in to comment.
Something went wrong with that request. Please try again.