-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_v2_integration.rb
More file actions
265 lines (221 loc) · 9.57 KB
/
client_v2_integration.rb
File metadata and controls
265 lines (221 loc) · 9.57 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: true
describe 'Mindee::ClientV2 – integration tests (V2)', :integration, order: :defined do
let(:api_key) { ENV.fetch('MINDEE_V2_API_KEY') }
let(:model_id) { ENV.fetch('MINDEE_V2_FINDOC_MODEL_ID') }
let(:blank_pdf_url) { ENV.fetch('MINDEE_V2_SE_TESTS_BLANK_PDF_URL') }
let(:client) { Mindee::ClientV2.new(api_key: api_key) }
context 'An input file' do
it 'parses an empty multi-page PDF successfully' do
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'multipage_cut-2.pdf')
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'multipage_cut-2.pdf')
polling = Mindee::Input::PollingOptions.new(
initial_delay_sec: 3.0,
delay_sec: 1.5,
max_retries: 80
)
inference_params = Mindee::Input::InferenceParameters.new(
model_id,
rag: false,
raw_text: true,
polygon: false,
confidence: false,
file_alias: 'rb_integration_test',
polling_options: polling,
text_context: 'this is a test'
)
response = client.enqueue_and_get_inference(input, inference_params)
expect(response).not_to be_nil
expect(response.inference).not_to be_nil
file = response.inference.file
expect(file).not_to be_nil
expect(file).to be_a(Mindee::Parsing::V2::InferenceFile)
expect(file.name).to eq('multipage_cut-2.pdf')
expect(file.page_count).to eq(2)
model = response.inference.model
expect(model).not_to be_nil
expect(model).to be_a(Mindee::Parsing::V2::InferenceModel)
expect(model.id).to eq(model_id)
active_options = response.inference.active_options
expect(active_options).not_to be_nil
expect(active_options).to be_a(Mindee::Parsing::V2::InferenceActiveOptions)
expect(active_options.raw_text).to eq(true)
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.rag).to eq(false)
expect(active_options.text_context).to eq(true)
result = response.inference.result
expect(result).not_to be_nil
expect(result.raw_text).not_to be_nil
expect(result.raw_text.pages.length).to eq(2)
expect(result.fields).not_to be_nil
end
it 'parses a filled single-page image successfully' do
src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg')
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'default_sample.jpg')
inference_params = Mindee::Input::InferenceParameters.new(
model_id,
raw_text: false,
polygon: false,
confidence: false,
rag: false,
file_alias: 'rb_integration_test'
)
response = client.enqueue_and_get_inference(input, inference_params)
expect(response).not_to be_nil
file = response.inference.file
expect(file).not_to be_nil
expect(file).to be_a(Mindee::Parsing::V2::InferenceFile)
expect(file.name).to eq('default_sample.jpg')
expect(file.page_count).to eq(1)
model = response.inference.model
expect(model).not_to be_nil
expect(model).to be_a(Mindee::Parsing::V2::InferenceModel)
expect(model.id).to eq(model_id)
active_options = response.inference.active_options
expect(active_options).not_to be_nil
expect(active_options).to be_a(Mindee::Parsing::V2::InferenceActiveOptions)
expect(active_options.raw_text).to eq(false)
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.rag).to eq(false)
expect(active_options.text_context).to eq(false)
result = response.inference.result
expect(result).not_to be_nil
expect(result.raw_text).to be_nil
fields = result.fields
expect(fields).not_to be_nil
expect(fields['supplier_name']).not_to be_nil
expect(fields['supplier_name'].value).to eq('John Smith')
end
end
context 'An error' do
it 'raises MindeeHTTPErrorV2 (422) on invalid model id' do
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
inference_params = Mindee::Input::InferenceParameters.new('INVALID_MODEL_ID')
expect do
client.enqueue_inference(input, inference_params)
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e| expect(e.status).to eq(422) }
end
it 'raises MindeeHTTPErrorV2 (422) on invalid webhook id' do
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
params = Mindee::Input::InferenceParameters.new(
model_id,
webhook_ids: ['INVALID_WEBHOOK_ID']
)
expect do
client.enqueue_inference(input, params)
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
expect(e.status).to eq(422)
expect(e.code).to start_with('422-')
expect(e.detail).to_not be_nil
expect(e.title).to_not be_nil
expect(e.errors).to be_an_instance_of(Array)
expect(e.errors.count).to be_positive
}
end
it 'raises MindeeHTTPErrorV2 (422) on non-existant webhook id' do
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
inference_params = Mindee::Input::InferenceParameters.new(
model_id,
webhook_ids: ['fc405e37-4ba4-4d03-aeba-533a8d1f0f21', 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21']
)
expect do
client.enqueue_inference(input, inference_params)
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
expect(e.status).to eq(422)
expect(e.code).to start_with('422-')
expect(e.detail).to_not be_nil
expect(e.title).to_not be_nil
expect(e.errors).to be_an_instance_of(Array)
expect(e.errors.count).to be_positive
expect(e.errors[0]).to be_an_instance_of(Mindee::Parsing::V2::ErrorItem)
}
end
it 'raises MindeeHTTPErrorV2 on invalid job id' do
expect do
client.get_inference('INVALID_JOB_ID')
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
expect(e.status).to eq(422)
expect(e.code).to start_with('422-')
expect(e.detail).to_not be_nil
expect(e.title).to_not be_nil
expect(e.errors).to be_an_instance_of(Array)
expect(e.errors.count).to be_positive
}
end
it 'raises on invalid model ID' do
expect do
src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg')
input = Mindee::Input::Source::FileInputSource.new(
File.open(src_path, 'rb'),
'default_sample.jpg'
)
inference_params = Mindee::Input::InferenceParameters.new(
'fc405e37-4ba4-4d03-aeba-533a8d1f0f21',
raw_text: false,
polygon: false,
confidence: false,
rag: false,
file_alias: 'rb_integration_test'
)
client.enqueue_and_get_inference(input, inference_params)
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
expect(e.status).to eq(404)
expect(e.code).to start_with('404-')
expect(e.detail).to_not be_nil
expect(e.title).to_not be_nil
expect(e.errors).to be_an_instance_of(Array)
}
end
end
context 'A remote input source' do
it 'parses an URL input source without errors' do
url_input = Mindee::Input::Source::URLInputSource.new(blank_pdf_url)
inference_params = Mindee::Input::InferenceParameters.new(model_id)
response = client.enqueue_and_get_inference(url_input, inference_params)
expect(response).not_to be_nil
expect(response.inference).not_to be_nil
end
end
context 'A Data Schema Override' do
it 'Overrides successfully' do
data_schema_replace = File.read(File.join(V2_DATA_DIR, 'inference', 'data_schema_replace_param.json'))
input = Mindee::Input::Source::PathInputSource.new(File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf'))
inference_params = Mindee::Input::InferenceParameters.new(
model_id,
raw_text: false,
polygon: false,
confidence: false,
rag: false,
file_alias: 'rb_integration_data_schema_replace',
data_schema: data_schema_replace
)
response = client.enqueue_and_get_inference(input, inference_params)
expect(response).not_to be_nil
model = response.inference.model
expect(model).not_to be_nil
expect(model).to be_a(Mindee::Parsing::V2::InferenceModel)
expect(model.id).to eq(model_id)
active_options = response.inference.active_options
expect(active_options).not_to be_nil
expect(active_options).to be_a(Mindee::Parsing::V2::InferenceActiveOptions)
expect(active_options.raw_text).to eq(false)
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.rag).to eq(false)
expect(active_options.text_context).to eq(false)
expect(active_options.data_schema).to_not be_nil
expect(active_options.data_schema.replace).to eq(true)
result = response.inference.result
expect(result).not_to be_nil
expect(result.raw_text).to be_nil
fields = result.fields
expect(fields).not_to be_nil
expect(fields['test_replace']).not_to be_nil
expect(fields['test_replace'].value).to eq('a test value')
end
end
end