test_dummy 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +10 -1
- data/VERSION +1 -1
- data/lib/test_dummy/helper.rb +50 -0
- data/lib/test_dummy/railtie.rb +7 -4
- data/lib/test_dummy/test_helper.rb +17 -0
- data/lib/test_dummy.rb +36 -33
- data/test/config/database.yml +5 -0
- data/test/db/.gitignore +1 -0
- data/test/helper.rb +4 -0
- data/test/models/simple.rb +5 -0
- data/test/test_test_dummy.rb +7 -2
- data/test_dummy.gemspec +7 -1
- metadata +8 -2
data/Rakefile
CHANGED
@@ -36,7 +36,7 @@ rescue LoadError
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
task :test => :check_dependencies
|
39
|
+
task :test => [ :check_dependencies, "test:db:prepare" ]
|
40
40
|
|
41
41
|
task :default => :test
|
42
42
|
|
@@ -49,3 +49,12 @@ Rake::RDocTask.new do |rdoc|
|
|
49
49
|
rdoc.rdoc_files.include('README*')
|
50
50
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
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.1.
|
1
|
+
0.1.3
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module TestDummy::Helper
|
2
|
+
ALPHANUMERIC_SET = [ 'a'..'z', 'A'..'Z', '0'..'9' ].collect(&:to_a).flatten.freeze
|
3
|
+
|
4
|
+
# There are certain substrings that shouldn't be emitted as part of the
|
5
|
+
# random strings in order to keep them safe and inoffensive. While this is
|
6
|
+
# not exhaustive, it should scrub the random strings enough that it would
|
7
|
+
# be a stretch to see something wrong with them, at least in English.
|
8
|
+
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
|
+
|
12
|
+
def random_string(length = 12)
|
13
|
+
string = nil
|
14
|
+
|
15
|
+
while (!string or !AVOID_WORDS.match(string))
|
16
|
+
string = ''
|
17
|
+
|
18
|
+
length.times do
|
19
|
+
string << CHARACTER_SET.rand
|
20
|
+
end
|
21
|
+
|
22
|
+
# As it's not especially hard to find a random word that passes this
|
23
|
+
# simple filter for relatively short strings, the chance of this loop
|
24
|
+
# spinning for an extended period of time is very slim.
|
25
|
+
end
|
26
|
+
|
27
|
+
string
|
28
|
+
end
|
29
|
+
|
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
|
+
def random_phonetic_string(length = 12)
|
34
|
+
string = nil
|
35
|
+
|
36
|
+
while (!string or !AVOID_WORDS.match(string))
|
37
|
+
string = ''
|
38
|
+
|
39
|
+
length.times do |i|
|
40
|
+
word << (i % 2 != 0) ? CONSONANTS[rand(CONSONANTS.size)] : VOWELS[rand(VOWELS.size)]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# The generated string is probably longer than it needs to be, so trim
|
45
|
+
# to fit.
|
46
|
+
string.to_s[0, length]
|
47
|
+
end
|
48
|
+
|
49
|
+
extend(self)
|
50
|
+
end
|
data/lib/test_dummy/railtie.rb
CHANGED
@@ -3,14 +3,17 @@ when 2
|
|
3
3
|
if (defined?(ActiveRecord) and defined?(ActiveRecord::Base))
|
4
4
|
ActiveRecord::Base.send(:include, TestDummy)
|
5
5
|
end
|
6
|
+
if (defined?(ActiveSupport) and defined?(ActiveSupport::TestCase))
|
7
|
+
ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
|
8
|
+
end
|
6
9
|
else
|
7
10
|
class TestDummy::Railtie < Rails::Railtie
|
8
|
-
|
9
|
-
|
10
|
-
config.after_initialize do
|
11
|
-
if (defined?(ActiveRecord) and defined?(ActiveRecord::Base))
|
11
|
+
config.before_configuration do
|
12
|
+
if (defined?(ActiveRecord))
|
12
13
|
ActiveRecord::Base.send(:include, TestDummy)
|
13
14
|
end
|
15
|
+
|
16
|
+
ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module TestDummy::TestHelper
|
2
|
+
def dummy(scope, options = { })
|
3
|
+
instance = scope.respond_to?(:build) ? scope.build(options) : scope.new(options)
|
4
|
+
|
5
|
+
if (block_given?)
|
6
|
+
yield(instance)
|
7
|
+
end
|
8
|
+
|
9
|
+
instance.dummy!
|
10
|
+
|
11
|
+
instance.save!
|
12
|
+
instance
|
13
|
+
end
|
14
|
+
alias_method :a, :dummy
|
15
|
+
alias_method :an, :dummy
|
16
|
+
alias_method :one_of, :dummy
|
17
|
+
end
|
data/lib/test_dummy.rb
CHANGED
@@ -1,40 +1,45 @@
|
|
1
|
-
require 'test_dummy/railtie'
|
2
|
-
|
3
1
|
module TestDummy
|
2
|
+
require 'test_dummy/railtie'
|
3
|
+
|
4
|
+
autoload(:Helper, File.expand_path('test_dummy/helper', File.dirname(__FILE__)))
|
5
|
+
autoload(:TestHelper, File.expand_path('test_dummy/test_helper', File.dirname(__FILE__)))
|
6
|
+
|
4
7
|
def self.included(base)
|
5
8
|
base.send(:extend, ClassMethods)
|
6
9
|
base.send(:include, InstanceMethods)
|
7
10
|
end
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
module Support
|
13
|
+
# Combines several sets of parameters together into a single set in order
|
14
|
+
# of lowest priority to highest priority. Supplied list can contain nil
|
15
|
+
# values which will be ignored. Returns a Hash with symbolized keys.
|
16
|
+
def self.combine_attributes(*sets)
|
17
|
+
combined_attributes = { }
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
# Apply sets in order they are listed
|
20
|
+
sets.compact.each do |set|
|
21
|
+
set.each do |k, v|
|
22
|
+
case (v)
|
23
|
+
when nil
|
24
|
+
# Ignore nil assignments
|
25
|
+
else
|
26
|
+
combined_attributes[k.to_sym] = v
|
27
|
+
end
|
23
28
|
end
|
24
29
|
end
|
25
|
-
end
|
26
30
|
|
27
|
-
|
31
|
+
combined_attributes
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
|
-
# Adds a mixin to the core
|
35
|
+
# Adds a mixin to the core Helper module
|
31
36
|
def self.add_module(new_module)
|
32
|
-
|
37
|
+
Helper.send(:extend, new_module)
|
33
38
|
end
|
34
39
|
|
35
|
-
# Used in an initializer to define things that can be
|
40
|
+
# Used in an initializer to define things that can be dummied by all
|
36
41
|
# models if these properties are available.
|
37
|
-
def self.
|
42
|
+
def self.to_dummy(*names, &block)
|
38
43
|
case (names.last)
|
39
44
|
when Hash
|
40
45
|
options = names.pop
|
@@ -44,7 +49,9 @@ module TestDummy
|
|
44
49
|
block = options[:with]
|
45
50
|
end
|
46
51
|
|
47
|
-
|
52
|
+
# Create a temporary Module and use this to roll up the methods defined
|
53
|
+
# into the Helper module
|
54
|
+
Helper.send(
|
48
55
|
:extend,
|
49
56
|
names.inject(Module.new) do |m, name|
|
50
57
|
m.send(:define_method, name, &block)
|
@@ -58,10 +65,6 @@ module TestDummy
|
|
58
65
|
TestDummy.instance_eval(&block)
|
59
66
|
end
|
60
67
|
|
61
|
-
module DummyMethods
|
62
|
-
# Container for common data faking methods as they are defined.
|
63
|
-
end
|
64
|
-
|
65
68
|
module ClassMethods
|
66
69
|
# Returns a Hash which describes the dummy configuration for this
|
67
70
|
# Model class.
|
@@ -73,7 +76,7 @@ module TestDummy
|
|
73
76
|
# that can receive up to two parameters, the first the instance of
|
74
77
|
# the model being created, the second the parameters supplied to create
|
75
78
|
# it. The first and second parameters may be nil.
|
76
|
-
def
|
79
|
+
def to_dummy(*names, &block)
|
77
80
|
options = nil
|
78
81
|
|
79
82
|
case (names.last)
|
@@ -93,7 +96,7 @@ module TestDummy
|
|
93
96
|
|
94
97
|
# For associations, delay creation of block until first call
|
95
98
|
# to allow for additional relationships to be defined after
|
96
|
-
# the
|
99
|
+
# the to_dummy call. Leave placeholder (true) instead.
|
97
100
|
|
98
101
|
@test_dummy[name] = block || true
|
99
102
|
@test_dummy_order << name
|
@@ -115,7 +118,7 @@ module TestDummy
|
|
115
118
|
# the dummy operation is completed. Returns a dummy model which has not
|
116
119
|
# been saved.
|
117
120
|
def build_dummy(with_attributes = nil)
|
118
|
-
model = new(
|
121
|
+
model = new(TestDummy::Support.combine_attributes(scoped.scope_for_create, with_attributes))
|
119
122
|
|
120
123
|
yield(model) if (block_given?)
|
121
124
|
|
@@ -140,7 +143,7 @@ module TestDummy
|
|
140
143
|
# Builds a dummy model with some parameters set as supplied. The
|
141
144
|
# new model is provided to the optional block for manipulation before
|
142
145
|
# the dummy operation is completed and the model is saved. Returns a
|
143
|
-
# dummy model. Will throw ActiveRecord::RecordInvalid if there was
|
146
|
+
# dummy model. Will throw ActiveRecord::RecordInvalid if there was al20
|
144
147
|
# validation failure, or ActiveRecord::RecordNotSaved if the save was
|
145
148
|
# blocked by a callback.
|
146
149
|
def create_dummy!(with_attributes = nil, &block)
|
@@ -153,7 +156,7 @@ module TestDummy
|
|
153
156
|
|
154
157
|
# Produces dummy data for a single attribute.
|
155
158
|
def dummy(name, with_attributes = nil)
|
156
|
-
with_attributes = TestDummy.combine_attributes(
|
159
|
+
with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes)
|
157
160
|
|
158
161
|
dummy_method_call(nil, with_attributes, dummy_method(name))
|
159
162
|
end
|
@@ -161,7 +164,7 @@ module TestDummy
|
|
161
164
|
# Produces a complete set of dummy attributes. These can be used to
|
162
165
|
# create a model.
|
163
166
|
def dummy_attributes(with_attributes = nil)
|
164
|
-
with_attributes = TestDummy.combine_attributes(
|
167
|
+
with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes)
|
165
168
|
|
166
169
|
@test_dummy_order.each do |field|
|
167
170
|
unless (with_attributes.key?(field))
|
@@ -219,7 +222,7 @@ module TestDummy
|
|
219
222
|
when Module
|
220
223
|
block.method(name)
|
221
224
|
when Symbol
|
222
|
-
|
225
|
+
Helper.method(name)
|
223
226
|
when true
|
224
227
|
# Configure association dummyr the first time it is called
|
225
228
|
if (reflection = reflect_on_association(name))
|
data/test/db/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.sqlite3
|
data/test/helper.rb
CHANGED
data/test/test_test_dummy.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestTestDummy < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
def test_simple_model
|
5
|
+
simple = Simple.create_fake
|
6
|
+
|
7
|
+
assert_equal [ ], simple.errors.full_messages
|
8
|
+
assert !simple.new_record?
|
9
|
+
|
10
|
+
assert_equal 8, simple.name.length
|
6
11
|
end
|
7
12
|
end
|
data/test_dummy.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{test_dummy}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["tadman"]
|
@@ -24,8 +24,13 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/test_dummy.rb",
|
27
|
+
"lib/test_dummy/helper.rb",
|
27
28
|
"lib/test_dummy/railtie.rb",
|
29
|
+
"lib/test_dummy/test_helper.rb",
|
30
|
+
"test/config/database.yml",
|
31
|
+
"test/db/.gitignore",
|
28
32
|
"test/helper.rb",
|
33
|
+
"test/models/simple.rb",
|
29
34
|
"test/test_test_dummy.rb",
|
30
35
|
"test_dummy.gemspec"
|
31
36
|
]
|
@@ -36,6 +41,7 @@ Gem::Specification.new do |s|
|
|
36
41
|
s.summary = %q{Quick test data generator and fake model maker}
|
37
42
|
s.test_files = [
|
38
43
|
"test/helper.rb",
|
44
|
+
"test/models/simple.rb",
|
39
45
|
"test/test_test_dummy.rb"
|
40
46
|
]
|
41
47
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- tadman
|
@@ -35,8 +35,13 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- VERSION
|
37
37
|
- lib/test_dummy.rb
|
38
|
+
- lib/test_dummy/helper.rb
|
38
39
|
- lib/test_dummy/railtie.rb
|
40
|
+
- lib/test_dummy/test_helper.rb
|
41
|
+
- test/config/database.yml
|
42
|
+
- test/db/.gitignore
|
39
43
|
- test/helper.rb
|
44
|
+
- test/models/simple.rb
|
40
45
|
- test/test_test_dummy.rb
|
41
46
|
- test_dummy.gemspec
|
42
47
|
has_rdoc: true
|
@@ -73,4 +78,5 @@ specification_version: 3
|
|
73
78
|
summary: Quick test data generator and fake model maker
|
74
79
|
test_files:
|
75
80
|
- test/helper.rb
|
81
|
+
- test/models/simple.rb
|
76
82
|
- test/test_test_dummy.rb
|