Browse Source

Fix some tests

pull/2605/head
Manny Dinssa 2 years ago
parent
commit
9c77a8ae2a
  1. 11
      app/helpers/locations_helper.rb
  2. 7
      app/models/location.rb
  3. 24
      spec/requests/schemes_controller_spec.rb

11
app/helpers/locations_helper.rb

@ -107,12 +107,15 @@ private
periods << ActivePeriod.new(deactivation.reactivation_date, nil) periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end end
last_location_deactivation_date = sorted_deactivation_periods.last&.deactivation_date
scheme_periods = location.scheme.scheme_deactivation_periods scheme_periods = location.scheme.scheme_deactivation_periods
.where("deactivation_date > ? OR reactivation_date > ?", location.available_from, location.available_from) .where("deactivation_date > ? AND (reactivation_date < ? OR reactivation_date IS NULL)", location.available_from, last_location_deactivation_date || Time.zone.now)
.sort_by(&:deactivation_date) .sort_by(&:deactivation_date)
scheme_periods.each do |scheme_period| if scheme_periods.any?
periods.last.to = scheme_period.deactivation_date scheme_periods.each do |scheme_period|
periods << ActivePeriod.new(scheme_period.reactivation_date, nil) periods.last.to = scheme_period.deactivation_date
periods << ActivePeriod.new(scheme_period.reactivation_date, nil)
end
end end
remove_overlapping_and_empty_periods(periods) remove_overlapping_and_empty_periods(periods)

7
app/models/location.rb

@ -90,7 +90,6 @@ class Location < ApplicationRecord
where.not("location_deactivation_periods.reactivation_date IS NULL") where.not("location_deactivation_periods.reactivation_date IS NULL")
.where("location_deactivation_periods.reactivation_date > ?", date) .where("location_deactivation_periods.reactivation_date > ?", date)
.where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id))
.or(reactivating_soon_by_scheme(date))
} }
scope :reactivating_soon_by_scheme, lambda { |date = Time.zone.now| scope :reactivating_soon_by_scheme, lambda { |date = Time.zone.now|
@ -102,7 +101,8 @@ class Location < ApplicationRecord
} }
scope :active_status, lambda { scope :active_status, lambda {
where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon.pluck(:id)) where.not(id: joins(:location_deactivation_periods).reactivating_soon.pluck(:id))
.where.not(id: joins(scheme: [:scheme_deactivation_periods]).reactivating_soon_by_scheme.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly).pluck(:id)) .where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly).pluck(:id))
.where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme).pluck(:id)) .where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme).pluck(:id))
.where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id)) .where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id))
@ -113,7 +113,8 @@ class Location < ApplicationRecord
} }
scope :active, lambda { |date = Time.zone.now| scope :active, lambda { |date = Time.zone.now|
where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon(date).pluck(:id)) where.not(id: joins(:location_deactivation_periods).reactivating_soon(date).pluck(:id))
.where.not(id: joins(scheme: [:scheme_deactivation_periods]).reactivating_soon_by_scheme.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly(date)).pluck(:id)) .where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly(date)).pluck(:id))
.where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme(date)).pluck(:id)) .where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme(date)).pluck(:id))
.where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id)) .where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id))

24
spec/requests/schemes_controller_spec.rb

@ -2534,14 +2534,18 @@ RSpec.describe SchemesController, type: :request do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} log")
end end
end end
context "and no affected logs" do fcontext "and no affected logs" do
let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) } let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) }
it "redirects to the location page and updates the deactivation period" do before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:)
end
it "redirects to the scheme page and updates the deactivation period" do
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
@ -2561,14 +2565,18 @@ RSpec.describe SchemesController, type: :request do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} log")
end end
end end
context "and no affected logs" do context "and no affected logs" do
let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) } let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) }
it "redirects to the location page and updates the deactivation period" do before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:)
end
it "redirects to the scheme page and updates the deactivation period" do
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
@ -2752,6 +2760,10 @@ RSpec.describe SchemesController, type: :request do
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2023" } } } let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2023" } } }
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) } let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:)
end
it "redirects to the scheme page and updates the existing deactivation period" do it "redirects to the scheme page and updates the existing deactivation period" do
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
@ -2772,7 +2784,7 @@ RSpec.describe SchemesController, type: :request do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect 1 logs") expect(page).to have_content("This change will affect 1 log")
end end
end end
end end

Loading…
Cancel
Save