test_dummy 0.2.10 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ gem 'rails'
4
+
5
+ group :development do
6
+ gem 'jeweler'
7
+ gem 'sqlite3'
8
+ end
data/README.md ADDED
@@ -0,0 +1,51 @@
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
+ After installing the gem, methods to declare how to fake data are made
7
+ available within ActiveRecord-derived models. There are several ways to
8
+ declare how to dummy something:
9
+
10
+ ```
11
+ class MyModel < ActiveRecord::Base
12
+ # Pass a block that defines how to dummy this attribute
13
+ dummy :name do
14
+ "user%d" % rand(10e6)
15
+ end
16
+
17
+ # Pass a block that defines how to dummy several attributes
18
+ dummy :password, :password_confirmation do
19
+ 'tester'
20
+ end
21
+
22
+ # Use one of the pre-defined helper methods to dummy this attribute
23
+ dummy :nickname, :use => :random_phonetic_string
24
+ end
25
+ ```
26
+
27
+ Dummy configuration can also be stored in test/dummy to avoid cluttering
28
+ your models up with generators. An equivalent external declaration would look
29
+ like this:
30
+
31
+ ```ruby
32
+ TestDummy.declare(MyModel) do
33
+ dummy :name do
34
+ "user%d" % rand(10e6)
35
+ end
36
+
37
+ dummy :password, :password_confirmation do
38
+ 'tester'
39
+ end
40
+
41
+ dummy :nickname, :use => :random_phonetic_string
42
+ end
43
+ ```
44
+
45
+ The name of the test/dummy file should be the same as the main model
46
+ defined in app/models. For instance, app/models/my_model.rb would have a
47
+ corresponding test/dummy/my_model.rb that would be loaded as required.
48
+
49
+ == Copyright
50
+
51
+ Copyright (c) 2010-2012 Scott Tadman, The Working Group
data/Rakefile CHANGED
@@ -23,38 +23,4 @@ Rake::TestTask.new(:test) do |test|
23
23
  test.verbose = true
24
24
  end
25
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, "test:db:prepare" ]
40
-
41
26
  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
52
-
53
- namespace :test do
54
- namespace :db do
55
- desc "Prepares the test Sqlite3 database"
56
- task :prepare do
57
- # ...
58
- end
59
- end
60
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.10
1
+ 0.3.0
@@ -1,13 +1,27 @@
1
1
  module TestDummy::Helper
2
- ALPHANUMERIC_SET = [ 'a'..'z', 'A'..'Z', '0'..'9' ].collect(&:to_a).flatten.freeze
2
+ ALPHANUMERIC_SET = [
3
+ 'a'..'z', 'A'..'Z', '0'..'9'
4
+ ].collect(&:to_a).flatten.freeze
3
5
 
4
6
  # There are certain substrings that shouldn't be emitted as part of the
5
7
  # random strings in order to keep them safe and inoffensive. While this is
6
8
  # not exhaustive, it should scrub the random strings enough that it would
7
9
  # be a stretch to see something wrong with them, at least in English.
8
10
  AVOID_SUBSTRINGS = Regexp.new(%w[
9
- ass a55 as5 a5s bal bit btc b1t bul bll bls but btu chi ch1 cun cnt dic dik d1c d1k fag f4g fuc fuk fck fcu fkn fkc kik kyk k1k jer jrk j3r jew j3w mot m0t m07 mth m7h mtr m7r neg n3g ngr nig n1g pak p4k pof p0f poo po0 p00 que qu3 qee q3e qe3 shi sh1 shy stf sht sfu spi spk sp1 tar t4r trd wtf wth xxx
10
- ].join('|'))
11
+ ass a55 as5 a5s bal bit btc b1t bul bll bls but btu chi
12
+ ch1 cun cnt dic dik d1c d1k fag f4g fuc fuk fck fcu fkn
13
+ fkc kik kyk k1k jer jrk j3r jew j3w mot m0t m07 mth m7h
14
+ mtr m7r neg n3g ngr nig n1g pak p4k pof p0f poo po0 p00
15
+ que qu3 qee q3e qe3 shi sh1 shy stf sht sfu spi spk sp1
16
+ tar t4r trd wtf wth xxx f1f fif msl mus mlm grl kil kll
17
+ ].join('|')).freeze
18
+
19
+ CONSONANTS = %w[
20
+ b c d f g h j k l m n p qu r s t v w x z ch cr fr
21
+ nd ng nk nt ph pr rd sh sl sp st th tr
22
+ ].freeze
23
+
24
+ VOWELS = %w[ a e i o u y ].freeze
11
25
 
12
26
  def random_string(length = 12)
13
27
  string = nil
@@ -27,9 +41,6 @@ module TestDummy::Helper
27
41
  string
28
42
  end
29
43
 
30
- CONSONANTS = %w[ b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr ]
31
- VOWELS = %w[ a e i o u y ]
32
-
33
44
  def random_phonetic_string(length = 12)
34
45
  string = nil
35
46
 
@@ -6,6 +6,9 @@ when 2
6
6
  if (defined?(ActiveSupport) and defined?(ActiveSupport::TestCase))
7
7
  ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
8
8
  end
9
+ if (defined?(Test) and defined?(Test::Unit))
10
+ Test::Unit::TestCase.send(:include, TestDummy::TestHelper)
11
+ end
9
12
  else
10
13
  class TestDummy::Railtie < Rails::Railtie
11
14
  def self.apply!
@@ -14,6 +17,7 @@ else
14
17
  end
15
18
 
16
19
  ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
20
+ Test::Unit::TestCase.send(:include, TestDummy::TestHelper)
17
21
  end
18
22
 
19
23
  config.before_configuration do
data/lib/test_dummy.rb CHANGED
@@ -4,15 +4,27 @@ module TestDummy
4
4
  require 'test_dummy/railtie'
5
5
  end
6
6
 
7
+ class Exception < ::Exception
8
+ end
9
+
7
10
  autoload(:Helper, File.expand_path('test_dummy/helper', File.dirname(__FILE__)))
8
11
  autoload(:TestHelper, File.expand_path('test_dummy/test_helper', File.dirname(__FILE__)))
9
12
 
10
- def self.dummy_directory
11
- @dummy_directory or (defined?(Rails) and Rails.root)
13
+ # Returns the current path used to load dummy extensions into models, or
14
+ # nil if no path is currently defined. Defaults to "test/dummy" off of the
15
+ # Rails root if Rails is available.
16
+ def self.dummy_extensions_path
17
+ @dummy_extensions_path ||= begin
18
+ if (defined?(Rails))
19
+ File.expand_path('test/dummy', Rails.root)
20
+ else
21
+ nil
22
+ end
23
+ end
12
24
  end
13
25
 
14
- def self.dummy_directory=(value)
15
- @dummy_directory = value
26
+ def self.dummy_extensions_path=(value)
27
+ @dummy_extensions_path = value
16
28
  end
17
29
 
18
30
  def self.included(base)
@@ -45,12 +57,36 @@ module TestDummy
45
57
 
46
58
  combined_attributes
47
59
  end
60
+
61
+ # This method is used to provide a unified interface to the otherwise
62
+ # irregular methods to discover information on assocations. Rails 3
63
+ # introduces a new method. Returns the reflected class and foreign key
64
+ # properties for a named attribute, or nil if no association could be found.
65
+ def self.reflection_properties(model_class, attribute)
66
+ if (model_class.respond_to?(:reflect_on_association) and reflection = model_class.reflect_on_association(attribute))
67
+ [
68
+ reflection.klass,
69
+ (reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name).to_sym
70
+ ]
71
+ elsif (model_class.respond_to?(:association_reflection) and reflection = model_class.association_reflection(attribute))
72
+ [
73
+ reflection[:associated_class],
74
+ reflection[:key] || :"#{attribute.to_s.underscore}_id"
75
+ ]
76
+ end
77
+ end
48
78
  end
