@ -196,8 +196,8 @@ RSpec.describe CaseLog do
described_class . create ( {
managing_organisation : organisation ,
owning_organisation : organisation ,
property_p ostcode : " M1 1AE " ,
previous_ postcode : " M2 2AE " ,
postcode_full : " M1 1AE " ,
ppostcode_full : " M2 2AE " ,
startdate : Time . gm ( 2021 , 10 , 10 ) ,
mrcdate : Time . gm ( 2021 , 5 , 4 ) ,
property_void_date : Time . gm ( 2021 , 3 , 3 ) ,
@ -1134,6 +1134,15 @@ RSpec.describe CaseLog do
end
end
def check_postcode_fields ( postcode_field , outcode_field , incode_field )
record_from_db = ActiveRecord :: Base . connection . execute ( " select #{ postcode_field } , #{ outcode_field } , #{ incode_field } from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( address_case_log [ postcode_field ] ) . to eq ( " M11AE " )
expect ( record_from_db [ postcode_field ] ) . to eq ( " M11AE " )
expect ( address_case_log [ outcode_field ] ) . to eq ( " M1 " )
expect ( record_from_db [ outcode_field ] ) . to eq ( " M1 " )
expect ( address_case_log [ incode_field ] ) . to eq ( " 1AE " )
expect ( record_from_db [ incode_field ] ) . to eq ( " 1AE " )
end
context " when saving addresses " do
before do
stub_request ( :get , / api.postcodes.io / )
@ -1145,10 +1154,28 @@ RSpec.describe CaseLog do
managing_organisation : organisation ,
owning_organisation : organisation ,
postcode_known : 1 ,
property_p ostcode : " M1 1AE " ,
postcode_full : " M1 1AE " ,
} )
end
def check_property_postcode_fields
check_postcode_fields ( " postcode_full " , " postcode " , " postcod2 " )
end
it " correctly formats previous postcode " do
address_case_log . update! ( postcode_full : " M1 1AE " )
check_property_postcode_fields
address_case_log . update! ( postcode_full : " m1 1ae " )
check_property_postcode_fields
address_case_log . update! ( postcode_full : " m11Ae " )
check_property_postcode_fields
address_case_log . update! ( postcode_full : " m11ae " )
check_property_postcode_fields
end
it " correctly infers la " do
record_from_db = ActiveRecord :: Base . connection . execute ( " select la from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( address_case_log . la ) . to eq ( " E08000003 " )
@ -1156,12 +1183,12 @@ RSpec.describe CaseLog do
end
it " errors if the property postcode is emptied " do
expect { address_case_log . update! ( { property_p ostcode : " " } ) }
expect { address_case_log . update! ( { postcode_full : " " } ) }
. to raise_error ( ActiveRecord :: RecordInvalid , / #{ I18n . t ( " validations.postcode " ) } / )
end
it " errors if the property postcode is not valid " do
expect { address_case_log . update! ( { property_p ostcode : " invalid_postcode " } ) }
expect { address_case_log . update! ( { postcode_full : " invalid_postcode " } ) }
. to raise_error ( ActiveRecord :: RecordInvalid , / #{ I18n . t ( " validations.postcode " ) } / )
end
@ -1172,15 +1199,15 @@ RSpec.describe CaseLog do
it " logs a warning " do
expect ( Rails . logger ) . to receive ( :warn ) . with ( " Postcodes.io lookup timed out " )
address_case_log . update! ( { postcode_known : 1 , property_p ostcode : " M1 1AD " } )
address_case_log . update! ( { postcode_known : 1 , postcode_full : " M1 1AD " } )
end
end
it " correctly resets all fields if property postcode not known " do
address_case_log . update! ( { postcode_known : 0 } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, property_p ostcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " property_p ostcode " ] ) . to eq ( nil )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, postcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " postcode_full " ] ) . to eq ( nil )
expect ( address_case_log . la ) . to eq ( nil )
expect ( record_from_db [ " la " ] ) . to eq ( nil )
end
@ -1189,15 +1216,15 @@ RSpec.describe CaseLog do
address_case_log . update! ( { postcode_known : 0 } )
address_case_log . update! ( { la_known : 1 , la : " E09000033 " } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, property_p ostcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " property_p ostcode " ] ) . to eq ( nil )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, postcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " postcode_full " ] ) . to eq ( nil )
expect ( address_case_log . la ) . to eq ( " E09000033 " )
expect ( record_from_db [ " la " ] ) . to eq ( " E09000033 " )
address_case_log . update! ( { postcode_known : 1 , property_p ostcode : " M1 1AD " } )
address_case_log . update! ( { postcode_known : 1 , postcode_full : " M1 1AD " } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, property_p ostcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " property_p ostcode " ] ) . to eq ( " M1 1AD " )
record_from_db = ActiveRecord :: Base . connection . execute ( " select la, postcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " postcode_full " ] ) . to eq ( " M11AD " )
expect ( address_case_log . la ) . to eq ( " E08000003 " )
expect ( record_from_db [ " la " ] ) . to eq ( " E08000003 " )
end
@ -1214,10 +1241,28 @@ RSpec.describe CaseLog do
managing_organisation : organisation ,
owning_organisation : organisation ,
previous_postcode_known : 1 ,
previous_ postcode : " M1 1AE " ,
ppostcode_full : " M1 1AE " ,
} )
end
def check_previous_postcode_fields
check_postcode_fields ( " ppostcode_full " , " ppostc1 " , " ppostc2 " )
end
it " correctly formats previous postcode " do
address_case_log . update! ( ppostcode_full : " M1 1AE " )
check_previous_postcode_fields
address_case_log . update! ( ppostcode_full : " m1 1ae " )
check_previous_postcode_fields
address_case_log . update! ( ppostcode_full : " m11Ae " )
check_previous_postcode_fields
address_case_log . update! ( ppostcode_full : " m11ae " )
check_previous_postcode_fields
end
it " correctly infers prevloc " do
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( address_case_log . prevloc ) . to eq ( " E08000003 " )
@ -1225,20 +1270,20 @@ RSpec.describe CaseLog do
end
it " errors if the previous postcode is emptied " do
expect { address_case_log . update! ( { previous_ postcode : " " } ) }
expect { address_case_log . update! ( { ppostcode_full : " " } ) }
. to raise_error ( ActiveRecord :: RecordInvalid , / #{ I18n . t ( " validations.postcode " ) } / )
end
it " errors if the previous postcode is not valid " do
expect { address_case_log . update! ( { previous_ postcode : " invalid_postcode " } ) }
expect { address_case_log . update! ( { ppostcode_full : " invalid_postcode " } ) }
. to raise_error ( ActiveRecord :: RecordInvalid , / #{ I18n . t ( " validations.postcode " ) } / )
end
it " correctly resets all fields if previous postcode not known " do
address_case_log . update! ( { previous_postcode_known : 0 } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, previous_ postcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " previous_ postcode " ] ) . to eq ( nil )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, ppostcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " ppostcode_full " ] ) . to eq ( nil )
expect ( address_case_log . prevloc ) . to eq ( nil )
expect ( record_from_db [ " prevloc " ] ) . to eq ( nil )
end
@ -1260,15 +1305,15 @@ RSpec.describe CaseLog do
address_case_log . update! ( { previous_postcode_known : 0 } )
address_case_log . update! ( { previous_la_known : 1 , prevloc : " E09000033 " } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, previous_ postcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " previous_ postcode " ] ) . to eq ( nil )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, ppostcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " ppostcode_full " ] ) . to eq ( nil )
expect ( address_case_log . prevloc ) . to eq ( " E09000033 " )
expect ( record_from_db [ " prevloc " ] ) . to eq ( " E09000033 " )
address_case_log . update! ( { previous_postcode_known : 0 , previous_ postcode : " M1 1AD " } )
address_case_log . update! ( { previous_postcode_known : 0 , ppostcode_full : " M1 1AD " } )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, previous_ postcode from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " previous_ postcode " ] ) . to eq ( " M1 1AD " )
record_from_db = ActiveRecord :: Base . connection . execute ( " select prevloc, ppostcode_full from case_logs where id= #{ address_case_log . id } " ) . to_a [ 0 ]
expect ( record_from_db [ " ppostcode_full " ] ) . to eq ( " M11AD " )
expect ( address_case_log . prevloc ) . to eq ( " E08000003 " )
expect ( record_from_db [ " prevloc " ] ) . to eq ( " E08000003 " )
end