factory_girl-preload 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ tmp
4
+ *.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ factory_girl-preload (0.1.0)
5
+ factory_girl (~> 1.3.3)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ abstract (1.0.0)
11
+ actionpack (3.0.6)
12
+ activemodel (= 3.0.6)
13
+ activesupport (= 3.0.6)
14
+ builder (~> 2.1.2)
15
+ erubis (~> 2.6.6)
16
+ i18n (~> 0.5.0)
17
+ rack (~> 1.2.1)
18
+ rack-mount (~> 0.6.14)
19
+ rack-test (~> 0.5.7)
20
+ tzinfo (~> 0.3.23)
21
+ activemodel (3.0.6)
22
+ activesupport (= 3.0.6)
23
+ builder (~> 2.1.2)
24
+ i18n (~> 0.5.0)
25
+ activerecord (3.0.6)
26
+ activemodel (= 3.0.6)
27
+ activesupport (= 3.0.6)
28
+ arel (~> 2.0.2)
29
+ tzinfo (~> 0.3.23)
30
+ activesupport (3.0.6)
31
+ archive-tar-minitar (0.5.2)
32
+ arel (2.0.9)
33
+ builder (2.1.2)
34
+ columnize (0.3.2)
35
+ diff-lcs (1.1.2)
36
+ erubis (2.6.6)
37
+ abstract (>= 1.0.0)
38
+ factory_girl (1.3.3)
39
+ i18n (0.5.0)
40
+ linecache19 (0.5.12)
41
+ ruby_core_source (>= 0.1.4)
42
+ mysql2 (0.2.7)
43
+ rack (1.2.2)
44
+ rack-mount (0.6.14)
45
+ rack (>= 1.0.0)
46
+ rack-test (0.5.7)
47
+ rack (>= 1.0)
48
+ railties (3.0.6)
49
+ actionpack (= 3.0.6)
50
+ activesupport (= 3.0.6)
51
+ rake (>= 0.8.7)
52
+ thor (~> 0.14.4)
53
+ rake (0.8.7)
54
+ rspec (2.5.0)
55
+ rspec-core (~> 2.5.0)
56
+ rspec-expectations (~> 2.5.0)
57
+ rspec-mocks (~> 2.5.0)
58
+ rspec-core (2.5.1)
59
+ rspec-expectations (2.5.0)
60
+ diff-lcs (~> 1.1.2)
61
+ rspec-mocks (2.5.0)
62
+ rspec-rails (2.5.0)
63
+ actionpack (~> 3.0)
64
+ activesupport (~> 3.0)
65
+ railties (~> 3.0)
66
+ rspec (~> 2.5.0)
67
+ ruby-debug-base19 (0.11.25)
68
+ columnize (>= 0.3.1)
69
+ linecache19 (>= 0.5.11)
70
+ ruby_core_source (>= 0.1.4)
71
+ ruby-debug19 (0.11.6)
72
+ columnize (>= 0.3.1)
73
+ linecache19 (>= 0.5.11)
74
+ ruby-debug-base19 (>= 0.11.19)
75
+ ruby_core_source (0.1.5)
76
+ archive-tar-minitar (>= 0.5.2)
77
+ thor (0.14.6)
78
+ tzinfo (0.3.26)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ actionpack (~> 3.0.6)
85
+ activerecord (~> 3.0.6)
86
+ factory_girl-preload!
87
+ mysql2 (~> 0.2.7)
88
+ rspec-rails (~> 2.5.0)
89
+ ruby-debug19
data/README.rdoc ADDED
@@ -0,0 +1,119 @@
1
+ = factory_girl-preload
2
+
3
+ We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).
4
+
5
+ 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.
6
+
7
+ 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).
8
+
9
+ == Installation
10
+
11
+ gem install factory_girl-preload
12
+
13
+ == Usage
14
+
15
+ 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:
16
+
17
+ Add both FG and FGP to your Gemfile:
18
+
19
+ source :rubygems
20
+ gem "rails", "3.0.6"
21
+ gem "mysql2", "~> 0.2.7"
22
+
23
+ group :test, :development do
24
+ gem "rspec-rails", "~> 2.5.0"
25
+ gem "factory_girl", "~> 1.3.3"
26
+ gem "factory_girl-preload", "~> 0.1.0"
27
+ end
28
+
29
+ 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:
30
+
31
+ ENV["RAILS_ENV"] ||= "test"
32
+ require File.expand_path("../../config/environment", __FILE__)
33
+ require "rspec/rails"
34
+
35
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
36
+
37
+ RSpec.configure do |config|
38
+ config.use_transactional_fixtures = true
39
+ config.mock_with :rspec
40
+ end
41
+
42
+ Create your fixtures on <tt>spec/support/factories.rb</tt>. You may have something like this:
43
+
44
+ Factory.sequence :email do |n|
45
+ "john#{n}@doe.com"
46
+ end
47
+
48
+ Factory.sequence :username do |n|
49
+ "john#{n}"
50
+ end
51
+
52
+ Factory.sequence :public_key do |n|
53
+ File.read(Rails.root.join("spec/fixtures/hash.rsa")) + n.to_s
54
+ end
55
+
56
+ Factory.define :user do |f|
57
+ f.name "John Doe"
58
+ f.email { Factory.next(:email) }
59
+ f.username { Factory.next(:username) }
60
+ f.password "test"
61
+ f.password_confirmation "test"
62
+ end
63
+
64
+ Factory.define :projects do |f|
65
+ f.name "My Project"
66
+ f.association :user
67
+ end
68
+
69
+ Finally, you can create your preloadable factories. Put those on <tt>spec/support/preloadable_factories.rb</tt>:
70
+
71
+ Factory.preload do
72
+ factory(:john) { Factory(:user) }
73
+ factory(:myapp) { Factory(:project, :user => users(:john)) }
74
+ end
75
+
76
+ Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.
77
+
78
+ require "spec_helper"
79
+
80
+ describe User do
81
+ let(:user) { users(:john) }
82
+
83
+ it "returns john's record" do
84
+ users(:john).should be_an(User)
85
+ end
86
+
87
+ it "returns myapp's record" do
88
+ projects(:myapp).user.should == users(:john)
89
+ end
90
+ end
91
+
92
+ Easy and, probably, faster!
93
+
94
+ == Maintainer
95
+
96
+ * Nando Vieira (http://nandovieira.com.br)
97
+
98
+ == License
99
+
100
+ (The MIT License)
101
+
102
+ Permission is hereby granted, free of charge, to any person obtaining
103
+ a copy of this software and associated documentation files (the
104
+ 'Software'), to deal in the Software without restriction, including
105
+ without limitation the rights to use, copy, modify, merge, publish,
106
+ distribute, sublicense, and/or sell copies of the Software, and to
107
+ permit persons to whom the Software is furnished to do so, subject to
108
+ the following conditions:
109
+
110
+ The above copyright notice and this permission notice shall be
111
+ included in all copies or substantial portions of the Software.
112
+
113
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
114
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
115
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
116
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
117
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
118
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
119
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "factory_girl-preload"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "factory_girl-preload"
7
+ s.version = Factory::Preload::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/factory_girl-preload"
12
+ s.summary = "Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!"
13
+ s.description = s.summary
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "factory_girl", "~> 1.3.3"
21
+ s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
22
+ s.add_development_dependency "activerecord", "~> 3.0.6"
23
+ s.add_development_dependency "actionpack", "~> 3.0.6"
24
+ s.add_development_dependency "rspec-rails", "~> 2.5.0"
25
+ s.add_development_dependency "mysql2", "~> 0.2.7"
26
+ end
@@ -0,0 +1,46 @@
1
+ class Factory
2
+ module Preload
3
+ module Helpers
4
+ def self.extended(base)
5
+ included(base)
6
+ end
7
+
8
+ def self.included(base)
9
+ Dir[Rails.application.root.join("app/models/**/*.rb")].each do |file|
10
+ require_dependency file
11
+ end
12
+
13
+ ActiveRecord::Base.descendants.each do |model|
14
+ method_name = model.name.underscore.gsub("/", "_").pluralize
15
+
16
+ class_eval <<-RUBY, __FILE__, __LINE__
17
+ def #{method_name}(name)
18
+ factory(name, #{model})
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+
24
+ def factory(name, model = nil, &block)
25
+ if block_given?
26
+ factory_set(name, &block)
27
+ else
28
+ factory_get(name, model)
29
+ end
30
+ end
31
+
32
+ private
33
+ def factory_get(name, model)
34
+ factory = Factory::Preload.factories[model.name][name] rescue nil
35
+ raise "Couldn't find #{name.inspect} factory for #{model.name.inspect} model" unless factory
36
+ factory
37
+ end
38
+
39
+ def factory_set(name, &block)
40
+ record = block.call
41
+ Factory::Preload.factories[record.class.name] ||= {}
42
+ Factory::Preload.factories[record.class.name][name.to_sym] = record
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.configure do |config|
2
+ config.include Factory::Preload::Helpers
3
+ config.before(:suite) do
4
+ Factory::Preload.clean
5
+ Factory::Preload.run
6
+ end
7
+
8
+ config.before(:each) do
9
+ Factory::Preload.reload_factories
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class Factory
2
+ module Preload
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ class Factory
2
+ module Preload
3
+ autoload :Helpers, "factory_girl/preload/helpers"
4
+ autoload :Version, "factory_girl/preload/version"
5
+
6
+ require "factory_girl/preload/rspec2" if defined?(RSpec)
7
+
8
+ class << self
9
+ attr_accessor :preloaders
10
+ attr_accessor :factories
11
+ end
12
+
13
+ self.preloaders = []
14
+ self.factories = {}
15
+
16
+ def self.run
17
+ helper = Object.new.extend(Helpers)
18
+
19
+ ActiveRecord::Base.connection.transaction :requires_new => true do
20
+ preloaders.each do |block|
21
+ helper.instance_eval(&block)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.clean(*names)
27
+ query = ActiveRecord::Base.connection.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" ? "DELETE FROM `%s`" : "TRUNCATE TABLE `%s`"
28
+ names = ActiveRecord::Base.descendants.collect(&:table_name).uniq if names.empty?
29
+ names.each {|table| ActiveRecord::Base.connection.execute(query % table)}
30
+ end
31
+
32
+ def self.reload_factories
33
+ factories.each do |class_name, group|
34
+ group.values.each {|factory| factory.reload}
35
+ end
36
+ end
37
+ end
38
+
39
+ def self.preload(&block)
40
+ Preload.preloaders << block
41
+ end
42
+ end
@@ -0,0 +1 @@
1
+ require "factory_girl/preload"
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe Factory::Preload do
4
+ include Factory::Preload::Helpers
5
+
6
+ it "queues preloader block" do
7
+ block = proc {}
8
+ Factory.preload(&block)
9
+ Factory::Preload.preloaders.should include(block)
10
+ end
11
+
12
+ it "injects model methods" do
13
+ expect { users(:john) }.to_not raise_error
14
+ end
15
+
16
+ it "returns :john factory for User model" do
17
+ users(:john).should be_an(User)
18
+ end
19
+
20
+ it "returns :ruby factory for Skill model" do
21
+ skills(:ruby).should be_a(Skill)
22
+ end
23
+
24
+ it "reuses existing factories" do
25
+ skills(:ruby).user.should == users(:john)
26
+ end
27
+
28
+ it "raises error for missing factories" do
29
+ expect { users(:mary) }.to raise_error(%[Couldn't find :mary factory for "User" model])
30
+ end
31
+
32
+ it "removes records" do
33
+ User.count.should == 1
34
+ Factory::Preload.clean
35
+ User.count.should == 0
36
+ end
37
+
38
+ context "reloadable factories" do
39
+ before :all do
40
+ Factory::Preload.clean
41
+ Factory::Preload.run
42
+ end
43
+
44
+ before :each do
45
+ Factory::Preload.reload_factories
46
+ end
47
+
48
+ it "updates invitation count" do
49
+ users(:john).increment(:invitations)
50
+ users(:john).save
51
+ users(:john).invitations.should == 1
52
+ end
53
+
54
+ it "reloads factory" do
55
+ users(:john).invitations.should == 0
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,34 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require "rails"
3
+
4
+ ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../Gemfile"
5
+ require "bundler"
6
+ Bundler.setup
7
+ require "rails/all"
8
+ Bundler.require(:default)
9
+
10
+ require "rspec/rails"
11
+ require "factory_girl"
12
+ require File.dirname(__FILE__) + "/support/factories"
13
+
14
+ module RSpec
15
+ class Application < ::Rails::Application
16
+ config.root = File.dirname(__FILE__) + "/support/app"
17
+ config.active_support.deprecation = :log
18
+ # config.active_record.logger = Logger.new(STDOUT)
19
+ end
20
+ end
21
+
22
+ RSpec::Application.initialize!
23
+ load File.dirname(__FILE__) + "/support/app/db/schema.rb"
24
+
25
+ RSpec.configure do |config|
26
+ config.use_transactional_fixtures = true
27
+ end
28
+
29
+ Factory.preload do
30
+ factory(:john) { Factory(:user) }
31
+ factory(:ruby) { Factory(:skill, :user => users(:john)) }
32
+ end
33
+
34
+ Factory::Preload.run
@@ -0,0 +1,3 @@
1
+ class Skill < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :skills
3
+ end
@@ -0,0 +1,5 @@
1
+ test:
2
+ adapter: mysql2
3
+ database: test
4
+ username: root
5
+ host: localhost
@@ -0,0 +1,19 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ begin
3
+ drop_table :users
4
+ drop_table :skills
5
+ rescue Exception => e
6
+ end
7
+
8
+ create_table :users do |t|
9
+ t.string :name, :null => false
10
+ t.string :email, :null => false
11
+ t.integer :invitations, :null => false, :default => 0
12
+ end
13
+
14
+ add_index :users, :email, :unique => true
15
+
16
+ create_table :skills do |t|
17
+ t.references :user
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ Factory.define :user do |f|
2
+ f.name "John Doe"
3
+ f.email { Factory.next(:email) }
4
+ end
5
+
6
+ Factory.sequence :email do |i|
7
+ "john#{i}@doe.com"
8
+ end
9
+
10
+ Factory.define :skill do |f|
11
+ f.association :user
12
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_girl-preload
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Nando Vieira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-11 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: factory_girl
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.3
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: ruby-debug19
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.6
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: actionpack
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.6
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec-rails
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 2.5.0
68
+ type: :development
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: mysql2
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 0.2.7
79
+ type: :development
80
+ version_requirements: *id006
81
+ description: Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!
82
+ email:
83
+ - fnando.vieira@gmail.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files: []
89
+
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - README.rdoc
96
+ - Rakefile
97
+ - factory_girl-preload.gemspec
98
+ - lib/factory_girl-preload.rb
99
+ - lib/factory_girl/preload.rb
100
+ - lib/factory_girl/preload/helpers.rb
101
+ - lib/factory_girl/preload/rspec2.rb
102
+ - lib/factory_girl/preload/version.rb
103
+ - spec/factory_girl/preload_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/app/app/models/skill.rb
106
+ - spec/support/app/app/models/user.rb
107
+ - spec/support/app/config/database.yml
108
+ - spec/support/app/db/schema.rb
109
+ - spec/support/factories.rb
110
+ homepage: http://rubygems.org/gems/factory_girl-preload
111
+ licenses: []
112
+
113
+ post_install_message:
114
+ rdoc_options: []
115
+
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.7.2
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!
137
+ test_files:
138
+ - spec/factory_girl/preload_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/app/app/models/skill.rb
141
+ - spec/support/app/app/models/user.rb
142
+ - spec/support/app/config/database.yml
143
+ - spec/support/app/db/schema.rb
144
+ - spec/support/factories.rb