Browse Source

add tests for calculate_status that implicitly also check if the factory traits for copmleted are working

also some minor renaming and refactoring
pull/1698/head
Arthur Campbell 3 years ago
parent
commit
38da80d902
  1. 8
      app/models/log.rb
  2. 5
      spec/models/form_spec.rb
  3. 23
      spec/models/log_spec.rb

8
app/models/log.rb

@ -139,9 +139,9 @@ class Log < ApplicationRecord
def calculate_status def calculate_status
return "deleted" if discarded_at.present? return "deleted" if discarded_at.present?
if all_fields_completed? && errors.empty? if all_subsections_completed? && errors.empty?
"completed" "completed"
elsif all_fields_nil? elsif all_subsections_unstarted?
"not_started" "not_started"
else else
"in_progress" "in_progress"
@ -210,11 +210,11 @@ private
self.status = calculate_status self.status = calculate_status
end end
def all_fields_completed? def all_subsections_completed?
form.subsections.all? { |subsection| subsection.complete?(self) || subsection.not_displayed_in_tasklist?(self) } form.subsections.all? { |subsection| subsection.complete?(self) || subsection.not_displayed_in_tasklist?(self) }
end end
def all_fields_nil? def all_subsections_unstarted?
not_started_statuses = %i[not_started cannot_start_yet] not_started_statuses = %i[not_started cannot_start_yet]
form.subsections.all? { |subsection| not_started_statuses.include? subsection.status(self) } form.subsections.all? { |subsection| not_started_statuses.include? subsection.status(self) }
end end

5
spec/models/form_spec.rb

@ -205,10 +205,9 @@ RSpec.describe Form, type: :model do
let(:lettings_log) { build(:lettings_log, :completed, status: "in_progress") } let(:lettings_log) { build(:lettings_log, :completed, status: "in_progress") }
let(:subsection) { form.get_subsection("setup") } let(:subsection) { form.get_subsection("setup") }
around do |example| before do
Timecop.return
FormHandler.instance.use_real_forms! FormHandler.instance.use_real_forms!
example.run
end end
it "should not raise a Stack Error" do it "should not raise a Stack Error" do

23
spec/models/log_spec.rb

@ -5,4 +5,27 @@ RSpec.describe Log, type: :model do
expect(SalesLog).to be < described_class expect(SalesLog).to be < described_class
expect(LettingsLog).to be < described_class expect(LettingsLog).to be < described_class
end end
describe "#calculate_status" do
it "returns the correct status for a completed sales log" do
complete_sales_log = create(:sales_log, :completed, status: nil)
expect(complete_sales_log.calculate_status).to eq "completed"
end
it "returns the correct status for an in progress sales log" do
in_progress_sales_log = create(:sales_log, :in_progress, status: nil)
expect(in_progress_sales_log.calculate_status).to eq "in_progress"
end
it "returns the correct status for a completed lettings log" do
complete_lettings_log = create(:lettings_log, :completed, status: nil)
binding.pry
expect(complete_lettings_log.calculate_status).to eq "completed"
end
it "returns the correct status for an in progress lettings log" do
in_progress_lettings_log = create(:lettings_log, :in_progress, status: nil)
expect(in_progress_lettings_log.calculate_status).to eq "in_progress"
end
end
end end

Loading…
Cancel
Save