test_dummy 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -0,0 +1,16 @@
1
+ case (Rails::VERSION::MAJOR)
2
+ when 2
3
+ if (defined?(ActiveRecord) and defined?(ActiveRecord::Base))
4
+ ActiveRecord::Base.send(:include, TestDummy)
5
+ end
6
+ else
7
+ class TestDummy::Railtie < Rails::Railtie
8
+ railtie_name :test_dummy
9
+
10
+ config.after_initialize do
11
+ if (defined?(ActiveRecord) and defined?(ActiveRecord::Base))
12
+ ActiveRecord::Base.send(:include, TestDummy)
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/test_dummy.rb CHANGED
@@ -1,28 +1,40 @@
1
+ require 'test_dummy/railtie'
2
+
1
3
  module TestDummy
2
4
  def self.included(base)
3
5
  base.send(:extend, ClassMethods)
4
6
  base.send(:include, InstanceMethods)
5
7
  end
6
8
 
7
- def self.combine_create_params(*param_sets)
8
- final_params = { }
9
+ # Combines several sets of parameters together into a single set in order
10
+ # of lowest priority to highest priority. Supplied list can contain nil
11
+ # values which will be ignored. Returns a Hash with symbolized keys.
12
+ def self.combine_attributes(*sets)
13
+ combined_attributes = { }
9
14
 
10
- # Apply param_sets in order they are listed
11
- param_sets.compact.each do |params|
12
- params.each do |k, v|
13
- # Ignore nil assignments
14
- final_params[k.to_sym] = v if (v)
15
+ # Apply sets in order they are listed
16
+ sets.compact.each do |set|
17
+ set.each do |k, v|
18
+ case (v)
19
+ when nil
20
+ # Ignore nil assignments
21
+ else
22
+ combined_attributes[k.to_sym] = v
23
+ end
15
24
  end
16
25
  end
17
26
 
18
- final_params
27
+ combined_attributes
19
28
  end
20
-
29
+
30
+ # Adds a mixin to the core DummyMethods module
21
31
  def self.add_module(new_module)
22
- FakeMethods.send(:extend, new_module)
32
+ DummyMethods.send(:extend, new_module)
23
33
  end
24
34
 
25
- def self.can_fake(*names, &block)
35
+ # Used in an initializer to define things that can be dummyd by all
36
+ # models if these properties are available.
37
+ def self.can_dummy(*names, &block)
26
38
  case (names.last)
27
39
  when Hash
28
40
  options = names.pop
@@ -32,7 +44,7 @@ module TestDummy
32
44
  block = options[:with]
33
45
  end
34
46
 
