-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextracted_pdf_spec.rb
More file actions
108 lines (89 loc) · 3.75 KB
/
extracted_pdf_spec.rb
File metadata and controls
108 lines (89 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true
require 'mindee'
describe 'Mindee::PDF::ExtractedPDF', :all_deps do
# Workaround for mindee-lite
if Mindee::Dependencies.all_deps_available?
let(:described_class) do
Mindee::PDF::ExtractedPDF
end
end
let(:output_dir) { File.join(V1_DATA_DIR, 'output') }
let(:valid_pdf_path) { "#{V1_PRODUCT_DATA_DIR}/invoices/invoice.pdf" }
let(:invalid_pdf_path) { "#{FILE_TYPES_DIR}/receipt.txt" }
let(:output_path) { "#{output_dir}/sample_output.pdf" }
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:exist?).and_return(true)
allow(File).to receive(:extname).and_return('.pdf')
allow(File).to receive(:binwrite)
end
describe '#initialize' do
it 'initializes with valid pdf bytes and filename' do
File.open(valid_pdf_path, 'r') do |pdf_stream|
extracted_pdf = described_class.new(pdf_stream, 'invoice.pdf')
expect(extracted_pdf.pdf_bytes).to be_a(StringIO)
pdf_stream.rewind
extracted_pdf.pdf_bytes.rewind
expect(extracted_pdf.pdf_bytes.read).to eq(pdf_stream.read)
expect(extracted_pdf.filename).to eq('invoice.pdf')
end
end
end
describe '#page_count' do
it 'raises an error for invalid PDF content' do
File.open(invalid_pdf_path, 'r') do |jpg_stream|
pdf_wrapper = described_class.new(jpg_stream, 'dummy.pdf')
expect do
pdf_wrapper.page_count
end.to raise_error Mindee::Error::MindeePDFError, %r{Could not retrieve page count}
end
end
it 'returns the correct page count for a valid PDF' do
File.open(valid_pdf_path, 'r') do |pdf_stream|
allow(Mindee::PDF::PDFProcessor).to receive(:open_pdf).and_return(double(pages: [1, 2, 3]))
pdf_wrapper = described_class.new(pdf_stream, 'invoice.pdf')
expect(pdf_wrapper.page_count).to eq(3)
end
end
end
describe '#write_to_file' do
it 'writes the PDF bytes to a specified file path' do
File.open(valid_pdf_path, 'r') do |pdf_stream|
expected_pdf_content = pdf_stream.read
pdf_stream.rewind
pdf_wrapper = described_class.new(pdf_stream, 'invoice.pdf')
expect { pdf_wrapper.write_to_file(output_path) }.not_to raise_error
expect(File).to have_received(:binwrite).with(output_path, expected_pdf_content)
end
end
it 'raises an error if the output path is a directory' do
allow(File).to receive(:directory?).and_return(true)
File.open(valid_pdf_path, 'r') do |pdf_stream|
pdf_wrapper = described_class.new(pdf_stream, 'invoice.pdf')
expect do
pdf_wrapper.write_to_file(output_path)
end.to raise_error Mindee::Error::MindeePDFError, %r{Provided path is not a file}
end
end
it 'raises an error if the save path is invalid' do
allow(File).to receive(:exist?).and_return(false)
File.open(valid_pdf_path, 'r') do |pdf_stream|
pdf_wrapper = described_class.new(pdf_stream, 'invoice.pdf')
expect do
pdf_wrapper.write_to_file(output_path)
end.to raise_error Mindee::Error::MindeePDFError, %r{Invalid save path provided}
end
end
end
describe '#as_input_source' do
it 'returns a BytesInputSource object with correct attributes' do
pdf_stream = StringIO.new('pdf content')
input_source_double = double('BytesInputSource', content: 'pdf content', filename: 'invoice.pdf')
allow(Mindee::Input::Source::BytesInputSource).to receive(:new).and_return(input_source_double)
pdf_wrapper = described_class.new(pdf_stream, 'invoice.pdf')
input_source = pdf_wrapper.as_input_source
expect(input_source.content).to eq('pdf content')
expect(input_source.filename).to eq('invoice.pdf')
end
end
end