Skip to content

Commit

Permalink
Add remainder of 10282 test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexchan-va committed Dec 17, 2024
1 parent 87b665f commit 53fce20
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 15 deletions.
2 changes: 1 addition & 1 deletion spec/factories/va10282.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
end

factory :va10282_full_form, class: 'SavedClaim::EducationBenefits::VA10282', parent: :education_benefits do
form { File.read(Rails.root.join('spec', 'fixtures', 'education_benefits_claims', '10282', 'minimal.json')) }
form { Rails.root.join('spec', 'fixtures', 'education_benefits_claims', '10282', 'minimal.json').read }
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/education_form/minimal.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name,First Name,Last Name,Select Military Affiliation,Phone Number,Email Address,Country,State,Race/Ethnicity,Gender of Applicant,What is your highest level of education?,Are you currently employed?,What is your current salary?,"Are you currently working in the technology industry? (If so, please select one)"
a c,a,c,Veteran,1234567890,a@c.com,United States,FL,Black or African American,Male,Master's Degree,Yes,"More than $75,000",Computer Programming
25 changes: 25 additions & 0 deletions spec/mailers/create_daily_excel_files_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CreateDailyExcelFilesMailer, type: %i[mailer aws_helpers] do
describe 'excel mailer' do
subject do
described_class.build('eastern').deliver_now
end

context 'when not sending staging emails' do
before do
expect(FeatureFlipper).to receive(:staging_email?).once.and_return(false)
end

it 'emails the the right recipients' do
expect(subject.to).to eq(
%w[
alex.chan1@va.gov
]
)
end
end
end
end
41 changes: 41 additions & 0 deletions spec/mailers/create_staging_excel_files_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CreateStagingExcelFilesMailer, type: %i[mailer aws_helpers] do
describe '#build' do
subject do
File.write("tmp/#{filename}", csv_contents)
described_class.build(filename).deliver_now
end

let(:filename) { "22-10282_#{Time.zone.now.strftime('%m%d%Y_%H%M%S')}.csv" }
let(:csv_contents) do
CSV.generate(row_sep: "\r\n") do |csv|
csv << ['Name', 'First Name', 'Last Name', 'Select Military Affiliation',
'Phone Number', 'Email Address', 'Country', 'State', 'Race/Ethnicity',
'Gender of Applicant', 'What is your highest level of education?',
'Are you currently employed?', 'What is your current salary?',
'Are you currently working in the technology industry? (If so, please select one)']
csv << ['a c', 'a', 'c', 'veteran', '1234567890', 'a@c.com', 'United States', 'FL',
'{"isBlackOrAfricanAmerican"=>true}', 'M', 'MD', 'true', 'moreThanSeventyFive', 'CP']
end
end

after do
FileUtils.rm_f("tmp/#{filename}")
end

context 'when sending emails' do
it 'sends the right email' do
date = Time.zone.now.strftime('%m/%d/%Y')

expect(subject.subject).to eq("Staging CSV file for #{date}")
expect(subject.content_type).to eq('text/csv; charset=UTF-8')
expect(subject.body.raw_source).to eq(csv_contents)
expect(subject.attachments.size).to eq(0)
expect(subject.header['Content-Disposition'].to_s).to include("attachment; filename=#{filename}")
end
end
end
end
20 changes: 11 additions & 9 deletions spec/mailers/create_staging_spool_files_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

require 'rails_helper'

RSpec.describe CreateStagingSpoolFilesMailer, type: %i[mailer aws_helpers] do
RSpec.describe CreateStagingExcelFilesMailer, type: %i[mailer aws_helpers] do
describe '#build' do
subject do
contents = File.read('spec/fixtures/education_form/stagingspool.txt')
described_class.build(contents).deliver_now
allow(File).to receive(:read).with("tmp/#{filename}").and_return(file_contents)
described_class.build(filename).deliver_now
end

let(:filename) { 'test_data.csv' }
let(:file_contents) { "header1,header2\nvalue1,value2" }

context 'when sending emails' do
it 'sends the right email' do
date = Time.zone.now.strftime('%m%d%Y')

