sequel-seed 1.0.0 → 1.1.1
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 +5 -5
- data/CHANGELOG.md +5 -0
- data/lib/sequel/extensions/seed.rb +24 -22
- data/spec/extensions/seed_spec.rb +165 -163
- data/spec/extensions/seed_with_pg_array_spec.rb +185 -182
- data/spec/spec_helper.rb +9 -7
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e34a3a615615a7cc5b6050ce49470d0919c3f6634b0ba1d77998a71842c02dce
|
4
|
+
data.tar.gz: f665062fdc93aca2532252b9fcb2b2ff28feecaf55e516a1e8f0d41a3efee5a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5559a37528433b3bdfc7c73b156b23013654f6e3cf78626582d8782a1afbaab4375ff86c390f3ad6a8eb7e157d2d49988ed38efc24a375a81ddd782745a595a
|
7
|
+
data.tar.gz: 69f96f352870e6cdb64a3215b8c7c91c759bd8c5a368bde581007a60a2102b6d53caa93a70eb7c365353f77617b8a1bb5961751d18115a807a323e9545437516
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "json"
|
3
5
|
|
4
6
|
##
|
5
7
|
# Extension based upon Sequel::Migration and Sequel::Migrator
|
@@ -44,7 +46,7 @@ module Sequel
|
|
44
46
|
# end
|
45
47
|
#
|
46
48
|
|
47
|
-
def seed
|
49
|
+
def seed(*env_labels, &block)
|
48
50
|
return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment)
|
49
51
|
|
50
52
|
seed = Class.new(Seed::Base)
|
@@ -62,7 +64,7 @@ module Sequel
|
|
62
64
|
attr_reader :environment
|
63
65
|
|
64
66
|
##
|
65
|
-
# Sets the Sequel::Seed
|
67
|
+
# Sets the Sequel::Seed"s environment to +env+ over which the Seeds should be applied
|
66
68
|
def setup(env, opts = {})
|
67
69
|
@environment = env.to_sym
|
68
70
|
@options ||= {}
|
@@ -112,7 +114,7 @@ module Sequel
|
|
112
114
|
when Hash
|
113
115
|
apply_seed_hash(seed_descriptor)
|
114
116
|
when Array
|
115
|
-
seed_descriptor.each {|seed_hash| apply_seed_hash(seed_hash)}
|
117
|
+
seed_descriptor.each { |seed_hash| apply_seed_hash(seed_hash) }
|
116
118
|
end
|
117
119
|
end
|
118
120
|
|
@@ -120,30 +122,30 @@ module Sequel
|
|
120
122
|
|
121
123
|
def apply_seed_hash(seed_hash)
|
122
124
|
return unless seed_hash.class <= Hash
|
123
|
-
if seed_hash.has_key?(
|
124
|
-
case seed_hash[
|
125
|
+
if seed_hash.has_key?("environment")
|
126
|
+
case seed_hash["environment"]
|
125
127
|
when String, Symbol
|
126
|
-
return if seed_hash[
|
128
|
+
return if seed_hash["environment"].to_sym != Seed.environment
|
127
129
|
when Array
|
128
|
-
return unless seed_hash[
|
130
|
+
return unless seed_hash["environment"].map(&:to_sym).include?(Seed.environment)
|
129
131
|
end
|
130
132
|
end
|
131
133
|
|
132
134
|
keys = seed_hash.keys
|
133
|
-
keys.delete(
|
135
|
+
keys.delete("environment")
|
134
136
|
keys.each do |key|
|
135
137
|
key_hash = seed_hash[key]
|
136
138
|
entries = nil
|
137
|
-
class_name = if key_hash.has_key?(
|
138
|
-
entries = key_hash[
|
139
|
-
key_hash[
|
139
|
+
class_name = if key_hash.has_key?("class")
|
140
|
+
entries = key_hash["entries"]
|
141
|
+
key_hash["class"]
|
140
142
|
else
|
141
143
|
Helpers.camelize(key)
|
142
144
|
end
|
143
145
|
# It will raise an error if the class name is not defined
|
144
146
|
class_const = Kernel.const_get(class_name)
|
145
147
|
if entries
|
146
|
-
entries.each {|hash| create_model(class_const, hash)}
|
148
|
+
entries.each { |hash| create_model(class_const, hash) }
|
147
149
|
else
|
148
150
|
create_model(class_const, key_hash)
|
149
151
|
end
|
@@ -197,7 +199,7 @@ module Sequel
|
|
197
199
|
RUBY_SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb)\z/i.freeze
|
198
200
|
YAML_SEED_FILE_PATTERN = /\A(\d+)_.+\.(yml|yaml)\z/i.freeze
|
199
201
|
JSON_SEED_FILE_PATTERN = /\A(\d+)_.+\.(json)\z/i.freeze
|
200
|
-
SEED_SPLITTER =
|
202
|
+
SEED_SPLITTER = "_".freeze
|
201
203
|
MINIMUM_TIMESTAMP = 20000101
|
202
204
|
|
203
205
|
Error = Seed::Error
|
@@ -212,7 +214,7 @@ module Sequel
|
|
212
214
|
next unless SEED_FILE_PATTERN.match(file)
|
213
215
|
return TimestampSeeder if file.split(SEED_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP
|
214
216
|
end
|
215
|
-
raise(Error, "seeder not available for files; please check the configured seed directory
|
217
|
+
raise(Error, "seeder not available for files; please check the configured seed directory \"#{directory}\". Also ensure seed files are in YYYYMMDD_seed_file.rb format.")
|
216
218
|
else
|
217
219
|
self
|
218
220
|
end
|
@@ -308,7 +310,7 @@ module Sequel
|
|
308
310
|
fi = f.downcase
|
309
311
|
ds.insert(column => fi)
|
310
312
|
end
|
311
|
-
db.log_info("Seed file `#{f}` applied, it took #{sprintf(
|
313
|
+
db.log_info("Seed file `#{f}` applied, it took #{sprintf("%0.6f", Time.now - t)} seconds")
|
312
314
|
end
|
313
315
|
nil
|
314
316
|
end
|
@@ -317,9 +319,9 @@ module Sequel
|
|
317
319
|
|
318
320
|
def get_applied_seeds
|
319
321
|
am = ds.select_order_map(column)
|
320
|
-
missing_seed_files = am - files.map{|f| File.basename(f).downcase}
|
322
|
+
missing_seed_files = am - files.map { |f| File.basename(f).downcase }
|
321
323
|
if missing_seed_files.length > 0 && !@allow_missing_seed_files
|
322
|
-
raise(Error, "Seed files not in file system: #{missing_seed_files.join(
|
324
|
+
raise(Error, "Seed files not in file system: #{missing_seed_files.join(", ")}")
|
323
325
|
end
|
324
326
|
am
|
325
327
|
end
|
@@ -330,7 +332,7 @@ module Sequel
|
|
330
332
|
next unless SEED_FILE_PATTERN.match(file)
|
331
333
|
files << File.join(directory, file)
|
332
334
|
end
|
333
|
-
files.sort_by{|f| SEED_FILE_PATTERN.match(File.basename(f))[1].to_i}
|
335
|
+
files.sort_by { |f| SEED_FILE_PATTERN.match(File.basename(f))[1].to_i }
|
334
336
|
end
|
335
337
|
|
336
338
|
def get_seed_tuples
|
@@ -396,9 +398,9 @@ module Sequel
|
|
396
398
|
c = column
|
397
399
|
ds = db.from(table)
|
398
400
|
if !db.table_exists?(table)
|
399
|
-
db.create_table(table){String c, :
|
401
|
+
db.create_table(table) { String c, primary_key: true }
|
400
402
|
elsif !ds.columns.include?(c)
|
401
|
-
raise(Error, "Seeder table
|
403
|
+
raise(Error, "Seeder table \"#{table}\" does not contain column \"#{c}\"")
|
402
404
|
end
|
403
405
|
ds
|
404
406
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe Sequel.seed do
|
4
6
|
before(:all) do
|
@@ -14,17 +16,17 @@ describe Sequel.seed do
|
|
14
16
|
Sequel::Seed.setup(:test)
|
15
17
|
end
|
16
18
|
|
17
|
-
it
|
19
|
+
it "should create a Seed descendant according to the given environment" do
|
18
20
|
seed = Sequel.seed(:test) {}
|
19
21
|
expect(Sequel::Seed::Base.descendants).to include seed
|
20
22
|
end
|
21
23
|
|
22
|
-
it
|
24
|
+
it "should ignore a Seed not applicable to the given environment" do
|
23
25
|
seed = Sequel.seed(:development) {}
|
24
26
|
expect(Sequel::Seed::Base.descendants).not_to include seed
|
25
27
|
end
|
26
28
|
|
27
|
-
it
|
29
|
+
it "should create a Seed applicable to every environment" do
|
28
30
|
seed = Sequel.seed {}
|
29
31
|
expect(Sequel::Seed::Base.descendants).to include seed
|
30
32
|
end
|
@@ -40,14 +42,14 @@ describe Sequel::Seed.environment do
|
|
40
42
|
Sequel::Seed::Base.descendants.clear
|
41
43
|
end
|
42
44
|
|
43
|
-
it
|
45
|
+
it "should be possible to set the environment with Sequel::Seed.setup method" do
|
44
46
|
Sequel::Seed.setup(:mock)
|
45
47
|
expect(Sequel::Seed.environment).to eq :mock
|
46
48
|
Sequel::Seed.setup("test")
|
47
49
|
expect(Sequel::Seed.environment).to eq :test
|
48
50
|
end
|
49
51
|
|
50
|
-
it
|
52
|
+
it "should be possible to set the environment with Sequel::Seed.environment= method" do
|
51
53
|
Sequel::Seed.environment = :mock
|
52
54
|
expect(Sequel::Seed.environment).to eq :mock
|
53
55
|
Sequel::Seed.environment = "test"
|
@@ -67,21 +69,21 @@ describe Sequel::Seed do
|
|
67
69
|
|
68
70
|
describe "to guarantee backward compatibility" do
|
69
71
|
it "should point Sequel::Seed.descendants to Sequel::Seed::Base.descendants" do
|
70
|
-
Sequel::Seed::Base.descendants <<
|
71
|
-
expect(Sequel::Seed.descendants).to contain_exactly(
|
72
|
+
Sequel::Seed::Base.descendants << "hi"
|
73
|
+
expect(Sequel::Seed.descendants).to contain_exactly("hi")
|
72
74
|
end
|
73
75
|
|
74
76
|
it "should point Sequel::Seed.inherited() to Sequel::Seed::Base.inherited()" do
|
75
|
-
Sequel::Seed::Base.inherited(
|
76
|
-
Sequel::Seed.inherited(
|
77
|
-
expect(Sequel::Seed.descendants).to contain_exactly(
|
77
|
+
Sequel::Seed::Base.inherited("1")
|
78
|
+
Sequel::Seed.inherited("2")
|
79
|
+
expect(Sequel::Seed.descendants).to contain_exactly("1", "2")
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
81
83
|
|
82
84
|
describe Sequel::Seeder do
|
83
|
-
let!(:environment) {"#{Faker::Lorem.word}_#{Faker::Lorem.word}"}
|
84
|
-
let!(:random_word) {Faker::Lorem.word}
|
85
|
+
let!(:environment) { "#{Faker::Lorem.word}_#{Faker::Lorem.word}" }
|
86
|
+
let!(:random_word) { Faker::Lorem.word }
|
85
87
|
|
86
88
|
before do
|
87
89
|
Sequel.extension :seed
|
@@ -91,15 +93,15 @@ describe Sequel::Seeder do
|
|
91
93
|
|
92
94
|
before(:all) do
|
93
95
|
dsn = begin
|
94
|
-
if RUBY_PLATFORM ==
|
95
|
-
|
96
|
+
if RUBY_PLATFORM == "java"
|
97
|
+
"jdbc:sqlite::memory:"
|
96
98
|
else
|
97
|
-
|
99
|
+
"sqlite:/"
|
98
100
|
end
|
99
101
|
end
|
100
102
|
@db = Sequel.connect(dsn)
|
101
103
|
@db.create_table(:spec_models) do
|
102
|
-
primary_key :id, :
|
104
|
+
primary_key :id, auto_increment: true
|
103
105
|
String :sentence
|
104
106
|
end
|
105
107
|
class SpecModel < Sequel::Model; end
|
@@ -116,337 +118,337 @@ describe Sequel::Seeder do
|
|
116
118
|
Sequel::Seed.setup environment
|
117
119
|
|
118
120
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
119
|
-
expect{Sequel::Seeder.apply(@db,
|
121
|
+
expect { Sequel::Seeder.apply(@db, "/") }.to raise_error("seeder not available for files; please check the configured seed directory \"/\". Also ensure seed files are in YYYYMMDD_seed_file.rb format.")
|
120
122
|
expect(SpecModel.dataset.all.length).to be 0
|
121
123
|
end
|
122
124
|
|
123
125
|
describe "Seeds defined using Ruby code (.rb extension)" do
|
124
|
-
describe
|
125
|
-
context
|
126
|
-
it
|
126
|
+
describe "environment references should be indistinguishable between Symbol and String" do
|
127
|
+
context "when the environment is defined using a String" do
|
128
|
+
it "should apply the Seed accordingly" do
|
127
129
|
Sequel::Seed.setup environment
|
128
130
|
|
129
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
131
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
130
132
|
f.puts "Sequel.seed(:#{environment}) do"
|
131
|
-
f.puts
|
132
|
-
f.puts
|
133
|
-
f.puts
|
134
|
-
f.puts
|
133
|
+
f.puts " def run"
|
134
|
+
f.puts " SpecModel.create :sentence => \"environment defined by String\""
|
135
|
+
f.puts " end"
|
136
|
+
f.puts "end"
|
135
137
|
end
|
136
138
|
|
137
139
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
138
140
|
expect(Sequel::Seeder.seeder_class(seed_test_dir)).to be Sequel::TimestampSeeder
|
139
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
141
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
140
142
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
141
143
|
expect(SpecModel.dataset.all.length).to be 1
|
142
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
144
|
+
expect(SpecModel.dataset.first.sentence).to eq "environment defined by String"
|
143
145
|
end
|
144
146
|
end
|
145
147
|
|
146
|
-
context
|
147
|
-
it
|
148
|
+
context "when the Seed is defined using a String" do
|
149
|
+
it "should apply the Seed accordingly" do
|
148
150
|
Sequel::Seed.setup environment.to_sym
|
149
151
|
|
150
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
152
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
151
153
|
f.puts "Sequel.seed(\"#{environment}\") do"
|
152
|
-
f.puts
|
153
|
-
f.puts
|
154
|
-
f.puts
|
155
|
-
f.puts
|
154
|
+
f.puts " def run"
|
155
|
+
f.puts " SpecModel.create :sentence => \"Seed defined by String\""
|
156
|
+
f.puts " end"
|
157
|
+
f.puts "end"
|
156
158
|
end
|
157
159
|
|
158
160
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
159
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
161
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
160
162
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
161
163
|
expect(SpecModel.dataset.all.length).to be 1
|
162
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
164
|
+
expect(SpecModel.dataset.first.sentence).to eq "Seed defined by String"
|
163
165
|
end
|
164
166
|
end
|
165
167
|
|
166
|
-
context
|
167
|
-
it
|
168
|
+
context "when both Seed and environment are defined using a String" do
|
169
|
+
it "should apply the Seed accordingly" do
|
168
170
|
Sequel::Seed.setup environment
|
169
171
|
|
170
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
172
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
171
173
|
f.puts "Sequel.seed(\"#{environment}\") do"
|
172
|
-
f.puts
|
173
|
-
f.puts
|
174
|
-
f.puts
|
175
|
-
f.puts
|
174
|
+
f.puts " def run"
|
175
|
+
f.puts " SpecModel.create :sentence => \"Seed and environment defined by String\""
|
176
|
+
f.puts " end"
|
177
|
+
f.puts "end"
|
176
178
|
end
|
177
179
|
|
178
180
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
179
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
181
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
180
182
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
181
183
|
expect(SpecModel.dataset.all.length).to be 1
|
182
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
184
|
+
expect(SpecModel.dataset.first.sentence).to eq "Seed and environment defined by String"
|
183
185
|
end
|
184
186
|
end
|
185
187
|
|
186
|
-
context
|
187
|
-
it
|
188
|
+
context "when both Seed and environment are defined using a Symbol" do
|
189
|
+
it "should apply the Seed accordingly" do
|
188
190
|
Sequel::Seed.setup environment.to_sym
|
189
191
|
|
190
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
192
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
191
193
|
f.puts "Sequel.seed(:#{environment}) do"
|
192
|
-
f.puts
|
193
|
-
f.puts
|
194
|
-
f.puts
|
195
|
-
f.puts
|
194
|
+
f.puts " def run"
|
195
|
+
f.puts " SpecModel.create :sentence => \"Seed and environment defined by Symbol\""
|
196
|
+
f.puts " end"
|
197
|
+
f.puts "end"
|
196
198
|
end
|
197
199
|
|
198
200
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
199
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
201
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
200
202
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
201
203
|
expect(SpecModel.dataset.all.length).to be 1
|
202
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
204
|
+
expect(SpecModel.dataset.first.sentence).to eq "Seed and environment defined by Symbol"
|
203
205
|
end
|
204
206
|
end
|
205
207
|
|
206
|
-
context
|
207
|
-
it
|
208
|
+
context "when the environment is defined using a String and we have a wildcard Seed" do
|
209
|
+
it "should apply the Seed accordingly" do
|
208
210
|
Sequel::Seed.setup environment
|
209
211
|
|
210
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
211
|
-
f.puts
|
212
|
-
f.puts
|
213
|
-
f.puts
|
214
|
-
f.puts
|
215
|
-
f.puts
|
212
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
213
|
+
f.puts "Sequel.seed do"
|
214
|
+
f.puts " def run"
|
215
|
+
f.puts " SpecModel.create :sentence => \"Wildcard Seed and environment defined by String\""
|
216
|
+
f.puts " end"
|
217
|
+
f.puts "end"
|
216
218
|
end
|
217
219
|
|
218
220
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
219
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
221
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
220
222
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
221
223
|
expect(SpecModel.dataset.all.length).to be 1
|
222
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
224
|
+
expect(SpecModel.dataset.first.sentence).to eq "Wildcard Seed and environment defined by String"
|
223
225
|
end
|
224
226
|
end
|
225
227
|
end
|
226
228
|
|
227
|
-
context
|
228
|
-
it
|
229
|
+
context "when there\"s a Seed created" do
|
230
|
+
it "should change the database accordingly only once" do
|
229
231
|
Sequel::Seed.setup environment
|
230
232
|
|
231
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
232
|
-
f.puts
|
233
|
-
f.puts
|
234
|
-
f.puts
|
235
|
-
f.puts
|
236
|
-
f.puts
|
233
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
234
|
+
f.puts "Sequel.seed do"
|
235
|
+
f.puts " def run"
|
236
|
+
f.puts " SpecModel.create :sentence => \"should have changed (from Ruby file)\""
|
237
|
+
f.puts " end"
|
238
|
+
f.puts "end"
|
237
239
|
end
|
238
240
|
|
239
241
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
240
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
242
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
241
243
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
242
244
|
expect(SpecModel.dataset.all.length).to be 1
|
243
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
245
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from Ruby file)"
|
244
246
|
# Once again
|
245
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
247
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
246
248
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
247
249
|
expect(SpecModel.dataset.all.length).to be 1
|
248
|
-
expect(SpecModel.dataset.first.sentence).to eq
|
250
|
+
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from Ruby file)"
|
249
251
|
end
|
250
252
|
end
|
251
253
|
|
252
|
-
context
|
253
|
-
it
|
254
|
+
context "when the specified Seed is not applicable to the given environment" do
|
255
|
+
it "should not make any change to the database" do
|
254
256
|
Sequel::Seed.setup environment
|
255
257
|
|
256
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.rb",
|
258
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
|
257
259
|
f.puts "Sequel.seed(:another_#{Faker::Lorem.word}_word) do"
|
258
|
-
f.puts
|
259
|
-
f.puts
|
260
|
-
f.puts
|
261
|
-
f.puts
|
260
|
+
f.puts " def run"
|
261
|
+
f.puts " SpecModel.create :sentence => \"should not have changed (from Ruby file)\""
|
262
|
+
f.puts " end"
|
263
|
+
f.puts "end"
|
262
264
|
end
|
263
265
|
|
264
266
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
265
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
267
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
266
268
|
expect(SpecModel.dataset.all.length).to be 0
|
267
269
|
end
|
268
270
|
end
|
269
271
|
end
|
270
272
|
|
271
|
-
describe
|
272
|
-
it
|
273
|
+
describe "Seeds defined using YAML code (.{yaml,yml} extension)" do
|
274
|
+
it "should apply a basic YAML Seed if it was specified for the given environment" do
|
273
275
|
Sequel::Seed.setup environment
|
274
276
|
|
275
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.yml",
|
277
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
|
276
278
|
f.puts "environment: :#{environment}"
|
277
|
-
f.puts
|
278
|
-
f.puts " sentence:
|
279
|
-
f.puts
|
279
|
+
f.puts "spec_model:"
|
280
|
+
f.puts " sentence: \"should have changed (from YAML file) #{random_word}\""
|
281
|
+
f.puts ""
|
280
282
|
end
|
281
283
|
|
282
284
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
283
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
285
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
284
286
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
285
287
|
expect(SpecModel.dataset.all.length).to be 1
|
286
288
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
287
289
|
end
|
288
290
|
|
289
|
-
it
|
291
|
+
it "should apply a YAML Seed if it was specified for the given environment" do
|
290
292
|
Sequel::Seed.setup environment
|
291
293
|
|
292
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.yml",
|
294
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
|
293
295
|
f.puts "environment: :#{environment}"
|
294
|
-
f.puts
|
295
|
-
f.puts
|
296
|
-
f.puts
|
297
|
-
f.puts
|
298
|
-
f.puts " sentence:
|
299
|
-
f.puts
|
296
|
+
f.puts "model:"
|
297
|
+
f.puts " class: \"SpecModel\""
|
298
|
+
f.puts " entries:"
|
299
|
+
f.puts " -"
|
300
|
+
f.puts " sentence: \"should have changed (from YAML file) #{random_word}\""
|
301
|
+
f.puts ""
|
300
302
|
end
|
301
303
|
|
302
304
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
303
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
305
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
304
306
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
305
307
|
expect(SpecModel.dataset.all.length).to be 1
|
306
308
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
307
309
|
end
|
308
310
|
|
309
|
-
it
|
311
|
+
it "should apply a YAML file with multiple Seeds descriptors if they were specified for the given environment" do
|
310
312
|
Sequel::Seed.setup environment
|
311
313
|
|
312
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.yml",
|
313
|
-
f.puts
|
314
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
|
315
|
+
f.puts "-"
|
314
316
|
f.puts " environment: :#{environment}"
|
315
|
-
f.puts
|
316
|
-
f.puts
|
317
|
-
f.puts
|
318
|
-
f.puts
|
319
|
-
f.puts " sentence:
|
320
|
-
f.puts
|
317
|
+
f.puts " model:"
|
318
|
+
f.puts " class: \"SpecModel\""
|
319
|
+
f.puts " entries:"
|
320
|
+
f.puts " -"
|
321
|
+
f.puts " sentence: \"should have changed (from YAML file) #{random_word}\""
|
322
|
+
f.puts "-"
|
321
323
|
f.puts " environment: :another_#{environment}"
|
322
|
-
f.puts
|
323
|
-
f.puts " sentence:
|
324
|
-
f.puts
|
324
|
+
f.puts " spec_model:"
|
325
|
+
f.puts " sentence: \"should not have changed (from YAML file) #{random_word}\""
|
326
|
+
f.puts ""
|
325
327
|
end
|
326
328
|
|
327
329
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
328
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
330
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
329
331
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
330
332
|
expect(SpecModel.dataset.all.length).to be 1
|
331
333
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
|
332
334
|
end
|
333
335
|
|
334
|
-
it
|
336
|
+
it "should not apply a basic Seed if it was not specified for the given environment" do
|
335
337
|
Sequel::Seed.setup environment
|
336
338
|
|
337
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.yml",
|
339
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
|
338
340
|
f.puts "environment: :another_environment_#{Faker::Lorem.word}"
|
339
|
-
f.puts
|
340
|
-
f.puts
|
341
|
-
f.puts
|
341
|
+
f.puts "spec_model:"
|
342
|
+
f.puts " sentence: \"should not have changed (from YAML file)\""
|
343
|
+
f.puts ""
|
342
344
|
end
|
343
345
|
|
344
346
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
345
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
347
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
346
348
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
347
349
|
expect(SpecModel.dataset.all.length).to be 0
|
348
350
|
end
|
349
351
|
end
|
350
352
|
|
351
|
-
describe
|
352
|
-
it
|
353
|
+
describe "Seeds defined using JSON code (.json extension)" do
|
354
|
+
it "should apply a basic JSON Seed if it was specified for the given environment" do
|
353
355
|
Sequel::Seed.setup environment
|
354
356
|
|
355
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.json",
|
356
|
-
f.puts
|
357
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
|
358
|
+
f.puts "{"
|
357
359
|
f.puts " \"environment\": \"#{environment}\","
|
358
360
|
f.puts " \"spec_model\": {"
|
359
361
|
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
360
|
-
f.puts
|
361
|
-
f.puts
|
362
|
-
f.puts
|
362
|
+
f.puts " }"
|
363
|
+
f.puts "}"
|
364
|
+
f.puts ""
|
363
365
|
end
|
364
366
|
|
365
367
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
366
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
368
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
367
369
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
368
370
|
expect(SpecModel.dataset.all.length).to be 1
|
369
371
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
370
372
|
end
|
371
373
|
|
372
|
-
it
|
374
|
+
it "should apply a JSON Seed if it was specified for the given environment" do
|
373
375
|
Sequel::Seed.setup environment
|
374
376
|
|
375
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.json",
|
376
|
-
f.puts
|
377
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
|
378
|
+
f.puts "{"
|
377
379
|
f.puts " \"environment\": \"#{environment}\","
|
378
380
|
f.puts " \"model\": {"
|
379
381
|
f.puts " \"class\": \"SpecModel\","
|
380
382
|
f.puts " \"entries\": ["
|
381
|
-
f.puts
|
383
|
+
f.puts " {"
|
382
384
|
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
383
|
-
f.puts
|
384
|
-
f.puts
|
385
|
-
f.puts
|
386
|
-
f.puts
|
387
|
-
f.puts
|
385
|
+
f.puts " }"
|
386
|
+
f.puts " ]"
|
387
|
+
f.puts " }"
|
388
|
+
f.puts "}"
|
389
|
+
f.puts ""
|
388
390
|
end
|
389
391
|
|
390
392
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
391
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
393
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
392
394
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
393
395
|
expect(SpecModel.dataset.all.length).to be 1
|
394
396
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
395
397
|
end
|
396
398
|
|
397
|
-
it
|
399
|
+
it "should apply a JSON file with multiple Seeds descriptors if they were specified for the given environment" do
|
398
400
|
Sequel::Seed.setup environment
|
399
401
|
|
400
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.json",
|
401
|
-
f.puts
|
402
|
-
f.puts
|
402
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
|
403
|
+
f.puts "["
|
404
|
+
f.puts " {"
|
403
405
|
f.puts " \"environment\": \"#{environment}\","
|
404
406
|
f.puts " \"model\": {"
|
405
407
|
f.puts " \"class\": \"SpecModel\","
|
406
408
|
f.puts " \"entries\": ["
|
407
|
-
f.puts
|
409
|
+
f.puts " {"
|
408
410
|
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
409
|
-
f.puts
|
410
|
-
f.puts
|
411
|
-
f.puts
|
412
|
-
f.puts
|
413
|
-
f.puts
|
411
|
+
f.puts " }"
|
412
|
+
f.puts " ]"
|
413
|
+
f.puts " }"
|
414
|
+
f.puts " },"
|
415
|
+
f.puts " {"
|
414
416
|
f.puts " \"environment\": \"another_#{environment}\","
|
415
417
|
f.puts " \"model\": {"
|
416
418
|
f.puts " \"class\": \"SpecModel\","
|
417
419
|
f.puts " \"entries\": ["
|
418
|
-
f.puts
|
420
|
+
f.puts " {"
|
419
421
|
f.puts " \"sentence\": \"should have changed (from JSON file) #{random_word}\""
|
420
|
-
f.puts
|
421
|
-
f.puts
|
422
|
-
f.puts
|
423
|
-
f.puts
|
424
|
-
f.puts
|
425
|
-
f.puts
|
422
|
+
f.puts " }"
|
423
|
+
f.puts " ]"
|
424
|
+
f.puts " }"
|
425
|
+
f.puts " }"
|
426
|
+
f.puts "]"
|
427
|
+
f.puts ""
|
426
428
|
end
|
427
429
|
|
428
430
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
429
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
431
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
430
432
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
431
433
|
expect(SpecModel.dataset.all.length).to be 1
|
432
434
|
expect(SpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
|
433
435
|
end
|
434
436
|
|
435
|
-
it
|
437
|
+
it "should not apply a basic Seed if it was not specified for the given environment" do
|
436
438
|
Sequel::Seed.setup environment
|
437
439
|
|
438
|
-
File.open("#{seed_test_dir}/#{seed_file_name}.json",
|
439
|
-
f.puts
|
440
|
+
File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
|
441
|
+
f.puts "{"
|
440
442
|
f.puts " \"environment\": \"another_#{environment}\","
|
441
443
|
f.puts " \"spec_model\": {"
|
442
444
|
f.puts " \"sentence\": \"should not changed (from JSON file) #{random_word}\""
|
443
|
-
f.puts
|
444
|
-
f.puts
|
445
|
-
f.puts
|
445
|
+
f.puts " }"
|
446
|
+
f.puts "}"
|
447
|
+
f.puts ""
|
446
448
|
end
|
447
449
|
|
448
450
|
expect(Sequel::Seed::Base.descendants.length).to be 0
|
449
|
-
expect{Sequel::Seeder.apply(@db, seed_test_dir)}.not_to raise_error
|
451
|
+
expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
|
450
452
|
expect(Sequel::Seed::Base.descendants.length).to be 1
|
451
453
|
expect(SpecModel.dataset.all.length).to be 0
|
452
454
|
end
|