Skip to content

Commit

Permalink
add unit test generate and create_tempfile methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmagic80 committed Oct 25, 2024
1 parent 264e21d commit e8befec
Showing 1 changed file with 64 additions and 27 deletions.
91 changes: 64 additions & 27 deletions modules/ivc_champva/spec/services/pdf_filler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require 'rails_helper'
require_relative IvcChampva::Engine.root.join('spec', 'support', 'pdf_matcher.rb')
require IvcChampva::Engine.root.join('spec', 'support', 'pdf_matcher.rb')
require IvcChampva::Engine.root.join('spec', 'spec_helper.rb')
require IvcChampva::Engine.root.join('app', 'services', 'ivc_champva', 'pdf_stamper')

describe IvcChampva::PdfFiller do
forms = %w[vha_10_10d vha_10_7959f_1 vha_10_7959f_2 vha_10_7959c]
Expand All @@ -11,10 +12,12 @@
context 'when the filler is instantiated without a form_number' do
it 'throws an error' do
form_number = forms.first
data = JSON.parse(File.read("modules/ivc_champva/spec/fixtures/form_json/#{form_number}.json"))
file_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', "#{form_number}.json")
expect(File.exist?(file_path)).to be(true), "Fixture file not found: #{file_path}"
data = JSON.parse(File.read(file_path))
form = "IvcChampva::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
expect do
described_class.new(form_number: nil, form:)
described_class.new(form_number: nil, form: form)
end.to raise_error(RuntimeError, 'form_number is required')
end
end
Expand All @@ -23,35 +26,69 @@
it 'throws an error' do
form_number = forms.first
expect do
described_class.new(form_number:, form: nil)
described_class.new(form_number: form_number, form: nil)
end.to raise_error(RuntimeError, 'form needs a data attribute')
end
end
end

# describe '#generate' do
# forms.each do |file_name|
# context "when mapping the pdf data given JSON file: #{file_name}" do
# let(:form_number) { file_name.gsub('-min', '') }
# let(:expected_pdf_path) { "tmp/#{file_name}-tmp.pdf" }
# let(:data) { JSON.parse(File.read("modules/ivc_champva/spec/fixtures/form_json/#{file_name}.json")) }
# let(:form) { "IvcChampva::#{form_number.titleize.gsub(' ', '')}".constantize.new(data) }

# after { FileUtils.rm_f(expected_pdf_path) }

# context 'when a legitimate JSON payload is provided' do
# it 'properly fills out the associated PDF' do
# filled_pdf_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pdfs',
# "#{file_name}-filled.pdf")

# described_class.new(form_number:, form:).generate

# expect(expected_pdf_path).to match_pdf_content_of(filled_pdf_path)
# end
# end
# end
# end
# end
describe '#generate' do
context 'when the stamped template file exists' do
it 'generates the form correctly' do
form_number = forms.first
file_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', "#{form_number}.json")
expect(File.exist?(file_path)).to be(true), "Fixture file not found: #{file_path}"
data = JSON.parse(File.read(file_path))
form = "IvcChampva::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
pdf_filler = described_class.new(form_number: form_number, form: form)

allow(File).to receive(:exist?).and_return(true)
allow(IvcChampva::PdfStamper).to receive(:stamp_pdf)
allow(PdfForms).to receive(:new).and_return(double(fill_form: true))
allow(Common::FileHelpers).to receive(:delete_file_if_exists)

expect(pdf_filler.generate).to match(%r{tmp/#{form_number}-.*-tmp.pdf})
end
end

context 'when the stamped template file does not exist' do
it 'raises an error' do
form_number = forms.first
file_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', "#{form_number}.json")
expect(File.exist?(file_path)).to be(true), "Fixture file not found: #{file_path}"
data = JSON.parse(File.read(file_path))
form = "IvcChampva::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
pdf_filler = described_class.new(form_number: form_number, form: form)

allow(File).to receive(:exist?).and_return(false)

expect { pdf_filler.generate }.to raise_error(RuntimeError, /stamped template file does not exist/)
end
end
end

describe '#create_tempfile' do
context 'when creating a tempfile' do
it 'creates and copies the template form correctly' do
form_number = forms.first
file_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', "#{form_number}.json")
expect(File.exist?(file_path)).to be(true), "Fixture file not found: #{file_path}"
data = JSON.parse(File.read(file_path))
form = "IvcChampva::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
pdf_filler = described_class.new(form_number: form_number, form: form)

template_path = "#{IvcChampva::PdfFiller::TEMPLATE_BASE}/#{form_number}.pdf"
tempfile = double('Tempfile')

allow(Tempfile).to receive(:new).and_return(tempfile)
allow(IO).to receive(:copy_stream)
allow(tempfile).to receive(:close)

expect(IO).to receive(:copy_stream).with(template_path, tempfile)
pdf_filler.create_tempfile
end
end
end

describe 'form mappings' do
list = forms.map { |f| f.gsub('-min', '') }.uniq
Expand Down

0 comments on commit e8befec

Please sign in to comment.