49
79
 
50
80
  # Adds a mixin to the core Helper module
51
81
  def self.add_module(new_module)
52
82
  Helper.send(:extend, new_module)
53
83
  end
84
+
85
+ # Used to configure defaults or aliases that can be used by all definitions.
86
+ # Takes a block that should call definition methods like `dummy`.
87
+ def self.define(&block)
88
+ instance_eval(&block)
89
+ end
54
90
 
55
91
  # Used in an initializer to define things that can be dummied by all
56
92
  # models if these properties are available.
@@ -61,18 +97,22 @@ module TestDummy
61
97
  end
62
98
 
63
99
  if (options and options[:with])
64
- block = options[:with]
65
- end
100
+ with = options[:with]
66
101
 
67
- # Create a temporary Module and use this to roll up the methods defined
68
- # into the Helper module
69
- Helper.send(
70
- :extend,
71
- names.inject(Module.new) do |m, name|
72
- m.send(:define_method, name, &block)
73
- m
102
+ names.each do |name|
103
+ if (Helper.respond_to?(with))
104
+ Helper.send(:alias_method, name, with)
105
+ else
106
+ Helper.send(:define_method, name) do
107
+ send(with)
108
+ end
109
+ end
110
+ end
111
+ else
112
+ names.each do |name|
113
+ Helper.send(:define_method, name, &block)
74
114
  end
75
- )
115
+ end
76
116
  end
77
117
 
78
118
  # Used in an initializer to define configuration parameters.
@@ -99,15 +139,103 @@ module TestDummy
99
139
  options = names.pop
100
140
  end
101
141
 
102
- if (options and options[:with])
103
- block = options[:with]
104
- end
105
-
106
142
  @test_dummy ||= { }
107
143
  @test_dummy_order ||= [ ]
108
144
 
109
145
  names.flatten.each do |name|
110
146
  name = name.to_sym
147
+ from = nil
148
+ create_options_proc = nil
149
+
150
+ if (options)
151
+ if (options[:with])
152
+ if (block)
153
+ raise TestDummy::Exception, "Cannot use block and :with option at the same time."
154
+ end
155
+
156
+ block =
157
+ case (with = options[:with])
158
+ when Proc
159
+ with
160
+ when String, Symbol
161
+ lambda { send(with) }
162
+ else
163
+ lambda { with }
164
+ end
165
+ end
166
+
167
+ # The :inherit directive is used to pass arguments through to the
168
+ # create_dummy call on the association's class.
169
+ if (inherit = options[:inherit])
170
+ inherit_options = Hash[
171
+ inherit.collect do |attribute, spec|
172
+ [
173
+ attribute.to_sym,
174
+ case (spec)
175
+ when Array
176
+ spec.collect(&:to_sym)
177
+ when String
178
+ spec.split('.').collect(&:to_sym)
179
+ when Proc
180
+ spec
181
+ end
182
+ ]
183
+ end
184
+ ]
185
+
186
+ create_options_proc = lambda do |target, model, with_attributes|
187
+ inherit_options.each do |attribute, spec|
188
+ target[attribute] ||=
189
+ case (spec)
190
+ when Array
191
+ spec.inject(model) do |_model, _method|
192
+ _model ? _model.send(_method) : nil
193
+ end
194
+ when Proc
195
+ proc.call(model, with_attributes)
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ if (from = options[:from])
202
+ if (block)
203
+ raise TestDummy::Exception, "Cannot use block, :with, or :from option at the same time."
204
+ end
205
+
206
+ case (from)
207
+ when Array
208
+ # Already in correct form
209
+ when Hash
210
+ from = from.to_a
211
+ when String
212
+ from = from.split('.')
213
+ else
214
+ raise TestDummy::Exception, "Argument to :from must be a String, Array or Hash."
215
+ end
216
+ end
217
+
218
+ block = lambda do |model, with_attributes|
219
+ reflection_class, foreign_key = TestDummy::Support.reflection_properties(self, name)
220
+
221
+ if (reflection_class and foreign_key)
222
+ unless ((with_attributes and (with_attributes.key?(name) or with_attributes.key?(foreign_key))) or model.send(name).present?)
223
+ object = from && from.inject(model) do |_model, _method|
224
+ _model ? _model.send(_method) : nil
225
+ end
226
+
227
+ object ||=
228
+ reflection_class.create_dummy(with_attributes) do |target|
229
+ if (create_options_proc)
230
+ create_options_proc.call(target, model, with_attributes)
231
+ end
232
+ end
233
+
234
+ model.send(:"#{name}=", object)
235
+ end
236
+ end
237
+ end
238
+ end
111
239
 
