From 6139866565cc53cc5325c938aa5da610877ef2ae Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Mon, 9 May 2022 09:57:50 +0100 Subject: [PATCH] Improve DB seeding (#556) * Improve DB seeding * Don't seed LA Rent Ranges in test --- app/models/form_handler.rb | 2 +- db/seeds.rb | 55 +++++++++++++++++++++++++++----------- lib/tasks/rent_ranges.rake | 2 +- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index c74a75db3..13efcb9ca 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -20,7 +20,7 @@ private forms = {} directories.each do |directory| Dir.glob("#{directory}/*.json").each do |form_path| - form_name = form_path.sub(".json", "").split("/")[-1] + form_name = File.basename(form_path, ".json") forms[form_name] = Form.new(form_path, form_name) end end diff --git a/db/seeds.rb b/db/seeds.rb index 2ee176d38..ce968cf1b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,7 +6,8 @@ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) -org = Organisation.create!( +# rubocop:disable Rails/Output +org = Organisation.find_or_create_by!( name: "DLUHC", address_line1: "2 Marsham Street", address_line2: "London", @@ -15,19 +16,43 @@ org = Organisation.create!( other_stock_owners: "None", managing_agents: "None", provider_type: "LA", -) -User.create!( - email: "test@example.com", - password: "password", - organisation: org, - role: "data_provider", -) +) do + info = "Seeded DLUHC Organisation" + if Rails.env.development? + pp info + else + Rails.logger.info info + end +end -User.create!( - email: "coordinator@example.com", - password: "password", - organisation: org, - role: "data_coordinator", -) +if Rails.env.development? && User.count.zero? + User.create!( + email: "provider@example.com", + organisation: org, + role: "data_provider", + ) -AdminUser.create!(email: "admin@example.com", password: "password", phone: "000000000") + User.create!( + email: "coordinator@example.com", + password: "password", + organisation: org, + role: "data_coordinator", + ) + + User.create!( + email: "support@example.com", + password: "password", + organisation: org, + role: "support", + ) + + pp "Seeded 3 dummy users" +end + +if LaRentRange.count.zero? && !Rails.env.test? + Dir.glob("config/rent_range_data/*.csv").each do |path| + start_year = File.basename(path, ".csv") + Rake::Task["data_import:rent_ranges"].invoke(start_year, path) + end +end +# rubocop:enable Rails/Output diff --git a/lib/tasks/rent_ranges.rake b/lib/tasks/rent_ranges.rake index f4b3b24e7..eafa5495a 100644 --- a/lib/tasks/rent_ranges.rake +++ b/lib/tasks/rent_ranges.rake @@ -24,6 +24,6 @@ namespace :data_import do ) count += 1 end - pp "Created/updated #{count} records" unless Rails.env.test? + pp "Created/updated #{count} LA Rent Range records" unless Rails.env.test? end end