1
# frozen_string_literal: true
2

3 6
require "spec_helper"
4

5 6
describe Sequel::Seeder do
6 6
  let!(:environment) { "#{Faker::Lorem.word}_#{Faker::Lorem.word}" }
7 6
  let!(:random_word) { Faker::Lorem.word }
8

9 6
  before do
10 6
    Sequel.extension :seed
11 6
    Sequel.extension :pg_array
12 6
    ArraySpecModel.dataset.delete
13 6
    Sequel::Seed::Base.descendants.clear
14
  end
15

16 6
  before(:all) do
17 5
    dsn = begin
18 6
      if RUBY_PLATFORM == "java"
19 0
        "jdbc:postgresql://localhost/sequel_seed_test"
20
      else
21 6
        "postgres://localhost/sequel_seed_test"
22
      end
23
    end
24 6
    @db = Sequel.connect(dsn)
25 6
    @db.extension(:pg_array)
26 6
    @db.drop_table?(:array_spec_models)
27 6
    @db.create_table(:array_spec_models) do
28 6
      primary_key :id, :serial
29 6
      column :selectors, "text[]"
30 6
      String :sentence
31
    end
32 6
    class ArraySpecModel < Sequel::Model(@db); end
33 6
    ArraySpecModel.dataset = @db[:array_spec_models]
34
    #Sequel::Model.db = @db
35
  end
36

37 6
  after(:each) do
38 6
    ArraySpecModel.dataset.delete
39 6
    Sequel::Seed::Base.descendants.clear
40
  end
41

42 6
  it "should raise an error when there is not any seed file to apply" do
43 6
    Sequel::Seed.setup environment
44

45 6
    expect(Sequel::Seed::Base.descendants.length).to be 0
46 6
    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.")
47 6
    expect(ArraySpecModel.dataset.all.length).to be 0
48
  end
49

50 6
  describe "Seeds defined using Ruby code (.rb extension)" do
51 6
    describe "environment references should be indistinguishable between Symbol and String" do
52 6
      context "when the environment is defined using a String" do
53 6
        it "should apply the Seed accordingly" do
54 6
          Sequel::Seed.setup environment
55

56 6
          File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
57 6
            f.puts "Sequel.seed(:#{environment}) do"
58 6
            f.puts "  def run"
59 6
            f.puts "    ArraySpecModel.create \\"
60 6
            f.puts "      :sentence => \"environment defined by String\","
61 6
            f.puts "      :selectors => [\".body\", \".header\", \".string\"]"
62 6
            f.puts "  end"
63 6
            f.puts "end"
64
          end
65

66 6
          expect(Sequel::Seed::Base.descendants.length).to be 0
67 6
          expect(Sequel::Seeder.seeder_class(seed_test_dir)).to be Sequel::TimestampSeeder
68 6
          expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
69 6
          expect(Sequel::Seed::Base.descendants.length).to be 1
70 6
          expect(ArraySpecModel.dataset.all.length).to be 1
71 6
          expect(ArraySpecModel.dataset.first.sentence).to eq "environment defined by String"
72 6
          expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string")
73
        end
74
      end
75

76 6
      context "when the Seed is defined using a String" do
77 6
        it "should apply the Seed accordingly" do
78 6
          Sequel::Seed.setup environment.to_sym
79

80 6
          File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
81 6
            f.puts "Sequel.seed(\"#{environment}\") do"
82 6
            f.puts "  def run"
83 6
            f.puts "    ArraySpecModel.create \\"
84 6
            f.puts "      :sentence => \"Seed defined by String\","
85 6
            f.puts "      :selectors => [\".body\", \".header\", \".environment\"]"
86 6
            f.puts "  end"
87 6
            f.puts "end"
88
          end
89

90 6
          expect(Sequel::Seed::Base.descendants.length).to be 0
91 6
          expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
92 6
          expect(Sequel::Seed::Base.descendants.length).to be 1
93 6
          expect(ArraySpecModel.dataset.all.length).to be 1
94 6
          expect(ArraySpecModel.dataset.first.sentence).to eq "Seed defined by String"
95 6
          expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".environment")
96
        end
97
      end
98

99 6
      context "when both Seed and environment are defined using a String" do
100 6
        it "should apply the Seed accordingly" do
101 6
          Sequel::Seed.setup environment
102

103 6
          File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
104 6
            f.puts "Sequel.seed(\"#{environment}\") do"
105 6
            f.puts "  def run"
106 6
            f.puts "    ArraySpecModel.create \\"
107 6
            f.puts "      :sentence => \"Seed and environment defined by String\","
