Browse Source

fix seeding with added tests (#1059)

pull/1061/head
Phil Lee 2 years ago committed by GitHub
parent
commit
47d7e5eae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      db/seeds.rb
  2. 47
      spec/db/seeds_spec.rb
  3. 2
      spec/spec_helper.rb

29
db/seeds.rb

@ -146,33 +146,36 @@ unless Rails.env.test?
child_organisation: managing_agent2, child_organisation: managing_agent2,
) )
if (Rails.env.development? || Rails.env.review?) && User.count.zero? if Rails.env.development? || Rails.env.review?
User.create!( User.find_or_create_by!(
name: "Provider", name: "Provider",
email: "provider@example.com", email: "provider@example.com",
password: "password",
organisation: org, organisation: org,
role: "data_provider", role: "data_provider",
confirmed_at: Time.zone.now, ) do |user|
) user.password = "password"
user.confirmed_at = Time.zone.now
end
User.create!( User.find_or_create_by!(
name: "Coordinator", name: "Coordinator",
email: "coordinator@example.com", email: "coordinator@example.com",
password: "password",
organisation: org, organisation: org,
role: "data_coordinator", role: "data_coordinator",
confirmed_at: Time.zone.now, ) do |user|
) user.password = "password"
user.confirmed_at = Time.zone.now
end
User.create!( User.find_or_create_by!(
name: "Support", name: "Support",
email: "support@example.com", email: "support@example.com",
password: "password",
organisation: org, organisation: org,
role: "support", role: "support",
confirmed_at: Time.zone.now, ) do |user|
) user.password = "password"
user.confirmed_at = Time.zone.now
end
pp "Seeded 3 dummy users" pp "Seeded 3 dummy users"
end end

47
spec/db/seeds_spec.rb

@ -0,0 +1,47 @@
require "rails_helper"
require "rake"
RSpec.describe "seeding process", type: task do
# rubocop:disable RSpec/ExpectOutput
around do |example|
original_stdout = $stdout
$stdout = File.open(File::NULL, "w")
example.run
$stdout = original_stdout
end
# rubocop:enable RSpec/ExpectOutput
before do
Rake.application.rake_require("tasks/rent_ranges")
Rake::Task.define_task(:environment)
allow(Rails.env).to receive(:test?).and_return(false)
allow(Rails.env).to receive(:review?).and_return(true)
end
it "sets up correct data" do
expect {
Rails.application.load_seed
}.to change(User, :count).by(7)
.and change(Organisation, :count).by(8)
.and change(OrganisationRelationship, :count).by(4)
.and change(Scheme, :count).by(3)
.and change(Location, :count).by(3)
.and change(LaRentRange, :count).by(15_450)
end
it "is idempotent" do
Rails.application.load_seed
expect {
Rails.application.load_seed
}.to not_change(User, :count)
.and not_change(Organisation, :count)
.and not_change(OrganisationRelationship, :count)
.and not_change(Scheme, :count)
.and not_change(Location, :count)
.and not_change(LaRentRange, :count)
end
end

2
spec/spec_helper.rb

@ -114,3 +114,5 @@ RSpec.configure do |config|
config.before { RequestHelper.stub_http_requests } config.before { RequestHelper.stub_http_requests }
config.after { RequestHelper.real_http_requests } config.after { RequestHelper.real_http_requests }
end end
RSpec::Matchers.define_negated_matcher :not_change, :change

Loading…
Cancel
Save