112
240
  # For associations, delay creation of block until first call
113
241
  # to allow for additional relationships to be defined after
@@ -136,7 +264,7 @@ module TestDummy
136
264
  load_dummy_declaration!
137
265
 
138
266
  build_scope = (method(:scoped).arity == 1) ? scoped(nil).scope(:create) : scoped.scope_for_create
139
-
267
+
140
268
  model = new(TestDummy::Support.combine_attributes(build_scope, with_attributes))
141
269
 
142
270
  yield(model) if (block_given?)
@@ -210,8 +338,16 @@ module TestDummy
210
338
  return model unless (@test_dummy_order)
211
339
 
212
340
  @test_dummy_order.each do |name|
213
- if (reflection = reflect_on_association(name))
214
- unless ((with_attributes and with_attributes.key?(name.to_sym)) or model.send(name).present?)
341
+ if (respond_to?(:reflect_on_association) and reflection = reflect_on_association(name))
342
+ foreign_key = (reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name).to_sym
343
+
344
+ unless ((with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(foreign_key.to_sym))) or model.send(name).present?)
345
+ model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name)))
346
+ end
347
+ elsif (respond_to?(:association_reflection) and reflection = association_reflection(name))
348
+ key = reflection[:key] || :"#{name.to_s.underscore}_id"
349
+
350
+ unless ((with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(key.to_sym))) or model.send(name).present?)
215
351
  model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name)))
216
352
  end
217
353
  else
@@ -230,15 +366,13 @@ module TestDummy
230
366
 
231
367
  @_dummy_module =
232
368
  begin
233
- dummy_path = File.expand_path(
234
- "test/dummy/#{name.underscore}.rb",
235
- TestDummy.dummy_directory
236
- )
369
+ dummy_path = File.expand_path("#{name.underscore}.rb", TestDummy.dummy_extensions_path)
237
370
 
238
371
  if (File.exist?(dummy_path))
239
- load dummy_path
372
+ load(dummy_path)
240
373
  end
241
374
  rescue LoadError
375
+ # Persist that this load attempt failed and don't retry later.
242
376
  false
243
377
  end
244
378
  end
@@ -265,14 +399,25 @@ module TestDummy
265
399
  when Symbol
266
400
  Helper.method(name)
267
401
  when true
268
- # Configure association dummyr the first time it is called
269
- if (reflection = reflect_on_association(name))
402
+ # Configure association dummy the first time it is called
403
+ if (respond_to?(:reflect_on_association) and reflection = reflect_on_association(name))
270
404
  foreign_key = (reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name).to_sym
271
405
 
272
406
  @test_dummy[name] =
273
407
  lambda do |model, with_attributes|
274
- (with_attributes and with_attributes.key?(foreign_key)) ? nil : reflection.klass.send(:create_dummy)
408
+ (with_attributes and (with_attributes.key?(foreign_key) or with_attributes.key?(name))) ? nil : reflection.klass.send(:create_dummy)
409
+ end
410
+ elsif (respond_to?(:association_reflection) and reflection = association_reflection(name))
411
+ key = reflection[:key] || :"#{name.to_s.underscore}_id"
412
+
413
+ @test_dummy[name] =
414
+ lambda do |model, with_attributes|
415
+ (with_attributes and (with_attributes.key?(key) or with_attributes.key?(name))) ? nil : reflection[:associated_class].send(:create_dummy)
275
416
  end
