fixture_dependencies 1.6.0 → 1.7.0
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.md +27 -0
- data/lib/fixture_dependencies.rb +27 -5
- data/lib/fixture_dependencies/rspec/sequel.rb +8 -0
- data/lib/fixture_dependencies/test_unit.rb +8 -0
- 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: f8ad357a8dd82bac97d857dbff858e3e319b37a8
|
4
|
+
data.tar.gz: fdadc103e8677cd3cbfc4ea66469ec239162925d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94338b1ff1c00c1da084ab47ebe717d0623a6ba2c762b104626d1da58cb92f0d35c3188f9423492f877f2a568577ab3819aca330d809e96ac48365e402f01b06
|
7
|
+
data.tar.gz: 00230fb715dbec6733402c0ec3400324f3d872fdcc1966d0611bce385e122f0d65b77a4326343547fc4fd2520ecf06c82b3eec0214d49b39a6cb4b07ad765314
|
data/README.md
CHANGED
@@ -187,6 +187,33 @@ the loosest coupling possible. Here's an example:
|
|
187
187
|
Don't worry about loading the same fixture twice, if a fixture is already
|
188
188
|
loaded, it won't attempt to load it again.
|
189
189
|
|
190
|
+
## Loading attributes only
|
191
|
+
|
192
|
+
You can load only the attributes of fixtures, without saving them with
|
193
|
+
load\_attributes. This is useful for occasions where you want to mutate
|
194
|
+
attributes without having to create lots of fixtures or want to test
|
195
|
+
code that is run before or after the database transaction (validations,
|
196
|
+
model hooks).
|
197
|
+
|
198
|
+
```
|
199
|
+
# load_attributes responds like load, but without saving the record
|
200
|
+
fruit = load_attributes(:fruit__banana)
|
201
|
+
# test the behaviour before saving the record
|
202
|
+
fruit.save
|
203
|
+
# test the behaviour after saving the record
|
204
|
+
```
|
205
|
+
|
206
|
+
You can also use the build method for loading the attributes of a
|
207
|
+
single record, merging the attributes passed as options. This is useful
|
208
|
+
for testing changes in behaviour when mutating a single parameter:
|
209
|
+
|
210
|
+
```
|
211
|
+
old_banana = build(:fruit__banana, :age=>'old')
|
212
|
+
fresh_banana = build(:fruit__banana, :age=>'new')
|
213
|
+
old_banana.must_be :rotten?
|
214
|
+
new_banana.wont_be :rotten?
|
215
|
+
```
|
216
|
+
|
190
217
|
## one_to_many/many_to_many/has_many/has_and_belongs_to_many assocations
|
191
218
|
|
192
219
|
Here's an example of using has_one (logon_information), has_many (assets), and
|
data/lib/fixture_dependencies.rb
CHANGED
@@ -31,6 +31,26 @@ class FixtureDependencies
|
|
31
31
|
# This will load the data from the yaml files for each argument whose model
|
32
32
|
# is not already in the fixture hash.
|
33
33
|
def self.load(*records)
|
34
|
+
load_with_options(records)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Load the attributes for the record arguments. This method responds
|
38
|
+
# to the same interface as 'load', the difference being that has_many
|
39
|
+
# associations are not loaded.
|
40
|
+
def self.load_attributes(*records)
|
41
|
+
load_with_options(records, :attributes_only=>true)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Loads the attribute for a single record, merging optional attributes.
|
45
|
+
def self.build(record, attributes = {})
|
46
|
+
obj = FixtureDependencies.load_attributes([record])
|
47
|
+
|
48
|
+
attributes.each { |key, value| obj.send("#{key}=", value) }
|
49
|
+
|
50
|
+
obj
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.load_with_options(records, opts = {})
|
34
54
|
ret = records.map do |record|
|
35
55
|
if record.is_a?(Hash)
|
36
56
|
record.map do |k, vals|
|
@@ -43,14 +63,14 @@ class FixtureDependencies
|
|
43
63
|
end.flatten.compact.map do |record|
|
44
64
|
model_name, name = split_name(record)
|
45
65
|
if name
|
46
|
-
use(record.to_sym)
|
66
|
+
use(record.to_sym, opts)
|
47
67
|
else
|
48
68
|
model_name = model_name.singularize
|
49
69
|
unless loaded[model_name.to_sym]
|
50
70
|
puts "loading #{model_name}.yml" if verbose > 0
|
51
71
|
load_yaml(model_name)
|
52
72
|
end
|
53
|
-
fixtures[model_name.to_sym].keys.map{|name| use(:"#{model_name}__#{name}")}
|
73
|
+
fixtures[model_name.to_sym].keys.map{|name| use(:"#{model_name}__#{name}", opts)}
|
54
74
|
end
|
55
75
|
end
|
56
76
|
ret.length == 1 ? ret[0] : ret
|
@@ -142,7 +162,7 @@ class << FixtureDependencies
|
|
142
162
|
# the database, return it. Will check the yaml file for fixtures if no
|
143
163
|
# fixtures yet exist for the model. If the fixture isn't in the fixture
|
144
164
|
# hash, raise an error.
|
145
|
-
def use(record, loading = [], procs = {})
|
165
|
+
def use(record, opts = {}, loading = [], procs = {})
|
146
166
|
spaces = " " * loading.length
|
147
167
|
puts "#{spaces}using #{record}" if verbose > 0
|
148
168
|
puts "#{spaces}load stack:#{loading.inspect}" if verbose > 1
|
@@ -218,7 +238,7 @@ class << FixtureDependencies
|
|
218
238
|
else
|
219
239
|
# Regular assocation, load it
|
220
240
|
puts "#{spaces}#{record}.#{attr}: belongs_to:#{dep_name}" if verbose > 1
|
221
|
-
use(dep_name, loading, procs)
|
241
|
+
use(dep_name, {}, loading, procs)
|
222
242
|
value = get(dep_name)
|
223
243
|
end
|
224
244
|
else
|
@@ -230,6 +250,8 @@ class << FixtureDependencies
|
|
230
250
|
obj.send("#{attr}=", value)
|
231
251
|
end
|
232
252
|
|
253
|
+
return obj if opts[:attributes_only]
|
254
|
+
|
233
255
|
puts "#{spaces}saving #{record}" if verbose > 1
|
234
256
|
|
235
257
|
model_method(:model_save, mtype, obj)
|
@@ -260,7 +282,7 @@ class << FixtureDependencies
|
|
260
282
|
else
|
261
283
|
# Regular association, add it
|
262
284
|
puts "#{spaces}#{record}.#{attr}: #{rtype}:#{dep_name}" if verbose > 1
|
263
|
-
model_method(:add_associated_object, mtype, reflection, attr, obj, use(dep_name, loading, procs))
|
285
|
+
model_method(:add_associated_object, mtype, reflection, attr, obj, use(dep_name, {}, loading, procs))
|
264
286
|
end
|
265
287
|
end
|
266
288
|
end
|
@@ -9,6 +9,14 @@ module Test
|
|
9
9
|
def load(*fixture)
|
10
10
|
FixtureDependencies.load(*fixture)
|
11
11
|
end
|
12
|
+
|
13
|
+
def load_attributes(*args)
|
14
|
+
FixtureDependencies.load_attributes(*args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def build(*args)
|
18
|
+
FixtureDependencies.build(*args)
|
19
|
+
end
|
12
20
|
end
|
13
21
|
end
|
14
22
|
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.7.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: 2016-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: code@jeremyevans.net
|
@@ -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.5.1
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Sequel/ActiveRecord fixture loader that handles dependency graphs
|