fixture_dependencies 1.2.6 → 1.3.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.
- data/MIT-LICENSE +1 -1
- data/README +11 -4
- data/lib/fixture_dependencies.rb +35 -7
- data/lib/fixture_dependencies/active_record.rb +1 -1
- data/lib/fixture_dependencies/sequel.rb +1 -1
- metadata +5 -5
data/MIT-LICENSE
CHANGED
data/README
CHANGED
@@ -7,7 +7,7 @@ the following features:
|
|
7
7
|
- Fixtures specify association names instead of foreign keys
|
8
8
|
- Support both Sequel and ActiveRecord
|
9
9
|
- Supports many_to_one/belongs_to, one_to_many/has_many,
|
10
|
-
many_to_many/has_and_belongs_to_many, and has_one associations
|
10
|
+
many_to_many/has_and_belongs_to_many, and has_one/one_to_one associations
|
11
11
|
- Loads a fixture's dependency graph in such a manner that foreign key
|
12
12
|
constraints aren't violated
|
13
13
|
- Has a very simple API (FixtureDependencies.load(:model__fixture))
|
@@ -128,7 +128,7 @@ using the following syntax:
|
|
128
128
|
|
129
129
|
This would load just the jeremy fixture and its dependencies. I find this is
|
130
130
|
much better than loading all fixtures in most of my test suites. Even better
|
131
|
-
is loading just the fixtures you want
|
131
|
+
is loading just the fixtures you want inside every test method (see below).
|
132
132
|
This leads to the most robust testing.
|
133
133
|
|
134
134
|
== Loading fixtures inside test methods
|
@@ -244,6 +244,13 @@ test/test_helper.rb:
|
|
244
244
|
This will give a verbose description of the loading and saving of fixtures for
|
245
245
|
every test, including the recursive loading of the dependency graph.
|
246
246
|
|
247
|
+
== Specs
|
248
|
+
|
249
|
+
The specs for fixture dependencies and be run with Rake. They require
|
250
|
+
the sequel, activerecord, and sqlite3 gems installed. The default rake task
|
251
|
+
runs the specs. You should run the spec_migrate task first to create the
|
252
|
+
spec database.
|
253
|
+
|
247
254
|
== Similar Ideas
|
248
255
|
|
249
256
|
Rails now supports something similar by default. Honestly, I'm not sure what
|
@@ -255,8 +262,8 @@ it doesn't support has_* associations.
|
|
255
262
|
|
256
263
|
== License
|
257
264
|
|
258
|
-
fixture_dependencies is released under the MIT License. See the LICENSE
|
259
|
-
for details.
|
265
|
+
fixture_dependencies is released under the MIT License. See the MIT-LICENSE
|
266
|
+
file for details.
|
260
267
|
|
261
268
|
== Author
|
262
269
|
|
data/lib/fixture_dependencies.rb
CHANGED
@@ -76,7 +76,7 @@ class << FixtureDependencies
|
|
76
76
|
def get(record)
|
77
77
|
model_name, name = split_name(record)
|
78
78
|
model = model_name.camelize.constantize
|
79
|
-
model_method(:model_find, model_type(model), model, fixtures[model_name.to_sym][name.to_sym][model
|
79
|
+
model_method(:model_find, model_type(model), model, fixtures[model_name.to_sym][name.to_sym][fixture_pk(model)])
|
80
80
|
end
|
81
81
|
|
82
82
|
# Adds all fixtures in the yaml fixture file for the model to the fixtures
|
@@ -145,16 +145,19 @@ class << FixtureDependencies
|
|
145
145
|
end
|
146
146
|
mtype = model_type(model)
|
147
147
|
model_method(:raise_model_error, mtype, "Couldn't use fixture #{record.inspect}") unless attributes = fixtures[model_name.to_sym][name.to_sym]
|
148
|
+
fpk = fixture_pk(model)
|
149
|
+
cpk = fpk.is_a?(Array)
|
148
150
|
# return if object has already been loaded into the database
|
149
|
-
if existing_obj = model_method(:model_find_by_pk, mtype, model, attributes
|
150
|
-
puts "#{spaces}using #{record}: already in database" if verbose > 2
|
151
|
+
if existing_obj = model_method(:model_find_by_pk, mtype, model, fixture_pkv(attributes,fpk))
|
152
|
+
puts "#{spaces}using #{record}: already in database (pk: #{fixture_pkv(attributes,fpk).inspect})" if verbose > 2
|
151
153
|
loading.pop
|
152
154
|
return existing_obj
|
153
155
|
end
|
154
156
|
obj = model.respond_to?(:sti_key) ? attributes[model.sti_key].to_s.camelize.constantize.new : model.new
|
155
|
-
puts "#{spaces}#{model} STI plugin detected, initializing instance of #{obj}" if (verbose > 1 && model.sti_dataset)
|
157
|
+
puts "#{spaces}#{model} STI plugin detected, initializing instance of #{obj}" if (verbose > 1 && model.respond_to?(:sti_dataset))
|
156
158
|
many_associations = []
|
157
159
|
attributes.each do |attr, value|
|
160
|
+
next if attr.is_a?(Array)
|
158
161
|
if reflection = model_method(:reflection, mtype, model, attr.to_sym)
|
159
162
|
if [:belongs_to, :many_to_one].include?(model_method(:reflection_type, mtype, reflection))
|
160
163
|
dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}".to_sym
|
@@ -162,13 +165,20 @@ class << FixtureDependencies
|
|
162
165
|
# Self referential record, use primary key
|
163
166
|
puts "#{spaces}#{record}.#{attr}: belongs_to self-referential" if verbose > 1
|
164
167
|
attr = model_method(:reflection_key, mtype, reflection)
|
165
|
-
value = attributes
|
168
|
+
value = fixture_pkv(attributes,fpk)
|
169
|
+
if cpk
|
170
|
+
attr.zip(value).each do |k, v|
|
171
|
+
puts "#{spaces}#{record}.#{k} = #{v.inspect}" if verbose > 2
|
172
|
+
obj.send("#{k}=", v)
|
173
|
+
end
|
174
|
+
next
|
175
|
+
end
|
166
176
|
elsif loading.include?(dep_name)
|
167
177
|
# Association cycle detected, set foreign key for this model afterward using procs
|
168
178
|
# This is will fail if the column is set to not null or validates_presence_of
|
169
179
|
puts "#{spaces}#{record}.#{attr}: belongs-to cycle detected:#{dep_name}" if verbose > 1
|
170
180
|
(procs[dep_name] ||= []) << Proc.new do |assoc|
|
171
|
-
m = model_method(:model_find, mtype, model, attributes
|
181
|
+
m = model_method(:model_find, mtype, model, fixture_pkv(attributes,fpk))
|
172
182
|
m.send("#{attr}=", assoc)
|
173
183
|
model_method(:model_save, mtype, m)
|
174
184
|
end
|
@@ -192,7 +202,7 @@ class << FixtureDependencies
|
|
192
202
|
|
193
203
|
model_method(:model_save, mtype, obj)
|
194
204
|
# after saving the model, we set the primary key within the fixture hash, in case it was not explicitly specified in the fixture and was generated by an auto_increment / serial field
|
195
|
-
fixtures[model_name.to_sym][name.to_sym][
|
205
|
+
fixtures[model_name.to_sym][name.to_sym][fpk] ||= fixture_pkv(obj,fpk)
|
196
206
|
|
197
207
|
loading.pop
|
198
208
|
# Update the circular references
|
@@ -224,4 +234,22 @@ class << FixtureDependencies
|
|
224
234
|
end
|
225
235
|
obj
|
226
236
|
end
|
237
|
+
|
238
|
+
def fixture_pk(model)
|
239
|
+
case pk = model.primary_key
|
240
|
+
when Symbol, Array
|
241
|
+
pk
|
242
|
+
else
|
243
|
+
pk.to_sym
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def fixture_pkv(attributes, fpk)
|
248
|
+
case fpk
|
249
|
+
when Symbol
|
250
|
+
attributes[fpk]
|
251
|
+
else
|
252
|
+
fpk.map{|v| attributes[v]}
|
253
|
+
end
|
254
|
+
end
|
227
255
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixture_dependencies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeremy Evans
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-04-13 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|