417
+ elsif (TestDummy::Helper.respond_to?(name))
418
+ @test_dummy[name] = lambda do |model, with_attributes|
419
+ TestDummy::Helper.send(name)
420
+ end
276
421
  else
277
422
  raise "Cannot dummy unknown relationship #{name}"
278
423
  end
@@ -0,0 +1,8 @@
1
+ class CreateExamples < ActiveRecord::Migration
2
+ def change
3
+ create_table :examples do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class CreateAccounts < ActiveRecord::Migration
2
+ def change
3
+ create_table :accounts do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class CreateBills < ActiveRecord::Migration
2
+ def change
3
+ create_table :bills do |t|
4
+ t.integer :account_id
5
+ t.date :order_date
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class CreateItems < ActiveRecord::Migration
2
+ def change
3
+ create_table :items do |t|
4
+ t.integer :account_id
5
+ t.integer :bill_id
6
+ t.string :description
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -1,4 +1,4 @@
1
- class Simple < ActiveRecord::Base
1
+ class Account
2
2
  dummy :name do
3
3
  TestDummy::Helper.random_string(8)
4
4
  end
@@ -0,0 +1,7 @@
1
+ class Bill
2
+ dummy :account
3
+
4
+ dummy :order_date do
5
+ Date.today.advance(:days => rand(-2000))
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ class Item
2
+ dummy :account,
3
+ :from => 'bill.account'
4
+
5
+ dummy :bill,
6
+ :inherit => {
7
+ :account_id => [ :account, :id ]
8
+ }
9
+
10
+ dummy :description
11
+ end
data/test/helper.rb CHANGED
@@ -3,10 +3,14 @@ require 'test/unit'
3
3
 
4
4
  ENV['RAILS_ENV'] = 'test'
