-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsources_spec.rb
More file actions
120 lines (107 loc) · 4.64 KB
/
sources_spec.rb
File metadata and controls
120 lines (107 loc) · 4.64 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
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
require 'mindee'
require 'mindee/input/sources'
require 'mindee/error'
require 'pdf-reader' if Mindee::Dependencies.all_deps_available?
require_relative '../../data'
describe Mindee::Input::Source do
context 'An image input file' do
it 'should load a JPEG from a path' do
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.jpg')
)
expect(input_source.file_mimetype).to eq('image/jpeg')
expect(input_source.filename).to eq('receipt.jpg')
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.jpga')
)
expect(input_source.file_mimetype).to eq('image/jpeg')
expect(input_source.filename).to eq('receipt.jpga')
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
end
it 'should load a TIFF from a path' do
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.tif')
)
expect(input_source.file_mimetype).to eq('image/tiff')
expect(input_source.filename).to eq('receipt.tif')
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.tiff')
)
expect(input_source.file_mimetype).to eq('image/tiff')
expect(input_source.filename).to eq('receipt.tiff')
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
end
it 'should load a HEIC from a path', :all_deps do
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.heic')
)
expect(input_source.file_mimetype).to eq('image/heic')
expect(input_source.filename).to eq('receipt.heic')
expect(input_source.page_count).to eq(1)
expect(input_source.pdf?).to eq(false)
end
end
context 'A PDF input file', :all_deps do
it 'should load a multi-page PDF from a path' do
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(V1_DATA_DIR, 'products/invoices/invoice.pdf')
)
expect(input_source.file_mimetype).to eq('application/pdf')
expect(input_source.filename).to eq('invoice.pdf')
expect(input_source.page_count).to eq(2)
expect(input_source.pdf?).to eq(true)
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(V1_DATA_DIR, 'products/invoices/invoice.pdf')
)
expect(input_source.file_mimetype).to eq('application/pdf')
expect(input_source.filename).to eq('invoice.pdf')
expect(input_source.page_count).to eq(2)
expect(input_source.pdf?).to eq(true)
input_source = Mindee::Input::Source::PathInputSource.new(
File.join(V1_DATA_DIR, 'products/invoices/invoice_10p.pdf')
)
expect(input_source.file_mimetype).to eq('application/pdf')
expect(input_source.filename).to eq('invoice_10p.pdf')
expect(input_source.page_count).to eq(10)
expect(input_source.pdf?).to eq(true)
end
end
context 'A broken fixable PDF' do
mindee_client = Mindee::V1::Client.new(api_key: 'invalid-api-key')
it 'Should not raise a mime error' do
expect do
mindee_client.source_from_path(
"#{FILE_TYPES_DIR}/pdf/broken_fixable.pdf", repair_pdf: true
)
end.not_to raise_error
end
end
context 'A broken unfixable PDF' do
mindee_client = Mindee::V1::Client.new(api_key: 'invalid-api-key')
it 'Should raise an error' do
expect do
mindee_client.source_from_path(
"#{FILE_TYPES_DIR}/pdf/broken_unfixable.pdf", repair_pdf: true
)
end.to raise_error Mindee::Error::MindeePDFError
end
end
context 'A broken fixable invoice PDF' do
mindee_client = Mindee::V1::Client.new(api_key: 'invalid-api-key')
it 'Should send correct results' do
source_doc_original = mindee_client.source_from_path("#{V1_DATA_DIR}/products/invoices/invoice.pdf")
expect do
source_doc_fixed = mindee_client.source_from_path("#{FILE_TYPES_DIR}/pdf/broken_invoice.pdf",
repair_pdf: true)
expect(source_doc_fixed.read_contents[0].to_s).to eq(source_doc_original.read_contents[0].to_s)
end.not_to raise_error
end
end
end