Last active
May 6, 2020 16:55
-
-
Save ltello/2a4eeb2bb731aeecd04675d556a2f065 to your computer and use it in GitHub Desktop.
Sample model test file (minitest)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "test_helper" | |
class GasSafetyRecordTest < ActiveSupport::TestCase | |
describe GasSafetyRecord do | |
let(:gas_safety_record) { GasSafetyRecord.new } | |
subject { gas_safety_record } | |
describe "Columns" do | |
should have_db_column(:co_alarm_in_date) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:co_alarm_present) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:co_alarm_working) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:created_at) .of_type(:datetime).with_options(null: false) | |
should have_db_column(:customer_job_address) .of_type(:string) | |
should have_db_column(:customer_name) .of_type(:string) | |
should have_db_column(:customer_telephone) .of_type(:string) | |
should have_db_column(:defects) .of_type(:text).with_options(array: true, default: []) | |
should have_db_column(:details_of_work_carried_out) .of_type(:text) | |
should have_db_column(:emergency_control_accessible) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:equipotential_bonding_satisfactory).of_type(:boolean).with_options(null: false) | |
should have_db_column(:gas_installation_pipework_safe) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:gas_safe_register_number) .of_type(:integer) | |
should have_db_column(:gas_tightness_test_satisfactory) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:installation_job_id) .of_type(:uuid) | |
should have_db_column(:issued_by_date) .of_type(:string) | |
should have_db_column(:issued_by_license_number) .of_type(:string) | |
should have_db_column(:issued_by_name) .of_type(:string) | |
should have_db_column(:issued_by_signature) .of_type(:text) | |
should have_db_column(:label_in_the_emergency_control) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:landlord_address) .of_type(:string) | |
should have_db_column(:received_by_date) .of_type(:string) | |
should have_db_column(:received_by_missing) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:received_by_name) .of_type(:string) | |
should have_db_column(:received_by_property_relationship) .of_type(:string) | |
should have_db_column(:received_by_signature) .of_type(:text) | |
should have_db_column(:registered_business_address) .of_type(:string) | |
should have_db_column(:registered_business_telephone) .of_type(:string) | |
should have_db_column(:remedials) .of_type(:string) | |
should have_db_column(:smoke_alarm_present) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:smoke_alarm_working) .of_type(:boolean).with_options(null: false) | |
should have_db_column(:updated_at) .of_type(:datetime).with_options(null: false) | |
should have_db_column(:warning_advices) .of_type(:boolean) | |
end | |
describe "Associations" do | |
should belong_to(:installation_job).inverse_of(:gas_safety_record) | |
should have_many(:appliances).dependent(:destroy) | |
.class_name("GasSafetyRecordAppliance") | |
.inverse_of(:gas_safety_record) | |
should accept_nested_attributes_for(:appliances) | |
end | |
describe "Validations" do | |
should validate_presence_of(:installation_job_id) | |
should validate_inclusion_of(:co_alarm_in_date) .in_array([true, false]) | |
should validate_inclusion_of(:co_alarm_present) .in_array([true, false]) | |
should validate_inclusion_of(:co_alarm_working) .in_array([true, false]) | |
should validate_inclusion_of(:equipotential_bonding_satisfactory).in_array([true, false]) | |
should validate_inclusion_of(:emergency_control_accessible) .in_array([true, false]) | |
should validate_inclusion_of(:gas_installation_pipework_safe) .in_array([true, false]) | |
should validate_inclusion_of(:gas_tightness_test_satisfactory) .in_array([true, false]) | |
should validate_inclusion_of(:label_in_the_emergency_control) .in_array([true, false]) | |
should validate_inclusion_of(:smoke_alarm_present) .in_array([true, false]) | |
should validate_inclusion_of(:smoke_alarm_working) .in_array([true, false]) | |
describe ".received_by_consistency" do | |
let(:gas_safety_record) do | |
build(:gas_safety_record, received_by_missing: received_by_missing, **received_by_property) | |
end | |
describe "when received_by_name is blank" do | |
let(:received_by_property) { { received_by_name: "" } } | |
describe "When received_by_missing is false" do | |
let(:received_by_missing) { false } | |
it "add an error on :received_by_missing" do | |
refute_predicate gas_safety_record, :valid? | |
assert_includes gas_safety_record.errors.messages[:received_by_missing], | |
"must be consistent with received_by_* values" | |
end | |
end | |
describe "When received_by_missing is true" do | |
let(:received_by_missing) { true } | |
it "the record is valid" do | |
assert_predicate gas_safety_record, :valid? | |
end | |
end | |
end | |
describe "when received_by_date is blank" do | |
let(:received_by_property) { { received_by_date: "" } } | |
describe "When received_by_missing is false" do | |
let(:received_by_missing) { false } | |
it "add an error on :received_by_missing" do | |
refute_predicate gas_safety_record, :valid? | |
assert_includes gas_safety_record.errors.messages[:received_by_missing], | |
"must be consistent with received_by_* values" | |
end | |
end | |
describe "When received_by_missing is true" do | |
let(:received_by_missing) { true } | |
it "the record is valid" do | |
assert_predicate gas_safety_record, :valid? | |
end | |
end | |
end | |
describe "when received_by_signature is blank" do | |
let(:received_by_property) { { received_by_signature: "" } } | |
describe "When received_by_missing is false" do | |
let(:received_by_missing) { false } | |
it "add an error on :received_by_missing" do | |
refute_predicate gas_safety_record, :valid? | |
assert_includes gas_safety_record.errors.messages[:received_by_missing], | |
"must be consistent with received_by_* values" | |
end | |
end | |
describe "When received_by_missing is true" do | |
let(:received_by_missing) { true } | |
it "the record is valid" do | |
assert_predicate gas_safety_record, :valid? | |
end | |
end | |
end | |
describe "when received_by_property_relationship is blank" do | |
let(:received_by_property) { { received_by_property_relationship: "" } } | |
describe "When received_by_missing is false" do | |
let(:received_by_missing) { false } | |
it "add an error on :received_by_missing" do | |
refute_predicate gas_safety_record, :valid? | |
assert_includes gas_safety_record.errors.messages[:received_by_missing], | |
"must be consistent with received_by_* values" | |
end | |
end | |
describe "When received_by_missing is true" do | |
let(:received_by_missing) { true } | |
it "the record is valid" do | |
assert_predicate gas_safety_record, :valid? | |
end | |
end | |
end | |
describe "when all received_by_* properties are present" do | |
let(:received_by_property) { {} } | |
describe "When received_by_missing is true" do | |
let(:received_by_missing) { true } | |
it "add an error on :received_by_missing" do | |
refute_predicate gas_safety_record, :valid? | |
assert_includes gas_safety_record.errors.messages[:received_by_missing], | |
"must be consistent with received_by_* values" | |
end | |
end | |
describe "When received_by_missing is false" do | |
let(:received_by_missing) { false } | |
it "the record is valid" do | |
assert_predicate gas_safety_record, :valid? | |
end | |
end | |
end | |
end | |
end | |
describe "Instance methods" do | |
let(:gas_safety_record) { build(:gas_safety_record) } | |
let(:appliance) { gas_safety_record.appliances.first } | |
subject { gas_safety_record } | |
should delegate_method(:order_short_id) .to(:installation_job) | |
should delegate_method(:standing_pressure_at_the_meter) .to(:boiler_appliance) | |
should delegate_method(:working_pressure_at_the_meter) .to(:boiler_appliance) | |
should delegate_method(:working_pressure_at_the_appliance).to(:boiler_appliance) | |
describe ".boiler_appliance" do | |
describe "when there are boiler appliances" do | |
let(:gas_safety_record) { build(:gas_safety_record) } | |
describe "when one of them has pressures measurements set" do | |
it "should return a boiler appliance" do | |
assert_equal appliance, gas_safety_record.boiler_appliance | |
end | |
end | |
describe "when none of them has pressures measurements set" do | |
let(:gas_safety_record) { build(:gas_safety_record, type: :no_pressures) } | |
it "should return nil" do | |
assert_nil gas_safety_record.boiler_appliance | |
end | |
end | |
end | |
describe "when there are no boiler appliances" do | |
let(:gas_safety_record) { build(:gas_safety_record, with_appliance: false) } | |
it "should return nil" do | |
assert_nil gas_safety_record.boiler_appliance | |
end | |
end | |
end | |
describe ".has_defects_remedials_or_advices?" do | |
let(:defects) { [] } | |
let(:remedials) {} | |
let(:gas_safety_record) do | |
create(:gas_safety_record, defects: defects, remedials: remedials) | |
end | |
describe "when defects are present" do | |
let(:defects) { Array.new(2) { Faker::Lorem.sentence } } | |
it "return true" do | |
assert_equal true, gas_safety_record.has_defects_remedials_or_advices? | |
end | |
end | |
describe "when remedials are present" do | |
let(:remedials) { Array.new(2) { Faker::Lorem.sentence }.join("\n") } | |
it "return true" do | |
assert_equal true, gas_safety_record.has_defects_remedials_or_advices? | |
end | |
end | |
describe "when none of them are present" do | |
it "return false" do | |
assert_equal false, gas_safety_record.has_defects_remedials_or_advices? | |
end | |
end | |
end | |
describe ".most_recent_updated_at" do | |
let(:gas_safety_record) { create(:gas_safety_record) } | |
describe "when the records object has been modified after it's appliances" do | |
before do | |
gas_safety_record.update(remedials: Faker::Lorem.sentence) | |
end | |
it "return the records update_at value" do | |
assert_equal gas_safety_record.updated_at, gas_safety_record.most_recent_updated_at | |
end | |
end | |
describe "when one of the appliances objects has been modified after it's parent records" do | |
before do | |
appliance.update(remedials: Faker::Lorem.sentence) | |
end | |
it "return the appliance update_at value" do | |
assert_equal appliance.updated_at, gas_safety_record.most_recent_updated_at | |
end | |
end | |
describe "when nothing has been modified since creation" do | |
let(:most_recent_updated_at) { [gas_safety_record, appliance].map(&:updated_at).max } | |
before do | |
most_recent_updated_at | |
end | |
it "return the most recently created object" do | |
assert_equal most_recent_updated_at, gas_safety_record.most_recent_updated_at | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment