fixture_builder 0.5.1.rc4 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.markdown +66 -36
- data/lib/fixture_builder/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 499d1a3d06e5d9732242c5ad93180428b5096c0f
|
4
|
+
data.tar.gz: 314a0687affb10304071948600b16be784b86522
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb6dba58db8919c5505aab4e195dc1a9893f8694b5f890769f9181a3f2029b9445d49c903684f6b4e416db0dc628bdde78d8164b303c4353e9f26f739ed52324
|
7
|
+
data.tar.gz: 15bd4e3b6cb7a7500637ed77e0e5822be2d90b0207b13b21bd76a54127c089e282cd847ae50c857421fa6dd24faf3072341b9c6545a3123d4ebb23b4c57bbe29
|
data/README.markdown
CHANGED
@@ -9,23 +9,32 @@ shared across all your tests and development environment.
|
|
9
9
|
|
10
10
|
The best of all worlds!
|
11
11
|
|
12
|
-
* Speed
|
12
|
+
* **Speed**: Leverage the high-performance speed of Rails' transactional tests/fixtures to avoid test suite slowdown
|
13
13
|
as your app's number of tests grows, because [creating and persisting data is slow!](https://robots.thoughtbot.com/speed-up-tests-by-selectively-avoiding-factory-girl)
|
14
|
-
* Maintainability/
|
14
|
+
* **Maintainability/Reuse/Abstraction**: Use object mother factories to generate fixtures via
|
15
15
|
FactoryGirl or your favorite tool
|
16
|
-
* Flexibility
|
16
|
+
* **Flexibility**: You can always fall back to object mothers in tests if needed, or load a fixture
|
17
17
|
and modify only an attribute or two without the overhead of creating an entire object dependency graph.
|
18
|
-
* Consistency
|
18
|
+
* **Consistency**: Use the exact same fixture data in all environments: test, development, and demo/staging servers.
|
19
19
|
Makes reproduction and acceptance testing of bugs/features faster and easier!
|
20
|
-
* Simplicity
|
20
|
+
* **Simplicity**: Avoid having to maintain and generate `seeds.rb` sample data set separately from your test fixture/factory data set,
|
21
21
|
or [pick which of the myriad seeds helper gems to use](https://rubygems.org/search?query=seed). *Just delete
|
22
22
|
`seeds.rb` and forget about it!*
|
23
23
|
|
24
24
|
Installing
|
25
25
|
==========
|
26
26
|
|
27
|
-
1. Install
|
28
|
-
|
27
|
+
1. Install:
|
28
|
+
* Directly: `gem install fixture_builder`
|
29
|
+
* Bundler:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# Gemfile
|
33
|
+
group :development, :test do
|
34
|
+
gem 'fixture_builder'
|
35
|
+
|
36
|
+
```
|
37
|
+
1. Create a file which configures and declares your fixtures (see below for examples)
|
29
38
|
1. Require the above file in your `spec_helper.rb` or `test_helper.rb`
|
30
39
|
1. If you are using rspec, ensure you have
|
31
40
|
* Set the `FIXTURES_PATH` in `config/application.rb` (not test.rb, or you can't use `rake db:fixtures:load`). E.g.:
|
@@ -52,10 +61,10 @@ Usage
|
|
52
61
|
|
53
62
|
* When running tests/specs, fixtures will build/rebuild automatically as needed
|
54
63
|
* `rake spec:fixture_builder:build` to force a build of fixtures
|
55
|
-
* `rake spec:fixture_builder:
|
64
|
+
* `rake spec:fixture_builder:clean` to delete all existing fixture files
|
65
|
+
* `rake spec:fixture_builder:rebuild` to force a rebuild of fixtures (just a clean + build)
|
56
66
|
* `rake db:fixtures:load` to load built fixtures into your development environment (this is a standard Rails rake task)
|
57
67
|
|
58
|
-
|
59
68
|
Configuration Example
|
60
69
|
=====================
|
61
70
|
|
@@ -137,7 +146,7 @@ By default these are set as:
|
|
137
146
|
|
138
147
|
* files_to_check: %w{ db/schema.rb }
|
139
148
|
* fixture_builder_file: RAILS_ROOT/tmp/fixture_builder.yml
|
140
|
-
* record_name_fields: %w{
|
149
|
+
* record_name_fields: %w{ unique_name display_name name title username login }
|
141
150
|
* skip_tables: %w{ schema_migrations }
|
142
151
|
* select_sql: SELECT * FROM %{table}
|
143
152
|
* delete_sql: DELETE FROM %{table}
|
@@ -150,7 +159,7 @@ When the fixtures are generated only as needed, sometimes the process that
|
|
150
159
|
generates the fixtures will be different than the process that runs the tests.
|
151
160
|
This results in collisions when you still use factories in your tests.
|
152
161
|
|
153
|
-
There's a couple of
|
162
|
+
There's a couple of solutions for this.
|
154
163
|
|
155
164
|
Here's a solution for FactoryGirl which resets sequences numbers to 1000
|
156
165
|
(to avoid conflicts with fixture data which should be sequenced < 1000)
|
@@ -168,7 +177,7 @@ end
|
|
168
177
|
FactoryGirl.sequences.each do |seq|
|
169
178
|
|
170
179
|
# Factory Girl 4 uses an Enumerator Adapter, otherwise simply set a Fixnum
|
171
|
-
seq.instance_variable_set(:@value, FactoryGirl::Sequence::EnumeratorAdapter.new(
|
180
|
+
seq.instance_variable_set(:@value, FactoryGirl::Sequence::EnumeratorAdapter.new(1000))
|
172
181
|
|
173
182
|
end
|
174
183
|
```
|
@@ -186,8 +195,45 @@ end
|
|
186
195
|
|
187
196
|
```
|
188
197
|
|
189
|
-
|
190
|
-
|
198
|
+
It's probably a good idea to use both of these approaches together, especially if you are
|
199
|
+
going to fall back to using FactoryGirl object mothers in addition to fixtures.
|
200
|
+
|
201
|
+
Tips
|
202
|
+
====
|
203
|
+
|
204
|
+
* Don't use `seeds.rb` (just delete it). Instead, just use `rake db:fixtures:load` to get fixtures into dev.
|
205
|
+
* If you want fixture data on a staging/demo environment, either run `db:fixtures:load` on that environment, or
|
206
|
+
load fixtures into the dev with `rake db:fixtures:load`, dump the dev database, then load it on your environment.
|
207
|
+
* Always use fixtures instead of object mothers in tests when possible - this will keep your test suite fast!
|
208
|
+
[Even FactoryGirl says to avoid using factories when you can, because creating and persisting data is slow](https://robots.thoughtbot.com/speed-up-tests-by-selectively-avoiding-factory-girl)
|
209
|
+
* If you only need to tweak an attribute or two to test an edge case, load the fixture object,
|
210
|
+
then just set the attribute on the object (if you don't need it persisted, this is fastest), or
|
211
|
+
set it via `#update_attributes!` (only if you need it persisted, this is slower).
|
212
|
+
* Avoid referring to any fixtures by ID anywhere, unless you hardcode the ID when creating it. They can change
|
213
|
+
if you add more fixtures in the future and cause tests to break.
|
214
|
+
* To set up associations between different types of created fixture model objects, you can
|
215
|
+
use a couple of approaches:
|
216
|
+
1. When creating fixtures, keep a hash of all created models by type + name (not ID), and then look them up
|
217
|
+
out of the hash to use as an associated object when creating subsequent related objects.
|
218
|
+
1. Do a `MyModel.find_by_some_unique_field` to find a previously created instance that didn't have a name.
|
219
|
+
* If you delete a table, old fixture files for the deleted table can hang around and still get loaded
|
220
|
+
into the database, causing confusion or errors. Use `rake spec:fixture_builder:clean` or
|
221
|
+
`rake spec:fixture_builder:rebuild` to ensure they get cleaned up.
|
222
|
+
* As you build more advanced fixture creation logic for your app's domain and try to DRY it up, you'll probably
|
223
|
+
end up having an easier time if:
|
224
|
+
1. You don't use any namespaced models
|
225
|
+
1. You keep your factory names consistent and exactly matching your model names
|
226
|
+
* Modify `bin/setup` to run fixture builder and load your dev database:
|
227
|
+
```ruby
|
228
|
+
puts "\n== Building fixtures =="
|
229
|
+
system! 'bin/rails spec:fixture_builder:rebuild'
|
230
|
+
|
231
|
+
puts "\n== Loading fixtures into dev database =="
|
232
|
+
system! 'bin/rails db:fixtures:load'
|
233
|
+
```
|
234
|
+
|
235
|
+
More Complete Config Example
|
236
|
+
============================
|
191
237
|
|
192
238
|
As you get more fixtures, you may want to move the creation of fixtures to a separate file. For example:
|
193
239
|
|
@@ -208,6 +254,11 @@ FixtureBuilder.configure do |fbuilder|
|
|
208
254
|
CreateFixtures.new(fbuilder).create_all
|
209
255
|
end
|
210
256
|
end
|
257
|
+
|
258
|
+
# Have factory girl generate non-colliding sequences starting at 1000 for data created after the fixtures
|
259
|
+
FactoryGirl.sequences.each do |seq|
|
260
|
+
seq.instance_variable_set(:@value, FactoryGirl::Sequence::EnumeratorAdapter.new(1000))
|
261
|
+
end
|
211
262
|
```
|
212
263
|
|
213
264
|
Then, you can do more extensive and advanced fixture creation in that class. Here's
|
@@ -252,32 +303,11 @@ class CreateFixtures
|
|
252
303
|
|
253
304
|
# other creation and helper methods to abstract common logic, e.g.
|
254
305
|
# * custom naming rules via #name_model_with
|
255
|
-
# * set up associations by storing created model records in a hash
|
306
|
+
# * set up associations by storing created model records in a hash so you can retrieve them
|
256
307
|
# etc... (hopefully some of these helper patterns can be standardized and included in the gem in the future)
|
257
308
|
end
|
258
309
|
```
|
259
310
|
|
260
|
-
Tips
|
261
|
-
====
|
262
|
-
|
263
|
-
* Don't use `seeds.rb`. Instead, just use `rake db:fixtures:load` to get fixtures into dev. If you
|
264
|
-
want fixture data on a staging/demo environment, either run `db:fixtures:load`, or
|
265
|
-
dump the database and load it there.
|
266
|
-
* Always use fixtures instead of object mothers in tests when possible - this will keep your test suite fast!
|
267
|
-
[Even FactoryGirl says to avoid using factories when you can, because creating and persisting data is slow](https://robots.thoughtbot.com/speed-up-tests-by-selectively-avoiding-factory-girl)
|
268
|
-
* If you only need to tweak an attribute or two to test an edge case, load the fixture object,
|
269
|
-
then just set the attribute on the object (if you don't need it persisted, this is fastest), or
|
270
|
-
set it via `#update_attributes!` (only if you need it persisted, this is slower).
|
271
|
-
* Modify `bin/setup` to run fixture builder and load your dev database:
|
272
|
-
```ruby
|
273
|
-
puts "\n== Building fixtures =="
|
274
|
-
system! 'bin/rails spec:fixture_builder:build'
|
275
|
-
|
276
|
-
puts "\n== Loading fixtures into dev database =="
|
277
|
-
system! 'bin/rails db:fixtures:load'
|
278
|
-
```
|
279
|
-
|
280
|
-
|
281
311
|
Copyright (c) 2009 Ryan Dy & David Stevenson, released under the MIT license
|
282
312
|
|
283
313
|
Currently maintained by [Chad Woolley](thewoolleyman@gmail.com)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixture_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.1
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Dy
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2018-01-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
version: '0'
|
152
152
|
requirements: []
|
153
153
|
rubyforge_project: fixture_builder
|
154
|
-
rubygems_version: 2.4.
|
154
|
+
rubygems_version: 2.4.5.1
|
155
155
|
signing_key:
|
156
156
|
specification_version: 4
|
157
157
|
summary: Build Rails fixtures using object mother factories
|