application_seeds 0.1.1 → 0.2.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/CHANGELOG.md +15 -0
- data/README.md +140 -1
- data/lib/application_seeds/version.rb +1 -1
- data/lib/application_seeds.rb +148 -16
- data/spec/application_seeds_spec.rb +122 -30
- data/spec/attributes_spec.rb +6 -6
- data/spec/seed_data/test_data_set/companies.yml +9 -0
- data/spec/seed_data/test_data_set/departments.yml +5 -0
- data/spec/seed_data/test_data_set/people.yml +18 -6
- metadata +8 -4
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
v0.2.0, 2013-12-27
|
2
|
+
------------------
|
3
|
+
|
4
|
+
* Changed the seed data file format from a YAML Array to a YAML Hash
|
5
|
+
* Added support for seed data labels
|
6
|
+
* Added support for generated IDs
|
7
|
+
* Added support for integer or UUID ids
|
8
|
+
|
9
|
+
|
10
|
+
v0.1.1, 2013-12-18
|
11
|
+
------------------
|
12
|
+
|
13
|
+
* No change. Redeployed to fix bad 0.1.0 rubygems push.
|
14
|
+
|
15
|
+
|
1
16
|
v0.1.0, 2013-12-13
|
2
17
|
------------------
|
3
18
|
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ in a non-production environment.
|
|
15
15
|
#### Include the gem in your Gemfile
|
16
16
|
|
17
17
|
group :development do
|
18
|
-
gem 'application_seeds'
|
18
|
+
gem 'application_seeds'
|
19
19
|
end
|
20
20
|
|
21
21
|
You should add this gem to any environment group that would need access
|
@@ -160,6 +160,135 @@ Then, you can seed a remote database by running the following:
|
|
160
160
|
bundle exec cap <environment> deploy:application_seeds -s dataset=your_data_set
|
161
161
|
|
162
162
|
|
163
|
+
## The Seed Files
|
164
|
+
|
165
|
+
The seed files contain the data that the Rake task works with to
|
166
|
+
populate the database. The seed files look and work much like Rails
|
167
|
+
fixtures files.
|
168
|
+
|
169
|
+
Here is an example from the `people.yml` file in this library's test
|
170
|
+
suite:
|
171
|
+
|
172
|
+
```
|
173
|
+
joe_smith:
|
174
|
+
first_name: Joe
|
175
|
+
last_name: Smith
|
176
|
+
company_id: mega_corp
|
177
|
+
start_date: <%= 2.months.ago.to_date %>
|
178
|
+
|
179
|
+
jane_doe:
|
180
|
+
first_name: Jane
|
181
|
+
last_name: Doe
|
182
|
+
company_id: mega_corp
|
183
|
+
start_date: <%= 10.months.ago.to_date %>
|
184
|
+
```
|
185
|
+
|
186
|
+
Seed data must contain a label that is unique to the file.
|
187
|
+
|
188
|
+
|
189
|
+
### ERB
|
190
|
+
|
191
|
+
Seed files may contain ERB snippets to support more dynamic data, or
|
192
|
+
data that may change over time.
|
193
|
+
|
194
|
+
|
195
|
+
### Establishing relationships
|
196
|
+
|
197
|
+
Relationships can be established between seed data files using labels.
|
198
|
+
One piece of seed data can specify a `belongs_to` relationship with
|
199
|
+
another piece of seed data by specifying the other data's label in the
|
200
|
+
`_id` field.
|
201
|
+
|
202
|
+
In this example, `ApplicationSeeds` will look in the `companies.yml`
|
203
|
+
file for a seed data element with the label `mega_corp`.
|
204
|
+
```ruby
|
205
|
+
company_id: mega_corp
|
206
|
+
```
|
207
|
+
|
208
|
+
If the `_id` field does not share a name with the file that the
|
209
|
+
corresponding seed data can be found, you can specify the name of the
|
210
|
+
seed file, like so:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
employer_id: mega_corp (companies)
|
214
|
+
```
|
215
|
+
|
216
|
+
|
217
|
+
#### Many to Many
|
218
|
+
|
219
|
+
Many to many relationships can be specified using arrays. The name of
|
220
|
+
the field must end in `ids` or `uuids`.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
company_ids: [mega_corp, ma_and_pa]
|
224
|
+
```
|
225
|
+
|
226
|
+
If the `_ids` field does not share a name with the file that the
|
227
|
+
corresponding seed data can be found, you can specify the name of the
|
228
|
+
seed file, like so:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
employer_ids: "[mega_corp, ma_and_pa] (companies)"
|
232
|
+
```
|
233
|
+
|
234
|
+
Here, the array must be enclosed in a string, to prevent the YAML parser
|
235
|
+
from erroring out due to invalid YAML syntax.
|
236
|
+
|
237
|
+
|
238
|
+
### Hard coding IDs
|
239
|
+
|
240
|
+
By default, `ApplicationSeeds` will generate a unique ID for each piece
|
241
|
+
of seed data based on the name of the file containing the data and the
|
242
|
+
data's label. The IDs will not change, as long as the name of the file
|
243
|
+
containing the seed data and the labels do not change.
|
244
|
+
|
245
|
+
If you need to specify a specific id for a piece of seed data, you can
|
246
|
+
specify the id in the list of attributes.
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
joe_smith:
|
250
|
+
id: 123
|
251
|
+
first_name: Joe
|
252
|
+
last_name: Smith
|
253
|
+
company_id: mega_corp
|
254
|
+
start_date: <%= 2.months.ago.to_date %>
|
255
|
+
```
|
256
|
+
|
257
|
+
`ApplicationSeeds` will not generate an ID for you if one has been
|
258
|
+
specified.
|
259
|
+
|
260
|
+
|
261
|
+
### Examples
|
262
|
+
|
263
|
+
See `spec/seed_data/test_data_set` for more examples of seed data files.
|
264
|
+
|
265
|
+
|
266
|
+
## Configuration
|
267
|
+
|
268
|
+
The `ApplicationSeeds` module can generate integer or UUID ids. You can
|
269
|
+
use the `config` method to tell `ApplicationSeeds` which id type you would
|
270
|
+
like to use.
|
271
|
+
|
272
|
+
ID types can be specified at the global level (to be applied to all seed data types)...
|
273
|
+
```ruby
|
274
|
+
ApplicationSeeds.config = { :id_type => :uuid }
|
275
|
+
```
|
276
|
+
|
277
|
+
...at the data type level (if some types have UUID primary keys and other have integer primary keys)...
|
278
|
+
```ruby
|
279
|
+
ApplicationSeeds.config = { :people_id_type => :uuid, :companies_id_type => :integer }
|
280
|
+
```
|
281
|
+
|
282
|
+
...or a combination of both (if every type uses integer primary keys except for one, for example)
|
283
|
+
```ruby
|
284
|
+
ApplicationSeeds.config = { :id_type => :uuid, :companies_id_type => :integer }
|
285
|
+
```
|
286
|
+
|
287
|
+
`integer` is the default id type.
|
288
|
+
|
289
|
+
**`config` needs to be called before the dataset is specified using `dataset=`**
|
290
|
+
|
291
|
+
|
163
292
|
## The API
|
164
293
|
|
165
294
|
The `ApplicationSeeds` module provides an API that enables the programmatic retrieval of seed data,
|
@@ -226,6 +355,16 @@ The IDs of the object are the keys, and a hash containing the object's attribute
|
|
226
355
|
An exception is raised if no seed data could be with the given name.
|
227
356
|
|
228
357
|
|
358
|
+
### Fetching seed data by label
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
ApplicationSeeds.campaigns(:some_campaign) # where "campaigns" is the name of the seed file, and :some_campaign is the label of the campaign
|
362
|
+
```
|
363
|
+
|
364
|
+
This call returns a hash containing the object's attributes. An exception is raised if no
|
365
|
+
seed data could be found with the given label.
|
366
|
+
|
367
|
+
|
229
368
|
### Fetching seed data by ID
|
230
369
|
|
231
370
|
```ruby
|
data/lib/application_seeds.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
require "zlib"
|
1
3
|
require "yaml"
|
2
4
|
require "erb"
|
3
5
|
require "pg"
|
@@ -20,12 +22,19 @@ require "application_seeds/attributes"
|
|
20
22
|
# The IDs of the object are the keys, and a hash containing the object's attributes are the values.
|
21
23
|
# An exception is raised if no seed data could be with the given name.
|
22
24
|
#
|
23
|
-
# === Fetching seed data by
|
25
|
+
# === Fetching seed data by label
|
24
26
|
#
|
25
|
-
# ApplicationSeeds.campaigns(
|
27
|
+
# ApplicationSeeds.campaigns(:label) # where "campaigns" is the name of the seed file, and :label is the label of the campaign
|
26
28
|
#
|
27
29
|
# This call returns a hash containing the object's attributes. An exception is raised if no
|
28
|
-
# seed data could be found with the given
|
30
|
+
# seed data could be found with the given label.
|
31
|
+
#
|
32
|
+
# === Fetching seed data by id
|
33
|
+
#
|
34
|
+
# ApplicationSeeds.campaigns(12345) # where "campaigns" is the name of the seed file, and 12345 is the id of the campaign
|
35
|
+
#
|
36
|
+
# This call returns a hash containing the object's attributes. An exception is raised if no
|
37
|
+
# seed data could be found with the given id.
|
29
38
|
#
|
30
39
|
# === Fetching seed data by some other attribute
|
31
40
|
#
|
@@ -73,6 +82,20 @@ require "application_seeds/attributes"
|
|
73
82
|
module ApplicationSeeds
|
74
83
|
class << self
|
75
84
|
|
85
|
+
#
|
86
|
+
# Specify any configuration, such as the type of ids to generate (:integer or :uuid).
|
87
|
+
#
|
88
|
+
def config=(config)
|
89
|
+
@_config = config
|
90
|
+
end
|
91
|
+
|
92
|
+
#
|
93
|
+
# Fetch the configuration.
|
94
|
+
#
|
95
|
+
def config
|
96
|
+
@_config ||= { :id_type => :integer }
|
97
|
+
end
|
98
|
+
|
76
99
|
#
|
77
100
|
# Specify the name of the gem that contains the application seed data.
|
78
101
|
#
|
@@ -127,6 +150,8 @@ module ApplicationSeeds
|
|
127
150
|
end
|
128
151
|
|
129
152
|
store_dataset(dataset)
|
153
|
+
process_labels(dataset)
|
154
|
+
load_seed_data(dataset)
|
130
155
|
@dataset = dataset
|
131
156
|
end
|
132
157
|
|
@@ -196,28 +221,85 @@ module ApplicationSeeds
|
|
196
221
|
end
|
197
222
|
|
198
223
|
def seed_data(type, options)
|
199
|
-
|
200
|
-
@seed_data[type] ||= load_seed_data(type)
|
224
|
+
type = type.to_s
|
201
225
|
raise "No seed data could be found for '#{type}'" if @seed_data[type].nil?
|
202
226
|
|
203
227
|
if options.nil?
|
204
228
|
fetch(type)
|
205
229
|
elsif options.is_a?(Fixnum) || options.is_a?(String)
|
206
230
|
fetch_with_id(type, options)
|
231
|
+
elsif options.is_a?(Symbol)
|
232
|
+
fetch_with_label(type, options.to_s)
|
207
233
|
elsif options.is_a? Hash
|
208
234
|
fetch(type) do |attributes|
|
209
|
-
|
235
|
+
options.stringify_keys!
|
236
|
+
options = replace_labels_with_ids(options)
|
237
|
+
(options.to_a - attributes.to_a).empty?
|
210
238
|
end
|
211
239
|
end
|
212
240
|
end
|
213
241
|
|
214
|
-
def load_seed_data(
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
242
|
+
def load_seed_data(dataset)
|
243
|
+
@seed_data = {}
|
244
|
+
seed_files(dataset).each do |seed_file|
|
245
|
+
basename = File.basename(seed_file, ".yml")
|
246
|
+
data = YAML.load(ERB.new(File.read(seed_file)).result)
|
247
|
+
if data
|
248
|
+
data.each do |label, attributes|
|
249
|
+
data[label] = replace_labels_with_ids(attributes)
|
250
|
+
end
|
251
|
+
@seed_data[basename] = data
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def replace_labels_with_ids(attributes)
|
257
|
+
new_attributes = {}
|
258
|
+
attributes.each do |key, value|
|
259
|
+
new_attributes[key] = value
|
260
|
+
if key =~ /^(.*)_id$/ || key =~ /^(.*)_uuid$/
|
261
|
+
new_attributes[key] = replace_single_label($1.pluralize, value)
|
262
|
+
end
|
263
|
+
|
264
|
+
if key =~ /^(.*)_ids$/ || key =~ /^(.*)_uuids$/
|
265
|
+
new_attributes[key] = replace_array_of_labels($1.pluralize, value)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
new_attributes
|
269
|
+
end
|
270
|
+
|
271
|
+
def replace_single_label(type, value)
|
272
|
+
# Handle the case where seed data type cannot be determined by the
|
273
|
+
# name of the attribute -- employer_id: ma_and_pa (companies)
|
274
|
+
if value =~ /\((.*)\)/
|
275
|
+
type = $1
|
276
|
+
value = value.sub(/\((.*)\)/, "").strip
|
277
|
+
end
|
278
|
+
|
279
|
+
if @seed_labels[type]
|
280
|
+
label_ids = @seed_labels[type][value.to_s]
|
281
|
+
value = label_ids[id_type(type)] if label_ids
|
220
282
|
end
|
283
|
+
value
|
284
|
+
end
|
285
|
+
|
286
|
+
def replace_array_of_labels(type, value)
|
287
|
+
# Handle the case where seed data type cannot be determined by the
|
288
|
+
# name of the attribute -- employer_ids: [ma_and_pa, super_corp] (companies)
|
289
|
+
if value =~ /\((.*)\)/
|
290
|
+
type = $1
|
291
|
+
value = value.sub(/\((.*)\)/, "").strip
|
292
|
+
value =~ /^\[(.*)\]$/
|
293
|
+
value = $1.split(',').map(&:strip)
|
294
|
+
end
|
295
|
+
|
296
|
+
if @seed_labels[type]
|
297
|
+
value = value.map do |v|
|
298
|
+
label_ids = @seed_labels[type][v.to_s]
|
299
|
+
(label_ids && label_ids[id_type(type)]) || v
|
300
|
+
end
|
301
|
+
end
|
302
|
+
value
|
221
303
|
end
|
222
304
|
|
223
305
|
def seed_data_path
|
@@ -233,9 +315,9 @@ module ApplicationSeeds
|
|
233
315
|
|
234
316
|
def fetch(type, &block)
|
235
317
|
result = {}
|
236
|
-
@seed_data[type].each do |
|
237
|
-
attributes =
|
238
|
-
id =
|
318
|
+
@seed_data[type].each do |label, attrs|
|
319
|
+
attributes = attrs.clone
|
320
|
+
id = @seed_labels[type][label][id_type(type)]
|
239
321
|
if !block_given? || (block_given? && yield(attributes) == true)
|
240
322
|
result[id] = Attributes.new(attributes)
|
241
323
|
end
|
@@ -244,15 +326,65 @@ module ApplicationSeeds
|
|
244
326
|
end
|
245
327
|
|
246
328
|
def fetch_with_id(type, id)
|
247
|
-
data =
|
329
|
+
data = nil
|
330
|
+
@seed_labels[type].each do |label, ids|
|
331
|
+
if ids.values.map(&:to_s).include?(id.to_s)
|
332
|
+
data = @seed_data[type][label]
|
333
|
+
data['id'] = @seed_labels[type][label][id_type(type)]
|
334
|
+
break
|
335
|
+
end
|
336
|
+
end
|
248
337
|
raise "No seed data could be found for '#{type}' with id #{id}" if data.nil?
|
249
338
|
Attributes.new(data)
|
250
339
|
end
|
251
340
|
|
341
|
+
def fetch_with_label(type, label)
|
342
|
+
data = @seed_data[type][label]
|
343
|
+
raise "No seed data could be found for '#{type}' with label #{label}" if data.nil?
|
344
|
+
data['id'] = @seed_labels[type][label][id_type(type)]
|
345
|
+
Attributes.new(data)
|
346
|
+
end
|
347
|
+
|
252
348
|
def store_dataset(dataset)
|
253
349
|
Database.create_metadata_table
|
254
350
|
Database.connection.exec("INSERT INTO application_seeds (dataset) VALUES ('#{dataset}');")
|
255
351
|
end
|
256
352
|
|
353
|
+
def process_labels(dataset)
|
354
|
+
return @seed_labels unless @seed_labels.nil?
|
355
|
+
|
356
|
+
@seed_labels = {}
|
357
|
+
seed_files(dataset).each do |seed_file|
|
358
|
+
seed_type = File.basename(seed_file, ".yml")
|
359
|
+
@seed_labels[seed_type] = {}
|
360
|
+
|
361
|
+
data = YAML.load(File.read(seed_file))
|
362
|
+
if data
|
363
|
+
data.each do |label, attributes|
|
364
|
+
specified_id = attributes['id']
|
365
|
+
ids = specified_id.nil? ? generate_unique_ids(seed_type, label) : generate_ids(specified_id)
|
366
|
+
@seed_labels[seed_type][label] = ids
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
MAX_ID = 2 ** 30 - 1
|
373
|
+
def generate_unique_ids(seed_type, label)
|
374
|
+
checksum = Zlib.crc32(seed_type + label) % MAX_ID
|
375
|
+
generate_ids(checksum)
|
376
|
+
end
|
377
|
+
|
378
|
+
def generate_ids(id)
|
379
|
+
{ :integer => id, :uuid => "00000000-0000-0000-0000-%012d" % id }
|
380
|
+
end
|
381
|
+
|
382
|
+
def seed_files(dataset)
|
383
|
+
Dir[File.join(seed_data_path, dataset, "*.yml")]
|
384
|
+
end
|
385
|
+
|
386
|
+
def id_type(type)
|
387
|
+
self.config["#{type}_id_type".to_sym] || self.config[:id_type]
|
388
|
+
end
|
257
389
|
end
|
258
390
|
end
|
@@ -25,13 +25,13 @@ describe "ApplicationSeeds" do
|
|
25
25
|
|
26
26
|
describe "#data_gem_name" do
|
27
27
|
it "defaults to 'application_seed_data'" do
|
28
|
-
ApplicationSeeds.data_gem_name.
|
28
|
+
expect(ApplicationSeeds.data_gem_name).to eql("application_seed_data")
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe "#data_directory" do
|
33
33
|
it "is able to set the data directory successfully" do
|
34
|
-
ApplicationSeeds.data_directory.
|
34
|
+
expect(ApplicationSeeds.data_directory).to eql(File.join(File.dirname(__FILE__), "seed_data"))
|
35
35
|
end
|
36
36
|
it "raises an error if a non-existant directory specified" do
|
37
37
|
expect { ApplicationSeeds.data_directory = "/foo/bar" }.to raise_error
|
@@ -63,7 +63,7 @@ describe "ApplicationSeeds" do
|
|
63
63
|
ApplicationSeeds.dataset = "test_data_set"
|
64
64
|
end
|
65
65
|
it "sets the dataset" do
|
66
|
-
ApplicationSeeds.instance_variable_get(:@dataset).
|
66
|
+
expect(ApplicationSeeds.instance_variable_get(:@dataset)).to eql("test_data_set")
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
@@ -76,16 +76,16 @@ describe "ApplicationSeeds" do
|
|
76
76
|
ApplicationSeeds::Database.should_receive(:connection) { connection_dummy }
|
77
77
|
end
|
78
78
|
it "fetches the dataset name from the database" do
|
79
|
-
ApplicationSeeds.dataset.
|
79
|
+
expect(ApplicationSeeds.dataset).to eql("test_data_set")
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
83
|
describe "#seed_data_exists?" do
|
84
84
|
it "returns true if the specified seed data exists" do
|
85
|
-
ApplicationSeeds.seed_data_exists?(:people).
|
85
|
+
expect(ApplicationSeeds.seed_data_exists?(:people)).to be_true
|
86
86
|
end
|
87
87
|
it "returns false if the specified seed data does not exist" do
|
88
|
-
ApplicationSeeds.seed_data_exists?(:missing).
|
88
|
+
expect(ApplicationSeeds.seed_data_exists?(:missing)).to_not be_true
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
@@ -97,17 +97,17 @@ describe "ApplicationSeeds" do
|
|
97
97
|
|
98
98
|
describe "#create_object" do
|
99
99
|
before do
|
100
|
-
@attributes = ApplicationSeeds.people(
|
100
|
+
@attributes = ApplicationSeeds.people(:joe_smith)
|
101
101
|
@object = ApplicationSeeds.create_object!(Person, @attributes['id'], @attributes)
|
102
102
|
end
|
103
103
|
it "assigns the id" do
|
104
|
-
@object.id.
|
104
|
+
expect(@object.id).to eql(636095969)
|
105
105
|
end
|
106
106
|
it "assigns the attributes" do
|
107
|
-
@object.attributes.
|
107
|
+
expect(@object.attributes).to eql(@attributes.reject { |k,v| k == "bogus_attribute" })
|
108
108
|
end
|
109
109
|
it "saves the object in the database" do
|
110
|
-
@object.saved.
|
110
|
+
expect(@object.saved).to be_true
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
@@ -116,20 +116,38 @@ describe "ApplicationSeeds" do
|
|
116
116
|
@people = ApplicationSeeds.people
|
117
117
|
end
|
118
118
|
it "returns all people" do
|
119
|
-
@people.size.
|
119
|
+
expect(@people.size).to eql(5)
|
120
120
|
end
|
121
121
|
it "returns the attributes for each person" do
|
122
|
-
person = @people[
|
123
|
-
person['first_name'].
|
124
|
-
person['last_name'].
|
122
|
+
person = @people.values.sort { |a,b| b['start_date'] <=> a['start_date'] }[1]
|
123
|
+
expect(person['first_name']).to eql("Jane")
|
124
|
+
expect(person['last_name']).to eql("Doe")
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
128
|
-
describe "fetching seed data by
|
128
|
+
describe "fetching seed data by label" do
|
129
129
|
it "returns the attributes for each person" do
|
130
|
-
|
131
|
-
|
132
|
-
|
130
|
+
person = ApplicationSeeds.people(:jane_doe)
|
131
|
+
expect(person['first_name']).to eql("Jane")
|
132
|
+
expect(person['last_name']).to eql("Doe")
|
133
|
+
end
|
134
|
+
it "raises an error if no data could be found with the specified label" do
|
135
|
+
expect { ApplicationSeeds.people(:bogus) }.to raise_error(RuntimeError)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "fetching seed data by id" do
|
140
|
+
it "can find the seed with an integer id" do
|
141
|
+
person = ApplicationSeeds.people(487117267)
|
142
|
+
expect(person['id']).to eql(487117267)
|
143
|
+
expect(person['first_name']).to eql("Jane")
|
144
|
+
expect(person['last_name']).to eql("Doe")
|
145
|
+
end
|
146
|
+
it "can find the seed with an string id" do
|
147
|
+
person = ApplicationSeeds.people("487117267")
|
148
|
+
expect(person['id']).to eql(487117267)
|
149
|
+
expect(person['first_name']).to eql("Jane")
|
150
|
+
expect(person['last_name']).to eql("Doe")
|
133
151
|
end
|
134
152
|
it "raises an error if no data could be found with the specified id" do
|
135
153
|
expect { ApplicationSeeds.people(404) }.to raise_error(RuntimeError)
|
@@ -138,27 +156,101 @@ describe "ApplicationSeeds" do
|
|
138
156
|
|
139
157
|
describe "fetching seed data by property values" do
|
140
158
|
it "returns the found person" do
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
159
|
+
people = ApplicationSeeds.people(:last_name => 'Walsh', :company_id => :ma_and_pa)
|
160
|
+
expect(people.size).to eql(1)
|
161
|
+
expect(people.values.first['first_name']).to eql("John")
|
162
|
+
expect(people.values.first['last_name']).to eql("Walsh")
|
145
163
|
end
|
146
164
|
it "returns multiple people if there are multiple matches" do
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
165
|
+
people = ApplicationSeeds.people(:company_id => :mega_corp)
|
166
|
+
expect(people.size).to eql(2)
|
167
|
+
expect(people.values.first['first_name']).to eql("Joe")
|
168
|
+
expect(people.values.last['first_name']).to eql("Jane")
|
169
|
+
end
|
170
|
+
it "can find elements using the id instead of the label" do
|
171
|
+
people = ApplicationSeeds.people(:last_name => 'Walsh', :company_id => 47393448)
|
172
|
+
expect(people.size).to eql(1)
|
173
|
+
expect(people.values.first['first_name']).to eql("John")
|
174
|
+
expect(people.values.first['last_name']).to eql("Walsh")
|
151
175
|
end
|
152
176
|
it "returns an empty hash if no matches could be found" do
|
153
|
-
|
154
|
-
|
177
|
+
people = ApplicationSeeds.people(:last_name => '404')
|
178
|
+
expect(people).to eql({})
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "specifying ids" do
|
183
|
+
it "can fetch people by their specified id" do
|
184
|
+
person = ApplicationSeeds.people(456)
|
185
|
+
expect(person['id']).to eql(456)
|
186
|
+
expect(person['first_name']).to eql("Sam")
|
187
|
+
expect(person['last_name']).to eql("Jones")
|
188
|
+
expect(person['company_id']).to eql(123)
|
155
189
|
end
|
156
190
|
end
|
157
191
|
|
158
192
|
describe "ERB" do
|
159
193
|
it "processes ERB snippets in the fixtures" do
|
160
|
-
|
161
|
-
|
194
|
+
person = ApplicationSeeds.people(:joe_smith)
|
195
|
+
expect(person['start_date']).to eql(2.months.ago.to_date)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "when specifying the seed data type in the seed data file" do
|
200
|
+
it "should look for the seed data in the specified type file" do
|
201
|
+
person = ApplicationSeeds.people(:ken_adams)
|
202
|
+
expect(person['employer_id']).to eql(47393448)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe "for attributes containing arrays of labels" do
|
207
|
+
describe "when the target data type matches the attribute name" do
|
208
|
+
it "builds the array of ids" do
|
209
|
+
department = ApplicationSeeds.departments(:engineering)
|
210
|
+
expect(department['people_ids']).to eql([636095969, 487117267, 10284664])
|
211
|
+
end
|
212
|
+
end
|
213
|
+
describe "when specifying the seed data type in the seed data file" do
|
214
|
+
it "should look for the seed data in the specified type file" do
|
215
|
+
department = ApplicationSeeds.departments(:sales)
|
216
|
+
expect(department['employee_ids']).to eql([456, 420015031])
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "with UUIDs configured for all seed types" do
|
223
|
+
before do
|
224
|
+
ApplicationSeeds.stub(:store_dataset)
|
225
|
+
ApplicationSeeds.config = { :id_type => :uuid }
|
226
|
+
ApplicationSeeds.dataset = "test_data_set"
|
227
|
+
end
|
228
|
+
|
229
|
+
describe "when fetching seed data" do
|
230
|
+
before do
|
231
|
+
@person = ApplicationSeeds.people(:john_walsh)
|
232
|
+
end
|
233
|
+
it "uses UUIDs for the keys" do
|
234
|
+
expect(@person['id']).to eql("00000000-0000-0000-0000-000010284664")
|
235
|
+
expect(@person['company_id']).to eql("00000000-0000-0000-0000-000047393448")
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe "with data type specific key types configured" do
|
241
|
+
before do
|
242
|
+
ApplicationSeeds.stub(:store_dataset)
|
243
|
+
ApplicationSeeds.config = { :id_type => :uuid, :companies_id_type => :integer }
|
244
|
+
ApplicationSeeds.dataset = "test_data_set"
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "when fetching seed data" do
|
248
|
+
before do
|
249
|
+
@person = ApplicationSeeds.people(:john_walsh)
|
250
|
+
end
|
251
|
+
it "uses UUIDs for the keys" do
|
252
|
+
expect(@person['id']).to eql("00000000-0000-0000-0000-000010284664")
|
253
|
+
expect(@person['company_id']).to eql(47393448)
|
162
254
|
end
|
163
255
|
end
|
164
256
|
end
|
data/spec/attributes_spec.rb
CHANGED
@@ -12,10 +12,10 @@ describe "Attributes" do
|
|
12
12
|
@selected_attributes = @attributes.select_attributes(:first_name, :occupation)
|
13
13
|
end
|
14
14
|
it "returns only the select attributes" do
|
15
|
-
@selected_attributes.
|
15
|
+
expect(@selected_attributes).to eql({ "first_name" => "Billy", "occupation" => "Bus Driver" })
|
16
16
|
end
|
17
17
|
it "returns a new instance of the Attributes class" do
|
18
|
-
@selected_attributes.
|
18
|
+
expect(@selected_attributes).to be_a(ApplicationSeeds::Attributes)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -24,10 +24,10 @@ describe "Attributes" do
|
|
24
24
|
@rejected_attributes = @attributes.reject_attributes(:first_name, :last_name)
|
25
25
|
end
|
26
26
|
it "returns only the select attributes" do
|
27
|
-
@rejected_attributes.
|
27
|
+
expect(@rejected_attributes).to eql({ "occupation" => "Bus Driver" })
|
28
28
|
end
|
29
29
|
it "returns a new instance of the Attributes class" do
|
30
|
-
@rejected_attributes.
|
30
|
+
expect(@rejected_attributes).to be_a(ApplicationSeeds::Attributes)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -36,10 +36,10 @@ describe "Attributes" do
|
|
36
36
|
@mapped_attributes = @attributes.map_attributes(:first_name => :fname, :last_name => :lname)
|
37
37
|
end
|
38
38
|
it "returns only the select attributes" do
|
39
|
-
@mapped_attributes.
|
39
|
+
expect(@mapped_attributes).to eql({ "fname" => "Billy", "lname" => "Bob", "occupation" => "Bus Driver" })
|
40
40
|
end
|
41
41
|
it "returns a new instance of the Attributes class" do
|
42
|
-
@mapped_attributes.
|
42
|
+
expect(@mapped_attributes).to be_a(ApplicationSeeds::Attributes)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -1,21 +1,33 @@
|
|
1
|
-
|
1
|
+
joe_smith:
|
2
2
|
first_name: Joe
|
3
3
|
last_name: Smith
|
4
|
-
company_id:
|
4
|
+
company_id: mega_corp
|
5
5
|
start_date: <%= 2.months.ago.to_date %>
|
6
6
|
bogus_attribute: foo
|
7
7
|
|
8
|
-
|
8
|
+
jane_doe:
|
9
9
|
first_name: Jane
|
10
10
|
last_name: Doe
|
11
|
-
company_id:
|
11
|
+
company_id: mega_corp
|
12
12
|
start_date: <%= 10.months.ago.to_date %>
|
13
13
|
bogus_attribute: foo
|
14
14
|
|
15
|
-
|
15
|
+
john_walsh:
|
16
16
|
first_name: John
|
17
17
|
last_name: Walsh
|
18
|
-
company_id:
|
18
|
+
company_id: ma_and_pa
|
19
19
|
start_date: <%= 10.years.ago.to_date %>
|
20
20
|
bogus_attribute: foo
|
21
21
|
|
22
|
+
sam_jones:
|
23
|
+
id: 456
|
24
|
+
first_name: Sam
|
25
|
+
last_name: Jones
|
26
|
+
company_id: 123
|
27
|
+
start_date: <%= 10.years.ago.to_date %>
|
28
|
+
|
29
|
+
ken_adams:
|
30
|
+
first_name: Sam
|
31
|
+
last_name: Jones
|
32
|
+
employer_id: ma_and_pa (companies)
|
33
|
+
start_date: <%= 10.years.ago.to_date %>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: application_seeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -96,6 +96,8 @@ files:
|
|
96
96
|
- lib/application_seeds/version.rb
|
97
97
|
- spec/application_seeds_spec.rb
|
98
98
|
- spec/attributes_spec.rb
|
99
|
+
- spec/seed_data/test_data_set/companies.yml
|
100
|
+
- spec/seed_data/test_data_set/departments.yml
|
99
101
|
- spec/seed_data/test_data_set/people.yml
|
100
102
|
homepage: https://github.com/centro/application_seeds
|
101
103
|
licenses:
|
@@ -112,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
114
|
version: '0'
|
113
115
|
segments:
|
114
116
|
- 0
|
115
|
-
hash: -
|
117
|
+
hash: -987523196009034101
|
116
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
119
|
none: false
|
118
120
|
requirements:
|
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
123
|
version: '0'
|
122
124
|
segments:
|
123
125
|
- 0
|
124
|
-
hash: -
|
126
|
+
hash: -987523196009034101
|
125
127
|
requirements: []
|
126
128
|
rubyforge_project:
|
127
129
|
rubygems_version: 1.8.23
|
@@ -132,4 +134,6 @@ summary: A library for managing a standardized set of seed data for applications
|
|
132
134
|
test_files:
|
133
135
|
- spec/application_seeds_spec.rb
|
134
136
|
- spec/attributes_spec.rb
|
137
|
+
- spec/seed_data/test_data_set/companies.yml
|
138
|
+
- spec/seed_data/test_data_set/departments.yml
|
135
139
|
- spec/seed_data/test_data_set/people.yml
|