5
5
 
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ base_path = File.dirname(__FILE__)
7
+
8
+ $LOAD_PATH.unshift(base_path)
9
+ $LOAD_PATH.unshift(File.join(base_path, '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.join(base_path, '..', 'test', 'models'))
8
11
 
9
12
  gem 'rails'
13
+
10
14
  require 'rails'
11
15
  require 'active_record'
12
16
 
@@ -15,13 +19,32 @@ require 'test_dummy'
15
19
  TestDummy::Railtie.apply!
16
20
 
17
21
  class Test::Unit::TestCase
22
+ include TestDummy::TestHelper
18
23
  end
19
24
 
20
- module TestDummy
21
- class Application < Rails::Application
22
- config.active_support.deprecation = :warn
23
- end
25
+ class TestDummy::Application < Rails::Application
26
+ config.active_support.deprecation = :warn
24
27
  end
25
28
 
26
- TestDummy::Application.initialize!
27
- ActiveRecord::Base.establish_connection(Rails.env)
29
+ TestDummy.dummy_extensions_path = File.expand_path('dummy', File.dirname(__FILE__))
30
+
31
+ ActiveRecord::Base.establish_connection(
32
+ 'adapter' => "sqlite3",
33
+ 'database' => File.expand_path('db/test.sqlite3', base_path)
34
+ )
35
+
36
+ ActiveRecord::Migrator.migrate(File.expand_path('db/migrate', base_path))
37
+
38
+ ActiveSupport::Dependencies.autoload_paths << File.expand_path('models', base_path)
39
+
40
+ # Trigger loading the Rails root path here
41
+ Rails.root
42
+
43
+ TestDummy.define do
44
+ dummy :description,
45
+ :with => :phonetic_string
46
+
47
+ dummy :phonetic_string do
48
+ TestDummy::Helper.random_phonetic_string(32)
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ class Account < ActiveRecord::Base
2
+ attr_accessible :name
3
+
4
+ validates :name,
5
+ :presence => true
6
+
7
+ has_many :bills
8
+ has_many :items,
9
+ :through => :bills
10
+ end
@@ -0,0 +1,4 @@
1
+ class Bill < ActiveRecord::Base
2
+ belongs_to :account
3
+ has_many :items
4
+ end
@@ -0,0 +1,5 @@
1
+ class Example < ActiveRecord::Base
2
+ dummy :name do
3
+ TestDummy::Helper.random_string(8)
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ class Item < ActiveRecord::Base
2
+ belongs_to :account
3
+ belongs_to :bill
4
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../helper', File.dirname(__FILE__))
2
+
3
+ class TestAccount < ActiveSupport::TestCase
4
+ def test_extension_loaded
5
+ assert Account.respond_to?(:create_dummy)
6
+ end
7
+
8
+ def test_create_dummy
9
+ account = Account.create_dummy
10
+
11
+ assert account
12
+
13
+ assert account.name?
14
+
15
+ assert_equal true, account.valid?
16
+ assert_equal false, account.new_record?
17
+
18
+ assert_equal [ ], account.bills.collect(&:ids)
19
+ assert_equal [ ], account.items.collect(&:ids)
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../helper', File.dirname(__FILE__))
2
+
3
+ class TestBill < ActiveSupport::TestCase
4
+ def test_extension_loaded
5
+ assert Bill.respond_to?(:create_dummy)
6
+ end
7
+
8
+ def test_create_dummy
9
+ bill = Bill.create_dummy
10
+
11
+ assert bill
12
+
13
+ assert bill.order_date
14
+
15
+ assert_equal true, bill.valid?
16
+ assert_equal false, bill.new_record?
17
+
18
+ assert bill.account
19
+ assert_equal false, bill.account.new_record?
20
+
21
+ assert_equal [ ], bill.items.collect(&:ids)
22
+ end
23
+
24
+ def test_create_dummy_via_association
25
+ account = an Account
26
+
27
+ bill = one_of account.bills
28
+
29
+ assert bill
30
+ assert_equal true, bill.valid?
31
+ assert_equal false, bill.new_record?
32
+
33
+ assert_equal account.id, bill.account_id
34
+
35
+ assert_equal [ bill.id ], account.bills.collect(&:id)
36
+ end
37
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../helper', File.dirname(__FILE__))
2
+
3
+ class TestItem < ActiveSupport::TestCase
4
+ def test_extension_loaded
5
+ assert Item.respond_to?(:create_dummy)
6
+ end
7
+
8
+ def test_reflection_properties
9
+ reflection_class, foreign_key = TestDummy::Support.reflection_properties(Item, :account)
10
+
11
+ assert_equal Account, reflection_class
12
+ assert_equal :account_id, foreign_key
13
+ end
14
+
15
+ def test_create_dummy
16
+ item = Item.create_dummy
17
+
18
+ assert item
19
+
20
+ assert item.description?
21
+
22
+ assert_equal true, item.valid?
23
+ assert_equal false, item.new_record?
24
+
25
+ assert item.account
26
+ assert_equal false, item.account.new_record?
27
+ assert_equal item.account.id, item.account_id
28
+
29
+ assert item.bill
30
+ assert_equal false, item.bill.new_record?
31
+ assert_equal item.bill.id, item.bill_id
32
+ assert_equal item.account.id, item.bill.account_id
33
+
34
+ assert_equal [ item.id ], item.account.items.collect(&:id)
35
+ assert_equal [ item.id ], item.bill.items.collect(&:id)
36
+ end
37
+
38
+ def test_create_dummy_via_association
39
+ bill = a Bill
40
+
41
+ assert_equal false, bill.new_record?
42
+
43
+ item = one_of bill.items
44
+
45
+ assert item
46
+ assert_equal true, item.valid?
47
+ assert_equal false, item.new_record?
48
+
49
+ assert_equal bill.id, item.bill_id
50
+
51
+ assert_equal bill.account_id, item.account_id
52
+
53
+ account = bill.account
54
+
55
+ assert_equal [ bill.id ], account.bills.collect(&:id)
56
+ assert_equal [ item.id ], account.items.collect(&:id)
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../helper', File.dirname(__FILE__))
2
+
3
+ require 'example'
4
+
5
+ class TestTestDummy < Test::Unit::TestCase
6
+ def test_example_model
7
+ example = Example.create_dummy
8
+
9
+ assert_equal Example, example.class
10
+
11
+ assert_equal [ ], example.errors.full_messages
12
+ assert !example.new_record?
13
+
14
+ assert_equal 8, example.name.length
15
+ end
16
+
17
+ def test_extensions
18
+ assert_equal true, respond_to?(:a)
19
+ assert_equal true, respond_to?(:an)
20
+ assert_equal true, respond_to?(:one_of)
21
+ end
22
+ end
data/test_dummy.gemspec CHANGED
@@ -5,46 +5,68 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "test_dummy"
8
- s.version = "0.2.10"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["tadman"]
12
- s.date = "2012-03-06"
12
+ s.date = "2012-11-30"
13
13
  s.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."
14
14
  s.email = "github@tadman.ca"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ "Gemfile",
21
22
  "LICENSE",
22
- "README.rdoc",
23
+ "README.md",
23
24
  "Rakefile",
24
25
  "VERSION",
25
26
  "lib/test_dummy.rb",
26
27
  "lib/test_dummy/helper.rb",
27
28
  "lib/test_dummy/railtie.rb",
28
29
  "lib/test_dummy/test_helper.rb",
29
- "test/config/database.yml",
30
30
  "test/db/.gitignore",
31
+ "test/db/migrate/0001_create_examples.rb",
32
+ "test/db/migrate/0002_create_accounts.rb",
33
+ "test/db/migrate/0003_create_bills.rb",
34
+ "test/db/migrate/0004_create_items.rb",
35
+ "test/dummy/account.rb",
36
+ "test/dummy/bill.rb",
37
+ "test/dummy/item.rb",
31
38
  "test/helper.rb",
32
- "test/models/simple.rb",
33
- "test/test_test_dummy.rb",
39
+ "test/models/account.rb",
40
+ "test/models/bill.rb",
41
+ "test/models/example.rb",
42
+ "test/models/item.rb",
43
+ "test/unit/test_account.rb",
44
+ "test/unit/test_bill.rb",
45
+ "test/unit/test_item.rb",
46
+ "test/unit/test_test_dummy.rb",
34
47
  "test_dummy.gemspec"
35
48
  ]
36
49
  s.homepage = "http://github.com/tadman/test_dummy"
37
50
  s.require_paths = ["lib"]
38
- s.rubygems_version = "1.8.11"
51
+ s.rubygems_version = "1.8.24"
39
52
  s.summary = "Quick test data generator and fake model maker"
40
53
 
41
54
  if s.respond_to? :specification_version then
42
55
  s.specification_version = 3
43
56
 
44
57
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<rails>, [">= 0"])
59
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
60
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
45
61
  else
