faker_maker 1.1.4 → 1.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +22 -1
- data/Guardfile +91 -0
- data/README.md +17 -279
- data/_config.yml +1 -0
- data/docs/.bundle/config +2 -0
- data/docs/.keep +1 -0
- data/docs/_config.yml +8 -0
- data/docs/contributing.md +9 -0
- data/docs/credits.md +9 -0
- data/docs/index.md +21 -0
- data/docs/installation.md +21 -0
- data/docs/logo.png +0 -0
- data/docs/usage/arrays.md +44 -0
- data/docs/usage/building_instances.md +64 -0
- data/docs/usage/dependencies.md +10 -0
- data/docs/usage/destroying_factories.md +54 -0
- data/docs/usage/embedding_factories.md +23 -0
- data/docs/usage/index.md +52 -0
- data/docs/usage/inheritance.md +37 -0
- data/docs/usage/json_field_names.md +29 -0
- data/docs/usage/lifecycle_hooks.md +33 -0
- data/docs/usage/omitting_fields copy.md +42 -0
- data/faker_maker.gemspec +16 -12
- data/img/unipug.svg +135 -0
- data/lib/faker_maker.rb +20 -5
- data/lib/faker_maker/attribute.rb +4 -4
- data/lib/faker_maker/base.rb +1 -2
- data/lib/faker_maker/definition_proxy.rb +7 -1
- data/lib/faker_maker/factory.rb +20 -15
- data/lib/faker_maker/lifecycle_hooks.rb +16 -0
- data/lib/faker_maker/version.rb +1 -1
- metadata +88 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9de2b8d5e7f4753789d7c41f7ac37ebb7943fca44d12d1d14a474078dc9ab902
|
4
|
+
data.tar.gz: 7853d0fe9adee31a5175a2da6101099774b489c6dbed96d56f9fb82afd893204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dc584ed0bbe6a758bb21308059393833a47a60c014370996ff83b202e9183285a981f31e7ab9c2cbf768435b810fcece7568e5cecd613245b261827aa1ffeeb
|
7
|
+
data.tar.gz: e62ca83bddc73d1a0edc9cfc9b90356749e62bc0734c0732c7fead6562693ff19353635e63ff6484fdb54eb7dc1faa314497456ea8480b93511f3e36d9079cf2
|
data/.rubocop.yml
CHANGED
@@ -13,4 +13,25 @@ Metrics/BlockLength:
|
|
13
13
|
- "**/*_spec.rb"
|
14
14
|
|
15
15
|
Layout/TrailingWhitespace:
|
16
|
-
Enabled:
|
16
|
+
Enabled: true
|
17
|
+
|
18
|
+
Layout/SpaceAroundMethodCallOperator:
|
19
|
+
Enabled: true
|
20
|
+
|
21
|
+
Lint/RaiseException:
|
22
|
+
Enabled: true
|
23
|
+
|
24
|
+
Lint/StructNewOverride:
|
25
|
+
Enabled: true
|
26
|
+
|
27
|
+
Style/ExponentialNotation:
|
28
|
+
Enabled: true
|
29
|
+
|
30
|
+
Style/HashEachMethods:
|
31
|
+
Enabled: true
|
32
|
+
|
33
|
+
Style/HashTransformKeys:
|
34
|
+
Enabled: true
|
35
|
+
|
36
|
+
Style/HashTransformValues:
|
37
|
+
Enabled: true
|
data/Guardfile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A sample Guardfile
|
4
|
+
# More info at https://github.com/guard/guard#readme
|
5
|
+
|
6
|
+
## Uncomment and set this to only include directories you want to watch
|
7
|
+
# directories %w(app lib config test spec features) \
|
8
|
+
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
9
|
+
|
10
|
+
## Note: if you are using the `directories` clause above and you are not
|
11
|
+
## watching the project directory ('.'), then you will want to move
|
12
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
13
|
+
#
|
14
|
+
# $ mkdir config
|
15
|
+
# $ mv Guardfile config/
|
16
|
+
# $ ln -s config/Guardfile .
|
17
|
+
#
|
18
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
19
|
+
|
20
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
21
|
+
# rspec may be run, below are examples of the most common uses.
|
22
|
+
# * bundler: 'bundle exec rspec'
|
23
|
+
# * bundler binstubs: 'bin/rspec'
|
24
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
25
|
+
# installed the spring binstubs per the docs)
|
26
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
27
|
+
# * 'just' rspec: 'rspec'
|
28
|
+
|
29
|
+
# rubocop:disable Metrics/BlockLength
|
30
|
+
guard :rspec, cmd: 'bundle exec rspec -f progress' do
|
31
|
+
require 'guard/rspec/dsl'
|
32
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
33
|
+
|
34
|
+
# Feel free to open issues for suggestions and improvements
|
35
|
+
|
36
|
+
# RSpec files
|
37
|
+
rspec = dsl.rspec
|
38
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
39
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
40
|
+
watch(rspec.spec_files)
|
41
|
+
|
42
|
+
# Ruby files
|
43
|
+
ruby = dsl.ruby
|
44
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
45
|
+
|
46
|
+
# Rails files
|
47
|
+
rails = dsl.rails(view_extensions: %w[erb haml slim])
|
48
|
+
dsl.watch_spec_files_for(rails.app_files)
|
49
|
+
dsl.watch_spec_files_for(rails.views)
|
50
|
+
|
51
|
+
watch(rails.controllers) do |m|
|
52
|
+
[
|
53
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
54
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
55
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
56
|
+
]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Rails config changes
|
60
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
61
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
62
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
63
|
+
|
64
|
+
# Capybara features specs
|
65
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
66
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
67
|
+
|
68
|
+
# Turnip features and steps
|
69
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
70
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
71
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
# rubocop:enable Metrics/BlockLength
|
75
|
+
|
76
|
+
guard :rubocop do
|
77
|
+
watch(/.+\.rb$/)
|
78
|
+
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
|
79
|
+
end
|
80
|
+
|
81
|
+
guard :bundler do
|
82
|
+
require 'guard/bundler'
|
83
|
+
require 'guard/bundler/verify'
|
84
|
+
helper = Guard::Bundler::Verify.new
|
85
|
+
|
86
|
+
files = ['Gemfile']
|
87
|
+
files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
|
88
|
+
|
89
|
+
# Assume files are symlinked from somewhere
|
90
|
+
files.each { |file| watch(helper.real_path(file)) }
|
91
|
+
end
|
data/README.md
CHANGED
@@ -1,285 +1,23 @@
|
|
1
|
+
<h1 align="center">
|
2
|
+
<img src="https://raw.githubusercontent.com/BillyRuffian/faker_maker/master/img/unipug.svg?sanitize=true" alt="Faker Maker" height="200">
|
3
|
+
<br>
|
4
|
+
Faker Maker
|
5
|
+
<br>
|
6
|
+
</h1>
|
1
7
|
|
2
|
-
|
3
|
-
|
4
|
-
|
8
|
+
<h4 align="center">
|
9
|
+
Factories over Fixtures
|
10
|
+
</h4>
|
5
11
|
|
6
|
-
|
12
|
+
<div align="center">
|
7
13
|
|
8
|
-
|
14
|
+
[![Gem Version](https://badge.fury.io/rb/faker_maker.svg)](https://badge.fury.io/rb/faker_maker)
|
15
|
+
![CircleCI branch](https://img.shields.io/circleci/project/github/BillyRuffian/faker_maker/master.svg?style=flat-square)
|
16
|
+
[![CodeFactor](https://www.codefactor.io/repository/github/billyruffian/faker_maker/badge?style=flat-square)](https://www.codefactor.io/repository/github/billyruffian/faker_maker)
|
17
|
+
![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/BillyRuffian/faker_maker.svg?style=flat-square)
|
9
18
|
|
10
|
-
|
19
|
+
</div>
|
11
20
|
|
12
|
-
|
21
|
+
FakerMaker is a simple factory builder so you can throw away your fixtures and generate test data instead.
|
13
22
|
|
14
|
-
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
gem 'faker_maker'
|
18
|
-
```
|
19
|
-
|
20
|
-
And then execute:
|
21
|
-
|
22
|
-
$ bundle
|
23
|
-
|
24
|
-
Or install it yourself as:
|
25
|
-
|
26
|
-
$ gem install faker_maker
|
27
|
-
|
28
|
-
## Usage
|
29
|
-
|
30
|
-
FakerMaker generates factories that build disposable objects for testing. Each factory has a name and a set of attributes.
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
FakerMaker.factory :user do
|
34
|
-
name {'Patsy Stone'}
|
35
|
-
email {'patsy@fabulous.co.uk'}
|
36
|
-
admin {false}
|
37
|
-
end
|
38
|
-
```
|
39
|
-
|
40
|
-
This will generate a `User` class with the attributes `name`, `email` and `admin` which will always return the same value.
|
41
|
-
|
42
|
-
It is possible to explicitly set the name of class which is particularly useful if there is a risk of redefining an existing one.
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
FakerMaker.factory :user, class: 'EmailUser' do
|
46
|
-
name {'Patsy Stone'}
|
47
|
-
email {'patsy@fabulous.co.uk'}
|
48
|
-
admin {false}
|
49
|
-
end
|
50
|
-
```
|
51
|
-
|
52
|
-
The class name will always be turned into a Ruby-style class name so `email_user` would become `EmailUser`.
|
53
|
-
|
54
|
-
Because of the block syntax in Ruby, defining attributes as `Hash`es requires two sets of curly brackets:
|
55
|
-
|
56
|
-
```ruby
|
57
|
-
FakerMaker.factory :response do
|
58
|
-
body { { title: 'First Post', content: 'This is part of a hash' } }
|
59
|
-
end
|
60
|
-
```
|
61
|
-
|
62
|
-
Blocks are executed in the context of their instance. This means you can refer to variables already defined:
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
FakerMaker.factory :user, class: 'EmailUser' do
|
66
|
-
title {'Ms'}
|
67
|
-
name {'Patsy Stone'}
|
68
|
-
formal_name {"#{title} #{name}"}
|
69
|
-
email {'patsy@fabulous.co.uk'}
|
70
|
-
admin {false}
|
71
|
-
end
|
72
|
-
```
|
73
|
-
|
74
|
-
### Inheritance
|
75
|
-
|
76
|
-
FakerMaker can exploit the Ruby class hierarchy to provide additional specialisation or to override some behaviours:
|
77
|
-
|
78
|
-
```ruby
|
79
|
-
FakerMaker.factory :vehicle do
|
80
|
-
wheels { 4 }
|
81
|
-
colour { Faker::Color.color_name }
|
82
|
-
engine_capacity { rand( 600..2500 ) }
|
83
|
-
end
|
84
|
-
|
85
|
-
FakerMaker.factory :motorbike, parent: :vehicle do
|
86
|
-
wheels { 2 }
|
87
|
-
sidecar { [true, false].sample }
|
88
|
-
end
|
89
|
-
```
|
90
|
-
|
91
|
-
This is the equivalent of:
|
92
|
-
|
93
|
-
```ruby
|
94
|
-
class Vehicle < Object
|
95
|
-
# ...
|
96
|
-
end
|
97
|
-
|
98
|
-
class Motorbike < Vehicle
|
99
|
-
# ...
|
100
|
-
end
|
101
|
-
```
|
102
|
-
|
103
|
-
so a motorbike will still have a colour and engine capacity between 600 and 2500.
|
104
|
-
|
105
|
-
### Arrays
|
106
|
-
|
107
|
-
It is possible to declare an attribute as having multiple values.
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
FakerMaker.factory :basket do
|
111
|
-
items( has: 10 ) { Faker::Commerce.product_name }
|
112
|
-
end
|
113
|
-
```
|
114
|
-
|
115
|
-
or to pick random number of attributes from a range:
|
116
|
-
|
117
|
-
```ruby
|
118
|
-
FakerMaker.factory :basket do
|
119
|
-
items( has: 5..20 ) { Faker::Commerce.product_name }
|
120
|
-
end
|
121
|
-
```
|
122
|
-
|
123
|
-
A range always generates an array, even if the range produces 1 items or the range is `0..1`.
|
124
|
-
|
125
|
-
It is possible to force an attribute to always be an array, even if only produces one item.
|
126
|
-
|
127
|
-
```ruby
|
128
|
-
FakerMaker.factory :basket do
|
129
|
-
items( array: true ) { Faker::Commerce.product_name }
|
130
|
-
end
|
131
|
-
```
|
132
|
-
|
133
|
-
You can always use long-form block syntax...
|
134
|
-
|
135
|
-
```ruby
|
136
|
-
FakerMaker.factory :basket do
|
137
|
-
items has: 5..20 do
|
138
|
-
Faker::Commerce.product_name
|
139
|
-
end
|
140
|
-
end
|
141
|
-
```
|
142
|
-
|
143
|
-
### Organising dependencies
|
144
|
-
|
145
|
-
Factory definition files are Plain Ol' Ruby. If you depend on another factory because you either extend from it or use it just add a `require` or (depending on your load path) `require_relative` to the top of your file.
|
146
|
-
|
147
|
-
### JSON field names
|
148
|
-
|
149
|
-
JavaScript likes to use camelCase, Ruby's idiom is to use snake_case. This can make make manipulating factory-built objects in ruby ugly. To avoid this, you can call your fields one thing and ask the JSON outputter to rename the field when generating JSON.
|
150
|
-
|
151
|
-
```ruby
|
152
|
-
FakerMaker.factory :vehicle do
|
153
|
-
wheels { 4 }
|
154
|
-
colour { Faker::Color.color_name }
|
155
|
-
engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) }
|
156
|
-
end
|
157
|
-
|
158
|
-
v = FM[:vehicle].build
|
159
|
-
v.engine_capacity = 125
|
160
|
-
```
|
161
|
-
|
162
|
-
and calls to `as_json` and `to_json` will report the fieldname as `engineCapacity`.
|
163
|
-
|
164
|
-
```ruby
|
165
|
-
v.to_json
|
166
|
-
|
167
|
-
=> "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
|
168
|
-
```
|
169
|
-
|
170
|
-
### Building instances
|
171
|
-
|
172
|
-
Instances are Plain Ol' Ruby Objects and the attributes are attached with getters and setters with their values assigned to the value return from their block at build time.
|
173
|
-
|
174
|
-
To build an object:
|
175
|
-
|
176
|
-
```ruby
|
177
|
-
result = FakerMaker[:basket].build
|
178
|
-
```
|
179
|
-
|
180
|
-
will generate a new instance using the Basket factory. Because an actual class is defined, you can instantiate an object directly through `Basket.new` but that will not populate any of the attributes.
|
181
|
-
|
182
|
-
It's possible to override attributes at build-time, either by passing values as a hash:
|
183
|
-
|
184
|
-
```ruby
|
185
|
-
result = FakerMaker[:item].build( name: 'Electric Blanket' )
|
186
|
-
```
|
187
|
-
|
188
|
-
or by passing in a block:
|
189
|
-
|
190
|
-
```ruby
|
191
|
-
result = FakerMaker[:item].build{ |i| i.name = 'Electric Sheep' }
|
192
|
-
```
|
193
|
-
|
194
|
-
this is particularly useful for overriding nested values, since all the getters and setters of the embedded objects are already constructed:
|
195
|
-
|
196
|
-
```ruby
|
197
|
-
result = FakerMaker[:basket].build do |b|
|
198
|
-
b.items.first.name = 'Neon Badger'
|
199
|
-
end
|
200
|
-
```
|
201
|
-
|
202
|
-
if you're crazy enough to want to do both styles during creation, the values in the block will be preserved, e.g.
|
203
|
-
|
204
|
-
```ruby
|
205
|
-
result = FakerMaker[:item].build( name: 'Electric Blanket' ) do |i|
|
206
|
-
i.name = 'Electric Sheep'
|
207
|
-
end
|
208
|
-
```
|
209
|
-
|
210
|
-
then the value of `result.name` is 'Electric Sheep'.
|
211
|
-
|
212
|
-
Beware when overriding values in this way: there is no type checking. You will get an exception if you try to set a value to an attribute that doesn't exist but you won't get one if you assign, say, an array of values where you would otherwise have a string and vice versa.
|
213
|
-
|
214
|
-
Calling `result.to_json` will give a stringified JSON representation. Because ActiveSupport is used under the covers, `as_json` will give you a `Hash` rather than the stringified version.
|
215
|
-
|
216
|
-
As a convenience, you can request a JSON representation directly:
|
217
|
-
|
218
|
-
```ruby
|
219
|
-
result = FakerMaker[:basket].to_json
|
220
|
-
```
|
221
|
-
|
222
|
-
As another convenience, `FakerMaker` is also assigned to the variable `FM` to it is possible to write just:
|
223
|
-
|
224
|
-
```ruby
|
225
|
-
result = FM[:basket].build
|
226
|
-
```
|
227
|
-
|
228
|
-
### Omitting fields
|
229
|
-
|
230
|
-
Sometimes you want a field present, other times you don't. This is often the case when you want to skip fields which have null or empty values.
|
231
|
-
|
232
|
-
```ruby
|
233
|
-
FakerMaker.factory :user do
|
234
|
-
name {'Patsy Stone'}
|
235
|
-
email(omit: :nil) {'patsy@fabulous.co.uk'}
|
236
|
-
admin {false}
|
237
|
-
end
|
238
|
-
|
239
|
-
FM[:user].build.as_json
|
240
|
-
=> {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk", :admin=>false}
|
241
|
-
|
242
|
-
FM[:user].build(email: nil).as_json
|
243
|
-
=> {:name=>"Patsy Stone", :admin=>false}
|
244
|
-
```
|
245
|
-
|
246
|
-
The `omit` modifier can take a single value or an array. If it is passed a value and the attribute equals this value, it will not be included in the output from `as_json` (which returns a Ruby Hash) or in `to_json` methods.
|
247
|
-
|
248
|
-
There are three special modifiers:
|
249
|
-
|
250
|
-
* `:nil` (symbol) to omit output when the attribute is set to nil
|
251
|
-
* `:empty` to omit output when the value is an empty string, an empty array or an empty hash
|
252
|
-
* `:always` to never output this attribute.
|
253
|
-
|
254
|
-
These can be mixed with real values, e.g.
|
255
|
-
|
256
|
-
```ruby
|
257
|
-
FakerMaker.factory :user do
|
258
|
-
name {'Patsy Stone'}
|
259
|
-
email(omit: [:nil, :empty, 'test@foobar.com']) {'patsy@fabulous.co.uk'}
|
260
|
-
admin {false}
|
261
|
-
end
|
262
|
-
```
|
263
|
-
|
264
|
-
### Embedding factories
|
265
|
-
|
266
|
-
To use factories with factories, the following pattern is recommended:
|
267
|
-
|
268
|
-
```ruby
|
269
|
-
FakerMaker.factory :item do
|
270
|
-
name { Faker::Commerce.product_name }
|
271
|
-
price { Faker::Commerce.price }
|
272
|
-
end
|
273
|
-
|
274
|
-
FakerMaker.factory :basket do
|
275
|
-
items( has: 10 ) { FakerMaker[:item].build }
|
276
|
-
end
|
277
|
-
```
|
278
|
-
|
279
|
-
## Contributing
|
280
|
-
|
281
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/BillyRuffian/faker_maker.
|
282
|
-
|
283
|
-
## License
|
284
|
-
|
285
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
23
|
+
Read the [documentation here](https://billyruffian.github.io/faker_maker/).
|
data/_config.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
theme: jekyll-theme-slate
|
data/docs/.bundle/config
ADDED
data/docs/.keep
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
data/docs/_config.yml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
remote_theme: pmarsceill/just-the-docs
|
2
|
+
logo: "https://raw.githubusercontent.com/BillyRuffian/faker_maker/master/docs/logo.png"
|
3
|
+
title: Faker Maker
|
4
|
+
description: A simple data generator with a straighforward syntax for Ruby
|
5
|
+
aux_links:
|
6
|
+
"Faker Maker on GitHub":
|
7
|
+
- "//github.com/BillyRuffian/faker_maker"
|
8
|
+
footer_content: "Copyright © 2019-2020 Nigel Brookes-Thomas. Distributed by an <a href=\"https://github.com/BillyRuffian/faker_maker/blob/master/LICENSE.txt\">MIT license.</a>"
|