notahat-machinist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -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.
data/README.markdown ADDED
@@ -0,0 +1,201 @@
1
+ Machinist
2
+ =========
3
+
4
+ *Fixtures aren't fun. Machinist is.*
5
+
6
+ Machinist lets you construct test data on the fly, but instead of doing this:
7
+
8
+ describe Comment do
9
+ before do
10
+ @user = User.create!(:name => "Test User")
11
+ @post = Post.create!(:title => "Test Post", :author => @user, :body => "Lorem ipsum...")
12
+ @comment = Comment.create!(
13
+ :post => @post, :author_name => "Test Commenter", :author_email => "commenter@example.com",
14
+ :spam => true
15
+ )
16
+ end
17
+
18
+ it "should not include comments marked as spam in the without_spam named scope" do
19
+ Comment.without_spam.should_not include(@comment)
20
+ end
21
+ end
22
+
23
+ you can just do this:
24
+
25
+ describe Comment do
26
+ before do
27
+ @comment = Comment.make(:spam => true)
28
+ end
29
+
30
+ it "should not include comments marked as spam in the without_spam named scope" do
31
+ Comment.without_spam.should_not include(@comment)
32
+ end
33
+ end
34
+
35
+ Machinist generates data for the fields you don't care about, and constructs any necessary associated objects.
36
+
37
+ You tell Machinist how to do this with blueprints:
38
+
39
+ require 'faker'
40
+
41
+ Sham.name { Faker::Name.name }
42
+ Sham.email { Faker::Internet.email }
43
+ Sham.title { Faker::Lorem.sentence }
44
+ Sham.body { Faker::Lorem.paragraph }
45
+
46
+ User.blueprint do
47
+ name { Sham.name }
48
+ end
49
+
50
+ Post.blueprint do
51
+ title { Sham.title }
52
+ author { User.make }
53
+ body { Sham.body }
54
+ end
55
+
56
+ Comment.blueprint do
57
+ post
58
+ author_name { Sham.name }
59
+ author_email { Sham.email }
60
+ body { Sham.body }
61
+ end
62
+
63
+
64
+ Installation
65
+ ------------
66
+
67
+ Install the plugin:
68
+
69
+ ./script/plugin install git://github.com/notahat/machinist.git
70
+
71
+ Create a blueprints.rb in your test (or spec) directory, and require it in your test\_helper.rb (or spec\_helper.rb):
72
+
73
+ require File.expand_path(File.dirname(__FILE__) + "/blueprints")
74
+
75
+ Set Sham to reset before each test. In the `class Test::Unit::TestCase` block in your test\_helper.rb, add:
76
+
77
+ setup { Sham.reset }
78
+
79
+ or, if you're on RSpec, in the `Spec::Runner.configure` block in your spec\_helper.rb, add:
80
+
81
+ config.before(:each) { Sham.reset }
82
+
83
+
84
+ Sham - Generating Attribute Values
85
+ ----------------------------------
86
+
87
+ Sham lets you generate random but repeatable unique attributes values.
88
+
89
+ For example, you could define a way to generate random names as:
90
+
91
+ Sham.name { (1..10).map { ('a'..'z').to_a.rand } }
92
+
93
+ Then, to generate a name, call:
94
+
95
+ Sham.name
96
+
97
+ So why not just define a method? Sham ensures two things for you:
98
+
99
+ 1. You get the same sequence of values each time your test is run
100
+ 2. You don't get any duplicate values
101
+
102
+ 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:
103
+
104
+ Sham.name { Faker::Name.name }
105
+
106
+ Sham also supports generating numbered sequences if you prefer.
107
+
108
+ Sham.name {|index| "Name #{index}" }
109
+
110
+ If you want to allow duplicate values for a sham, you can pass the `:unique` option:
111
+
112
+ Sham.coin_toss(:unique => false) { rand(2) == 0 : 'heads' : 'tails' }
113
+
114
+
115
+ Blueprints - Generating ActiveRecord Objects
116
+ --------------------------------------------
117
+
118
+ A blueprint describes how to build a generic object for an ActiveRecord model. The idea is that you let the blueprint take care of constructing all the objects and attributes that you don't care about in your test, leaving you to focus on the just the things that you're testing.
119
+
120
+ A simple blueprint might look like this:
121
+
122
+ Comment.blueprint do
123
+ body "A comment!"
124
+ end
125
+
126
+ Once that's defined, you can construct a comment from this blueprint with:
127
+
128
+ Comment.make
129
+
130
+ Machinist calls `save!` on your ActiveRecord model to create the comment, so it will throw an exception if the blueprint doesn't pass your validations. It also calls `reload` after the `save!`.
131
+
132
+ You can override values defined in the blueprint by passing parameters to make:
133
+
134
+ Comment.make(:body => "A different comment!")
135
+
136
+ Rather than providing a constant value for an attribute, you can use Sham to generate a value for each new object:
137
+
138
+ Sham.body { Faker::Lorem.paragraph }
139
+ Comment.blueprint do
140
+ body { Sham.body }
141
+ end
142
+
143
+ Notice the curly braces around `Sham.body`. If you call `Comment.make` with your own body attribute, this block will not be executed.
144
+
145
+ You can use this same syntax to generate associated objects:
146
+
147
+ Comment.blueprint do
148
+ post { Post.make }
149
+ end
150
+
151
+ If the associated model has the same name as the field, you can abbreviate this to:
152
+
153
+ Comment.blueprint do
154
+ post
155
+ end
156
+
157
+ You can refer to already assigned attributes when constructing a new attribute:
158
+
159
+ Comment.blueprint do
160
+ post
161
+ body { "Comment on " + post.name }
162
+ end
163
+
164
+ You can also override associated objects when calling make:
165
+
166
+ post = Post.make
167
+ 3.times { Comment.make(:post => post) }
168
+
169
+ It's common to need to construct an object with particular attributes, or a particular object graph, in a number of tests. The best way to abstract out the construction is to put something like this in your blueprints.rb:
170
+
171
+ class Post
172
+ def self.make_with_comments(attributes = {})
173
+ Post.make(attributes) do |post|
174
+ 3.times { Comment.make(:post => post) }
175
+ end
176
+ end
177
+ end
178
+
179
+ Note that make can take a block, into which it will pass the newly constructed object.
180
+
181
+ If you want to generate an object graph without saving to the database, use make\_unsaved:
182
+
183
+ Comment.make_unsaved
184
+
185
+ This will generate both the Comment and the associated Post without saving either.
186
+
187
+
188
+ Credits
189
+ -------
190
+
191
+ Written by [Pete Yandell](http://notahat.com/).
192
+
193
+ Contributors:
194
+
195
+ - [Roland Swingler](http://github.com/knaveofdiamonds)
196
+
197
+ 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.
198
+
199
+ ---
200
+
201
+ Copyright (c) 2008 Peter Yandell, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+ require 'echoe'
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc 'Run all the specs for the machinist plugin.'
10
+ Spec::Rake::SpecTask.new do |t|
11
+ t.spec_files = FileList['spec/**/*_spec.rb']
12
+ t.spec_opts = ['--colour']
13
+ t.rcov = true
14
+ end
15
+
16
+ Echoe.new('machinist', '0.1.0') do |p|
17
+ p.description = "Fixtures aren't fun. Machinist is."
18
+ p.url = "http://github.com/notahat/machinist"
19
+ p.author = "Pete Yandell"
20
+ p.email = "pete@notahat.com"
21
+ p.ignore_pattern = ["coverage/*", "pkg/**/*", "spec/*"]
22
+ p.has_rdoc = false
23
+ p.development_dependencies = []
24
+ end
data/init.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+
3
+ if RAILS_ENV == 'test'
4
+ require 'machinist'
5
+ require 'sham'
6
+
7
+ class ActiveRecord::Base
8
+ include Machinist::ActiveRecordExtensions
9
+ end
10
+ end
data/lib/machinist.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'active_support'
2
+
3
+ module Machinist
4
+ def self.with_save_nerfed
5
+ begin
6
+ @@nerfed = true
7
+ yield
8
+ ensure
9
+ @@nerfed = false
10
+ end
11
+ end
12
+
13
+ @@nerfed = false
14
+ def self.nerfed?
15
+ @@nerfed
16
+ end
17
+
18
+ module ActiveRecordExtensions
19
+ def self.included(base)
20
+ base.extend(ClassMethods)
21
+ end
22
+
23
+ module ClassMethods
24
+ def blueprint(&blueprint)
25
+ @blueprint = blueprint
26
+ end
27
+
28
+ def make(attributes = {})
29
+ raise "No blueprint for class #{self}" if @blueprint.nil?
30
+ lathe = Lathe.new(self.new, attributes)
31
+ lathe.instance_eval(&@blueprint)
32
+ unless Machinist.nerfed?
33
+ lathe.object.save!
34
+ lathe.object.reload
35
+ end
36
+ returning(lathe.object) do |object|
37
+ yield object if block_given?
38
+ end
39
+ end
40
+
41
+ def make_unsaved(attributes = {})
42
+ returning(Machinist.with_save_nerfed { make(attributes) }) do |object|
43
+ yield object if block_given?
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ class Lathe
50
+ def initialize(object, attributes)
51
+ @object = object
52
+ @assigned_attributes = []
53
+ attributes.each do |key, value|
54
+ @object.send("#{key}=", value)
55
+ @assigned_attributes << key
56
+ end
57
+ end
58
+
59
+ attr_reader :object
60
+
61
+ def method_missing(symbol, *args, &block)
62
+ if @assigned_attributes.include?(symbol)
63
+ @object.send(symbol)
64
+ else
65
+ value = if block
66
+ block.call
67
+ elsif args.first.is_a?(Hash) || args.empty?
68
+ symbol.to_s.camelize.constantize.make(args.first || {})
69
+ else
70
+ args.first
71
+ end
72
+ @object.send("#{symbol}=", value)
73
+ @assigned_attributes << symbol
74
+ end
75
+ end
76
+ end
77
+ end
data/lib/sham.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'active_support'
2
+
3
+ class Sham
4
+ @@shams = {}
5
+
6
+ # Over-ride module's built-in name method, so we can re-use it for
7
+ # generating names. This is a bit of a no-no, but we get away with
8
+ # it in this context.
9
+ def self.name(*args, &block)
10
+ method_missing(:name, *args, &block)
11
+ end
12
+
13
+ def self.method_missing(symbol, *args, &block)
14
+ if block_given?
15
+ @@shams[symbol] = Sham.new(symbol, args.pop || {}, &block)
16
+ else
17
+ sham = @@shams[symbol]
18
+ raise "No sham defined for #{symbol}" if sham.nil?
19
+ sham.fetch_value
20
+ end
21
+ end
22
+
23
+ def self.reset
24
+ @@shams.values.each(&:reset)
25
+ end
26
+
27
+ def initialize(name, options = {}, &block)
28
+ @name = name
29
+ @generator = block
30
+ @offset = 0
31
+ @unique = options.has_key?(:unique) ? options[:unique] : true
32
+ generate_values(12)
33
+ end
34
+
35
+ def reset
36
+ @offset = 0
37
+ end
38
+
39
+ def fetch_value
40
+ # Generate more values if we need them.
41
+ if @offset >= @values.length
42
+ generate_values(2 * @values.length)
43
+ raise "Can't generate more unique values for Sham.#{@name}" if @offset >= @values.length
44
+ end
45
+ returning @values[@offset] do
46
+ @offset += 1
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def generate_values(count)
53
+ @values = seeded { (1..count).map(&@generator) }
54
+ @values.uniq! if @unique
55
+ end
56
+
57
+ def seeded
58
+ begin
59
+ srand(1)
60
+ yield
61
+ ensure
62
+ srand
63
+ end
64
+ end
65
+ end
data/machinist.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{machinist}
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Pete Yandell"]
7
+ s.date = %q{2008-11-18}
8
+ s.description = %q{Fixtures aren't fun. Machinist is.}
9
+ s.email = %q{pete@notahat.com}
10
+ s.extra_rdoc_files = ["lib/machinist.rb", "lib/sham.rb", "README.markdown"]
11
+ s.files = ["init.rb", "lib/machinist.rb", "lib/sham.rb", "machinist.gemspec", "Manifest", "MIT-LICENSE", "rails/init.rb", "Rakefile", "README.markdown"]
12
+ s.homepage = %q{http://github.com/notahat/machinist}
13
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Machinist", "--main", "README.markdown"]
14
+ s.require_paths = ["lib"]
15
+ s.rubyforge_project = %q{machinist}
16
+ s.rubygems_version = %q{1.2.0}
17
+ s.summary = %q{Fixtures aren't fun. Machinist is.}
18
+
19
+ if s.respond_to? :specification_version then
20
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
21
+ s.specification_version = 2
22
+
23
+ if current_version >= 3 then
24
+ else
25
+ end
26
+ else
27
+ end
28
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+
3
+ if RAILS_ENV == 'test'
4
+ require 'machinist'
5
+ require 'sham'
6
+
7
+ class ActiveRecord::Base
8
+ include Machinist::ActiveRecordExtensions
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notahat-machinist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pete Yandell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-18 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Fixtures aren't fun. Machinist is.
17
+ email: pete@notahat.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/machinist.rb
24
+ - lib/sham.rb
25
+ - README.markdown
26
+ files:
27
+ - init.rb
28
+ - lib/machinist.rb
29
+ - lib/sham.rb
30
+ - machinist.gemspec
31
+ - Manifest
32
+ - MIT-LICENSE
33
+ - rails/init.rb
34
+ - Rakefile
35
+ - README.markdown
36
+ has_rdoc: false
37
+ homepage: http://github.com/notahat/machinist
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - Machinist
44
+ - --main
45
+ - README.markdown
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "1.2"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: machinist
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Fixtures aren't fun. Machinist is.
67
+ test_files: []
68
+