gonzales 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +23 -0
- data/README.md +107 -0
- data/Rakefile +38 -0
- data/lib/gonzales/collection.rb +70 -0
- data/lib/gonzales/factories.rb +57 -0
- data/lib/gonzales/factory_girl/definition_proxy.rb +77 -0
- data/lib/gonzales/test_helper.rb +51 -0
- data/lib/gonzales/version.rb +3 -0
- data/lib/gonzales.rb +72 -0
- data/lib/tasks/gonzales_tasks.rake +7 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/models/hat.rb +3 -0
- data/test/dummy/app/models/material.rb +3 -0
- data/test/dummy/app/models/outfit.rb +6 -0
- data/test/dummy/app/models/shoe.rb +6 -0
- data/test/dummy/app/models/wardrobe.rb +3 -0
- data/test/dummy/config/application.rb +59 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20121005205521_create_hats.rb +10 -0
- data/test/dummy/db/migrate/20121009054201_create_materials.rb +9 -0
- data/test/dummy/db/migrate/20121009055333_create_shoes.rb +16 -0
- data/test/dummy/db/migrate/20121009091652_create_outfits.rb +13 -0
- data/test/dummy/db/migrate/20121010161218_create_hats_outfits.rb +10 -0
- data/test/dummy/db/migrate/20121011131629_create_wardrobes.rb +9 -0
- data/test/dummy/db/migrate/20121011131724_add_wardrobe_to_outfit.rb +5 -0
- data/test/dummy/db/schema.rb +69 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +571 -0
- data/test/dummy/log/test.log +20542 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/test/factories/hats.rb +14 -0
- data/test/dummy/test/factories/materials.rb +11 -0
- data/test/dummy/test/factories/outfits.rb +16 -0
- data/test/dummy/test/factories/shoes.rb +16 -0
- data/test/dummy/test/factories/wardrobes.rb +7 -0
- data/test/dummy/test/speedy.rb +7 -0
- data/test/dummy/test/unit/hat_test.rb +14 -0
- data/test/dummy/test/unit/material_test.rb +7 -0
- data/test/dummy/test/unit/outfit_test.rb +36 -0
- data/test/dummy/test/unit/shoe_test.rb +13 -0
- data/test/dummy/test/unit/wardrobe_test.rb +7 -0
- data/test/dummy/tmp/speedy.yml +16 -0
- data/test/test_helper.rb +15 -0
- data/test/unit/gonzales/collection_test.rb +81 -0
- data/test/unit/gonzales/factories_test.rb +53 -0
- data/test/unit/gonzales/factory_girl/definition_proxy_test.rb +106 -0
- data/test/unit/gonzales/test_helper_test.rb +44 -0
- data/test/unit/gonzales_test.rb +15 -0
- metadata +251 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright 2012 Knut I Stenmark, Dynamic Project Management AS
|
2
|
+
Copyright 2012 Teknobingo AS
|
3
|
+
Copyright 2012 Bingo Entrepenøren AS
|
4
|
+
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Gonzales
|
2
|
+
|
3
|
+
After struggling with some slow running tests you will probably want to speed them up. Factory girl is somewhat slow, because it will try to create new records in the database before each test is running. Fixtures works much faster but with less flexibility.
|
4
|
+
|
5
|
+
Using Gonzales, reduced my unit-tests time from 1 minute 40 seconds, down to 28 seconds.
|
6
|
+
|
7
|
+
Well, Gonzales makes the best out of Factory Girl, by allowing you to load the factories into the database before the tests starts.
|
8
|
+
You just have to learn one new keyword - ```speedy```. You will use ```speedy``` for:
|
9
|
+
|
10
|
+
* Saving factories to test database before running tests
|
11
|
+
* Declaring associations in factories
|
12
|
+
* Refering to objects that were saved to test database before tests started
|
13
|
+
|
14
|
+
# Install and Setup
|
15
|
+
|
16
|
+
Install the gem
|
17
|
+
|
18
|
+
gem install gonzales
|
19
|
+
|
20
|
+
Or, in your Gemfile
|
21
|
+
|
22
|
+
gem 'gonzales'
|
23
|
+
|
24
|
+
## Initializing your factories before the tests are running
|
25
|
+
|
26
|
+
Define a file in your test directory called speedy.rb. Enlist all the factories you want to load before tests are running using the ```speedy``` command.
|
27
|
+
|
28
|
+
``` Ruby
|
29
|
+
# test/speedy.rb
|
30
|
+
Gonzales::Factories.load do |go|
|
31
|
+
go.speedy :address
|
32
|
+
go.speedy :organization
|
33
|
+
go.speedy :person
|
34
|
+
go.speedy :john, :person, :name => 'John'
|
35
|
+
end
|
36
|
+
```
|
37
|
+
The latter is creating the alias ```john``` to ```person``` and also changing its ```name``` attribute. This way you can easily customize associations in your factories.
|
38
|
+
|
39
|
+
Then, you will need to initialize Gonzales before you run your tests.
|
40
|
+
Add this file to your lib/tasks directory:
|
41
|
+
|
42
|
+
``` Ruby
|
43
|
+
# lib/tasks/gonzales_tasks.rake
|
44
|
+
namespace :db do
|
45
|
+
namespace :test do
|
46
|
+
task :prepare => :environment do
|
47
|
+
# place your seeds here, if you have any
|
48
|
+
Gonzales.initialize!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
# Important command - speedy
|
55
|
+
|
56
|
+
## Loading association with speedy
|
57
|
+
|
58
|
+
A model with many associations may be very slow to instantiate. Lets say you have a person that has an address, and the person belongs to an organization with an address; that is four records that needs to be created in the database just for one person. If you need multiple roles in a test, that is the multitude of the number of roles you use in the test. By creating the records in the test database before running the tests, the tests will run much faster.
|
59
|
+
|
60
|
+
You will use the keyword ```speedy``` with factory girl to define associations
|
61
|
+
|
62
|
+
``` Ruby
|
63
|
+
# test/factories.rb
|
64
|
+
FactoryGirl.define do
|
65
|
+
|
66
|
+
factory :organization do
|
67
|
+
name 'Looney tunes'
|
68
|
+
speedy :address
|
69
|
+
end
|
70
|
+
|
71
|
+
factory :person do
|
72
|
+
name 'Sylvester'
|
73
|
+
speedy :organization
|
74
|
+
speedy :address
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
```speedy``` is in reality implemented so that if you have defined the association with Factory.create command, it will be preserved. The code:
|
80
|
+
|
81
|
+
``` Ruby
|
82
|
+
Factory.create :person, :organization => @organization
|
83
|
+
```
|
84
|
+
|
85
|
+
In practice the ```speedy``` association command does something like this:
|
86
|
+
|
87
|
+
``` Ruby
|
88
|
+
after_build { |r| r.organization = pre_created_organization || Factory.create(:organization) unless r.organization }
|
89
|
+
```
|
90
|
+
|
91
|
+
## Instantiating a pre-created object defined with factory girl using speedy
|
92
|
+
|
93
|
+
In order to fetch an object that has already been pre-created you'll use the ```speedy``` command for that too.
|
94
|
+
In your test you will write something like this:
|
95
|
+
|
96
|
+
``` Ruby
|
97
|
+
class HatTest < ActiveSupport::TestCase
|
98
|
+
setup do
|
99
|
+
@person = speedy :person
|
100
|
+
end
|
101
|
+
...
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
If the factory has not been pre-created speedy will just call Factory.create. And of course, if you want to, you can still use Factory.build or Factory.create, but then they will work like traditional FactoryGirl usage.
|
106
|
+
|
107
|
+
Thats all there is to it. Speedy coding!
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Gonzales'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module Gonzales
|
25
|
+
# = Gonzales::Collection
|
26
|
+
#
|
27
|
+
# Holds references to the factories instantiated during db:test:prepare
|
28
|
+
class Collection
|
29
|
+
class << self
|
30
|
+
|
31
|
+
# Adds an object (entity) to the collection.
|
32
|
+
def add(name, object)
|
33
|
+
entities[name] = { :class => object.class.to_s, :id => object.id }
|
34
|
+
end
|
35
|
+
|
36
|
+
# Retrieves the collection of entities.
|
37
|
+
# If they are not already loded from the temporary cache map, the map is loaded.
|
38
|
+
def entities
|
39
|
+
@@entities ||= load
|
40
|
+
end
|
41
|
+
|
42
|
+
# Retrieves a specific object
|
43
|
+
def entity(factory_name)
|
44
|
+
if e = entities[factory_name]
|
45
|
+
e[:class].constantize.find e[:id]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Save a collection map to temporary file
|
50
|
+
def save #:nodoc:
|
51
|
+
File.open(cache_filename, "w+") do |file|
|
52
|
+
file.write entities.to_yaml
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def cache_filename #:nodoc:
|
58
|
+
@@cache_filename ||= Gonzales.factory_cache || Rails.root.join('tmp', 'speedy.yml')
|
59
|
+
end
|
60
|
+
|
61
|
+
def load #:nodoc:
|
62
|
+
begin
|
63
|
+
YAML::load_file cache_filename
|
64
|
+
rescue Errno::ENOENT
|
65
|
+
{}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module Gonzales
|
25
|
+
# = Gonzales::Factories
|
26
|
+
#
|
27
|
+
# Facitity to instantiate factories in database during db:test:prepare
|
28
|
+
class Factories
|
29
|
+
# Save a factory based record to the test database before executing tests
|
30
|
+
#
|
31
|
+
# === Arguments
|
32
|
+
# * alias_name - optional alias name (can be used to reference factory or associations later)
|
33
|
+
# * factory_name - the name of the factory (use to reference factory or associations later if alias is not given)
|
34
|
+
# * options - a hash to be passed to FactoryGirl when creating the record
|
35
|
+
#
|
36
|
+
# === Examples
|
37
|
+
# Gonzales::Factories.load do |go|
|
38
|
+
# go.speedy :address
|
39
|
+
# go.speedy :organization
|
40
|
+
# go.speedy :john, :person, :name => 'John'
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
def self.speedy(factory_or_alias_name, *args)
|
44
|
+
alias_name = factory_or_alias_name
|
45
|
+
options = args.extract_options!
|
46
|
+
factory_name = args.first || alias_name
|
47
|
+
Collection.add alias_name, Factory.create(factory_name, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Yields a block to define speedy statements, then saves a collection of references to a temporary file
|
51
|
+
def self.load(&block)
|
52
|
+
::FactoryGirl.reload
|
53
|
+
yield self
|
54
|
+
Collection.save
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module Gonzales
|
25
|
+
module FactoryGirl
|
26
|
+
# = Gonzales::FactoryGirl::DefinitionProxy
|
27
|
+
#
|
28
|
+
# Extends FactoryGirl with a new extension
|
29
|
+
module DefinitionProxy
|
30
|
+
# Define an association in a factory.
|
31
|
+
# Supports belongs_to and has_and_belongs_to_many associations
|
32
|
+
#
|
33
|
+
# speedy will assign the association in the following order:
|
34
|
+
#
|
35
|
+
# #. If you have specified the association then insgtantiating creating or building the factory,
|
36
|
+
# it will use that association
|
37
|
+
# #. If the association exists in the database, it will assign that.
|
38
|
+
# #. Lastly, when none of the above, it will create the factory.
|
39
|
+
#
|
40
|
+
# === Arguments
|
41
|
+
#
|
42
|
+
# * association_name - the name of the association
|
43
|
+
# * factory_name - the name of the factory (if the factory has the same name as the association, this parameter is optional)
|
44
|
+
# * options - a hash to be passed to FactoryGirl when creating the record using factory
|
45
|
+
#
|
46
|
+
# === Examples
|
47
|
+
#
|
48
|
+
# FactoryGirl.define do
|
49
|
+
# factory :organization do
|
50
|
+
# name "Looney tunes"
|
51
|
+
# speedy :contact # association to contact (belongs_to :contact),
|
52
|
+
# # same as +association :contact+
|
53
|
+
# speedy :addresses, :street_address, :postal_address # association to addresses (has_many_and_belongs_to_many :addresses)
|
54
|
+
# # same as +addresses { |org| [org.association(:street_address), org.association(:postal_address)] }+
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
def speedy(attribute_or_factory, *args)
|
59
|
+
attribute = attribute_or_factory
|
60
|
+
options = args.extract_options!
|
61
|
+
after_build do |r|
|
62
|
+
if r.class.reflect_on_association(attribute).macro.to_s.include? 'has_and_belongs_to_many'
|
63
|
+
if r.send(attribute).size == 0
|
64
|
+
factory_names = args.size > 0 ? args : [attribute]
|
65
|
+
r.send("#{attribute}=",
|
66
|
+
factory_names.collect { |factory_name| Gonzales::Collection.entity(factory_name) || Factory.create(factory_name, options) })
|
67
|
+
end
|
68
|
+
elsif !r.send(attribute)
|
69
|
+
factory_name = args.first || attribute
|
70
|
+
r.send("#{attribute}=", Gonzales::Collection.entity(factory_name) || Factory.create(factory_name, options))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
require 'factory_girl'
|
25
|
+
module Gonzales
|
26
|
+
# = Gonzales::TestHelper
|
27
|
+
#
|
28
|
+
# Helper to be used when writing tests when you use Gonzales
|
29
|
+
module TestHelper
|
30
|
+
# returns a created record to be used in tests
|
31
|
+
#
|
32
|
+
# speedy will return the record from the database if it alreday exists in the test database.
|
33
|
+
# If not it will call FactoryGirl to instantiate the record.
|
34
|
+
#
|
35
|
+
# === Arguments
|
36
|
+
#
|
37
|
+
# * factory_name - Name of the factory or the alias specified in the gonzales file
|
38
|
+
# * options - will be passed to FactoryGirl if the factory does not exist in the test database.
|
39
|
+
# Note!! options will not be passed to Gonzales.
|
40
|
+
#
|
41
|
+
# === Example
|
42
|
+
#
|
43
|
+
# setup do
|
44
|
+
# @person = speedy :person
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
def speedy(factory_name, *options)
|
48
|
+
Collection.entity(factory_name) || Factory.create(factory_name, *options)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/gonzales.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
require 'active_support/configurable'
|
25
|
+
|
26
|
+
module Gonzales
|
27
|
+
# = Gonzales provides a mechanism for speeding up tests when using FactoryGirl
|
28
|
+
#
|
29
|
+
# == Configutations
|
30
|
+
#
|
31
|
+
# * factory_module - the module containing the definitions of factories to be store in the test database.
|
32
|
+
# Defaults to +test/speedy.rb+
|
33
|
+
# * factory_cache - the entity lookup yaml-file to store factories that was save to the database when Gonzales.initialize! was run
|
34
|
+
# Defaults to +test/speedy.yml+
|
35
|
+
#
|
36
|
+
# === Example
|
37
|
+
#
|
38
|
+
# Gonzales.configure do |config|
|
39
|
+
# config.factory_module = Rails.root.join('tmp', 'gonzales.yml')
|
40
|
+
# config.factory_cache = Rails.root.join('test', 'speedy.rb')
|
41
|
+
# end
|
42
|
+
|
43
|
+
autoload :Collection, 'gonzales/collection'
|
44
|
+
autoload :Factories, 'gonzales/factories'
|
45
|
+
autoload :TestHelper, 'gonzales/test_helper'
|
46
|
+
|
47
|
+
include ActiveSupport::Configurable
|
48
|
+
config_accessor :factory_module, :factory_cache
|
49
|
+
|
50
|
+
# Runs the initialization of Gonzales. Put this in a rake task for your application.
|
51
|
+
#
|
52
|
+
# === Example
|
53
|
+
#
|
54
|
+
# # lib/tasks/gonzales_tasks.rake
|
55
|
+
# namespace :db do
|
56
|
+
# namespace :test do
|
57
|
+
# task :prepare => :environment do
|
58
|
+
# # place your seeds here, if you have any
|
59
|
+
# Gonzales.initialize!
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
def self.initialize!
|
65
|
+
load factory_module || Rails.root.join('test', 'speedy.rb')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
require 'gonzales/factory_girl/definition_proxy'
|
70
|
+
require 'factory_girl'
|
71
|
+
FactoryGirl::DefinitionProxy.send(:include, Gonzales::FactoryGirl::DefinitionProxy)
|
72
|
+
ActiveSupport::TestCase.send(:include, Gonzales::TestHelper)
|