gonzales 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
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
4
 
5
- Using Gonzales, reduced my unit-tests time from 1 minute 40 seconds, down to 28 seconds.
5
+ Using Gonzales, reduced my unit-tests time from 1 minute 40 seconds, down to 24 seconds.
6
6
 
7
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
8
  You just have to learn one new keyword - ```speedy```. You will use ```speedy``` for:
@@ -104,4 +104,41 @@ end
104
104
 
105
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
106
 
107
+ ## Using speedy with integration tests
108
+
109
+ If you are using (DatabaseCleaner)[https://github.com/bmabey/database_cleaner], and probably (Capybara)[https://github.com/jnicklas/capybara], when running integration tests, you will probably face some challenges. However, do not despair, you may be able to work around this. Since you do not want to load all your factories in advance, you can let Gonzales load them as you need them. I recomment the following settings in your integration_test_helper file:
110
+
111
+ ``` Ruby
112
+
113
+ Gonzales.configure do |config|
114
+ config.disable_preload = true # ensures that the spoeedy file is not loaded when initializing Gonzales
115
+ config.adapter = :registered # ensures factories created are cached as they would be when loaded from speedy file.
116
+ end
117
+
118
+ module ActionDispatch
119
+ class IntegrationTest
120
+ include Capybara::DSL
121
+ # Stop ActiveRecord from wrapping tests in transactions
122
+ self.use_transactional_fixtures = false
123
+ DatabaseCleaner.clean # Truncate the database before tests are run the first time
124
+ Gonzales.initialize! # Ensure any cache is cleared before we run the tests
125
+
126
+ teardown do
127
+ DatabaseCleaner.clean # Truncate the database
128
+ Capybara.reset_sessions! # Forget the (simulated) browser state
129
+ Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
130
+ Gonzales.initialize! # Ensure cache is cleared after running each test
131
+ end
132
+
133
+ def capture_and_open_screen
134
+ tmp = Tempfile.new('capture_and_open_screen')
135
+ tmp.close
136
+ system("screencapture -P #{tmp.path}")
137
+ system("open -a Preview \"file://#{tmp.path}\"")
138
+ end
139
+ end
140
+ end
141
+ ```
142
+
143
+
107
144
  Thats all there is to it. Speedy coding!
@@ -0,0 +1,37 @@
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 Adapter
26
+ # = Gonzales::Adapters::Registered
27
+ #
28
+ class Registered
29
+ # Facitity to instantiate factories in database during db:test.
30
+ # The record will be registered in the collection when created.
31
+ #
32
+ def self.create(factory_name, *options)
33
+ Gonzales::Factories.speedy(factory_name, *options)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
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 Adapter
26
+ # = Gonzales::Adapter::Unregistered
27
+ #
28
+ class Unregistered
29
+ # Facitity to instantiate factories in database during db:test.
30
+ # The record will not be registered in the collection when created.
31
+ #
32
+ def self.create(factory_name, *options)
33
+ Factory.create(factory_name, *options)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,49 @@
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
+
25
+ module Gonzales
26
+ # = Gonzales::Adapter
27
+ #
28
+ # Adapter for instantiating factories in database
29
+ module Adapter
30
+ autoload :Registered, 'gonzales/adapter/registered'
31
+ autoload :Unregistered, 'gonzales/adapter/unregistered'
32
+
33
+ # Instantiates a record in the database with the defined factory. This is an internal method.
34
+ #
35
+ # The method will call either registered or unregistered adapter, depending on what was defined in the use method.
36
+ #
37
+ def self.create(factory_name, *options)
38
+ @@adapter ||= Unregistered
39
+ @@adapter.create(factory_name, *options)
40
+ end
41
+
42
+ # Sets the adapter to be used for instantiating objects based on predefioned factories. Internal.
43
+ #
44
+ def self.use(name)
45
+ raise ArgumentError unless %w(registered unregistered).include? name.to_s
46
+ @@adapter = "#{self}::#{name.to_s.classify}".constantize
47
+ end
48
+ end
49
+ end
@@ -31,6 +31,7 @@ module Gonzales
31
31
  # Adds an object (entity) to the collection.
32
32
  def add(name, object)
33
33
  entities[name] = { :class => object.class.to_s, :id => object.id }
34
+ object
34
35
  end
35
36
 
36
37
  # Retrieves the collection of entities.
@@ -53,12 +54,18 @@ module Gonzales
53
54
  end
54
55
  end
55
56
 
57
+ # Clears the cache of objects collected from load or instantiation
58
+ def clear_cache
59
+ @@entities = {}
60
+ end
61
+
56
62
  private
57
63
  def cache_filename #:nodoc:
58
64
  @@cache_filename ||= Gonzales.factory_cache || Rails.root.join('tmp', 'speedy.yml')
59
65
  end
60
66
 
61
67
  def load #:nodoc:
68
+ return {} if Gonzales.disable_preload
62
69
  begin
63
70
  YAML::load_file cache_filename
64
71
  rescue Errno::ENOENT
@@ -49,6 +49,7 @@ module Gonzales
49
49
 
50
50
  # Yields a block to define speedy statements, then saves a collection of references to a temporary file
51
51
  def self.load(&block)
52
+ Collection.clear_cache
52
53
  ::FactoryGirl.reload
53
54
  yield self
54
55
  Collection.save
@@ -63,11 +63,11 @@ module Gonzales
63
63
  if r.send(attribute).size == 0
64
64
  factory_names = args.size > 0 ? args : [attribute]
65
65
  r.send("#{attribute}=",
66
- factory_names.collect { |factory_name| Gonzales::Collection.entity(factory_name) || Factory.create(factory_name, options) })
66
+ factory_names.collect { |factory_name| Collection.entity(factory_name) || Adapter.create(factory_name, options) })
67
67
  end