108 6
            f.puts "      :selectors => [\".body\", \".header\", \".string\", \".environment\"]"
109 6
            f.puts "  end"
110 6
            f.puts "end"
111
          end
112

113 6
          expect(Sequel::Seed::Base.descendants.length).to be 0
114 6
          expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
115 6
          expect(Sequel::Seed::Base.descendants.length).to be 1
116 6
          expect(ArraySpecModel.dataset.all.length).to be 1
117 6
          expect(ArraySpecModel.dataset.first.sentence).to eq "Seed and environment defined by String"
118 6
          expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string", ".environment")
119
        end
120
      end
121

122 6
      context "when both Seed and environment are defined using a Symbol" do
123 6
        it "should apply the Seed accordingly" do
124 6
          Sequel::Seed.setup environment.to_sym
125

126 6
          File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
127 6
            f.puts "Sequel.seed(:#{environment}) do"
128 6
            f.puts "  def run"
129 6
            f.puts "    ArraySpecModel.create \\"
130 6
            f.puts "      :sentence => \"Seed and environment defined by Symbol\","
131 6
            f.puts "      :selectors => [\".body\", \".header\", \".string\", \".environment\", \".symbol\"]"
132 6
            f.puts "  end"
133 6
            f.puts "end"
134
          end
135

136 6
          expect(Sequel::Seed::Base.descendants.length).to be 0
137 6
          expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
138 6
          expect(Sequel::Seed::Base.descendants.length).to be 1
139 6
          expect(ArraySpecModel.dataset.all.length).to be 1
140 6
          expect(ArraySpecModel.dataset.first.sentence).to eq "Seed and environment defined by Symbol"
141 6
          expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string", ".environment", ".symbol")
142
        end
143
      end
144

145 6
      context "when the environment is defined using a String and we have a wildcard Seed" do
146 6
        it "should apply the Seed accordingly" do
147 6
          Sequel::Seed.setup environment
148

149 6
          File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
150 6
            f.puts "Sequel.seed do"
151 6
            f.puts "  def run"
152 6
            f.puts "    ArraySpecModel.create \\"
153 6
            f.puts "      :sentence => \"Wildcard Seed and environment defined by String\","
154 6
            f.puts "      :selectors => [\".body\", \".header\", \".string\", \".wildcard\"]"
155 6
            f.puts "  end"
156 6
            f.puts "end"
157
          end
158

159 6
          expect(Sequel::Seed::Base.descendants.length).to be 0
160 6
          expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
161 6
          expect(Sequel::Seed::Base.descendants.length).to be 1
162 6
          expect(ArraySpecModel.dataset.all.length).to be 1
163 6
          expect(ArraySpecModel.dataset.first.sentence).to eq "Wildcard Seed and environment defined by String"
164 6
          expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string", ".wildcard")
165
        end
166
      end
167
    end
168

169 6
    context "when there\"s a Seed created" do
170 6
      it "should change the database accordingly only once" do
171 6
        Sequel::Seed.setup environment
172

173 6
        File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
174 6
          f.puts "Sequel.seed do"
175 6
          f.puts "  def run"
176 6
          f.puts "    ArraySpecModel.create \\"
177 6
          f.puts "      :sentence => \"should have changed (from Ruby file)\","
178 6
          f.puts "      :selectors => [\".body\", \".header\", \".string\", \".ruby\"]"
179 6
          f.puts "  end"
180 6
          f.puts "end"
181
        end
182

183 6
        expect(Sequel::Seed::Base.descendants.length).to be 0
184 6
        expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
185 6
        expect(Sequel::Seed::Base.descendants.length).to be 1
186 6
        expect(ArraySpecModel.dataset.all.length).to be 1
187 6
        expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from Ruby file)"
188 6
        expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string", ".ruby")
189
        # Once again
190 6
        expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
191 6
        expect(Sequel::Seed::Base.descendants.length).to be 0
192 6
        expect(ArraySpecModel.dataset.all.length).to be 1
193 6
        expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from Ruby file)"
194 6
        expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".string", ".ruby")
195
      end
196
    end
197

198 6
    context "when the specified Seed is not applicable to the given environment" do
199 6
      it "should not make any change to the database" do
200 6
        Sequel::Seed.setup environment
201

202 6
        File.open("#{seed_test_dir}/#{seed_file_name}.rb", "w+") do |f|
203 6
          f.puts "Sequel.seed(:another_#{Faker::Lorem.word}_word) do"
204 6
          f.puts "  def run"
205 6
          f.puts "    ArraySpecModel.create \\"
206 6
          f.puts "      :sentence => \"should not have changed (from Ruby file)\","
