fixture_dependencies 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{README → README.md} +77 -47
- data/lib/fixture_dependencies.rb +11 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f97d8604529a86b8c656e191bdb5a3c7ee1cbcd
|
4
|
+
data.tar.gz: 344a6f88b868604949ecd69f1cc616fa0fc92772
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9afe16c93a4dde823b5929a3e1437e90a42654b35246f95dcb21e4c01ed0f9c986d3d40e558a30ee0b31154d7ab793e9671fedb99febf5213ae1170055d1fb6d
|
7
|
+
data.tar.gz: 2412abac028e52a3dd52678eb539902eac6a7d28909b56433f98a107e41f0577a9adfa0a47301870f5dedf6817f4b560725c5d680c69cde2b9e0fa8f497ea36d
|
data/{README → README.md}
RENAMED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# fixture_dependencies
|
2
2
|
|
3
3
|
fixture_dependencies is an advanced fixture loader, allowing the loading of
|
4
4
|
models from YAML fixtures, along with their entire dependency graph. It has
|
@@ -16,73 +16,91 @@ the following features:
|
|
16
16
|
helper for RSpec) that load fixtures for every test inside a transaction,
|
17
17
|
so fixture data is never left in your database
|
18
18
|
|
19
|
-
|
19
|
+
## Installation
|
20
20
|
|
21
|
+
```
|
21
22
|
gem install fixture_dependencies
|
22
|
-
|
23
|
-
|
23
|
+
```
|
24
|
+
|
25
|
+
## Source
|
24
26
|
|
25
27
|
Source is available via github:
|
26
28
|
|
29
|
+
```
|
27
30
|
http://github.com/jeremyevans/fixture_dependencies
|
31
|
+
```
|
28
32
|
|
29
33
|
You can check it out with git:
|
30
34
|
|
35
|
+
```
|
31
36
|
git clone git://github.com/jeremyevans/fixture_dependencies.git
|
37
|
+
```
|
32
38
|
|
33
|
-
|
39
|
+
## Usage
|
34
40
|
|
35
|
-
|
41
|
+
### With Rails/ActiveRecord/Test::Unit:
|
36
42
|
|
37
43
|
Add the following to test/test_helper.rb after "require 'test_help'":
|
38
44
|
|
45
|
+
```
|
39
46
|
require 'fixture_dependencies/test_unit/rails'
|
47
|
+
```
|
40
48
|
|
41
49
|
This overrides the default test helper to load the fixtures inside transactions
|
42
50
|
and to use FixtureDependencies to load the fixtures.
|
43
51
|
|
44
|
-
|
52
|
+
### With Sequel/Test::Unit:
|
45
53
|
|
46
54
|
Somewhere before the test code is loaded:
|
47
55
|
|
56
|
+
```
|
48
57
|
require 'fixture_dependencies/test_unit/sequel'
|
49
|
-
|
58
|
+
```
|
59
|
+
|
50
60
|
Make sure the test case classes use FixtureDependencies::SequelTestCase:
|
51
61
|
|
62
|
+
```
|
52
63
|
class ModelTest < FixtureDependencies::SequelTestCase
|
64
|
+
```
|
53
65
|
|
54
66
|
This runs the test cases inside a Sequel transaction.
|
55
67
|
|
56
|
-
|
68
|
+
### With Sequel/RSpec:
|
57
69
|
|
58
70
|
Somewhere before the test code is loaded:
|
59
71
|
|
72
|
+
```
|
60
73
|
require 'fixture_dependencies/rspec/sequel'
|
61
|
-
|
74
|
+
```
|
75
|
+
|
62
76
|
This runs each spec inside a separate Sequel transaction.
|
63
77
|
|
64
|
-
|
78
|
+
### With other testing libraries:
|
65
79
|
|
66
80
|
You can just use FixtureDependencies.load to handle the loading of fixtures.
|
67
81
|
The use of transactions is up to you. One thing you must do if you are
|
68
82
|
not using the rails test helper is to set the fixture path for
|
69
83
|
FixtureDependencies:
|
70
84
|
|
85
|
+
```
|
71
86
|
FixtureDependencies.fixture_path = '/path/to/fixtures'
|
87
|
+
```
|
72
88
|
|
73
|
-
|
89
|
+
## Changes to Rails default fixtures:
|
74
90
|
|
75
91
|
fixture_dependencies is designed to require the least possible changes to
|
76
92
|
the default YAML fixtures used by Rails (well, at least Rails 1.2 and earlier).
|
77
93
|
For example, see the following changes:
|
78
94
|
|
95
|
+
```
|
79
96
|
OLD NEW
|
80
97
|
asset1: asset1:
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
98
|
+
id: 1 id: 1
|
99
|
+
employee_id: 2 employee: jeremy
|
100
|
+
product_id: 3 product: nx7010
|
101
|
+
vendor_id: 2 vendor: lxg_computers
|
102
|
+
note: in working order note: in working order
|
103
|
+
```
|
86
104
|
|
87
105
|
As you can see, you just replace the foreign key attribute and value with the
|
88
106
|
name of the association and the associations name. This assumes you have an
|
@@ -93,7 +111,7 @@ Fixture files still use the table_name of the model. Note that you make sure
|
|
93
111
|
to hard code primary key values for each fixture, as shown in the example
|
94
112
|
above.
|
95
113
|
|
96
|
-
|
114
|
+
## ERB Fixtures
|
97
115
|
|
98
116
|
Fixtures can also use ERB to preprocess the fixture file, useful if you need
|
99
117
|
to do any programming inside the fixture file, such as looping to create
|
@@ -103,13 +121,15 @@ mix ERB fixture files and regular fixture files, but you can not have an
|
|
103
121
|
ERB fixture file and a regular fixture file for the same table (the regular
|
104
122
|
fixture file will be used in that case).
|
105
123
|
|
106
|
-
|
124
|
+
## Changes to the fixtures Class Method:
|
107
125
|
|
108
126
|
fixture_dependencies can still use the fixtures class method in your test:
|
109
127
|
|
128
|
+
```
|
110
129
|
class EmployeeTest < Test::Unit::TestCase
|
111
130
|
fixtures :assets
|
112
131
|
end
|
132
|
+
```
|
113
133
|
|
114
134
|
In Rails default testing practices, the arguments to fixtures are table names.
|
115
135
|
fixture_dependencies changes this to underscored model names. If you are using
|
@@ -119,39 +139,42 @@ It is recommended that you do not use the fixtures method, and instead load
|
|
119
139
|
individual fixtures as needed (see below). This makes your tests much more
|
120
140
|
robust, in case you want to add or remove individual fixtures at a later date.
|
121
141
|
|
122
|
-
|
142
|
+
## Loading individual fixtures with fixtures class method
|
123
143
|
|
124
144
|
There is support for loading individual fixtures (and just their dependencies),
|
125
145
|
using the following syntax:
|
126
146
|
|
147
|
+
```
|
127
148
|
class EmployeeTest < Test::Unit::TestCase
|
128
149
|
fixtures :employee__jeremy # Note the double underscore
|
129
150
|
end
|
130
|
-
|
151
|
+
```
|
152
|
+
|
131
153
|
This would load just the jeremy fixture and its dependencies. I find this is
|
132
154
|
much better than loading all fixtures in most of my test suites. Even better
|
133
155
|
is loading just the fixtures you want inside every test method (see below).
|
134
156
|
This leads to the most robust testing.
|
135
157
|
|
136
|
-
|
158
|
+
## Loading fixtures inside test methods
|
137
159
|
|
138
160
|
I find that it is often better to skip the use of the fixtures method entirely,
|
139
161
|
and load the fixtures I want manually in each test method. This provides for
|
140
162
|
the loosest coupling possible. Here's an example:
|
141
163
|
|
164
|
+
```
|
142
165
|
class EmployeeTest < Test::Unit::TestCase
|
143
166
|
def test_employee_name
|
144
167
|
# Load the fixture and return the Employee object
|
145
168
|
employee = load(:employee__jeremy)
|
146
169
|
# Test the employee
|
147
170
|
end
|
148
|
-
|
171
|
+
|
149
172
|
def test_employees
|
150
173
|
# Load the fixtures and return two Employee objects
|
151
174
|
employee1, employee2 = load(:employees=>[:jeremy, :karl])
|
152
175
|
# Test the employees
|
153
176
|
end
|
154
|
-
|
177
|
+
|
155
178
|
def test_award_statistics
|
156
179
|
# Load all fixtures in both tables
|
157
180
|
load(:employee_award__jeremy_first, :award__first)
|
@@ -159,55 +182,58 @@ the loosest coupling possible. Here's an example:
|
|
159
182
|
# (which pulls data from the tables loaded above)
|
160
183
|
end
|
161
184
|
end
|
185
|
+
```
|
162
186
|
|
163
187
|
Don't worry about loading the same fixture twice, if a fixture is already
|
164
188
|
loaded, it won't attempt to load it again.
|
165
189
|
|
166
|
-
|
190
|
+
## one_to_many/many_to_many/has_many/has_and_belongs_to_many assocations
|
167
191
|
|
168
192
|
Here's an example of using has_one (logon_information), has_many (assets), and
|
169
193
|
has_and_belongs_to_many (groups) associations.
|
170
194
|
|
195
|
+
```
|
171
196
|
jeremy:
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
197
|
+
id: 2
|
198
|
+
name: Jeremy Evans
|
199
|
+
logon_information: jeremy
|
200
|
+
assets: [asset1, asset2, asset3]
|
201
|
+
groups: [group1]
|
202
|
+
```
|
203
|
+
|
204
|
+
`logon_information` is a has_one association to another table which was split
|
179
205
|
from the employees table due to database security requirements. Assets is a
|
180
206
|
has_many association, where one employee is responsible for the asset.
|
181
207
|
Employees can be a member of multiple groups, and each group can have multiple
|
182
208
|
employees.
|
183
209
|
|
184
|
-
For has_
|
210
|
+
For `has_*` associations, after fixture_dependencies saves jeremy, it will load
|
185
211
|
and save logon_information (and its dependencies...), it will load each asset
|
186
212
|
in the order specified (and their dependencies...), and it will load all of the
|
187
213
|
groups in the order specified (and their dependencies...). Note that there
|
188
214
|
is only a load order inside a specific association, associations are stored
|
189
215
|
in the same hash as attributes and are loaded in an arbitrary order.
|
190
216
|
|
191
|
-
|
217
|
+
## many_to_many/has_and_belongs_to_many join table fixtures
|
192
218
|
|
193
219
|
Another change is that Rails defaults allow you to specify habtm join tables in
|
194
220
|
fixtures. That doesn't work with fixture dependencies, as there is no
|
195
221
|
associated model. Instead, you use a has_and_belongs_to_many association name
|
196
222
|
in the the appropriate model fixtures (see above).
|
197
223
|
|
198
|
-
|
224
|
+
## Cyclic dependencies
|
199
225
|
|
200
226
|
fixture_dependencies handles almost all cyclic dependencies. It handles all
|
201
227
|
has_many, has_one, and habtm cyclic dependencies. It handles all
|
202
228
|
self-referential cyclic dependencies. It handles all belongs_to cyclic
|
203
229
|
dependencies except the case where there is a NOT NULL or validates_presence of
|
204
|
-
constraint on the cyclic dependency's foreign key.
|
230
|
+
constraint on the cyclic dependency's foreign key.
|
205
231
|
|
206
232
|
For example, a case that won't work is when employee belongs_to supervisor
|
207
233
|
(with a NOT NULL or validates_presence_of constraint on supervisor_id), and
|
208
234
|
john is karl's supervisor and karl is john's supervisor. Since you can't create
|
209
235
|
john without a valid supervisor_id, you need to create karl first, but you
|
210
|
-
can't create karl for the same reason (as john doesn't exist yet).
|
236
|
+
can't create karl for the same reason (as john doesn't exist yet).
|
211
237
|
|
212
238
|
There isn't a generic way to handle the belongs_to cyclic dependency, as far as
|
213
239
|
I know. Deferring foreign key checks could work, but may not be enabled (and
|
@@ -220,7 +246,7 @@ division belongs_to head_of_division when the employee is a member of the
|
|
220
246
|
division and also the head of the division), even that approach is not
|
221
247
|
possible.
|
222
248
|
|
223
|
-
|
249
|
+
## Known issues
|
224
250
|
|
225
251
|
Currently, the plugin only supports YAML fixtures, but other types of fixtures
|
226
252
|
would be fairly easy to add (send me a patch if you add support for another
|
@@ -230,12 +256,12 @@ The plugin is significantly slower than the default testing method, because it
|
|
230
256
|
loads all fixtures inside of a transaction (one per test method), where Rails
|
231
257
|
defaults to loading the fixtures once per test suite (outside of a
|
232
258
|
transaction), and only deletes fixtures from a table when overwriting it with
|
233
|
-
new fixtures.
|
259
|
+
new fixtures.
|
234
260
|
|
235
261
|
Instantiated fixtures are not available with this plugin. Instead, you should
|
236
262
|
use load(:model__fixture_name).
|
237
263
|
|
238
|
-
|
264
|
+
## Namespace Issues
|
239
265
|
|
240
266
|
By default, fixture dependencies is going to load the model with the camelized
|
241
267
|
name in the symbol used. So for :foo_bar__baz, it's going to look for
|
@@ -243,30 +269,34 @@ the fixture with name baz for the model FooBar. If your model is namespaced,
|
|
243
269
|
such as Foo::Bar, this isn't going to work well. In that case, you can
|
244
270
|
override the default mapping:
|
245
271
|
|
272
|
+
```
|
246
273
|
FixtureDependencies.class_map[:bar] = Foo::Bar
|
274
|
+
```
|
247
275
|
|
248
276
|
and then use :bar__baz to load the fixture with name baz for the model
|
249
277
|
Foo::Bar.
|
250
278
|
|
251
|
-
|
279
|
+
## Troubleshooting
|
252
280
|
|
253
281
|
If you run into problems with loading your fixtures, it can be difficult to see
|
254
|
-
where the problems are. To aid in debugging an error, add the following to
|
282
|
+
where the problems are. To aid in debugging an error, add the following to
|
255
283
|
test/test_helper.rb:
|
256
284
|
|
285
|
+
```
|
257
286
|
FixtureDependencies.verbose = 3
|
258
|
-
|
287
|
+
```
|
288
|
+
|
259
289
|
This will give a verbose description of the loading and saving of fixtures for
|
260
290
|
every test, including the recursive loading of the dependency graph.
|
261
291
|
|
262
|
-
|
292
|
+
## Specs
|
263
293
|
|
264
294
|
The specs for fixture dependencies and be run with Rake. They require
|
265
295
|
the sequel, activerecord, and sqlite3 gems installed. The default rake task
|
266
296
|
runs the specs. You should run the spec_migrate task first to create the
|
267
297
|
spec database.
|
268
298
|
|
269
|
-
|
299
|
+
## Similar Ideas
|
270
300
|
|
271
301
|
Rails now supports something similar by default. Honestly, I'm not sure what
|
272
302
|
the differences are.
|
@@ -275,11 +305,11 @@ fixture_references is a similar plugin. It uses erb inside yaml, and uses the
|
|
275
305
|
foreign key numbers inside of the association names, which leads me to believe
|
276
306
|
it doesn't support has_* associations.
|
277
307
|
|
278
|
-
|
308
|
+
## License
|
279
309
|
|
280
310
|
fixture_dependencies is released under the MIT License. See the MIT-LICENSE
|
281
311
|
file for details.
|
282
312
|
|
283
|
-
|
313
|
+
## Author
|
284
314
|
|
285
315
|
Jeremy Evans <code@jeremyevans.net>
|
data/lib/fixture_dependencies.rb
CHANGED
@@ -113,9 +113,9 @@ class << FixtureDependencies
|
|
113
113
|
# A symbol representing the base class of the model, currently
|
114
114
|
# ActiveRecord::Base and Sequel::Model are supported.
|
115
115
|
def model_type(model)
|
116
|
-
if model.ancestors.map
|
116
|
+
if model.ancestors.map(&:to_s).include?('ActiveRecord::Base')
|
117
117
|
:AR
|
118
|
-
elsif model.ancestors.map
|
118
|
+
elsif model.ancestors.map(&:to_s).include?('Sequel::Model')
|
119
119
|
:S
|
120
120
|
else
|
121
121
|
raise TypeError, 'not ActiveRecord or Sequel model'
|
@@ -166,8 +166,17 @@ class << FixtureDependencies
|
|
166
166
|
if model.respond_to?(:sti_load)
|
167
167
|
obj = model.sti_load(model.sti_key => attributes[model.sti_key])
|
168
168
|
obj.send(:initialize)
|
169
|
+
model = obj.model
|
169
170
|
elsif model.respond_to?(:sti_key)
|
170
171
|
obj = attributes[model.sti_key].to_s.camelize.constantize.new
|
172
|
+
elsif model.respond_to?(:cti_key) # support for Sequel's pre-4.24.0 hybrid CTI support
|
173
|
+
mv = attributes[model.cti_key]
|
174
|
+
if (mm = model.cti_model_map)
|
175
|
+
model = mm[mv].to_s.constantize
|
176
|
+
elsif !mv.nil?
|
177
|
+
model = mv.constantize
|
178
|
+
end
|
179
|
+
obj = model.new
|
171
180
|
else
|
172
181
|
obj = model.new
|
173
182
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixture_dependencies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: code@jeremyevans.net
|
@@ -18,7 +18,7 @@ extra_rdoc_files:
|
|
18
18
|
- MIT-LICENSE
|
19
19
|
files:
|
20
20
|
- MIT-LICENSE
|
21
|
-
- README
|
21
|
+
- README.md
|
22
22
|
- lib/fixture_dependencies.rb
|
23
23
|
- lib/fixture_dependencies/active_record.rb
|
24
24
|
- lib/fixture_dependencies/rspec/sequel.rb
|
@@ -35,7 +35,7 @@ post_install_message:
|
|
35
35
|
rdoc_options:
|
36
36
|
- "--inline-source"
|
37
37
|
- "--line-numbers"
|
38
|
-
- README
|
38
|
+
- README.md
|
39
39
|
- lib
|
40
40
|
require_paths:
|
41
41
|
- lib
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
54
|
+
rubygems_version: 2.4.5
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Sequel/ActiveRecord fixture loader that handles dependency graphs
|