68
68
  elsif !r.send(attribute)
69
69
  factory_name = args.first || attribute
70
- r.send("#{attribute}=", Gonzales::Collection.entity(factory_name) || Factory.create(factory_name, options))
70
+ r.send("#{attribute}=", Collection.entity(factory_name) || Adapter.create(factory_name, options))
71
71
  end
72
72
  end
73
73
  end
@@ -45,7 +45,7 @@ module Gonzales
45
45
  # end
46
46
  #
47
47
  def speedy(factory_name, *options)
48
- Collection.entity(factory_name) || Factory.create(factory_name, *options)
48
+ Collection.entity(factory_name) || Adapter.create(factory_name, *options)
49
49
  end
50
50
  end
51
51
  end
@@ -1,3 +1,3 @@
1
1
  module Gonzales
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/gonzales.rb CHANGED
@@ -26,26 +26,38 @@ require 'active_support/configurable'
26
26
  module Gonzales
27
27
  # = Gonzales provides a mechanism for speeding up tests when using FactoryGirl
28
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+
29
+ autoload :Adapter, 'gonzales/adapter'
30
+ autoload :Collection, 'gonzales/collection'
31
+ autoload :Factories, 'gonzales/factories'
32
+ autoload :TestHelper, 'gonzales/test_helper'
33
+
34
+ include ActiveSupport::Configurable
35
+
36
+ # == Configurations
35
37
  #
38
+ # * factory_module - the module containing the definitions of factories to be store in the test database.
39
+ # Defaults to +test/speedy.rb+
40
+ # * factory_cache - the entity lookup yaml-file to store factories that was save to the database when Gonzales.initialize! was run
41
+ # Defaults to +test/speedy.yml+
42
+ # * disable_preload - Set to true if you want to disable preload for integration tests. If you are using database_cleaner, you
43
+ # will probably also set this one to true.
44
+ # * adapter - Set to either :registered or :unregistered. When using database_cleaner with integration_tests and you want
45
+ # to reuse associations decleared with speedy, you should set this value to :registered
36
46
  # === Example
37
47
  #
38
48
  # Gonzales.configure do |config|
39
49
  # config.factory_module = Rails.root.join('tmp', 'gonzales.yml')
40
50
  # config.factory_cache = Rails.root.join('test', 'speedy.rb')
41
51
  # 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
52
+ config_accessor :factory_module, :factory_cache, :disable_preload, :adapter
53
+
54
+ self.config.class.redefine_method(:adapter=) do |name|
55
+ Gonzales::Adapter.use name
56
+ end
57
+
58
+ # def self.adapter=(name)
59
+ # Adapter.use name
60
+ # end
49
61
 
50
62
  # Runs the initialization of Gonzales. Put this in a rake task for your application.
51
63
  #
@@ -62,7 +74,11 @@ module Gonzales
62
74
  # end
63
75
  #
64
76
  def self.initialize!
65
- load factory_module || Rails.root.join('test', 'speedy.rb')
77
+ unless disable_preload
78
+ load(factory_module || Rails.root.join('test', 'speedy.rb'))
79
+ else
80
+ Collection.clear_cache
81
+ end
66
82
  end
67
83
  end
68
84