machinist_redux 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/.rubocop.yml +60 -0
- data/.rubocop_todo.yml +92 -0
- data/.travis.yml +23 -0
- data/Appraisals +11 -0
- data/Gemfile +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +307 -0
- data/Rakefile +20 -0
- data/gemfiles/rails_4.2.gemfile +38 -0
- data/gemfiles/rails_4.2.gemfile.lock +232 -0
- data/gemfiles/rails_5.0.gemfile +38 -0
- data/gemfiles/rails_5.0.gemfile.lock +232 -0
- data/gemfiles/rails_5.1.gemfile +34 -0
- data/gemfiles/rails_5.1.gemfile.lock +231 -0
- data/lib/generators/machinist/install/USAGE +2 -0
- data/lib/generators/machinist/install/install_generator.rb +46 -0
- data/lib/generators/machinist/install/templates/blueprints.rb +9 -0
- data/lib/generators/machinist/install/templates/machinist.rb.erb +7 -0
- data/lib/generators/machinist/model/model_generator.rb +11 -0
- data/lib/machinist.rb +5 -0
- data/lib/machinist/active_record.rb +14 -0
- data/lib/machinist/active_record/blueprint.rb +14 -0
- data/lib/machinist/active_record/lathe.rb +21 -0
- data/lib/machinist/blueprint.rb +84 -0
- data/lib/machinist/exceptions.rb +30 -0
- data/lib/machinist/lathe.rb +65 -0
- data/lib/machinist/machinable.rb +100 -0
- data/lib/machinist/version.rb +3 -0
- data/machinist.gemspec +23 -0
- data/spec/machinist/active_record_spec.rb +106 -0
- data/spec/machinist/blueprint_inheritance_spec.rb +101 -0
- data/spec/machinist/blueprint_spec.rb +76 -0
- data/spec/machinist/exceptions_spec.rb +16 -0
- data/spec/machinist/machinable_spec.rb +91 -0
- data/spec/spec_helper.rb +110 -0
- data/spec/support/active_record_environment.rb +62 -0
- metadata +94 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
RSpec.describe Machinist, 'exceptions' do
|
2
|
+
describe Machinist::BlueprintCantSaveError do
|
3
|
+
it 'presents the right message' do
|
4
|
+
blueprint = Machinist::Blueprint.new(String) {}
|
5
|
+
exception = Machinist::BlueprintCantSaveError.new(blueprint)
|
6
|
+
expect(exception.message).to eq('make! is not supported by blueprints for class String')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Machinist::NoBlueprintError do
|
11
|
+
it 'presents the right message' do
|
12
|
+
exception = Machinist::NoBlueprintError.new(String, :master)
|
13
|
+
expect(exception.message).to eq('No master blueprint defined for class String')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module MachinableSpecs
|
2
|
+
class Post
|
3
|
+
extend Machinist::Machinable
|
4
|
+
attr_accessor :title, :body, :comments
|
5
|
+
end
|
6
|
+
|
7
|
+
class Comment
|
8
|
+
extend Machinist::Machinable
|
9
|
+
attr_accessor :post, :title
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.describe Machinist::Machinable do
|
14
|
+
before do
|
15
|
+
MachinableSpecs::Post.clear_blueprints!
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'makes an object' do
|
19
|
+
MachinableSpecs::Post.blueprint do
|
20
|
+
title { 'First Post' }
|
21
|
+
end
|
22
|
+
|
23
|
+
post = MachinableSpecs::Post.make
|
24
|
+
expect(post).to be_a(MachinableSpecs::Post)
|
25
|
+
expect(post.title).to eq('First Post')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'makes an object from a named blueprint' do
|
29
|
+
MachinableSpecs::Post.blueprint do
|
30
|
+
title { 'First Post' }
|
31
|
+
body { 'Woot!' }
|
32
|
+
end
|
33
|
+
|
34
|
+
MachinableSpecs::Post.blueprint(:extra) do
|
35
|
+
title { 'Extra!' }
|
36
|
+
end
|
37
|
+
|
38
|
+
post = MachinableSpecs::Post.make(:extra)
|
39
|
+
expect(post).to be_a(MachinableSpecs::Post)
|
40
|
+
expect(post.title).to eq('Extra!')
|
41
|
+
expect(post.body).to eq('Woot!')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'makes an array of objects' do
|
45
|
+
MachinableSpecs::Post.blueprint do
|
46
|
+
title { 'First Post' }
|
47
|
+
end
|
48
|
+
|
49
|
+
posts = MachinableSpecs::Post.make(3)
|
50
|
+
expect(posts).to be_an(Array)
|
51
|
+
expect(posts.size).to eq(3)
|
52
|
+
posts.each do |post|
|
53
|
+
expect(post).to be_a(MachinableSpecs::Post)
|
54
|
+
expect(post.title).to eq('First Post')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'makes array attributes from the blueprint' do
|
59
|
+
MachinableSpecs::Comment.blueprint {}
|
60
|
+
MachinableSpecs::Post.blueprint do
|
61
|
+
comments(3) { MachinableSpecs::Comment.make }
|
62
|
+
end
|
63
|
+
|
64
|
+
post = MachinableSpecs::Post.make
|
65
|
+
expect(post.comments).to be_a(Array)
|
66
|
+
expect(post.comments.size).to eq(3)
|
67
|
+
post.comments.each do |comment|
|
68
|
+
expect(comment).to be_a(MachinableSpecs::Comment)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'fails without a blueprint' do
|
73
|
+
expect { MachinableSpecs::Post.make }.to raise_error(Machinist::NoBlueprintError) do |exception|
|
74
|
+
expect(exception.klass).to eq(MachinableSpecs::Post)
|
75
|
+
expect(exception.name).to eq(:master)
|
76
|
+
end
|
77
|
+
|
78
|
+
expect { MachinableSpecs::Post.make(:some_name) }.to raise_error(Machinist::NoBlueprintError) do |exception|
|
79
|
+
expect(exception.klass).to eq(MachinableSpecs::Post)
|
80
|
+
expect(exception.name).to eq(:some_name)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'fails when calling make! on an unsavable object' do
|
85
|
+
MachinableSpecs::Post.blueprint {}
|
86
|
+
|
87
|
+
expect { MachinableSpecs::Post.make! }.to raise_error(Machinist::BlueprintCantSaveError) do |exception|
|
88
|
+
expect(exception.blueprint.klass).to eq(MachinableSpecs::Post)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Configure Simplecov and Coveralls
|
2
|
+
unless ENV['NO_SIMPLECOV']
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
6
|
+
SimpleCov.start { add_filter '/spec/' }
|
7
|
+
Coveralls.wear! if ENV['COVERALLS_REPO_TOKEN']
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'machinist'
|
11
|
+
|
12
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
13
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
14
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
15
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
16
|
+
# files.
|
17
|
+
#
|
18
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
19
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
20
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
21
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
22
|
+
# a separate helper file that requires the additional dependencies and performs
|
23
|
+
# the additional setup, and require it from the spec files that actually need
|
24
|
+
# it.
|
25
|
+
#
|
26
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
27
|
+
# users commonly want.
|
28
|
+
#
|
29
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
30
|
+
RSpec.configure do |config|
|
31
|
+
# rspec-expectations config goes here. You can use an alternate
|
32
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
33
|
+
# assertions if you prefer.
|
34
|
+
config.expect_with :rspec do |expectations|
|
35
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
36
|
+
# and `failure_message` of custom matchers include text for helper methods
|
37
|
+
# defined using `chain`, e.g.:
|
38
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
39
|
+
# # => "be bigger than 2 and smaller than 4"
|
40
|
+
# ...rather than:
|
41
|
+
# # => "be bigger than 2"
|
42
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
46
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
47
|
+
config.mock_with :rspec do |mocks|
|
48
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
49
|
+
# a real object. This is generally recommended, and will default to
|
50
|
+
# `true` in RSpec 4.
|
51
|
+
mocks.verify_partial_doubles = true
|
52
|
+
end
|
53
|
+
|
54
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
55
|
+
# have no way to turn it off -- the option exists only for backwards
|
56
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
57
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
58
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
59
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
60
|
+
|
61
|
+
# This allows you to limit a spec run to individual examples or groups
|
62
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
63
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
64
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
65
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
66
|
+
config.filter_run_when_matching :focus
|
67
|
+
|
68
|
+
# Allows RSpec to persist some state between runs in order to support
|
69
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
70
|
+
# you configure your source control system to ignore this file.
|
71
|
+
config.example_status_persistence_file_path = 'tmp/rspec/examples.txt'
|
72
|
+
|
73
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
74
|
+
# recommended. For more details, see:
|
75
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
76
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
77
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
78
|
+
config.disable_monkey_patching!
|
79
|
+
|
80
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
81
|
+
# be too noisy due to issues in dependencies.
|
82
|
+
config.warnings = true
|
83
|
+
|
84
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
85
|
+
# file, and it's useful to allow more verbose output when running an
|
86
|
+
# individual spec file.
|
87
|
+
if config.files_to_run.one?
|
88
|
+
# Use the documentation formatter for detailed output,
|
89
|
+
# unless a formatter has already been configured
|
90
|
+
# (e.g. via a command-line flag).
|
91
|
+
config.default_formatter = 'doc'
|
92
|
+
end
|
93
|
+
|
94
|
+
# Print the 10 slowest examples and example groups at the
|
95
|
+
# end of the spec run, to help surface which specs are running
|
96
|
+
# particularly slow.
|
97
|
+
config.profile_examples = 10
|
98
|
+
|
99
|
+
# Run specs in random order to surface order dependencies. If you find an
|
100
|
+
# order dependency and want to debug it, you can fix the order by providing
|
101
|
+
# the seed, which is printed after each run.
|
102
|
+
# --seed 1234
|
103
|
+
config.order = :random
|
104
|
+
|
105
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
106
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
107
|
+
# test failures related to randomization by passing the same `--seed` value
|
108
|
+
# as the one that triggered the failure.
|
109
|
+
Kernel.srand config.seed
|
110
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'machinist/active_record'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(
|
5
|
+
adapter: 'sqlite3',
|
6
|
+
database: ':memory:',
|
7
|
+
timeout: 500
|
8
|
+
)
|
9
|
+
|
10
|
+
ActiveRecord::Schema.define(version: 0) do
|
11
|
+
create_table :users, force: true do |t|
|
12
|
+
t.column :username, :string
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :posts, force: true do |t|
|
16
|
+
t.column :title, :string
|
17
|
+
t.column :author_id, :integer
|
18
|
+
t.column :body, :text
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table :comments, force: true do |t|
|
22
|
+
t.column :post_id, :integer
|
23
|
+
t.column :body, :text
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table :tags, force: true do |t|
|
27
|
+
t.column :name, :string
|
28
|
+
end
|
29
|
+
|
30
|
+
create_table :posts_tags, id: false, force: true do |t|
|
31
|
+
t.column :post_id, :integer
|
32
|
+
t.column :tag_id, :integer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
validates_presence_of :username
|
38
|
+
validates_uniqueness_of :username
|
39
|
+
end
|
40
|
+
|
41
|
+
class Post < ActiveRecord::Base
|
42
|
+
has_many :comments
|
43
|
+
belongs_to :author, class_name: 'User'
|
44
|
+
has_and_belongs_to_many :tags
|
45
|
+
end
|
46
|
+
|
47
|
+
class Comment < ActiveRecord::Base
|
48
|
+
belongs_to :post
|
49
|
+
end
|
50
|
+
|
51
|
+
class Tag < ActiveRecord::Base
|
52
|
+
has_and_belongs_to_many :posts
|
53
|
+
end
|
54
|
+
|
55
|
+
module ActiveRecordEnvironment
|
56
|
+
def empty_database!
|
57
|
+
[User, Post, Comment].each do |klass|
|
58
|
+
klass.delete_all
|
59
|
+
klass.clear_blueprints!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist_redux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pete Yandell
|
8
|
+
- Attila Györffy
|
9
|
+
- Dominic Sayers
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2017-05-17 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Machinist makes it easy to create objects for use in tests. It generates
|
16
|
+
data for the attributes you don't care about, and constructs any necessary associated
|
17
|
+
objects, leaving you to specify only the fields you care about in your test.
|
18
|
+
email:
|
19
|
+
- dominic@sayers.cc
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- ".codeclimate.yml"
|
25
|
+
- ".gitignore"
|
26
|
+
- ".rspec"
|
27
|
+
- ".rubocop.yml"
|
28
|
+
- ".rubocop_todo.yml"
|
29
|
+
- ".travis.yml"
|
30
|
+
- Appraisals
|
31
|
+
- Gemfile
|
32
|
+
- LICENSE.txt
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- gemfiles/rails_4.2.gemfile
|
36
|
+
- gemfiles/rails_4.2.gemfile.lock
|
37
|
+
- gemfiles/rails_5.0.gemfile
|
38
|
+
- gemfiles/rails_5.0.gemfile.lock
|
39
|
+
- gemfiles/rails_5.1.gemfile
|
40
|
+
- gemfiles/rails_5.1.gemfile.lock
|
41
|
+
- lib/generators/machinist/install/USAGE
|
42
|
+
- lib/generators/machinist/install/install_generator.rb
|
43
|
+
- lib/generators/machinist/install/templates/blueprints.rb
|
44
|
+
- lib/generators/machinist/install/templates/machinist.rb.erb
|
45
|
+
- lib/generators/machinist/model/model_generator.rb
|
46
|
+
- lib/machinist.rb
|
47
|
+
- lib/machinist/active_record.rb
|
48
|
+
- lib/machinist/active_record/blueprint.rb
|
49
|
+
- lib/machinist/active_record/lathe.rb
|
50
|
+
- lib/machinist/blueprint.rb
|
51
|
+
- lib/machinist/exceptions.rb
|
52
|
+
- lib/machinist/lathe.rb
|
53
|
+
- lib/machinist/machinable.rb
|
54
|
+
- lib/machinist/version.rb
|
55
|
+
- machinist.gemspec
|
56
|
+
- spec/machinist/active_record_spec.rb
|
57
|
+
- spec/machinist/blueprint_inheritance_spec.rb
|
58
|
+
- spec/machinist/blueprint_spec.rb
|
59
|
+
- spec/machinist/exceptions_spec.rb
|
60
|
+
- spec/machinist/machinable_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/support/active_record_environment.rb
|
63
|
+
homepage: http://github.com/dominicsayers/machinist
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.6.12
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Fixtures aren't fun. Machinist was.
|
87
|
+
test_files:
|
88
|
+
- spec/machinist/active_record_spec.rb
|
89
|
+
- spec/machinist/blueprint_inheritance_spec.rb
|
90
|
+
- spec/machinist/blueprint_spec.rb
|
91
|
+
- spec/machinist/exceptions_spec.rb
|
92
|
+
- spec/machinist/machinable_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/support/active_record_environment.rb
|