subject_txt = "Staging Spool file on #{date}"
body = "The staging spool file for #{date}"
date = Time.zone.now.strftime('%m/%d/%Y')

expect(subject.subject).to eq(subject_txt)
expect(subject.body.raw_source).to include(body)
expect(subject.subject).to eq("Staging CSV file for #{date}")
expect(subject.body.raw_source).to eq(file_contents)
expect(subject.content_type).to include('text/csv')
expect(subject.headers['Content-Disposition']).to eq("attachment; filename=#{filename}")
end
end
end
Expand Down
18 changes: 17 additions & 1 deletion spec/models/education_benefits_claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
create(:va1990).education_benefits_claim
end

%w[1990 1995 1990e 5490 5495 1990n 0993 0994 10203 1990s].each do |form_type|
%w[1990 1995 1990e 5490 5495 1990n 0993 0994 10203 1990s 10282].each do |form_type|
method = "is_#{form_type}?"

describe "##{method}" do
Expand Down Expand Up @@ -240,6 +240,22 @@ def associated_submission
end
end

context 'with a form type of 10282' do
subject do
create(:va10282)
end

it 'creates a submission' do
subject

expect(associated_submission).to eq(
submission_attributes.merge(
'form_type' => '10282'
)
)
end
end

it 'does not create a submission after save if it was already submitted' do
subject.education_benefits_claim.update!(processed_at: Time.zone.now)
expect(EducationBenefitsSubmission.count).to eq(1)
Expand Down
6 changes: 3 additions & 3 deletions spec/models/excel_file_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
subject { described_class.new }

it 'validates filename uniqueness' do
create(:excel_file_event, filename: 'test_file.xlsx')
duplicate = build(:excel_file_event, filename: 'test_file.xlsx')
create(:excel_file_event, filename: 'test_file.csv')
duplicate = build(:excel_file_event, filename: 'test_file.csv')
expect(duplicate.valid?).to eq(false)
end

Expand All @@ -31,7 +31,7 @@
end

it 'returns a new event when filename pattern does not match existing events' do
filename = "#{Time.zone.now.strftime('%Y%m%d')}_vetsgov.xlsx"
filename = "#{Time.zone.now.strftime('%Y%m%d')}_vetsgov.csv"
event = ExcelFileEvent.build_event(filename)
expect(event.filename).to eq(filename)
expect(event.retry_attempt).to eq(0)
Expand Down
12 changes: 12 additions & 0 deletions spec/models/saved_claim/education_benefits/va10282_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'rails_helper'
require 'lib/saved_claims_spec_helper'

RSpec.describe SavedClaim::EducationBenefits::VA10282 do
let(:instance) { FactoryBot.build(:va10282) }

it_behaves_like 'saved_claim'

validate_inclusion(:form_id, '22-10282')
end
166 changes: 166 additions & 0 deletions spec/sidekiq/education_form/create_daily_excel_files_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe EducationForm::CreateDailyExcelFiles, form: :education_benefits, type: :model do
subject { described_class.new }

let!(:application_10282) do
create(:va10282).education_benefits_claim
end

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

after(:all) do
FileUtils.rm_rf('tmp/*.csv')
end

context 'scheduling' do
before do
allow(Rails.env).to receive('development?').and_return(true)
end

context 'job only runs on business days', run_at: '2016-12-31 00:00:00 EDT' do
let(:scheduler) { Rufus::Scheduler.new }
let(:possible_runs) do
{
'2017-01-02 03:00:00 -0500': false,
'2017-01-03 03:00:00 -0500': true,
'2017-01-04 03:00:00 -0500': true,
'2017-01-05 03:00:00 -0500': true,
'2017-01-06 03:00:00 -0500': true
}
end

it 'skips observed holidays' do
possible_runs.each do |day, should_run|
Timecop.freeze(Time.zone.parse(day.to_s).beginning_of_day) do
expect(subject.perform).to be(should_run)
end
end
end
end