207 6
          f.puts "      :selectors => [\".body\", \".header\", \".unchanged\", \".ruby\"]"
208 6
          f.puts "  end"
209 6
          f.puts "end"
210
        end
211

212 6
        expect(Sequel::Seed::Base.descendants.length).to be 0
213 6
        expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
214 6
        expect(ArraySpecModel.dataset.all.length).to be 0
215
      end
216
    end
217
  end
218

219 6
  describe "Seeds defined using YAML code (.{yaml,yml} extension)" do
220 6
    it "should apply a basic YAML Seed if it was specified for the given environment" do
221 6
      Sequel::Seed.setup environment
222

223 6
      File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
224 6
        f.puts "environment: :#{environment}"
225 6
        f.puts "array_spec_model:"
226 6
        f.puts "  sentence: \"should have changed (from YAML file) #{random_word}\""
227 6
        f.puts "  selectors:"
228 6
        f.puts "    - \".body\""
229 6
        f.puts "    - \".header\""
230 6
        f.puts "    - \".yaml\""
231 6
        f.puts ""
232
      end
233

234 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
235 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
236 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
237 6
      expect(ArraySpecModel.dataset.all.length).to be 1
238 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
239 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".yaml")
240
    end
241

242 6
    it "should apply a YAML Seed if it was specified for the given environment" do
243 6
      Sequel::Seed.setup environment
244

245 6
      File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
246 6
        f.puts "environment: :#{environment}"
247 6
        f.puts "model:"
248 6
        f.puts "  class: \"ArraySpecModel\""
249 6
        f.puts "  entries:"
250 6
        f.puts "    -"
251 6
        f.puts "      sentence: \"should have changed (from YAML file) #{random_word}\""
252 6
        f.puts "      selectors:"
253 6
        f.puts "        - .body"
254 6
        f.puts "        - .header"
255 6
        f.puts "        - .yaml"
256 6
        f.puts ""
257
      end
258

259 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
260 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
261 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
262 6
      expect(ArraySpecModel.dataset.all.length).to be 1
263 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
264 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".yaml")
265
    end
266

267 6
    it "should apply a YAML file with multiple Seeds descriptors if they were specified for the given environment" do
268 6
      Sequel::Seed.setup environment
269

270 6
      File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
271 6
        f.puts "-"
272 6
        f.puts "  environment: :#{environment}"
273 6
        f.puts "  model:"
274 6
        f.puts "    class: \"ArraySpecModel\""
275 6
        f.puts "    entries:"
276 6
        f.puts "      -"
277 6
        f.puts "        sentence: \"should have changed (from YAML file) #{random_word}\""
278 6
        f.puts "        selectors:"
279 6
        f.puts "          - .body"
280 6
        f.puts "          - .header"
281 6
        f.puts "          - .yaml"
282 6
        f.puts "          - .environment"
283 6
        f.puts "-"
284 6
        f.puts "  environment: :another_#{environment}"
285 6
        f.puts "  array_spec_model:"
286 6
        f.puts "    sentence: \"should not have changed (from YAML file) #{random_word}\""
287 6
        f.puts "    selectors:"
288 6
        f.puts "      - .body"
289 6
        f.puts "      - .header"
290 6
        f.puts "      - .yaml"
291 6
        f.puts "      - .another_environment"
292 6
        f.puts ""
293
      end
294

295 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
296 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
297 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
298 6
      expect(ArraySpecModel.dataset.all.length).to be 1
299 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from YAML file) #{random_word}"
300 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".yaml", ".environment")
301
    end
302

303 6
    it "should not apply a basic Seed if it was not specified for the given environment" do
304 6
      Sequel::Seed.setup environment
305

306 6
      File.open("#{seed_test_dir}/#{seed_file_name}.yml", "w+") do |f|
307 6
        f.puts "environment: :another_environment_#{Faker::Lorem.word}"
308 6
        f.puts "array_spec_model:"
309 6
        f.puts "  sentence: \"should not have changed (from YAML file)\""
310 6
        f.puts "  selectors:"
311 6
        f.puts "    - .body"
312 6
        f.puts "    - .header"
313 6
        f.puts "    - .yaml"
314 6
        f.puts ""
315
      end
316

317 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
318 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
319 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
320 6
      expect(ArraySpecModel.dataset.all.length).to be 0
321
    end
322
  end
323

324 6
  describe "Seeds defined using JSON code (.json extension)" do
325 6
    it "should apply a basic JSON Seed if it was specified for the given environment" do
326 6
      Sequel::Seed.setup environment
327

328 6
      File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
329 6
        f.puts "{"
330 6
        f.puts "  \"environment\": \"#{environment}\","
