Browse Source

Add more tests and format

pull/1762/head
Kat 3 years ago
parent
commit
2bb24bc9a5
  1. 26
      app/models/lettings_log.rb
  2. 18
      spec/models/lettings_log_spec.rb
  3. 16
      spec/models/sales_log_spec.rb

26
app/models/lettings_log.rb

@ -44,23 +44,23 @@ class LettingsLog < Log
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :search_by, lambda { |param| scope :search_by, lambda { |param|
filter_by_location_postcode(param) filter_by_location_postcode(param)
.or(filter_by_tenant_code(param)) .or(filter_by_tenant_code(param))
.or(filter_by_propcode(param)) .or(filter_by_propcode(param))
.or(filter_by_postcode(param)) .or(filter_by_postcode(param))
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :unresolved, -> { where(unresolved: true) } scope :unresolved, -> { where(unresolved: true) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :duplicate_logs, lambda { |log| scope :duplicate_logs, lambda { |log|
attrs_to_check = %w[tenancycode propcode startdate age1 sex1 ecstat1 tcharge postcode_full] attrs_to_check = %w[tenancycode propcode startdate age1 sex1 ecstat1 tcharge postcode_full]
where(log.slice(*attrs_to_check)) where(log.slice(*attrs_to_check))
.where.not(id: log.id) .where.not(id: log.id)
.where.not(status: "deleted") .where.not(status: "deleted")
.where.not("startdate IS NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR tcharge IS NULL OR postcode_full IS NULL OR propcode IS NULL OR needstype IS NULL") .where.not("startdate IS NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR tcharge IS NULL OR postcode_full IS NULL OR propcode IS NULL OR needstype IS NULL")
.where("location_id = ? OR needstype = 1", log.location_id) .where("location_id = ? OR needstype = 1", log.location_id)
} }
scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) } scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze

18
spec/models/lettings_log_spec.rb

@ -2921,6 +2921,24 @@ RSpec.describe LettingsLog do
end end
end end
end end
context "when filtering duplicate logs for organisation" do
let(:organisation) { create(:organisation) }
let(:log) { create(:lettings_log, :duplicate, owning_organisation: organisation) }
let!(:duplicate_log_same_owning_org) { create(:lettings_log, :duplicate, owning_organisation: organisation) }
let!(:duplicate_log_same_managing_org) { create(:lettings_log, :duplicate, managing_organisation: organisation) }
let!(:duplicate_log_different_org) { create(:lettings_log, :duplicate) }
it "returns all duplicate logs for given log" do
expect(described_class.duplicate_logs_for_organisation(organisation, log).count).to eq(2)
end
it "returns duplicate log" do
expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_owning_org)
expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_managing_org)
expect(described_class.duplicate_logs_for_organisation(organisation, log)).not_to include(duplicate_log_different_org)
end
end
end end
describe "#retirement_age_for_person" do describe "#retirement_age_for_person" do

16
spec/models/sales_log_spec.rb

@ -251,6 +251,22 @@ RSpec.describe SalesLog, type: :model do
end end
end end
context "when filtering duplicate logs for organisation" do
let(:organisation) { create(:organisation) }
let(:log) { create(:sales_log, :duplicate, owning_organisation: organisation) }
let!(:duplicate_log_same_owning_org) { create(:sales_log, :duplicate, owning_organisation: organisation) }
let!(:duplicate_log_different_org) { create(:sales_log, :duplicate) }
it "returns all duplicate logs for given log" do
expect(described_class.duplicate_logs_for_organisation(organisation, log).count).to eq(1)
end
it "returns duplicate log" do
expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_owning_org)
expect(described_class.duplicate_logs_for_organisation(organisation, log)).not_to include(duplicate_log_different_org)
end
end
describe "derived variables" do describe "derived variables" do
let(:sales_log) { create(:sales_log, :completed) } let(:sales_log) { create(:sales_log, :completed) }

Loading…
Cancel
Save