62
+ s.add_dependency(%q<rails>, [">= 0"])
63
+ s.add_dependency(%q<jeweler>, [">= 0"])
64
+ s.add_dependency(%q<sqlite3>, [">= 0"])
46
65
  end
47
66
  else
67
+ s.add_dependency(%q<rails>, [">= 0"])
68
+ s.add_dependency(%q<jeweler>, [">= 0"])
69
+ s.add_dependency(%q<sqlite3>, [">= 0"])
48
70
  end
49
71
  end
50
72
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_dummy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-06 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  description: Test Dummy allows you to define how to fake models automatically so that
15
63
  you can use dummy data for testing instead of fixtures. Dummy models are always
16
64
  generated using the current schema and don't need to me migrated like fixtures.
@@ -19,22 +67,35 @@ executables: []
19
67
  extensions: []
20
68
  extra_rdoc_files:
21
69
  - LICENSE
22
- - README.rdoc
70
+ - README.md
23
71
  files:
24
72
  - .document
73
+ - Gemfile
25
74
  - LICENSE
26
- - README.rdoc
75
+ - README.md
27
76
  - Rakefile
28
77
  - VERSION
29
78
  - lib/test_dummy.rb
30
79
  - lib/test_dummy/helper.rb
31
80
  - lib/test_dummy/railtie.rb