35
- FakeMethods.send(
47
+ DummyMethods.send(
36
48
  :extend,
37
49
  names.inject(Module.new) do |m, name|
38
50
  m.send(:define_method, name, &block)
@@ -41,24 +53,27 @@ module TestDummy
41
53
  )
42
54
  end
43
55
 
56
+ # Used in an initializer to define configuration parameters.
44
57
  def self.config(&block)
45
- RailsModelFaker.instance_eval(&block)
46
- end
47
-
48
- def self.include(addon)
49
- RailsModelFaker.send(:extend, addon)
58
+ TestDummy.instance_eval(&block)
50
59
  end
51
60
 
52
- module FakeMethods
53
- # Placeholder for generic fake methods
61
+ module DummyMethods
62
+ # Container for common data faking methods as they are defined.
54
63
  end
55
64
 
56
65
  module ClassMethods
57
- def fake_field_config
58
- @rmf_can_fake ||= { }
66
+ # Returns a Hash which describes the dummy configuration for this
67
+ # Model class.
68
+ def dummy_attributes
69
+ @test_dummy ||= { }
59
70
  end
60
71
 
61
- def can_fake(*names, &block)
72
+ # Declares how to fake one or more attributes. Accepts a block
73
+ # that can receive up to two parameters, the first the instance of
74
+ # the model being created, the second the parameters supplied to create
75
+ # it. The first and second parameters may be nil.
76
+ def can_dummy(*names, &block)
62
77
  options = nil
63
78
 
64
79
  case (names.last)
@@ -70,90 +85,112 @@ module TestDummy
70
85
  block = options[:with]
71
86
  end
72
87
 
73
- @rmf_can_fake ||= { }
74
- @rmf_can_fake_order ||= [ ]
88
+ @test_dummy ||= { }
89
+ @test_dummy_order ||= [ ]
75
90
 
76
91
  names.flatten.each do |name|
77
92
  name = name.to_sym
78
93
 
79
94
  # For associations, delay creation of block until first call
80
95
  # to allow for additional relationships to be defined after
81
- # the can_fake call. Leave placeholder (true) instead.
96
+ # the can_dummy call. Leave placeholder (true) instead.
82
97
 
83
- @rmf_can_fake[name] = block || true
84
- @rmf_can_fake_order << name
98
+ @test_dummy[name] = block || true
99
+ @test_dummy_order << name
85
100
  end
86
101
  end
87
102
 
88
- def can_fake?(*names)
89
- @rmf_can_fake ||= { }
103
+ # Returns true if all the supplied attribute names have defined
104
+ # dummy methods, or false otherwise.
105
+ def can_dummy?(*names)
106
+ @test_dummy ||= { }
90
107
 
91
108
  names.flatten.reject do |name|
92
- @rmf_can_fake.key?(name)
109
+ @test_dummy.key?(name)
93
110
  end.empty?
94
111
  end
95
112
 
96
- def build_fake(params = nil)
97
- model = new(RailsModelFaker.combine_create_params(scope(:create), params))
113
+ # Builds a dummy model with some parameters set as supplied. The
114
+ # new model is provided to the optional block for manipulation before
115
+ # the dummy operation is completed. Returns a dummy model which has not
116
+ # been saved.
117
+ def build_dummy(with_attributes = nil)
118
+ model = new(self.class.combine_attributes(scope(:create), with_attributes))
98
119
 
99
120
  yield(model) if (block_given?)
100
121
 
101
- self.execute_fake_operation(model, params)
122
+ self.execute_dummy_operation(model, with_attributes)
102
123
 
103
124
  model
104
125
  end
105
126
 
106
- def create_fake(params = nil, &block)
107
- model = build_fake(params, &block)
127
+ # Builds a dummy model with some parameters set as supplied. The
128
+ # new model is provided to the optional block for manipulation before
129
+ # the dummy operation is completed and the model is saved. Returns a
130
+ # dummy model. The model may not have been saved if there was a
131
+ # validation failure, or if it was blocked by a callback.
132
+ def create_dummy(with_attributes = nil, &block)
133
+ model = build_dummy(with_attributes, &block)
108
134
 
109
135
  model.save
110
136
 
111
137
  model
112
138
  end
113
139
 
114
- def create_fake!(params = nil, &block)
115
- model = build_fake(params, &block)
140
+ # Builds a dummy model with some parameters set as supplied. The
141
+ # new model is provided to the optional block for manipulation before
142
+ # the dummy operation is completed and the model is saved. Returns a
143
+ # dummy model. Will throw ActiveRecord::RecordInvalid if there was a
144
+ # validation failure, or ActiveRecord::RecordNotSaved if the save was
145
+ # blocked by a callback.
146
+ def create_dummy!(with_attributes = nil, &block)
147
+ model = build_dummy(with_attributes, &block)
116
148
 
117
149
  model.save!
118
150
 
119
151
  model
120
152
  end
121
153
 
122
- def fake(name, params = nil)
123
- params = RailsModelFaker.combine_create_params(scope(:create), params)
154
+ # Produces dummy data for a single attribute.
155
+ def dummy(name, with_attributes = nil)
156
+ with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes)
124
157
 
125
- fake_method_call(nil, params, fake_method(name))
158
+ dummy_method_call(nil, with_attributes, dummy_method(name))
126
159
  end
127
160
 
128
- def fake_params(params = nil)
129
- params = RailsModelFaker.combine_create_params(scope(:create), params)
161
+ # Produces a complete set of dummy attributes. These can be used to
162
+ # create a model.
163
+ def dummy_attributes(with_attributes = nil)
164
+ with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes)
130
165
 
131
- @rmf_can_fake_order.each do |field|
132
- unless (params.key?(field))
133
- result = fake(field, params)
166
+ @test_dummy_order.each do |field|
167
+ unless (with_attributes.key?(field))
168
+ result = dummy(field, with_attributes)
134
169
 
135
170
  case (result)
136
- when nil, params
171
+ when nil, with_attributes
137
172
  # Declined to populate parameters if method returns nil
138
173
  # or returns the existing parameter set.
139
174
  else
140
- params[field] = result
175
+ with_attributes[field] = result
141
176
  end
142
177
  end
143
178
  end
144
179
 
145
- params
180
+ with_attributes
146
181
  end
147
182
 
