machinist 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ Autotest.add_hook :initialize do |at|
2
+ at.clear_mappings
3
+
4
+ at.add_mapping(%r%^spec/(.*)_spec.rb$%) {|filename, _| filename }
5
+ at.add_mapping(%r%^lib/(.*).rb$%) {|_, match| "spec/#{match[1]}_spec.rb" }
6
+ at.add_mapping(%r%^spec/spec_helper.rb$%) { at.files_matching(%r%^spec/(.*)_spec.rb$%) }
7
+ end
@@ -0,0 +1,4 @@
1
+ coverage
2
+ doc
3
+ *.gem
4
+ *.project
@@ -0,0 +1,18 @@
1
+ Machinist FAQ
2
+ =============
3
+
4
+ ### My blueprint is giving me really weird errors. Any ideas?
5
+
6
+ If your object has an attribute that happens to correspond to a Ruby standard function, it won't work properly in a blueprint.
7
+
8
+ For example:
9
+
10
+ OpeningHours.blueprint do
11
+ open { Time.now }
12
+ end
13
+
14
+ This will result in Machinist attempting to run ruby's open command. To work around this use self.open instead.
15
+
16
+ OpeningHours.blueprint do
17
+ self.open { Time.now }
18
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Peter Yandell
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.
@@ -0,0 +1,317 @@
1
+ Machinist
2
+ =========
3
+
4
+ *Fixtures aren't fun. Machinist is.*
5
+
6
+ Machinist makes it easy to create test data within your tests. It generates data for the fields you don't care about, and constructs any necessary associated objects, leaving you to only specify the fields you *do* care about in your tests. For example:
7
+
8
+ describe Comment do
9
+ before do
10
+ # This will make a Comment, a Post, and a User (the author of
11
+ # the Post), and generate values for all their attributes:
12
+ @comment = Comment.make(:spam => true)
13
+ end
14
+
15
+ it "should not include comments marked as spam in the without_spam named scope" do
16
+ Comment.without_spam.should_not include(@comment)
17
+ end
18
+ end
19
+
20
+ You tell Machinist how to do this with blueprints:
21
+
22
+ require 'machinist/active_record'
23
+ require 'sham'
24
+ require 'faker'
25
+
26
+ Sham.name { Faker::Name.name }
27
+ Sham.email { Faker::Internet.email }
28
+ Sham.title { Faker::Lorem.sentence }
29
+ Sham.body { Faker::Lorem.paragraph }
30
+
31
+ User.blueprint do
32
+ name
33
+ email
34
+ end
35
+
36
+ Post.blueprint do
37
+ title
38
+ author
39
+ body
40
+ end
41
+
42
+ Comment.blueprint do
43
+ post
44
+ author_name { Sham.name }
45
+ author_email { Sham.email }
46
+ body
47
+ end
48
+
49
+
50
+ Download & Install
51
+ ==================
52
+
53
+ ### Installing as a Rails plugin
54
+
55
+ ./script/plugin install git://github.com/notahat/machinist.git
56
+
57
+ ### Installing as a Gem
58
+
59
+ sudo gem install machinist --source http://gemcutter.org
60
+
61
+ ### Setting up your project
62
+
63
+ Create a `blueprints.rb` file to hold your blueprints in your test (or spec) directory. It should start with:
64
+
65
+ require 'machinist/active_record'
66
+ require 'sham'
67
+
68
+ Substitute `data_mapper` or `sequel` for `active_record` if that's your weapon of choice.
69
+
70
+ Require `blueprints.rb` in your `test_helper.rb` (or `spec_helper.rb`):
71
+
72
+ require File.expand_path(File.dirname(__FILE__) + "/blueprints")
73
+
74
+ Set Sham to reset before each test. In the `class Test::Unit::TestCase` block in your `test_helper.rb`, add:
75
+
76
+ setup { Sham.reset }
77
+
78
+ or, if you're on RSpec, in the `Spec::Runner.configure` block in your `spec_helper.rb`, add:
79
+
80
+ config.before(:all) { Sham.reset(:before_all) }
81
+ config.before(:each) { Sham.reset(:before_each) }
82
+
83
+
84
+ Documentation
85
+ =============
86
+
87
+ Sham - Generating Attribute Values
88
+ ----------------------------------
89
+
90
+ Sham lets you generate random but repeatable unique attributes values.
91
+
92
+ For example, you could define a way to generate random names as:
93
+
94
+ Sham.name { (1..10).map { ('a'..'z').to_a.rand }.join }
95
+
96
+ Then, to generate a name, call:
97
+
98
+ Sham.name
99
+
100
+ So why not just define a helper method to do this? Sham ensures two things for you:
101
+
102
+ 1. You get the same sequence of values each time your test is run
103
+ 2. You don't get any duplicate values
104
+
105
+ Sham works very well with the excellent [Faker gem](http://faker.rubyforge.org/) by Benjamin Curtis. Using this, a much nicer way to generate names is:
106
+
107
+ Sham.name { Faker::Name.name }
108
+
109
+ Sham also supports generating numbered sequences if you prefer.
110
+
111
+ Sham.name {|index| "Name #{index}" }
112
+
113
+ If you want to allow duplicate values for a sham, you can pass the `:unique` option:
114
+
115
+ Sham.coin_toss(:unique => false) { rand(2) == 0 ? 'heads' : 'tails' }
116
+
117
+ You can create a bunch of sham definitions in one hit like this:
118
+
119
+ Sham.define do
120
+ title { Faker::Lorem.words(5).join(' ') }
121
+ name { Faker::Name.name }
122
+ body { Faker::Lorem.paragraphs(3).join("\n\n") }
123
+ end
124
+
125
+
126
+ Blueprints - Generating Objects
127
+ -------------------------------
128
+
129
+ A blueprint describes how to generate an object. The idea is that you let the blueprint take care of making up values for attributes that you don't care about in your test, leaving you to focus on the just the things that you're testing.
130
+
131
+ A simple blueprint might look like this:
132
+
133
+ Post.blueprint do
134
+ title { Sham.title }
135
+ author { Sham.name }
136
+ body { Sham.body }
137
+ end
138
+
139
+ You can then construct a Post from this blueprint with:
140
+
141
+ Post.make
142
+
143
+ When you call `make`, Machinist calls `Post.new`, then runs through the attributes in your blueprint, calling the block for each attribute to generate a value. The Post is then saved and reloaded. An exception is thrown if Post can't be saved.
144
+
145
+ You can override values defined in the blueprint by passing a hash to make:
146
+
147
+ Post.make(:title => "A Specific Title")
148
+
149
+ If you don't supply a block for an attribute in the blueprint, Machinist will look for a Sham definition with the same name as the attribute, so you can shorten the above blueprint to:
150
+
151
+ Post.blueprint do
152
+ title
153
+ author { Sham.name }
154
+ body
155
+ end
156
+
157
+ If you want to generate an object without saving it to the database, replace `make` with `make_unsaved`. (`make_unsaved` also ensures that any associated objects that need to be generated are not saved - although not if you are using Sequel. See the section on associations below.)
158
+
159
+ You can refer to already assigned attributes when constructing a new attribute:
160
+
161
+ Post.blueprint do
162
+ title
163
+ author { Sham.name }
164
+ body { "Post by #{author}" }
165
+ end
166
+
167
+
168
+ ### Named Blueprints
169
+
170
+ Named blueprints let you define variations on an object. For example, suppose some of your Users are administrators:
171
+
172
+ User.blueprint do
173
+ name
174
+ email
175
+ end
176
+
177
+ User.blueprint(:admin) do
178
+ name { Sham.name + " (admin)" }
179
+ admin { true }
180
+ end
181
+
182
+ Calling:
183
+
184
+ User.make(:admin)
185
+
186
+ will use the `:admin` blueprint.
187
+
188
+ Named blueprints call the default blueprint to set any attributes not specifically provided, so in this example the `email` attribute will still be generated even for an admin user.
189
+
190
+
191
+ ### Belongs\_to Associations
192
+
193
+ If you're generating an object that belongs to another object, you can generate the associated object like this:
194
+
195
+ Comment.blueprint do
196
+ post { Post.make }
197
+ end
198
+
199
+ Calling `Comment.make` will construct a Comment and its associated Post, and save both.
200
+
201
+ If you want to override the value for post when constructing the comment, you can do this:
202
+
203
+ post = Post.make(:title => "A particular title)
204
+ comment = Comment.make(:post => post)
205
+
206
+ Machinist will not call the blueprint block for the post attribute, so this won't generate two posts.
207
+
208
+ Machinist is smart enough to look at the association and work out what sort of object it needs to create, so you can shorten the above blueprint to:
209
+
210
+ Comment.blueprint do
211
+ post
212
+ end
213
+
214
+
215
+ ### Other Associations
216
+
217
+ For has\_many and has\_and\_belongs\_to\_many associations, ActiveRecord insists that the object be saved before any associated objects can be saved. That means you can't generate the associated objects from within the blueprint.
218
+
219
+ The simplest solution is to write a test helper:
220
+
221
+ def make_post_with_comments(attributes = {})
222
+ post = Post.make(attributes)
223
+ 3.times { post.comments.make }
224
+ post
225
+ end
226
+
227
+ Note here that you can call `make` on a has\_many association. (This isn't yet supported for DataMapper.)
228
+
229
+ Make can take a block, into which it passes the constructed object, so the above can be written as:
230
+
231
+ def make_post_with_comments
232
+ Post.make(attributes) do |post|
233
+ 3.times { post.comments.make }
234
+ end
235
+ end
236
+
237
+
238
+ ### Using Blueprints in Rails Controller Tests
239
+
240
+ The `plan` method behaves like `make`, except it returns a hash of attributes, and doesn't save the object. This is useful for passing in to controller tests:
241
+
242
+ test "should create post" do
243
+ assert_difference('Post.count') do
244
+ post :create, :post => Post.plan
245
+ end
246
+ assert_redirected_to post_path(assigns(:post))
247
+ end
248
+
249
+ `plan` will save any associated objects. In this example, it will create an Author, and it knows that the controller expects an `author_id` attribute, rather than an `author` attribute, and makes this translation for you.
250
+
251
+ You can also call plan on has\_many associations, making it easy to test nested controllers:
252
+
253
+ test "should create comment" do
254
+ post = Post.make
255
+ assert_difference('Comment.count') do
256
+ post :create, :post_id => post.id, :comment => post.comments.plan
257
+ end
258
+ assert_redirected_to post_comment_path(post, assigns(:comment))
259
+ end
260
+
261
+ (Calling plan on associations is not yet supported in DataMapper.)
262
+
263
+
264
+ ### Blueprints on Plain Old Ruby Objects
265
+
266
+ Machinist also works with plain old Ruby objects. Let's say you have a class like:
267
+
268
+ class Post
269
+ attr_accessor :title
270
+ attr_accessor :body
271
+ end
272
+
273
+ You can then do the following in your `blueprints.rb`:
274
+
275
+ require 'machinist/object'
276
+
277
+ Post.blueprint do
278
+ title "A title!"
279
+ body "A body!"
280
+ end
281
+
282
+ Community
283
+ =========
284
+
285
+ You can always find the [latest version on GitHub](http://github.com/notahat/machinist).
286
+
287
+ If you have questions, check out the [Google Group](http://groups.google.com/group/machinist-users).
288
+
289
+ File bug reports and feature requests in the [issue tracker](http://github.com/notahat/machinist/issues).
290
+
291
+ Contributors
292
+ ------------
293
+
294
+ Machinist is maintained by Pete Yandell ([pete@notahat.com](mailto:pete@notahat.com), [@notahat](http://twitter.com/notahat))
295
+
296
+ Other contributors include:
297
+
298
+ [Marcos Arias](http://github.com/yizzreel),
299
+ [Jack Dempsey](http://github.com/jackdempsey),
300
+ [Clinton Forbes](http://github.com/clinton),
301
+ [Perryn Fowler](http://github.com/perryn),
302
+ [Niels Ganser](http://github.com/Nielsomat),
303
+ [Jeremy Grant](http://github.com/jeremygrant),
304
+ [Jon Guymon](http://github.com/gnarg),
305
+ [James Healy](http://github.com/yob),
306
+ [Evan David Light](http://github.com/elight),
307
+ [Chris Lloyd](http://github.com/chrislloyd),
308
+ [Adam Meehan](http://github.com/adzap),
309
+ [Kyle Neath](http://github.com/kneath),
310
+ [Lawrence Pit](http://github.com/lawrencepit),
311
+ [T.J. Sheehy](http://github.com/tjsheehy),
312
+ [Roland Swingler](http://github.com/knaveofdiamonds),
313
+ [Gareth Townsend](http://github.com/quamen),
314
+ [Matt Wastrodowski](http://github.com/towski),
315
+ [Ian White](http://github.com/ianwhite)
316
+
317
+ Thanks to Thoughtbot's [Factory Girl](http://github.com/thoughtbot/factory_girl/tree/master). Machinist was written because I loved the idea behind Factory Girl, but I thought the philosophy wasn't quite right, and I hated the syntax.
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "machinist"
7
+ gem.summary = "Fixtures aren't fun. Machinist is."
8
+ gem.email = "pete@notahat.com"
9
+ gem.homepage = "http://github.com/notahat/machinist"
10
+ gem.authors = ["Pete Yandell"]
11
+ gem.has_rdoc = false
12
+ gem.add_development_dependency "rspec", ">= 1.2.8"
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
+
20
+ require 'spec/rake/spectask'
21
+ desc 'Run the specs.'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ desc 'Run the specs with rcov.'
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ desc 'Run the specs.'
37
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.6
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'machinist' if RAILS_ENV == 'test'
2
+
@@ -32,7 +32,7 @@ module Machinist
32
32
  attributes = {}
33
33
  lathe.assigned_attributes.each_pair do |attribute, value|
34
34
  association = lathe.object.class.reflect_on_association(attribute)
35
- if association && association.macro == :belongs_to
35
+ if association && association.macro == :belongs_to && !value.nil?
36
36
  attributes[association.primary_key_name.to_sym] = value.id
37
37
  else
38
38
  attributes[attribute] = value
@@ -0,0 +1,72 @@
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{machinist}
8
+ s.version = "1.0.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pete Yandell"]
12
+ s.date = %q{2009-11-29}
13
+ s.email = %q{pete@notahat.com}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ ".autotest",
19
+ ".gitignore",
20
+ "FAQ.markdown",
21
+ "MIT-LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "lib/machinist.rb",
27
+ "lib/machinist/active_record.rb",
28
+ "lib/machinist/blueprints.rb",
29
+ "lib/machinist/data_mapper.rb",
30
+ "lib/machinist/object.rb",
31
+ "lib/machinist/sequel.rb",
32
+ "lib/sham.rb",
33
+ "machinist.gemspec",
34
+ "spec/active_record_spec.rb",
35
+ "spec/data_mapper_spec.rb",
36
+ "spec/db/.gitignore",
37
+ "spec/db/schema.rb",
38
+ "spec/log/.gitignore",
39
+ "spec/machinist_spec.rb",
40
+ "spec/sequel_spec.rb",
41
+ "spec/sham_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/notahat/machinist}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{Fixtures aren't fun. Machinist is.}
49
+ s.test_files = [
50
+ "spec/active_record_spec.rb",
51
+ "spec/data_mapper_spec.rb",
52
+ "spec/db/schema.rb",
53
+ "spec/machinist_spec.rb",
54
+ "spec/sequel_spec.rb",
55
+ "spec/sham_spec.rb",
56
+ "spec/spec_helper.rb"
57
+ ]
58
+
59
+ if s.respond_to? :specification_version then
60
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
61
+ s.specification_version = 3
62
+
63
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
64
+ s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
65
+ else
66
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
70
+ end
71
+ end
72
+