dm-sweatshop 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/LICENSE +20 -0
- data/Manifest.txt +14 -0
- data/README.textile +224 -0
- data/Rakefile +63 -0
- data/TODO +0 -0
- data/lib/dm-sweatshop.rb +11 -0
- data/lib/dm-sweatshop/model.rb +35 -0
- data/lib/dm-sweatshop/sweatshop.rb +49 -0
- data/lib/dm-sweatshop/version.rb +5 -0
- data/spec/dm-sweatshop/model_spec.rb +76 -0
- data/spec/dm-sweatshop/sweatshop_spec.rb +143 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +9 -0
- metadata +99 -0
data/History.txt
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Ben Burkert
|
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/Manifest.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
History.txt
|
2
|
+
LICENSE
|
3
|
+
Manifest.txt
|
4
|
+
README.textile
|
5
|
+
Rakefile
|
6
|
+
TODO
|
7
|
+
lib/dm-sweatshop.rb
|
8
|
+
lib/dm-sweatshop/sweatshop.rb
|
9
|
+
lib/dm-sweatshop/model.rb
|
10
|
+
lib/dm-sweatshop/version.rb
|
11
|
+
spec/dm-sweatshop/model_spec.rb
|
12
|
+
spec/dm-sweatshop/sweatshop_spec.rb
|
13
|
+
spec/spec.opts
|
14
|
+
spec/spec_helper.rb
|
data/README.textile
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
h1. dm-sweatshop
|
2
|
+
|
3
|
+
h2. Overview
|
4
|
+
|
5
|
+
dm-sweatshop is a model factory for DataMapper. It makes it easy & painless to crank out complex pseudo random models -- useful for tests and seed data. Production Goals:
|
6
|
+
|
7
|
+
* Easy generation of random models with data that fits the application domain.
|
8
|
+
* Simple syntax for declaring and generating model patterns.
|
9
|
+
* Add context to model patterns, allowing grouping and
|
10
|
+
* Effortlessly generate or fill in associations for creating complex models with few lines of code.
|
11
|
+
|
12
|
+
h2. Examples
|
13
|
+
|
14
|
+
Starting off with a simple user model.
|
15
|
+
<pre>
|
16
|
+
<code>
|
17
|
+
class User
|
18
|
+
include DataMapper::Resource
|
19
|
+
|
20
|
+
property :id, Serial
|
21
|
+
property :username, String
|
22
|
+
property :email, String
|
23
|
+
property :password, String
|
24
|
+
end
|
25
|
+
</code>
|
26
|
+
</pre>
|
27
|
+
|
28
|
+
A fixture for the user model can be defined using the @fixture@ method.
|
29
|
+
<pre>
|
30
|
+
<code>
|
31
|
+
User.fixture {{
|
32
|
+
:username => (username = /\w+/.gen),
|
33
|
+
:email => "#{username}@example.com",
|
34
|
+
:password => (password = /\w+/.gen),
|
35
|
+
:pasword_confirmation => password
|
36
|
+
|
37
|
+
# The /\w+/.gen notation is part of the randexp gem:
|
38
|
+
# http://github.com/benburkert/randexp/
|
39
|
+
}}
|
40
|
+
</code>
|
41
|
+
</pre>
|
42
|
+
|
43
|
+
Notice the double curly brace (@{{@), a quick little way to pass a block that returns a hash to the fixture method. This is important because it ensures the data is random when we generate a new instance of the model, by calling the block every time.
|
44
|
+
|
45
|
+
And here's how you generate said model.
|
46
|
+
<pre>
|
47
|
+
<code>
|
48
|
+
User.generate
|
49
|
+
</code>
|
50
|
+
</pre>
|
51
|
+
|
52
|
+
That's it. In fact, it can even be shortened.
|
53
|
+
<pre>
|
54
|
+
<code>
|
55
|
+
User.gen
|
56
|
+
</code>
|
57
|
+
</pre>
|
58
|
+
|
59
|
+
h3. Associations
|
60
|
+
|
61
|
+
The real power of sweatshop is generating working associations.
|
62
|
+
<pre>
|
63
|
+
<code>
|
64
|
+
DataMapper.setup(:default, "sqlite3::memory:")
|
65
|
+
|
66
|
+
class Tweet
|
67
|
+
include DataMapper::Resource
|
68
|
+
|
69
|
+
property :id, Serial
|
70
|
+
property :message, String, :length => 140
|
71
|
+
property :user_id, Integer
|
72
|
+
|
73
|
+
belongs_to :user
|
74
|
+
has n, :tags, :through => Resource
|
75
|
+
end
|
76
|
+
|
77
|
+
class Tag
|
78
|
+
include DataMapper::Resource
|
79
|
+
|
80
|
+
property :id, Serial
|
81
|
+
property :name, String
|
82
|
+
|
83
|
+
has n, :tweets, :through => Resource
|
84
|
+
end
|
85
|
+
|
86
|
+
class User
|
87
|
+
include DataMapper::Resource
|
88
|
+
|
89
|
+
property :id, Serial
|
90
|
+
property :username, String
|
91
|
+
|
92
|
+
has n, :tweets
|
93
|
+
end
|
94
|
+
|
95
|
+
DataMapper.auto_migrate!
|
96
|
+
|
97
|
+
User.fix {{
|
98
|
+
:username => /\w+/.gen,
|
99
|
+
:tweets => 500.of {Tweet.make}
|
100
|
+
}}
|
101
|
+
|
102
|
+
Tweet.fix {{
|
103
|
+
:message => /[:sentence:]/.gen[0..140],
|
104
|
+
:tags => (0..10).of {Tag.make}
|
105
|
+
}}
|
106
|
+
|
107
|
+
Tag.fix {{
|
108
|
+
:name => /\w+/.gen
|
109
|
+
}}
|
110
|
+
|
111
|
+
# now lets generate 100 users, each with 500 tweets. Also, the tweet's have 0 to 10 tags!
|
112
|
+
users = 10.of {User.gen}
|
113
|
+
</code>
|
114
|
+
</pre>
|
115
|
+
|
116
|
+
That's going to generate alot of tags, way more than you would see in the production app. Let's recylce some already generated tags instead.
|
117
|
+
|
118
|
+
<pre>
|
119
|
+
<code>
|
120
|
+
User.fix {{
|
121
|
+
:username => /\w+/.gen,
|
122
|
+
:tweets => 500.of {Tweet.make}
|
123
|
+
}}
|
124
|
+
|
125
|
+
Tweet.fix {{
|
126
|
+
:message => /[:sentence:]/.gen[0..140],
|
127
|
+
:tags => (0..10).of {Tag.pick} #lets pick, not make this time
|
128
|
+
}}
|
129
|
+
|
130
|
+
Tag.fix {{
|
131
|
+
:name => /\w+/.gen
|
132
|
+
}}
|
133
|
+
|
134
|
+
50.times {Tag.gen}
|
135
|
+
|
136
|
+
users = 10.of {User.gen}
|
137
|
+
</code>
|
138
|
+
</pre>
|
139
|
+
|
140
|
+
h3. Contexts
|
141
|
+
|
142
|
+
You can add multiple fixtures to a mode, dm-sweatshop will randomly pick between the available fixtures when it generates a new model.
|
143
|
+
|
144
|
+
<pre>
|
145
|
+
<code>
|
146
|
+
Tweet.fix {{
|
147
|
+
:message => /\@#{User.pick.name} [:sentence:]/.gen[0..140], #an @reply for some user
|
148
|
+
:tags => (0..10).of {Tag.pick}
|
149
|
+
}}
|
150
|
+
</code>
|
151
|
+
</pre>
|
152
|
+
|
153
|
+
To keep track of all of our new fixtures, we can even give them a context.
|
154
|
+
|
155
|
+
<pre>
|
156
|
+
<code>
|
157
|
+
Tweet.fix(:at_reply) {{
|
158
|
+
:message => /\@#{User.pick.name} [:sentence:]/.gen[0..140],
|
159
|
+
:tags => (0..10).of {Tag.pick}
|
160
|
+
}}
|
161
|
+
|
162
|
+
Tweet.fix(:conversation) {{
|
163
|
+
:message => /\@#{(tweet = Tweet.pick(:at_reply)).user.name} [:sentence:]/.gen[0..140],
|
164
|
+
:tags => tweet.tags
|
165
|
+
}}
|
166
|
+
</code>
|
167
|
+
</pre>
|
168
|
+
|
169
|
+
h3. Overriding a fixture
|
170
|
+
|
171
|
+
Sometimes you will want to change one of your fixtures a little bit. You create a new fixture with a whole new context, but this can be overkill. The other option is to specify attributes in the call to @generate@.
|
172
|
+
|
173
|
+
<pre>
|
174
|
+
<code>
|
175
|
+
User.gen(:username => 'datamapper') #uses 'datamapper' as the user name instead of the randomly generated word
|
176
|
+
</code>
|
177
|
+
</pre>
|
178
|
+
|
179
|
+
This works with contexts too.
|
180
|
+
|
181
|
+
<pre>
|
182
|
+
<code>
|
183
|
+
User.gen(:conversation, :tags => Tag.all) #a very, very broad conversation
|
184
|
+
</code>
|
185
|
+
</pre>
|
186
|
+
|
187
|
+
Go forth, and populate your data.
|
188
|
+
|
189
|
+
h2. Best Practices
|
190
|
+
|
191
|
+
h3. Specs
|
192
|
+
|
193
|
+
The suggested way to use dm-sweatshop with test specs is to create a @spec/spec_fixtures.rb@ file, then declare your fixtures in there. Next, @require@ it in your @spec/spec_helper.rb@ file, after your models have loaded.
|
194
|
+
|
195
|
+
<pre>
|
196
|
+
<code>
|
197
|
+
Merb.start_environment(:testing => true, :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'test')
|
198
|
+
|
199
|
+
require File.join(File.dirname(__FILE__), 'spec_fixtures')
|
200
|
+
</code>
|
201
|
+
</pre>
|
202
|
+
|
203
|
+
Add the @.generate@ calls in your @before@ setup. Make sure to clear your tables or @auto_migrate@ your models after each spec!
|
204
|
+
|
205
|
+
h2. Possible Improvements
|
206
|
+
|
207
|
+
h3. Enforcing Validations
|
208
|
+
|
209
|
+
Enforce validations at generation time, before the call to @new@/@create@.
|
210
|
+
|
211
|
+
<pre>
|
212
|
+
<code>
|
213
|
+
User.fix {{
|
214
|
+
:username.unique => /\w+/.gen,
|
215
|
+
:tweets => 500.of {Tweet.make}
|
216
|
+
}}
|
217
|
+
</code>
|
218
|
+
</pre>
|
219
|
+
|
220
|
+
h3. Better Exception Handling
|
221
|
+
|
222
|
+
h3. Smarter @pick@
|
223
|
+
|
224
|
+
Add multiple contexts to pick, or an ability to _fall back_ if one context has no generated models.
|
data/Rakefile
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
ROOT = Pathname(__FILE__).dirname.expand_path
|
7
|
+
require ROOT + 'lib/dm-sweatshop/version'
|
8
|
+
|
9
|
+
AUTHOR = "Ben Burkert"
|
10
|
+
EMAIL = "ben@benburkert.com"
|
11
|
+
GEM_NAME = "dm-sweatshop"
|
12
|
+
GEM_VERSION = DataMapper::Sweatshop::VERSION
|
13
|
+
GEM_DEPENDENCIES = [["dm-core", GEM_VERSION], "randexp"]
|
14
|
+
GEM_CLEAN = ["log", "pkg"]
|
15
|
+
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.textile LICENSE TODO ] }
|
16
|
+
|
17
|
+
PROJECT_NAME = "datamapper"
|
18
|
+
PROJECT_URL = "http://github.com/sam/dm-more/tree/master/dm-Sweatshop"
|
19
|
+
PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin for building pseudo random models"
|
20
|
+
|
21
|
+
require ROOT.parent + 'tasks/hoe'
|
22
|
+
|
23
|
+
task :default => [ :spec ]
|
24
|
+
|
25
|
+
WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
|
26
|
+
SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
|
27
|
+
|
28
|
+
desc "Install #{GEM_NAME} #{GEM_VERSION} (default ruby)"
|
29
|
+
task :install => [ :package, :install_randexp ] do
|
30
|
+
sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Install the randexp gem from rubyforge"
|
34
|
+
task :install_randexp do
|
35
|
+
sh "#{SUDO} gem install randexp --no-update-sources", :verbose => false
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
|
39
|
+
task :uninstall => [ :clobber ] do
|
40
|
+
sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
|
41
|
+
end
|
42
|
+
|
43
|
+
namespace :jruby do
|
44
|
+
desc "Install #{GEM_NAME} #{GEM_VERSION} with JRuby"
|
45
|
+
task :install => [ :package ] do
|
46
|
+
sh %{#{SUDO} jruby -S gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}, :verbose => false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Run specifications'
|
51
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
52
|
+
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
53
|
+
t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
|
54
|
+
|
55
|
+
begin
|
56
|
+
t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
|
57
|
+
t.rcov_opts << '--exclude' << 'spec'
|
58
|
+
t.rcov_opts << '--text-summary'
|
59
|
+
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
|
60
|
+
rescue Exception
|
61
|
+
# rcov not installed
|
62
|
+
end
|
63
|
+
end
|
data/TODO
ADDED
File without changes
|
data/lib/dm-sweatshop.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Model
|
3
|
+
def fixture(name = default_fauxture_name, &blk)
|
4
|
+
Sweatshop.add(self, name, &blk)
|
5
|
+
end
|
6
|
+
|
7
|
+
alias_method :fix, :fixture
|
8
|
+
|
9
|
+
def generate(name = default_fauxture_name, attributes = {})
|
10
|
+
name, attributes = default_fauxture_name, name if name.is_a? Hash
|
11
|
+
Sweatshop.create(self, name, attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :gen, :generate
|
15
|
+
|
16
|
+
def generate_attributes(name = default_fauxture_name)
|
17
|
+
Sweatshop.attributes(self, name)
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method :gen_attrs, :generate_attributes
|
21
|
+
|
22
|
+
def make(name = default_fauxture_name, attributes = {})
|
23
|
+
name, attributes = default_fauxture_name, name if name.is_a? Hash
|
24
|
+
Sweatshop.make(self, name, attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def pick(name = default_fauxture_name)
|
28
|
+
Sweatshop.pick(self, name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_fauxture_name
|
32
|
+
:default
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module DataMapper
|
2
|
+
class Sweatshop
|
3
|
+
class << self
|
4
|
+
attr_accessor :model_map
|
5
|
+
attr_accessor :record_map
|
6
|
+
end
|
7
|
+
|
8
|
+
self.model_map = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = []}}
|
9
|
+
self.record_map = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = []}}
|
10
|
+
|
11
|
+
def self.add(klass, name, &proc)
|
12
|
+
self.model_map[klass][name.to_sym] << proc
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.record(klass, name, instance)
|
16
|
+
self.record_map[klass][name.to_sym] << instance
|
17
|
+
instance
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.create(klass, name, attributes = {})
|
21
|
+
begin
|
22
|
+
record(klass, name, klass.create(attributes(klass, name).merge(attributes)))
|
23
|
+
rescue StandardError => e
|
24
|
+
retry if e.message =~ /^column \w+ is not unique$/
|
25
|
+
raise e
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.make(klass, name, attributes = {})
|
30
|
+
record(klass, name, klass.new(attributes(klass, name).merge(attributes)))
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.pick(klass, name)
|
34
|
+
self.record_map[klass][name.to_sym].pick || raise(NoFixturesExist, "no #{name} context fixtures have been generated for the #{klass} class")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.attributes(klass, name)
|
38
|
+
proc = model_map[klass][name.to_sym].pick
|
39
|
+
|
40
|
+
if not proc.nil?
|
41
|
+
proc.call
|
42
|
+
elsif klass.superclass.is_a?(DataMapper::Model)
|
43
|
+
attributes(klass.superclass, name)
|
44
|
+
else
|
45
|
+
raise "#{name} fixture was not found"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Model do
|
4
|
+
|
5
|
+
class Widget
|
6
|
+
include DataMapper::Resource
|
7
|
+
|
8
|
+
property :id, Integer, :serial => true
|
9
|
+
property :type, Discriminator
|
10
|
+
property :name, String
|
11
|
+
property :price, Integer
|
12
|
+
|
13
|
+
belongs_to :order
|
14
|
+
end
|
15
|
+
|
16
|
+
class Wonket < Widget
|
17
|
+
property :size, String
|
18
|
+
end
|
19
|
+
|
20
|
+
class Order
|
21
|
+
include DataMapper::Resource
|
22
|
+
|
23
|
+
property :id, Integer, :serial => true
|
24
|
+
|
25
|
+
has n, :widgets
|
26
|
+
end
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
DataMapper.auto_migrate!
|
30
|
+
DataMapper::Sweatshop.model_map.clear
|
31
|
+
DataMapper::Sweatshop.record_map.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".fixture" do
|
35
|
+
it "should add a fixture proc for the model" do
|
36
|
+
Widget.fixture {{
|
37
|
+
:name => /\w+/.gen.capitalize,
|
38
|
+
:price => /\d{4,5}/.gen.to_i
|
39
|
+
}}
|
40
|
+
|
41
|
+
DataMapper::Sweatshop.model_map[Widget][:default].should_not be_empty
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should allow handle complex named fixtures" do
|
46
|
+
Wonket.fix {{
|
47
|
+
:name => /\w+ Wonket/.gen.capitalize,
|
48
|
+
:price => /\d{2,3}99/.gen.to_i,
|
49
|
+
:size => %w[small medium large xl].pick
|
50
|
+
}}
|
51
|
+
|
52
|
+
Order.fix {{
|
53
|
+
:widgets => (1..5).of { Widget.gen }
|
54
|
+
}}
|
55
|
+
|
56
|
+
Order.fix(:wonket_order) {{
|
57
|
+
:widgets => (5..10).of { Wonket.gen }
|
58
|
+
}}
|
59
|
+
|
60
|
+
wonket_order = Order.gen(:wonket_order)
|
61
|
+
wonket_order.widgets.should_not be_empty
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should allow for STI fixtures" do
|
65
|
+
Widget.fix {{
|
66
|
+
:name => /\w+/.gen.capitalize,
|
67
|
+
:price => /\d{4,5}/.gen.to_i
|
68
|
+
}}
|
69
|
+
|
70
|
+
Order.fix {{
|
71
|
+
:widgets => (1..5).of { Wonket.gen }
|
72
|
+
}}
|
73
|
+
|
74
|
+
Order.gen.widgets.should_not be_empty
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Sweatshop do
|
4
|
+
|
5
|
+
class Parent
|
6
|
+
include DataMapper::Resource
|
7
|
+
property :id, Integer, :serial => true
|
8
|
+
property :type, Discriminator
|
9
|
+
property :first_name, String
|
10
|
+
property :last_name, String
|
11
|
+
end
|
12
|
+
|
13
|
+
class Child < Parent
|
14
|
+
property :age, Integer
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
DataMapper.auto_migrate!
|
19
|
+
DataMapper::Sweatshop.model_map.clear
|
20
|
+
DataMapper::Sweatshop.record_map.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".model_map" do
|
24
|
+
it "should return a Hash if the model is not mapped" do
|
25
|
+
DataMapper::Sweatshop.model_map[Class.new].should be_is_a(Hash)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return a map for names to an array of procs if the model is not mapped" do
|
29
|
+
DataMapper::Sweatshop.model_map[Class.new][:unnamed].should be_is_a(Array)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".add" do
|
34
|
+
it "should app a generator proc to the model map" do
|
35
|
+
proc = lambda {}
|
36
|
+
lambda {
|
37
|
+
DataMapper::Sweatshop.add(Parent, :default, &proc)
|
38
|
+
}.should change {
|
39
|
+
DataMapper::Sweatshop.model_map[Parent][:default].first
|
40
|
+
}.from(nil).to(proc)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should push repeat procs onto the mapped array" do
|
44
|
+
proc1, proc2 = lambda {}, lambda {}
|
45
|
+
|
46
|
+
DataMapper::Sweatshop.add(Parent, :default, &proc1)
|
47
|
+
DataMapper::Sweatshop.add(Parent, :default, &proc2)
|
48
|
+
|
49
|
+
DataMapper::Sweatshop.model_map[Parent][:default].first.should == proc1
|
50
|
+
DataMapper::Sweatshop.model_map[Parent][:default].last.should == proc2
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe ".attributes" do
|
55
|
+
it "should return an attributes hash" do
|
56
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
57
|
+
:first_name => /\w+/.gen.capitalize,
|
58
|
+
:last_name => /\w+/.gen.capitalize
|
59
|
+
}}
|
60
|
+
|
61
|
+
DataMapper::Sweatshop.attributes(Parent, :default).should be_is_a(Hash)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should call the attribute proc on each call to attributes" do
|
65
|
+
calls = 0
|
66
|
+
proc = lambda {{:calls => (calls += 1)}}
|
67
|
+
|
68
|
+
DataMapper::Sweatshop.add(Parent, :default, &proc)
|
69
|
+
DataMapper::Sweatshop.attributes(Parent, :default).should == {:calls => 1}
|
70
|
+
DataMapper::Sweatshop.attributes(Parent, :default).should == {:calls => 2}
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should call attributes with the superclass if the class is not mapped" do
|
74
|
+
DataMapper::Sweatshop.add(Parent, :default) {{:first_name => 'Bob'}}
|
75
|
+
DataMapper::Sweatshop.attributes(Child, :default).should == {:first_name => 'Bob'}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should raise an error if neither the class or it's parent class(es) have been mapped" do
|
79
|
+
lambda { DataMapper::Sweatshop.attributes(Child, :default) }.should raise_error("default fixture was not found")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe ".create" do
|
84
|
+
it "should call create on the model class with the attributes generated from a mapped proc" do
|
85
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
86
|
+
:first_name => 'Kenny',
|
87
|
+
:last_name => 'Rogers'
|
88
|
+
}}
|
89
|
+
|
90
|
+
Parent.should_receive(:create).with(:first_name => 'Kenny', :last_name => 'Rogers')
|
91
|
+
|
92
|
+
DataMapper::Sweatshop.create(Parent, :default)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should call create on the model with a parent class' mapped attributes proc when the original class has not been maped" do
|
96
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
97
|
+
:first_name => 'Kenny',
|
98
|
+
:last_name => 'Rogers'
|
99
|
+
}}
|
100
|
+
|
101
|
+
Child.should_receive(:create).with(:first_name => 'Kenny', :last_name => 'Rogers')
|
102
|
+
|
103
|
+
DataMapper::Sweatshop.create(Child, :default)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should merge in any attributes as an argument" do
|
107
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
108
|
+
:first_name => 'Kenny',
|
109
|
+
:last_name => 'Rogers'
|
110
|
+
}}
|
111
|
+
|
112
|
+
Parent.should_receive(:create).with(:first_name => 'Roddy', :last_name => 'Rogers')
|
113
|
+
|
114
|
+
DataMapper::Sweatshop.create(Parent, :default, :first_name => 'Roddy')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe ".make" do
|
119
|
+
it "should call new on the model class with the attributes generated from a mapped proc" do
|
120
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
121
|
+
:first_name => 'Kenny',
|
122
|
+
:last_name => 'Rogers'
|
123
|
+
}}
|
124
|
+
|
125
|
+
Parent.should_receive(:new).with(:first_name => 'Kenny', :last_name => 'Rogers')
|
126
|
+
|
127
|
+
DataMapper::Sweatshop.make(Parent, :default)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe ".pick" do
|
132
|
+
it "should return a pre existing instance of a model from the record map" do
|
133
|
+
DataMapper::Sweatshop.add(Parent, :default) {{
|
134
|
+
:first_name => 'George',
|
135
|
+
:last_name => 'Clinton'
|
136
|
+
}}
|
137
|
+
|
138
|
+
DataMapper::Sweatshop.create(Parent, :default)
|
139
|
+
|
140
|
+
DataMapper::Sweatshop.pick(Parent, :default).should be_is_a(Parent)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-sweatshop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Burkert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-28 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.5
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: randexp
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.6.0
|
44
|
+
version:
|
45
|
+
description: DataMapper plugin for building pseudo random models
|
46
|
+
email:
|
47
|
+
- ben@benburkert.com
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- README.textile
|
54
|
+
- LICENSE
|
55
|
+
- TODO
|
56
|
+
files:
|
57
|
+
- History.txt
|
58
|
+
- LICENSE
|
59
|
+
- Manifest.txt
|
60
|
+
- README.textile
|
61
|
+
- Rakefile
|
62
|
+
- TODO
|
63
|
+
- lib/dm-sweatshop.rb
|
64
|
+
- lib/dm-sweatshop/sweatshop.rb
|
65
|
+
- lib/dm-sweatshop/model.rb
|
66
|
+
- lib/dm-sweatshop/version.rb
|
67
|
+
- spec/dm-sweatshop/model_spec.rb
|
68
|
+
- spec/dm-sweatshop/sweatshop_spec.rb
|
69
|
+
- spec/spec.opts
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/sam/dm-more/tree/master/dm-Sweatshop
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --main
|
76
|
+
- README.txt
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
version:
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project: datamapper
|
94
|
+
rubygems_version: 1.2.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 2
|
97
|
+
summary: DataMapper plugin for building pseudo random models
|
98
|
+
test_files: []
|
99
|
+
|