it 'logs a message on holidays', run_at: '2017-01-02 03:00:00 EDT' do
expect(subject).not_to receive(:write_csv_file)
expect(subject).to receive('log_info').with("Skipping on a Holiday: New Year's Day")
expect(subject.perform).to be false
end
end

describe '#perform' do
context 'with a mix of valid and invalid records', run_at: '2016-09-16 03:00:00 EDT' do
before do
allow(Rails.env).to receive('development?').and_return(true)
application_10282.saved_claim.form = {}.to_json
application_10282.saved_claim.save!(validate: false) # Make this claim malformed
FactoryBot.create(:va10282_full_form)
# clear out old test files
FileUtils.rm_rf(Dir.glob('tmp/*.csv'))
end

it 'processes the valid records' do
expect { subject.perform }.to change { EducationBenefitsClaim.unprocessed.count }.from(2).to(0)
expect(Dir['tmp/*.csv'].count).to eq(1)
end
end

context 'with records in staging', run_at: '2016-09-16 03:00:00 EDT' do
before do
application_10282.saved_claim.form = {}.to_json
FactoryBot.create(:va10282_full_form)
ActionMailer::Base.deliveries.clear
end

it 'processes records and sends email' do
with_settings(Settings, hostname: 'staging-api.va.gov') do
expect { subject.perform }.to change { EducationBenefitsClaim.unprocessed.count }.from(2).to(0)
expect(ActionMailer::Base.deliveries.count).to be > 0
end
end
end

context 'with no records', run_at: '2016-09-16 03:00:00 EDT' do
before do
EducationBenefitsClaim.delete_all
end

it 'prints a statement and exits' do
expect(subject).not_to receive(:write_csv_file)
expect(subject).to receive('log_info').with('No records to process.').once
expect(subject.perform).to be(true)
end
end
end

describe '#write_csv_file' do
let(:filename) { "22-10282_#{Time.zone.now.strftime('%m%d%Y_%H%M%S')}.csv" }
let(:test_records) do
[
double('Record',
name: 'John Doe',
first_name: 'John',
last_name: 'Doe',
military_affiliation: 'Veteran',
phone_number: '555-555-5555',
email_address: 'john@example.com',
country: 'USA',
state: 'CA',
race_ethnicity: 'White',
gender: 'Male',
education_level: "Bachelor's",
employment_status: 'Employed',
salary: '75000',
technology_industry: 'Software')
]
end

before do
allow(File).to receive(:write)
allow(subject).to receive(:log_info)
end

it 'creates a CSV file with correct headers and data' do
csv_contents = subject.write_csv_file(test_records, filename)
parsed_csv = CSV.parse(csv_contents)

expect(parsed_csv.first).to eq(described_class::HEADERS)

data_row = parsed_csv[1]
expect(data_row).to eq([
'John Doe',
'John',
'Doe',
'Veteran',
'555-555-5555',
'john@example.com',
'USA',
'CA',
'White',
'Male',
"Bachelor's",
'Employed',
'75000',
'Software'
])
end

it 'writes the CSV contents to a file' do
subject.write_csv_file(test_records, filename)
expect(File).to have_received(:write).with("tmp/#{filename}", anything)
end

context 'when a record fails to process' do
let(:error_record) do
double('ErrorRecord').tap do |record|
allow(record).to receive(:name).and_raise(StandardError.new('Test error'))
described_class::EXCEL_FIELDS[1..-1].each do |field|
allow(record).to receive(field).and_return('test')
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_education_form_fixture(filename)
# outside of daily range, given the timecop freeze.
create(:education_benefits_submission, created_at: date - 26.hours, status: 'processed')
create(:education_benefits_submission, created_at: date, status: 'submitted')
%w[1995 1990e 5490 1990n 5495 10203].each do |form_type|
%w[1995 1990e 5490 1990n 5495 10203 10282].each do |form_type|
create(:education_benefits_submission, form_type:, created_at: date)
end
create(:education_benefits_submission, form_type: '0993', created_at: date, region: :western)
Expand Down

0 comments on commit 53fce20

Please sign in to comment.