Add local_census_records importation model

This model without database allow us to validate incoming file extension and
headers and also does the following during importation process:

* Ignore empty rows
* Classifiy rows in two groups: created_records, invalid_records
This commit is contained in:
Senén Rodero Rodríguez
2019-05-16 10:56:44 +02:00
committed by Javi Martín
parent 0239efef9d
commit 615bfadca8
8 changed files with 218 additions and 1 deletions

View File

@@ -5,6 +5,12 @@ FactoryBot.define do
date_of_birth Date.new(1970, 1, 31)
postal_code "28002"
end
factory :local_census_records_import, class: "LocalCensusRecords::Import" do
file {
path = %w[spec fixtures files local_census_records import valid.csv]
Rack::Test::UploadedFile.new(Rails.root.join(*path))
}
end
sequence(:document_number) { |n| "#{n.to_s.rjust(8, "0")}X" }

View File

@@ -0,0 +1,5 @@
"document_type","document_number","date_of_birth","postal_code"
,"44556678T","07/08/1984",7008
"DNI",,"07/08/1985",7009
"Passport","22556678T",,7010
"NIE","X11556678","07/08/1987",
1 document_type document_number date_of_birth postal_code
2 44556678T 07/08/1984 7008
3 DNI 07/08/1985 7009
4 Passport 22556678T 7010
5 NIE X11556678 07/08/1987

View File

@@ -0,0 +1,5 @@
,,,
"44556678T","DNI","07/08/84",7008
"33556678T","DNI","07/08/84",7008
"22556678T","DNI","07/08/84",7008
"X11556678","NIE","07/08/84",7008
1
2 44556678T DNI 07/08/84 7008
3 33556678T DNI 07/08/84 7008
4 22556678T DNI 07/08/84 7008
5 X11556678 NIE 07/08/84 7008

View File

@@ -0,0 +1,5 @@
"document_type","document_number","date_of_birth","postal_code"
"DNI","44556678T","07/08/1984",7008
"DNI","33556678T","07/08/1985",7008
"DNI","22556678T","07/08/1986",7008
"NIE","X11556678","07/08/1987",7008
1 document_type document_number date_of_birth postal_code
2 DNI 44556678T 07/08/1984 7008
3 DNI 33556678T 07/08/1985 7008
4 DNI 22556678T 07/08/1986 7008
5 NIE X11556678 07/08/1987 7008

View File

@@ -0,0 +1,88 @@
require "rails_helper"
describe LocalCensusRecords::Import do
let(:base_files_path) { %w[spec fixtures files local_census_records import] }
let(:import) { build(:local_census_records_import) }
describe "Validations" do
it "is valid" do
expect(import).to be_valid
end
it "is not valid without a file to import" do
import.file = nil
expect(import).not_to be_valid
end
context "When file extension" do
it "is wrong it should not be valid" do
file = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.gif")
import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid
end
it "is csv it should be valid" do
path = base_files_path << "valid.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
import = build(:local_census_records_import, file: file)
expect(import).to be_valid
end
end
context "When file headers" do
it "are all missing it should not be valid" do
path = base_files_path << "valid-without-headers.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid
end
end
end
context "#save" do
it "Create valid local census records with provided values" do
import.save
local_census_record = LocalCensusRecord.find_by(document_number: "X11556678")
expect(local_census_record).not_to be_nil
expect(local_census_record.document_type).to eq("NIE")
expect(local_census_record.document_number).to eq("X11556678")
expect(local_census_record.date_of_birth).to eq(Date.parse("07/08/1987"))
expect(local_census_record.postal_code).to eq("7008")
end
it "Add successfully created local census records to created_records array" do
import.save
valid_document_numbers = ["44556678T", "33556678T", "22556678T", "X11556678"]
expect(import.created_records.collect(&:document_number)).to eq(valid_document_numbers)
end
it "Add invalid local census records to invalid_records array" do
path = base_files_path << "invalid.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
import.file = file
import.save
invalid_records_document_types = [nil, "DNI", "Passport", "NIE"]
invalid_records_document_numbers = ["44556678T", nil, "22556678T", "X11556678"]
invalid_records_date_of_births = [Date.parse("07/08/1984"), Date.parse("07/08/1985"), nil,
Date.parse("07/08/1987")]
invalid_records_postal_codes = ["7008", "7009", "7010", nil]
expect(import.invalid_records.collect(&:document_type))
.to eq(invalid_records_document_types)
expect(import.invalid_records.collect(&:document_number))
.to eq(invalid_records_document_numbers)
expect(import.invalid_records.collect(&:date_of_birth))
.to eq(invalid_records_date_of_births)
expect(import.invalid_records.collect(&:postal_code))
.to eq(invalid_records_postal_codes)
end
end
end