test_dummy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ .DS_Store
2
+
3
+ *.gem
4
+
5
+ *.tmproj
6
+ tmtags
7
+
8
+ *~
9
+ \#*
10
+ .\#*
11
+
12
+ *.swp
13
+
14
+ coverage
15
+ rdoc
16
+ pkg
17
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2010 Scott Tadman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,8 @@
1
+ = test_dummy
2
+
3
+ Test Dummy is an easy fake data generator library with the ability to create
4
+ fake models or entirely faked data structures on-demand.
5
+
6
+ == Copyright
7
+
8
+ Copyright (c) 2010 Scott Tadman, The Working Group
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "test_dummy"
8
+ gem.summary = %q[Quick test data generator and fake model maker]
9
+ gem.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.]
10
+ gem.email = "github@tadman.ca"
11
+ gem.homepage = "http://github.com/tadman/test_dummy"
12
+ gem.authors = %w[ tadman ]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/test_*.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+ task :test => :check_dependencies
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "test_dummy #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/test_dummy.rb ADDED
@@ -0,0 +1,209 @@
1
+ module TestDummy
2
+ def self.included(base)
3
+ base.send(:extend, ClassMethods)
4
+ base.send(:include, InstanceMethods)
5
+ end
6
+
7
+ def self.combine_create_params(*param_sets)
8
+ final_params = { }
9
+
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
+ end
16
+ end
17
+
18
+ final_params
19
+ end
20
+
21
+ def self.add_module(new_module)
22
+ FakeMethods.send(:extend, new_module)
23
+ end
24
+
25
+ def self.can_fake(*names, &block)
26
+ case (names.last)
27
+ when Hash
28
+ options = names.pop
29
+ end
30
+
31
+ if (options and options[:with])
32
+ block = options[:with]
33
+ end
34
+
35
+ FakeMethods.send(
36
+ :extend,
37
+ names.inject(Module.new) do |m, name|
38
+ m.send(:define_method, name, &block)
39
+ m
40
+ end
41
+ )
42
+ end
43
+
44
+ def self.config(&block)
45
+ RailsModelFaker.instance_eval(&block)
46
+ end
47
+
48
+ def self.include(addon)
49
+ RailsModelFaker.send(:extend, addon)
50
+ end
51
+
52
+ module FakeMethods
53
+ # Placeholder for generic fake methods
54
+ end
55
+
56
+ module ClassMethods
57
+ def fake_field_config
58
+ @rmf_can_fake ||= { }
59
+ end
60
+
61
+ def can_fake(*names, &block)
62
+ options = nil
63
+
64
+ case (names.last)
65
+ when Hash
66
+ options = names.pop
67
+ end
68
+
69
+ if (options and options[:with])
70
+ block = options[:with]
71
+ end
72
+
73
+ @rmf_can_fake ||= { }
74
+ @rmf_can_fake_order ||= [ ]
75
+
76
+ names.flatten.each do |name|
77
+ name = name.to_sym
78
+
79
+ # For associations, delay creation of block until first call
80
+ # to allow for additional relationships to be defined after
81
+ # the can_fake call. Leave placeholder (true) instead.
82
+
83
+ @rmf_can_fake[name] = block || true
84
+ @rmf_can_fake_order << name
85
+ end
86
+ end
87
+
88
+ def can_fake?(*names)
89
+ @rmf_can_fake ||= { }
90
+
91
+ names.flatten.reject do |name|
92
+ @rmf_can_fake.key?(name)
93
+ end.empty?
94
+ end
95
+
96
+ def build_fake(params = nil)
97
+ model = new(RailsModelFaker.combine_create_params(scope(:create), params))
98
+
99
+ yield(model) if (block_given?)
100
+
101
+ self.execute_fake_operation(model, params)
102
+
103
+ model
104
+ end
105
+
106
+ def create_fake(params = nil, &block)
107
+ model = build_fake(params, &block)
108
+
109
+ model.save
110
+
111
+ model
112
+ end
113
+
114
+ def create_fake!(params = nil, &block)
115
+ model = build_fake(params, &block)
116
+
117
+ model.save!
118
+
119
+ model
120
+ end
121
+
122
+ def fake(name, params = nil)
123
+ params = RailsModelFaker.combine_create_params(scope(:create), params)
124
+
125
+ fake_method_call(nil, params, fake_method(name))
126
+ end
127
+
128
+ def fake_params(params = nil)
129
+ params = RailsModelFaker.combine_create_params(scope(:create), params)
130
+
131
+ @rmf_can_fake_order.each do |field|
132
+ unless (params.key?(field))
133
+ result = fake(field, params)
134
+
135
+ case (result)
136
+ when nil, params
137
+ # Declined to populate parameters if method returns nil
138
+ # or returns the existing parameter set.
139
+ else
140
+ params[field] = result
141
+ end
142
+ end
143
+ end
144
+
145
+ params
146
+ end
147
+
148
+ def execute_fake_operation(model, params = nil)
149
+ @rmf_can_fake_order.each do |name|
150
+ 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)))
153
+ end
154
+ 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)))
157
+ end
158
+ end
159
+ end
160
+
161
+ model
162
+ end
163
+
164
+ protected
165
+ def fake_method_call(model, params, block)
166
+ case (block.arity)
167
+ when 2, -1
168
+ block.call(model, params)
169
+ when 1
170
+ block.call(model)
171
+ else
172
+ block.call
173
+ end
174
+ end
175
+
176
+ def fake_method(name)
177
+ name = name.to_sym
178
+
179
+ block = @rmf_can_fake[name]
180
+
181
+ case (block)
182
+ when Module
183
+ block.method(name)
184
+ when Symbol
185
+ FakeMethods.method(name)
186
+ when true
187
+ # Configure association faker the first time it is called
188
+ if (reflection = reflect_on_association(name))
189
+ primary_key = reflection.primary_key_name.to_sym
190
+
191
+ @rmf_can_fake[name] =
192
+ lambda do |model, params|
193
+ (params and params.key?(primary_key)) ? nil : reflection.klass.send(:create_fake)
194
+ end
195
+ else
196
+ raise "Cannot fake unknown relationship #{name}"
197
+ end
198
+ else
199
+ block
200
+ end
201
+ end
202
+ end
203
+
204
+ module InstanceMethods
205
+ def fake!(params = nil)
206
+ self.class.execute_fake_operation(self, params)
207
+ end
208
+ end
209
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+
7
+ require 'test_dummy'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTestDummy < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_dummy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - tadman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-31 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: 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.
22
+ email: github@tadman.ca
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/test_dummy.rb
38
+ - test/helper.rb
39
+ - test/test_test_dummy.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/tadman/test_dummy
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Quick test data generator and fake model maker
72
+ test_files:
73
+ - test/helper.rb
74
+ - test/test_test_dummy.rb