Skip to content

Commit 8036ad2

Browse files
authored
🎨 split each document type into its own spec file (#21)
1 parent a78eaed commit 8036ad2

15 files changed

+518
-475
lines changed

spec/data.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# frozen_string_literal: true
22

33
DATA_DIR = File.join(__dir__, 'data').freeze
4-
DIR_CUSTOM_V1 = File.join(DATA_DIR, 'custom', 'response_v1').freeze
5-
DIR_INVOICE_V4 = File.join(DATA_DIR, 'invoice', 'response_v4').freeze
6-
DIR_RECEIPT_V4 = File.join(DATA_DIR, 'receipt', 'response_v4').freeze
7-
DIR_FINANCIAL_DOCUMENT_V1 = File.join(DATA_DIR, 'financial_document', 'response_v1').freeze
8-
DIR_PROOF_OF_ADDRESS_V1 = File.join(DATA_DIR, 'proof_of_address', 'response_v1').freeze
9-
DIR_PASSPORT_V1 = File.join(DATA_DIR, 'passport', 'response_v1').freeze
10-
DIR_EU_LICENSE_PLATE_V1 = File.join(DATA_DIR, 'eu', 'license_plate', 'response_v1').freeze
11-
DIR_SHIPPING_CONTAINER_V1 = File.join(DATA_DIR, 'shipping_container', 'response_v1').freeze
12-
DIR_US_BANK_CHECK_V1 = File.join(DATA_DIR, 'us', 'bank_check', 'response_v1').freeze
13-
DIR_FR_BANK_ACCOUNT_DETAILS_V1 = File.join(DATA_DIR, 'fr', 'bank_account_details', 'response_v1').freeze
14-
DIR_FR_CARTE_VITALE_V1 = File.join(DATA_DIR, 'fr', 'carte_vitale', 'response_v1').freeze
15-
DIR_FR_ID_CARD_V1 = File.join(DATA_DIR, 'fr', 'id_card', 'response_v1').freeze
4+
5+
def load_json(dir_path, file_name)
6+
file_data = File.read(File.join(dir_path, file_name))
7+
JSON.parse(file_data)
8+
end
9+
10+
def read_file(dir_path, file_name)
11+
File.read(File.join(dir_path, file_name)).strip
12+
end

spec/document/custom_v1_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
require 'mindee/parsing'
5+
6+
require_relative '../data'
7+
8+
DIR_CUSTOM_V1 = File.join(DATA_DIR, 'custom', 'response_v1').freeze
9+
10+
describe Mindee::Prediction::CustomV1 do
11+
context 'A custom document V1' do
12+
it 'should load an empty document prediction' do
13+
response = load_json(DIR_CUSTOM_V1, 'empty.json')
14+
inference = Mindee::Document.new(Mindee::Prediction::CustomV1, response['document']).inference
15+
expect(inference.product.type).to eq('constructed')
16+
expect(inference.prediction.fields.length).to eq(10)
17+
expect(inference.prediction.classifications.length).to eq(1)
18+
end
19+
20+
it 'should load a complete document prediction' do
21+
to_string = read_file(DIR_CUSTOM_V1, 'summary_full.rst')
22+
response = load_json(DIR_CUSTOM_V1, 'complete.json')
23+
document = Mindee::Document.new(Mindee::Prediction::CustomV1, response['document'])
24+
expect(document.to_s).to eq(to_string)
25+
prediction = document.inference.prediction
26+
27+
prediction.fields.each do |field_name, field_data|
28+
expect(field_name).to be_kind_of(Symbol)
29+
expect(field_data.values).to be_kind_of(Array)
30+
expect(field_data.contents_str).to be_kind_of(String)
31+
end
32+
33+
expect(prediction.fields[:string_all].values.size).to eq(3)
34+
expect(prediction.fields['string_all']).to be_nil
35+
expect(prediction.fields[:string_all].contents_str).to eq('Mindee is awesome')
36+
expect(prediction.fields[:string_all].contents_list).to eq(['Mindee', 'is', 'awesome'])
37+
38+
expect(prediction.classifications[:doc_type].value).to eq('type_b')
39+
end
40+
41+
it 'should load a complete page 0 prediction' do
42+
to_string = read_file(DIR_CUSTOM_V1, 'summary_page0.rst')
43+
response = load_json(DIR_CUSTOM_V1, 'complete.json')
44+
inference = Mindee::Document.new(Mindee::Prediction::CustomV1, response['document']).inference
45+
expect(inference.pages[0].prediction.fields[:string_all].contents_str(separator: '_')).to eq('Jenny_is_great')
46+
expect(inference.pages[0].prediction.fields[:string_all].contents_list).to eq(['Jenny', 'is', 'great'])
47+
expect(inference.pages[0].to_s).to eq(to_string)
48+
end
49+
50+
it 'should load a complete page 1 prediction' do
51+
to_string = read_file(DIR_CUSTOM_V1, 'summary_page1.rst')
52+
response = load_json(DIR_CUSTOM_V1, 'complete.json')
53+
inference = Mindee::Document.new(Mindee::Prediction::CustomV1, response['document']).inference
54+
expect(inference.pages[1].to_s).to eq(to_string)
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)