sequel-seed 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/README.md +4 -3
- data/lib/sequel/extensions/seed.rb +126 -10
- data/spec/extensions/seed_spec.rb +270 -83
- data/spec/spec_helper.rb +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da09d9ce979c91b83d60523a679e5f284d47bead
|
4
|
+
data.tar.gz: 9cfc115fbe4e8cddc8a63cc07a8314201a391e80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57c5c5e645c3407808d4b77d17877aa31940e21152370978583852d653385be8df9a6e248b29a20a2711e5851623c0cd33038a143c5b2ae1fc8092327abce23d
|
7
|
+
data.tar.gz: 671cf4fd7f61cb15aeb3588ed461b8f39ab5ebbb785ac0ba4569712b96df3d203c88fd24da4dae67ab39dafcc4717de8b48f0703187549e0f4a53156b1b08086
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
Create a seed file (eg. `/path/to/seeds/
|
7
|
+
Create a seed file (eg. `/path/to/seeds/20150928000000_initial_seed.rb`)
|
8
8
|
|
9
9
|
```rb
|
10
10
|
Sequel.seed(:development, :test) do # Applies only to "development" and "test" environments
|
@@ -45,11 +45,12 @@ For more information, please check the [project website](//github.com/earaujoass
|
|
45
45
|
|
46
46
|
## Limitations
|
47
47
|
|
48
|
-
|
48
|
+
- JSON and YAML files don't work with associations
|
49
|
+
- Only timestamped seed/fixture files
|
49
50
|
|
50
51
|
## What's next?
|
51
52
|
|
52
|
-
JSON & YAML files
|
53
|
+
Work with Model's associations inside JSON & YAML files
|
53
54
|
|
54
55
|
## Support
|
55
56
|
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
|
1
4
|
##
|
2
5
|
# Extension based upon Sequel::Migration and Sequel::Migrator
|
3
6
|
#
|
@@ -52,6 +55,9 @@ module Sequel
|
|
52
55
|
end
|
53
56
|
|
54
57
|
module Seed
|
58
|
+
class Error < Sequel::Error
|
59
|
+
end
|
60
|
+
|
55
61
|
class << self
|
56
62
|
attr_reader :environment
|
57
63
|
|
@@ -84,6 +90,76 @@ module Sequel
|
|
84
90
|
end
|
85
91
|
end
|
86
92
|
|
93
|
+
##
|
94
|
+
# Helper methods for the Sequel::Seed project.
|
95
|
+
|
96
|
+
module Helpers
|
97
|
+
class << self
|
98
|
+
def camelize(term, uppercase_first_letter = true)
|
99
|
+
string = term.to_s
|
100
|
+
if uppercase_first_letter
|
101
|
+
string.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
102
|
+
else
|
103
|
+
string.first + camelize(string)[1..-1]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
module SeedDescriptor
|
110
|
+
def apply_seed_descriptor(seed_descriptor)
|
111
|
+
case seed_descriptor
|
112
|
+
when Hash
|
113
|
+
apply_seed_hash(seed_descriptor)
|
114
|
+
when Array
|
115
|
+
seed_descriptor.each {|seed_hash| apply_seed_hash(seed_hash)}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def apply_seed_hash(seed_hash)
|
122
|
+
return unless seed_hash.class <= Hash
|
123
|
+
if seed_hash.has_key?('environment')
|
124
|
+
case seed_hash['environment']
|
125
|
+
when String, Symbol
|
126
|
+
return if seed_hash['environment'].to_sym != Seed.environment
|
127
|
+
when Array
|
128
|
+
return unless seed_hash_environment.map(&:to_sym).include?(Seed.environment)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
keys = seed_hash.keys
|
133
|
+
keys.delete('environment')
|
134
|
+
keys.each do |key|
|
135
|
+
key_hash = seed_hash[key]
|
136
|
+
entries = nil
|
137
|
+
class_name = if key_hash.has_key?('class')
|
138
|
+
entries = key_hash['entries']
|
139
|
+
key_hash['class']
|
140
|
+
else
|
141
|
+
Helpers.camelize(key)
|
142
|
+
end
|
143
|
+
# It will raise an error if the class name is not defined
|
144
|
+
class_const = Kernel.const_get(class_name)
|
145
|
+
if entries
|
146
|
+
entries.each {|hash| create_model(class_const, hash)}
|
147
|
+
else
|
148
|
+
create_model(class_const, key_hash)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def create_model(class_const, hash)
|
154
|
+
object_instance = class_const.new
|
155
|
+
object_instance_attr = hash.each do |attr, value|
|
156
|
+
object_instance.set({attr.to_sym => value})
|
157
|
+
end
|
158
|
+
raise(Error, "Attempt to create invalid model instance of #{class_name}") unless object_instance.valid?
|
159
|
+
object_instance.save
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
87
163
|
class Base
|
88
164
|
class << self
|
89
165
|
def apply
|
@@ -117,15 +193,14 @@ module Sequel
|
|
117
193
|
end
|
118
194
|
|
119
195
|
class Seeder
|
120
|
-
SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb|json|yml)\z/i.freeze
|
196
|
+
SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb|json|yml|yaml)\z/i.freeze
|
197
|
+
RUBY_SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb)\z/i.freeze
|
198
|
+
YAML_SEED_FILE_PATTERN = /\A(\d+)_.+\.(yml|yaml)\z/i.freeze
|
199
|
+
JSON_SEED_FILE_PATTERN = /\A(\d+)_.+\.(json)\z/i.freeze
|
121
200
|
SEED_SPLITTER = '_'.freeze
|
122
201
|
MINIMUM_TIMESTAMP = 20000101
|
123
202
|
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
class NotCurrentError < Error
|
128
|
-
end
|
203
|
+
Error = Seed::Error
|
129
204
|
|
130
205
|
def self.apply(db, directory, opts = {})
|
131
206
|
seeder_class(directory).new(db, directory, opts).run
|
@@ -212,7 +287,7 @@ module Sequel
|
|
212
287
|
DEFAULT_SCHEMA_COLUMN = :filename
|
213
288
|
DEFAULT_SCHEMA_TABLE = :schema_seeds
|
214
289
|
|
215
|
-
Error =
|
290
|
+
Error = Seed::Error
|
216
291
|
|
217
292
|
attr_reader :applied_seeds
|
218
293
|
|
@@ -266,16 +341,57 @@ module Sequel
|
|
266
341
|
f = File.basename(path)
|
267
342
|
fi = f.downcase
|
268
343
|
if !applied_seeds.include?(fi)
|
269
|
-
|
344
|
+
#begin
|
345
|
+
load(path) if RUBY_SEED_FILE_PATTERN.match(f)
|
346
|
+
create_yaml_seed(path) if YAML_SEED_FILE_PATTERN.match(f)
|
347
|
+
create_json_seed(path) if JSON_SEED_FILE_PATTERN.match(f)
|
348
|
+
#rescue Exception => e
|
349
|
+
#raise(Error, "error while processing seed file #{path}: #{e.inspect}")
|
350
|
+
#end
|
270
351
|
el = [ms.last, f]
|
271
|
-
if
|
272
|
-
|
352
|
+
next if ms.last.nil?
|
353
|
+
if ms.last < Seed::Base && !seeds.include?(el)
|
354
|
+
seeds << el
|
273
355
|
end
|
274
356
|
end
|
275
357
|
end
|
276
358
|
seeds
|
277
359
|
end
|
278
360
|
|
361
|
+
def create_yaml_seed(path)
|
362
|
+
seed_descriptor = YAML::load(File.open(path))
|
363
|
+
seed = Class.new(Seed::Base)
|
364
|
+
seed.const_set "YAML_SEED", seed_descriptor
|
365
|
+
seed.class_eval do
|
366
|
+
include Seed::SeedDescriptor
|
367
|
+
|
368
|
+
def run
|
369
|
+
seed_descriptor = self.class.const_get "YAML_SEED"
|
370
|
+
raise(Error, "YAML seed improperly defined") if seed_descriptor.nil?
|
371
|
+
self.apply_seed_descriptor(seed_descriptor)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
|
375
|
+
seed
|
376
|
+
end
|
377
|
+
|
378
|
+
def create_json_seed(path)
|
379
|
+
seed_descriptor = JSON.parse(File.read(path))
|
380
|
+
seed = Class.new(Seed::Base)
|
381
|
+
seed.const_set "JSON_SEED", seed_descriptor
|
382
|
+
seed.class_eval do
|
383
|
+
include Seed::SeedDescriptor
|
384
|
+
|
385
|
+
def run
|
386
|
+
seed_descriptor = self.class.const_get "JSON_SEED"
|
387
|
+
raise(Error, "JSON seed improperly defined") if seed_descriptor.nil?
|
388
|
+
self.apply_seed_descriptor(seed_descriptor)
|
389
|
+
end
|
390
|
+
end
|
391
|
+
Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
|
392
|
+
seed
|
393
|
+
end
|
394
|
+
|
279
395
|
def schema_dataset
|
280
396
|
c = column
|
281
397
|
ds = db.from(table)
|
@@ -5,12 +5,12 @@ describe Sequel.seed do
|
|
5
5
|
Sequel::Seed.setup(:test)
|
6
6
|
end
|
7
7
|
|
8
|
-
it 'should create a Seed descendant according to the
|
8
|
+
it 'should create a Seed descendant according to the given environment' do
|
9
9
|
seed = Sequel.seed(:test) {}
|
10
10
|
expect(Sequel::Seed::Base.descendants).to include seed
|
11
11
|
end
|
12
12
|
|
13
|
-
it 'should ignore a Seed not applicable to the
|
13
|
+
it 'should ignore a Seed not applicable to the given environment' do
|
14
14
|
seed = Sequel.seed(:development) {}
|
15
15
|
expect(Sequel::Seed::Base.descendants).not_to include seed
|
16
16
|
end
|
@@ -55,6 +55,7 @@ end
|
|
55
55
|
describe Sequel::Seeder do
|
56
56
|
let(:DB) {Sequel.sqlite}
|
57
57
|
let!(:environment) {"#{Faker::Lorem.word}_#{Faker::Lorem.word}"}
|
58
|
+
let!(:random_word) {Faker::Lorem.word}
|
58
59
|
|
59
60
|
it "should raise an error when there is not any seed file to apply" do
|
60
61
|
Sequel::Seed.setup environment
|
@@ -64,36 +65,118 @@ describe Sequel::Seeder do
|
|
64
65
|
expect(SpecModel.dataset.all.length).to be 0
|
65
66
|
end
|
66
67
|
|
67
|
-
describe
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
describe "Seeds defined using Ruby code (.rb extension)" do
|
69
|
+
describe 'environment references should be indistinguishable between Symbol and String' do
|
70
|
+
context 'when the environment is defined using a String' do
|
71
|
+
it 'should apply the Seed accordingly' do
|
72
|
+
Sequel::Seed.setup environment
|
73
|
+
|
74
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
75
|
+
f.puts "Sequel.seed(:#{environment}) do"
|
76
|
+
f.puts ' def run'
|
77
|
+
f.puts ' SpecModel.create :sentence => \'environment defined by String\''
|
78
|
+
f.puts ' end'
|
79
|
+
f.puts 'end'
|
80
|
+
end
|
81
|
+
|
82
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
83
|
+
expect(Sequel::Seeder.seeder_class(seed_test_dir)).to be Sequel::TimestampSeeder
|
84
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
85
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
86
|
+
expect(SpecModel.dataset.all.length).to be 1
|
87
|
+
expect(SpecModel.dataset.first.sentence).to eq 'environment defined by String'
|
88
|
+
end
|
89
|
+
end
|
71
90
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
91
|
+
context 'when the Seed is defined using a String' do
|
92
|
+
it 'should apply the Seed accordingly' do
|
93
|
+
Sequel::Seed.setup environment.to_sym
|
94
|
+
|
95
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
96
|
+
f.puts "Sequel.seed(\"#{environment}\") do"
|
97
|
+
f.puts ' def run'
|
98
|
+
f.puts ' SpecModel.create :sentence => \'Seed defined by String\''
|
99
|
+
f.puts ' end'
|
100
|
+
f.puts 'end'
|
101
|
+
end
|
102
|
+
|
103
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
104
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
105
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
106
|
+
expect(SpecModel.dataset.all.length).to be 1
|
107
|
+
expect(SpecModel.dataset.first.sentence).to eq 'Seed defined by String'
|
78
108
|
end
|
109
|
+
end
|
79
110
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
111
|
+
context 'when both Seed and environment are defined using a String' do
|
112
|
+
it 'should apply the Seed accordingly' do
|
113
|
+
Sequel::Seed.setup environment
|
114
|
+
|
115
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
116
|
+
f.puts "Sequel.seed(\"#{environment}\") do"
|
117
|
+
f.puts ' def run'
|
118
|
+
f.puts ' SpecModel.create :sentence => \'Seed and environment defined by String\''
|
119
|
+
f.puts ' end'
|
120
|
+
f.puts 'end'
|
121
|
+
end
|
122
|
+
|
123
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
124
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
125
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
126
|
+
expect(SpecModel.dataset.all.length).to be 1
|
127
|
+
expect(SpecModel.dataset.first.sentence).to eq 'Seed and environment defined by String'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'when both Seed and environment are defined using a Symbol' do
|
132
|
+
it 'should apply the Seed accordingly' do
|
133
|
+
Sequel::Seed.setup environment.to_sym
|
134
|
+
|
135
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
136
|
+
f.puts "Sequel.seed(:#{environment}) do"
|
137
|
+
f.puts ' def run'
|
138
|
+
f.puts ' SpecModel.create :sentence => \'Seed and environment defined by Symbol\''
|
139
|
+
f.puts ' end'
|
140
|
+
f.puts 'end'
|
141
|
+
end
|
142
|
+
|
143
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
144
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
145
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
146
|
+
expect(SpecModel.dataset.all.length).to be 1
|
147
|
+
expect(SpecModel.dataset.first.sentence).to eq 'Seed and environment defined by Symbol'
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'when the environment is defined using a String and we have a wildcard Seed' do
|
152
|
+
it 'should apply the Seed accordingly' do
|
153
|
+
Sequel::Seed.setup environment
|
154
|
+
|
155
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
156
|
+
f.puts 'Sequel.seed do'
|
157
|
+
f.puts ' def run'
|
158
|
+
f.puts ' SpecModel.create :sentence => \'Wildcard Seed and environment defined by String\''
|
159
|
+
f.puts ' end'
|
160
|
+
f.puts 'end'
|
161
|
+
end
|
162
|
+
|
163
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
164
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
165
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
166
|
+
expect(SpecModel.dataset.all.length).to be 1
|
167
|
+
expect(SpecModel.dataset.first.sentence).to eq 'Wildcard Seed and environment defined by String'
|
168
|
+
end
|
86
169
|
end
|
87
170
|
end
|
88
171
|
|
89
|
-
context 'when
|
90
|
-
it 'should
|
91
|
-
Sequel::Seed.setup environment
|
172
|
+
context 'when there\'s a Seed created' do
|
173
|
+
it 'should change the database accordingly only once' do
|
174
|
+
Sequel::Seed.setup environment
|
92
175
|
|
93
|
-
File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
|
94
|
-
f.puts
|
176
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
177
|
+
f.puts 'Sequel.seed do'
|
95
178
|
f.puts ' def run'
|
96
|
-
f.puts ' SpecModel.create :sentence => \'
|
179
|
+
f.puts ' SpecModel.create :sentence => \'should have changed (from Ruby file)\''
|
97
180
|
f.puts ' end'
|
98
181
|
f.puts 'end'
|
99
182
|
end
|
@@ -102,110 +185,214 @@ describe Sequel::Seeder do
|
|
102
185
|
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
103
186
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
104
187
|
expect(SpecModel.dataset.all.length).to be 1
|
105
|
-
expect(SpecModel.dataset.first.sentence).to eq '
|
188
|
+
expect(SpecModel.dataset.first.sentence).to eq 'should have changed (from Ruby file)'
|
189
|
+
# Once again
|
190
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
191
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
192
|
+
expect(SpecModel.dataset.all.length).to be 1
|
193
|
+
expect(SpecModel.dataset.first.sentence).to eq 'should have changed (from Ruby file)'
|
106
194
|
end
|
107
195
|
end
|
108
196
|
|
109
|
-
context 'when
|
110
|
-
it 'should
|
197
|
+
context 'when the specified Seed is not applicable to the given environment' do
|
198
|
+
it 'should not make any change to the database' do
|
111
199
|
Sequel::Seed.setup environment
|
112
200
|
|
113
|
-
File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
|
114
|
-
f.puts "Sequel.seed(
|
201
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", 'w+') do |f|
|
202
|
+
f.puts "Sequel.seed(:another_#{Faker::Lorem.word}_word) do"
|
115
203
|
f.puts ' def run'
|
116
|
-
f.puts ' SpecModel.create :sentence => \'
|
204
|
+
f.puts ' SpecModel.create :sentence => \'should not have changed (from Ruby file)\''
|
117
205
|
f.puts ' end'
|
118
206
|
f.puts 'end'
|
119
207
|
end
|
120
208
|
|
121
209
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
122
210
|
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
123
|
-
expect(
|
124
|
-
expect(SpecModel.dataset.all.length).to be 1
|
125
|
-
expect(SpecModel.dataset.first.sentence).to eq 'Seed and environment defined by String'
|
211
|
+
expect(SpecModel.dataset.all.length).to be 0
|
126
212
|
end
|
127
213
|
end
|
214
|
+
end
|
128
215
|
|
129
|
-
|
130
|
-
|
131
|
-
|
216
|
+
describe 'Seeds defined using YAML code (.{yaml,yml} extension)' do
|
217
|
+
it 'should apply a basic YAML Seed if it was specified for the given environment' do
|
218
|
+
Sequel::Seed.setup environment
|
132
219
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
220
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", 'w+') do |f|
|
221
|
+
f.puts "environment: :#{environment}"
|
222
|
+
f.puts 'spec_model:'
|
223
|
+
f.puts " sentence: 'should have changed (from YAML file) #{random_word}'"
|
224
|
+
f.puts ''
|
225
|
+
end
|
140
226
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
227
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
228
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
229
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
230
|
+
expect(SpecModel.dataset.all.length).to be 1
|
231
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'should apply a YAML Seed if it was specified for the given environment' do
|
235
|
+
Sequel::Seed.setup environment
|
236
|
+
|
237
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", 'w+') do |f|
|
238
|
+
f.puts "environment: :#{environment}"
|
239
|
+
f.puts 'model:'
|
240
|
+
f.puts ' class: \'SpecModel\''
|
241
|
+
f.puts ' entries:'
|
242
|
+
f.puts ' -'
|
243
|
+
f.puts " sentence: 'should have changed (from YAML file) #{random_word}'"
|
244
|
+
f.puts ''
|
146
245
|
end
|
246
|
+
|
247
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
248
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
249
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
250
|
+
expect(SpecModel.dataset.all.length).to be 1
|
251
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
147
252
|
end
|
148
253
|
|
149
|
-
|
150
|
-
|
151
|
-
Sequel::Seed.setup environment
|
254
|
+
it 'should apply a YAML file with multiple Seeds descriptors if they were specified for the given environment' do
|
255
|
+
Sequel::Seed.setup environment
|
152
256
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
257
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", 'w+') do |f|
|
258
|
+
f.puts '-'
|
259
|
+
f.puts " environment: :#{environment}"
|
260
|
+
f.puts ' model:'
|
261
|
+
f.puts ' class: \'SpecModel\''
|
262
|
+
f.puts ' entries:'
|
263
|
+
f.puts ' -'
|
264
|
+
f.puts " sentence: 'should have changed (from YAML file) #{random_word}'"
|
265
|
+
f.puts '-'
|
266
|
+
f.puts " environment: :another_#{environment}"
|
267
|
+
f.puts ' spec_model:'
|
268
|
+
f.puts " sentence: 'should not have changed (from YAML file) #{random_word}'"
|
269
|
+
f.puts ''
|
270
|
+
end
|
160
271
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
272
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
273
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
274
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
275
|
+
expect(SpecModel.dataset.all.length).to be 1
|
276
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'should not apply a basic Seed if it was not specified for the given environment' do
|
280
|
+
Sequel::Seed.setup environment
|
281
|
+
|
282
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", 'w+') do |f|
|
283
|
+
f.puts "environment: :another_environment_#{Faker::Lorem.word}"
|
284
|
+
f.puts 'spec_model:'
|
285
|
+
f.puts ' sentence: \'should not have changed (from YAML file)\''
|
286
|
+
f.puts ''
|
166
287
|
end
|
288
|
+
|
289
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
290
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
291
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
292
|
+
expect(SpecModel.dataset.all.length).to be 0
|
167
293
|
end
|
168
294
|
end
|
169
295
|
|
170
|
-
|
171
|
-
it 'should
|
296
|
+
describe 'Seeds defined using JSON code (.json extension)' do
|
297
|
+
it 'should apply a basic JSON Seed if it was specified for the given environment' do
|
172
298
|
Sequel::Seed.setup environment
|
173
299
|
|
174
|
-
File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
|
175
|
-
f.puts '
|
176
|
-
f.puts
|
177
|
-
f.puts
|
178
|
-
f.puts
|
179
|
-
f.puts '
|
300
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", 'w+') do |f|
|
301
|
+
f.puts '{'
|
302
|
+
f.puts " \"environment\": \"#{environment}\","
|
303
|
+
f.puts " \"spec_model\": {"
|
304
|
+
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
305
|
+
f.puts ' }'
|
306
|
+
f.puts '}'
|
307
|
+
f.puts ''
|
180
308
|
end
|
181
309
|
|
182
310
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
183
311
|
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
184
312
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
185
313
|
expect(SpecModel.dataset.all.length).to be 1
|
186
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
187
|
-
|
314
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
315
|
+
end
|
316
|
+
|
317
|
+
it 'should apply a JSON Seed if it was specified for the given environment' do
|
318
|
+
Sequel::Seed.setup environment
|
319
|
+
|
320
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", 'w+') do |f|
|
321
|
+
f.puts '{'
|
322
|
+
f.puts " \"environment\": \"#{environment}\","
|
323
|
+
f.puts " \"model\": {"
|
324
|
+
f.puts " \"class\": \"SpecModel\","
|
325
|
+
f.puts " \"entries\": ["
|
326
|
+
f.puts ' {'
|
327
|
+
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
328
|
+
f.puts ' }'
|
329
|
+
f.puts ' ]'
|
330
|
+
f.puts ' }'
|
331
|
+
f.puts '}'
|
332
|
+
f.puts ''
|
333
|
+
end
|
334
|
+
|
335
|
+
expect(Sequel::Seed::Base.descendants.length).to be 0
|
188
336
|
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
337
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
338
|
+
expect(SpecModel.dataset.all.length).to be 1
|
339
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'should apply a JSON file with multiple Seeds descriptors if they were specified for the given environment' do
|
343
|
+
Sequel::Seed.setup environment
|
344
|
+
|
345
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", 'w+') do |f|
|
346
|
+
f.puts '['
|
347
|
+
f.puts ' {'
|
348
|
+
f.puts " \"environment\": \"#{environment}\","
|
349
|
+
f.puts " \"model\": {"
|
350
|
+
f.puts " \"class\": \"SpecModel\","
|
351
|
+
f.puts " \"entries\": ["
|
352
|
+
f.puts ' {'
|
353
|
+
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
354
|
+
f.puts ' }'
|
355
|
+
f.puts ' ]'
|
356
|
+
f.puts ' }'
|
357
|
+
f.puts ' },'
|
358
|
+
f.puts ' {'
|
359
|
+
f.puts " \"environment\": \"another_#{environment}\","
|
360
|
+
f.puts " \"model\": {"
|
361
|
+
f.puts " \"class\": \"SpecModel\","
|
362
|
+
f.puts " \"entries\": ["
|
363
|
+
f.puts ' {'
|
364
|
+
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
365
|
+
f.puts ' }'
|
366
|
+
f.puts ' ]'
|
367
|
+
f.puts ' }'
|
368
|
+
f.puts ' }'
|
369
|
+
f.puts ']'
|
370
|
+
f.puts ''
|
371
|
+
end
|
372
|
+
|
189
373
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
374
|
+
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
375
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
190
376
|
expect(SpecModel.dataset.all.length).to be 1
|
191
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
377
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
192
378
|
end
|
193
|
-
end
|
194
379
|
|
195
|
-
|
196
|
-
it 'should not make any change to the database' do
|
380
|
+
it 'should not apply a basic Seed if it was not specified for the given environment' do
|
197
381
|
Sequel::Seed.setup environment
|
198
382
|
|
199
|
-
File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
|
200
|
-
f.puts
|
201
|
-
f.puts
|
202
|
-
f.puts
|
203
|
-
f.puts
|
204
|
-
f.puts '
|
383
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", 'w+') do |f|
|
384
|
+
f.puts '{'
|
385
|
+
f.puts " \"environment\": \"another_#{environment}\","
|
386
|
+
f.puts " \"spec_model\": {"
|
387
|
+
f.puts " \"sentence\": \"should not changed (from JSON file) #{random_word}\""
|
388
|
+
f.puts ' }'
|
389
|
+
f.puts '}'
|
390
|
+
f.puts ''
|
205
391
|
end
|
206
392
|
|
207
393
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
208
394
|
expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
|
395
|
+
expect(Sequel::Seed::Base.descendants.length).to be 1
|
209
396
|
expect(SpecModel.dataset.all.length).to be 0
|
210
397
|
end
|
211
398
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -16,7 +16,7 @@ module Sequel::Seed
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def seed_file_name
|
19
|
-
"#{Time.now.strftime('%Y%m%d%H%M%S')}_testing_#{Faker::Lorem.word}_#{Faker::Lorem.word}
|
19
|
+
"#{Time.now.strftime('%Y%m%d%H%M%S')}_testing_#{Faker::Lorem.word}_#{Faker::Lorem.word}"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -49,4 +49,5 @@ RSpec.configure do |config|
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
class SpecModel < Sequel::Model
|
52
|
+
class SpecModel < Sequel::Model
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-seed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ewerton Assis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|