331 6
        f.puts "  \"array_spec_model\": {"
332 6
        f.puts "    \"sentence\": \"should have changed (from JSON file) #{random_word}\","
333 6
        f.puts "    \"selectors\": [\".body\", \".header\", \".json\"]"
334 6
        f.puts "  }"
335 6
        f.puts "}"
336 6
        f.puts ""
337
      end
338

339 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
340 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
341 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
342 6
      expect(ArraySpecModel.dataset.all.length).to be 1
343 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
344 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".json")
345
    end
346

347 6
    it "should apply a JSON Seed if it was specified for the given environment" do
348 6
      Sequel::Seed.setup environment
349

350 6
      File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
351 6
        f.puts "{"
352 6
        f.puts "  \"environment\": \"#{environment}\","
353 6
        f.puts "  \"model\": {"
354 6
        f.puts "    \"class\": \"ArraySpecModel\","
355 6
        f.puts "    \"entries\": ["
356 6
        f.puts "      {"
357 6
        f.puts "        \"sentence\": \"should have changed (from JSON file) #{random_word}\","
358 6
        f.puts "        \"selectors\": [\".body\", \".header\", \".json\"]"
359 6
        f.puts "      }"
360 6
        f.puts "    ]"
361 6
        f.puts "  }"
362 6
        f.puts "}"
363 6
        f.puts ""
364
      end
365

366 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
367 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
368 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
369 6
      expect(ArraySpecModel.dataset.all.length).to be 1
370 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
371 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".json")
372
    end
373

374 6
    it "should apply a JSON file with multiple Seeds descriptors if they were specified for the given environment" do
375 6
      Sequel::Seed.setup environment
376

377 6
      File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
378 6
        f.puts "["
379 6
        f.puts "  {"
380 6
        f.puts "    \"environment\": \"#{environment}\","
381 6
        f.puts "    \"model\": {"
382 6
        f.puts "      \"class\": \"ArraySpecModel\","
383 6
        f.puts "      \"entries\": ["
384 6
        f.puts "        {"
385 6
        f.puts "          \"sentence\": \"should have changed (from JSON file) #{random_word}\","
386 6
        f.puts "          \"selectors\": [\".body\", \".header\", \".json\", \".environment\"]"
387 6
        f.puts "        }"
388 6
        f.puts "      ]"
389 6
        f.puts "    }"
390 6
        f.puts "  },"
391 6
        f.puts "  {"
392 6
        f.puts "    \"environment\": \"another_#{environment}\","
393 6
        f.puts "    \"model\": {"
394 6
        f.puts "      \"class\": \"ArraySpecModel\","
395 6
        f.puts "      \"entries\": ["
396 6
        f.puts "        {"
397 6
        f.puts "          \"sentence\": \"should have changed (from JSON file) #{random_word}\","
398 6
        f.puts "          \"selectors\": [\".body\", \".header\", \".json\", \".another_environment\"]"
399 6
        f.puts "        }"
400 6
        f.puts "      ]"
401 6
        f.puts "    }"
402 6
        f.puts "  }"
403 6
        f.puts "]"
404 6
        f.puts ""
405
      end
406

407 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
408 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
409 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
410 6
      expect(ArraySpecModel.dataset.all.length).to be 1
411 6
      expect(ArraySpecModel.dataset.first.sentence).to eq "should have changed (from JSON file) #{random_word}"
412 6
      expect(ArraySpecModel.dataset.first.selectors).to contain_exactly(".body", ".header", ".json", ".environment")
413
    end
414

415 6
    it "should not apply a basic Seed if it was not specified for the given environment" do
416 6
      Sequel::Seed.setup environment
417

418 6
      File.open("#{seed_test_dir}/#{seed_file_name}.json", "w+") do |f|
419 6
        f.puts "{"
420 6
        f.puts "  \"environment\": \"another_#{environment}\","
421 6
        f.puts "  \"array_spec_model\": {"
422 6
        f.puts "    \"sentence\": \"should not changed (from JSON file) #{random_word}\","
423 6
        f.puts "    \"selectors\": [\".body\", \".header\", \".json\"]"
424 6
        f.puts "  }"
425 6
        f.puts "}"
426 6
        f.puts ""
427
      end
428

429 6
      expect(Sequel::Seed::Base.descendants.length).to be 0
430 6
      expect { Sequel::Seeder.apply(@db, seed_test_dir) }.not_to raise_error
431 6
      expect(Sequel::Seed::Base.descendants.length).to be 1
432 6
      expect(ArraySpecModel.dataset.all.length).to be 0
433
    end
434
  end
435
end

Read our documentation on viewing source code .

Loading