32
81
  - lib/test_dummy/test_helper.rb
33
- - test/config/database.yml
34
82
  - test/db/.gitignore
83
+ - test/db/migrate/0001_create_examples.rb
84
+ - test/db/migrate/0002_create_accounts.rb
85
+ - test/db/migrate/0003_create_bills.rb
86
+ - test/db/migrate/0004_create_items.rb
87
+ - test/dummy/account.rb
88
+ - test/dummy/bill.rb
89
+ - test/dummy/item.rb
35
90
  - test/helper.rb
36
- - test/models/simple.rb
37
- - test/test_test_dummy.rb
91
+ - test/models/account.rb
92
+ - test/models/bill.rb
93
+ - test/models/example.rb
94
+ - test/models/item.rb
95
+ - test/unit/test_account.rb
96
+ - test/unit/test_bill.rb
97
+ - test/unit/test_item.rb
98
+ - test/unit/test_test_dummy.rb
38
99
  - test_dummy.gemspec
39
100
  homepage: http://github.com/tadman/test_dummy
40
101
  licenses: []
@@ -56,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
117
  version: '0'
57
118
  requirements: []
58
119
  rubyforge_project:
59
- rubygems_version: 1.8.11
120
+ rubygems_version: 1.8.24
60
121
  signing_key:
61
122
  specification_version: 3
62
123
  summary: Quick test data generator and fake model maker
data/README.rdoc DELETED
@@ -1,52 +0,0 @@
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
- After installing the gem, methods to declare how to fake data are made
7
- available within ActiveRecord-derived models. There are several ways to
8
- declare how to dummy something:
9
-
10
- class MyModel < ActiveRecord::Base
11
- # Pass a block that defines how to dummy this attribute
12
- dummy :name do
13
- "user%d" % rand(10e6)
14
- end
15
-
16
- # Pass a block that defines how to dummy several attributes
17
- dummy :password, :password_confirmation do
18
- 'tester'
19
- end
20
-
21
- # Use one of the pre-defined helper methods to dummy this attribute
22
- dummy :nickname, :use => :random_phonetic_string
23
- end
24
-
25
- Dummy configuration can also be stored in test/dummy to avoid cluttering
26
- your models up with generators. An equivalent external declaration would look
27
- like this:
28
-
29
- TestDummy.declare(MyModel) do
30
- dummy :name do
31
- "user%d" % rand(10e6)
32
- end
33
-
34
- dummy :password, :password_confirmation do
35
- 'tester'
36
- end
37
-
38
- dummy :nickname, :use => :random_phonetic_string
39
- end
40
-
41
- The name of the test/dummy file should be the same as the main model
42
- defined in app/models. For instance, app/models/my_model.rb would have a
43
- corresponding test/dummy/my_model.rb that would be loaded as required.
44
-
45
- If you need to override this directory, you can configure the gem using
46
- the `dummy_directory` option:
47
-
48
- TestDummy.dummy_directory = 'my_alternate/path'
49
-
50
- == Copyright
51
-
52
- Copyright (c) 2010-2011 Scott Tadman, The Working Group
@@ -1,5 +0,0 @@
1
- test:
2
- adapter: sqlite3
3
- database: db/test.sqlite3
4
- pool: 5
5
- timeout: 5000
@@ -1,16 +0,0 @@
1
- require 'helper'
2
-
3
- require 'models/simple'
4
-
5
- class TestTestDummy < Test::Unit::TestCase
6
- def test_simple_model
7
- simple = Simple.dummy
8
-
9
- assert_equal Simple, simple.class
10
-
11
- assert_equal [ ], simple.errors.full_messages
12
- assert !simple.new_record?
13
-
14
- assert_equal 8, simple.name.length
15
- end
16
- end