Skip to content

Commit 878142e

Browse files
🐛 fix extras failing at document level if missing from prediction (#138)
1 parent f5c42b7 commit 878142e

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

lib/mindee/parsing/common/document.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def to_s
6161
private
6262

6363
def inject_full_text_ocr(raw_prediction)
64-
return unless raw_prediction.dig('inference', 'pages') &&
65-
raw_prediction['inference']['pages'][0]['extras']['full_text_ocr']
64+
return unless raw_prediction.dig('inference', 'pages', 0, 'extras', 'full_text_ocr')
6665

6766
full_text_ocr = String.new
6867
raw_prediction.dig('inference', 'pages').each do |page|

spec/extras/full_text_ocr_spec.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
prediction_data
2626
).document
2727
end
28+
let(:load_invalid_document) do
29+
prediction_data = JSON.parse(
30+
File.read(File.join(DIR_PRODUCTS, 'bank_statement_fr', 'response_v1', 'complete.json'))
31+
)
32+
Mindee::Parsing::Common::ApiResponse.new(
33+
Mindee::Product::FR::BankStatement::BankStatementV1,
34+
prediction_data,
35+
prediction_data
36+
).document
37+
end
2838
end
2939

3040
describe 'FullTextOCR' do
@@ -40,4 +50,98 @@
4050
expect(full_text_ocr.to_s.strip).to eq(expected_text.strip)
4151
# expect(page0_ocr).to eq(expected_text.split("\n").join("\n"))
4252
end
53+
54+
it "doesn't get full text when the payload is empty" do
55+
full_text_ocr = load_invalid_document
56+
expect(full_text_ocr.extras).to be_nil
57+
end
58+
59+
it "doesn't get full text when loading dummy data" do
60+
synthetic_response = {
61+
'id' => 'mock_id',
62+
'name' => 'mock_name',
63+
'inference' => {
64+
'extras' => {},
65+
'finished_at' => '1900-01-00T00:00:00.000000',
66+
'is_rotation_applied' => nil,
67+
'product' => {
68+
'name' => 'mock_name',
69+
'type' => 'mock_type',
70+
'version' => 'mock_version',
71+
},
72+
'pages' => [
73+
{},
74+
{
75+
'extras' => {},
76+
'id' => 0,
77+
'orientation' => {
78+
'value' => nil,
79+
},
80+
'prediction' => {},
81+
},
82+
],
83+
'prediction' => {
84+
'account_number' => {
85+
'value' => nil,
86+
'confidence' => 0,
87+
},
88+
'bank_address' => {
89+
'value' => nil,
90+
'confidence' => 0,
91+
},
92+
'bank_name' => {
93+
'value' => nil,
94+
'confidence' => 0,
95+
},
96+
'client_address' => {
97+
'value' => nil,
98+
'confidence' => 0,
99+
},
100+
'client_name' => {
101+
'value' => nil,
102+
'confidence' => 0,
103+
},
104+
'closing_balance' => {
105+
'value' => nil,
106+
'confidence' => 0,
107+
},
108+
'opening_balance' => {
109+
'value' => nil,
110+
'confidence' => 0,
111+
},
112+
'statement_date' => {
113+
'value' => nil,
114+
'confidence' => 0,
115+
},
116+
'statement_end_date' => {
117+
'value' => nil,
118+
'confidence' => 0,
119+
},
120+
'statement_start_date' => {
121+
'value' => nil,
122+
'confidence' => 0,
123+
},
124+
'total_credits' => {
125+
'value' => nil,
126+
'confidence' => 0,
127+
},
128+
'total_debits' => {
129+
'value' => nil,
130+
'confidence' => 0,
131+
},
132+
'transactions' => [
133+
'value' => nil,
134+
'confidence' => 0,
135+
],
136+
},
137+
},
138+
'extras' => {},
139+
'n_pages' => 0,
140+
}
141+
built_doc = Mindee::Parsing::Common::Document.new(
142+
Mindee::Product::FR::BankStatement::BankStatementV1,
143+
synthetic_response
144+
)
145+
expect(built_doc.extras).to be_nil
146+
end
43147
end

0 commit comments

Comments
 (0)