148
- def execute_fake_operation(model, params = nil)
149
- @rmf_can_fake_order.each do |name|
183
+ # This performs the dummy operation on a model with an optional set
184
+ # of parameters.
185
+ def execute_dummy_operation(model, with_attributes = nil)
186
+ @test_dummy_order.each do |name|
150
187
  if (reflection = reflect_on_association(name))
151
- unless ((params and params.key?(name.to_sym)) or model.send(name))
152
- model.send(:"#{name}=", fake_method_call(model, params, fake_method(name)))
188
+ unless ((with_attributes and with_attributes.key?(name.to_sym)) or model.send(name))
189
+ model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name)))
153
190
  end
154
191
  else
155
- unless (params and (params.key?(name.to_sym) or params.key?(name.to_s)))
156
- model.send(:"#{name}=", fake_method_call(model, params, fake_method(name)))
192
+ unless (with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(name.to_s)))
193
+ model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name)))
157
194
  end
158
195
  end
159
196
  end
@@ -162,10 +199,10 @@ module TestDummy
162
199
  end
163
200
 
164
201
  protected
165
- def fake_method_call(model, params, block)
202
+ def dummy_method_call(model, with_attributes, block)
166
203
  case (block.arity)
167
204
  when 2, -1
168
- block.call(model, params)
205
+ block.call(model, with_attributes)
169
206
  when 1
170
207
  block.call(model)
171
208
  else
@@ -173,27 +210,27 @@ module TestDummy
173
210
  end
174
211
  end
175
212
 
176
- def fake_method(name)
213
+ def dummy_method(name)
177
214
  name = name.to_sym
178
215
 
179
- block = @rmf_can_fake[name]
216
+ block = @test_dummy[name]
180
217
 
181
218
  case (block)
182
219
  when Module
183
220
  block.method(name)
184
221
  when Symbol
185
- FakeMethods.method(name)
222
+ DummyMethods.method(name)
186
223
  when true
187
- # Configure association faker the first time it is called
224
+ # Configure association dummyr the first time it is called
188
225
  if (reflection = reflect_on_association(name))
189
226
  primary_key = reflection.primary_key_name.to_sym
190
227
 
191
- @rmf_can_fake[name] =
192
- lambda do |model, params|
193
- (params and params.key?(primary_key)) ? nil : reflection.klass.send(:create_fake)
228
+ @test_dummy[name] =
229
+ lambda do |model, with_attributes|
230
+ (with_attributes and with_attributes.key?(primary_key)) ? nil : reflection.klass.send(:create_dummy)
194
231
  end
195
232
  else
196
- raise "Cannot fake unknown relationship #{name}"
233
+ raise "Cannot dummy unknown relationship #{name}"
197
234
  end
198
235
  else
199
236
  block
@@ -202,8 +239,10 @@ module TestDummy
202
239
  end
203
240
 
204
241
  module InstanceMethods
205
- def fake!(params = nil)
206
- self.class.execute_fake_operation(self, params)
242
+ # Assigns any attributes which can be dummied that have not already
243
+ # been populated.
244
+ def dummy!(with_attributes = nil)
245
+ self.class.execute_dummy_operation(self, with_attributes)
207
246
  end
208
247
  end
209
248
  end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{test_dummy}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["tadman"]
12
+ s.date = %q{2010-09-01}
13
+ s.description = %q{Test Dummy allows you to define how to fake models automatically so that you can use dummy data for testing instead of fixtures. Dummy models are always generated using the current schema and don't need to me migrated like fixtures.}
14
+ s.email = %q{github@tadman.ca}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/test_dummy.rb",
27
+ "lib/test_dummy/railtie.rb",
28
+ "test/helper.rb",
29
+ "test/test_test_dummy.rb",
30
+ "test_dummy.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/tadman/test_dummy}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Quick test data generator and fake model maker}
37
+ s.test_files = [
38
+ "test/helper.rb",
39
+ "test/test_test_dummy.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - tadman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-31 00:00:00 -04:00
17
+ date: 2010-09-01 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -35,8 +35,10 @@ files:
35
35
  - Rakefile
36
36
  - VERSION
37
37
  - lib/test_dummy.rb
38
+ - lib/test_dummy/railtie.rb
38
39
  - test/helper.rb
39
40
  - test/test_test_dummy.rb
41
+ - test_dummy.gemspec
40
42
  has_rdoc: true
41
43
  homepage: http://github.com/tadman/test_dummy
42
44
  licenses: []