Skip to content

Commit 40e3e9c

Browse files
bump test lib, fix tests, add missing object
1 parent dc8e25e commit 40e3e9c

5 files changed

Lines changed: 160 additions & 16 deletions

File tree

src/main/java/com/mindee/parsing/v2/Inference.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
@AllArgsConstructor
1818
@NoArgsConstructor
1919
public class Inference {
20+
/**
21+
* Inference ID.
22+
*/
23+
@JsonProperty("id")
24+
private String id;
2025

2126
/**
2227
* Model info.

src/main/java/com/mindee/parsing/v2/InferenceOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
@Getter
1111
public final class InferenceOptions {
1212

13-
@JsonProperty("raw_text")
14-
private List<String> rawText;
13+
@JsonProperty("raw_texts")
14+
private List<RawText> rawTexts;
1515
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.mindee.parsing.v2;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.EqualsAndHashCode;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
10+
/**
11+
* Raw text as found in the document.
12+
*/
13+
@Getter
14+
@JsonIgnoreProperties(ignoreUnknown = true)
15+
@AllArgsConstructor
16+
@NoArgsConstructor
17+
public class RawText {
18+
19+
@JsonProperty("page")
20+
private Integer page;
21+
22+
@JsonProperty("content")
23+
private String content;
24+
}

src/test/java/com/mindee/v2/InferenceTest.java

Lines changed: 128 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
package com.mindee.v2;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.mindee.MindeeClientV2;
54
import com.mindee.input.LocalResponse;
6-
import com.mindee.parsing.v2.DynamicField;
5+
import com.mindee.parsing.v2.*;
76
import com.mindee.parsing.v2.DynamicField.FieldType;
8-
import com.mindee.parsing.v2.InferenceFields;
9-
import com.mindee.parsing.v2.InferenceResponse;
10-
import com.mindee.parsing.v2.ListField;
11-
import com.mindee.parsing.v2.ObjectField;
12-
13-
import java.io.File;
147
import java.io.IOException;
15-
import java.io.InputStream;
8+
import java.util.List;
169
import java.util.Map;
1710
import org.junit.jupiter.api.DisplayName;
1811
import org.junit.jupiter.api.Nested;
@@ -22,15 +15,21 @@
2215

2316
@DisplayName("InferenceV2 – field integrity checks")
2417
class InferenceTest {
18+
19+
private InferenceResponse loadFromResource(String resourcePath) throws IOException {
20+
MindeeClientV2 dummyClient = new MindeeClientV2("dummy");
21+
return dummyClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream(resourcePath)));
22+
}
23+
24+
2525
@Nested
2626
@DisplayName("When the async prediction is blank")
2727
class BlankPrediction {
2828

2929
@Test
3030
@DisplayName("all properties must be valid")
3131
void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException {
32-
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
33-
InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/blank.json")));
32+
InferenceResponse response = loadFromResource("v2/products/financial_document/blank.json");
3433
InferenceFields fields = response.getInference().getResult().getFields();
3534

3635
assertEquals(21, fields.size(), "Expected 21 fields");
@@ -84,8 +83,7 @@ class CompletePrediction {
8483
@Test
8584
@DisplayName("all properties must be valid")
8685
void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException {
87-
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
88-
InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/complete.json")));
86+
InferenceResponse response = loadFromResource("v2/products/financial_document/complete.json");
8987
InferenceFields fields = response.getInference().getResult().getFields();
9088

9189
assertEquals(21, fields.size(), "Expected 21 fields");
@@ -120,4 +118,121 @@ void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException {
120118
assertNotNull(supplierAddress.toString(), "'supplier_address'.toString() must not be null");
121119
}
122120
}
121+
122+
@Nested
123+
@DisplayName("deep_nested_fields.json")
124+
class DeepNestedFields {
125+
126+
@Test
127+
@DisplayName("all nested structures must be typed correctly")
128+
void deepNestedFields_mustExposeCorrectTypes() throws IOException {
129+
InferenceResponse resp = loadFromResource("v2/inference/deep_nested_fields.json");
130+
Inference inf = resp.getInference();
131+
assertNotNull(inf);
132+
133+
InferenceFields root = inf.getResult().getFields();
134+
assertNotNull(root.get("field_simple").getSimpleField());
135+
assertNotNull(root.get("field_object").getObjectField());
136+
137+
ObjectField fieldObject = root.get("field_object").getObjectField();
138+
InferenceFields lvl1 = fieldObject.getFields();
139+
assertNotNull(lvl1.get("sub_object_list").getListField());
140+
assertNotNull(lvl1.get("sub_object_object").getObjectField());
141+
142+
ObjectField subObjectObject = lvl1.get("sub_object_object").getObjectField();
143+
InferenceFields lvl2 = subObjectObject.getFields();
144+
assertNotNull(lvl2.get("sub_object_object_sub_object_list").getListField());
145+
146+
ListField nestedList = lvl2.get("sub_object_object_sub_object_list").getListField();
147+
List<DynamicField> items = nestedList.getItems();
148+
assertFalse(items.isEmpty());
149+
assertNotNull(items.get(0).getObjectField());
150+
151+
ObjectField firstItem = items.get(0).getObjectField();
152+
SimpleField deepSimple = firstItem.getFields()
153+
.get("sub_object_object_sub_object_list_simple").getSimpleField();
154+
assertEquals("value_9", deepSimple.getValue());
155+
}
156+
}
157+
158+
@Nested
159+
@DisplayName("standard_field_types.json")
160+
class StandardFieldTypes {
161+
162+
@Test
163+
@DisplayName("simple / object / list variants must be recognised")
164+
void standardFieldTypes_mustExposeCorrectTypes() throws IOException {
165+
InferenceResponse resp = loadFromResource("v2/inference/standard_field_types.json");
166+
Inference inf = resp.getInference();
167+
assertNotNull(inf);
168+
169+
InferenceFields root = inf.getResult().getFields();
170+
assertNotNull(root.get("field_simple").getSimpleField());
171+
assertNotNull(root.get("field_object").getObjectField());
172+
assertNotNull(root.get("field_simple_list").getListField());
173+
assertNotNull(root.get("field_object_list").getListField());
174+
}
175+
}
176+
177+
@Nested
178+
@DisplayName("raw_texts.json")
179+
class RawTexts {
180+
181+
@Test
182+
@DisplayName("raw texts option must be parsed and exposed")
183+
void rawTexts_mustBeAccessible() throws IOException {
184+
InferenceResponse resp = loadFromResource("v2/inference/raw_texts.json");
185+
Inference inf = resp.getInference();
186+
assertNotNull(inf);
187+
188+
InferenceOptions opts = inf.getResult().getOptions();
189+
assertNotNull(opts, "Options should not be null");
190+
191+
List<RawText> rawTexts = opts.getRawTexts();
192+
assertEquals(2, rawTexts.size());
193+
194+
RawText first = rawTexts.get(0);
195+
assertEquals(0, first.getPage());
196+
assertEquals("This is the raw text of the first page...", first.getContent());
197+
}
198+
}
199+
200+
@Nested
201+
@DisplayName("complete.json – full inference response")
202+
class FullInference {
203+
@Test
204+
@DisplayName("complete financial-document JSON must round-trip correctly")
205+
void fullInferenceResponse_mustExposeEveryProperty() throws IOException {
206+
InferenceResponse resp = loadFromResource("v2/products/financial_document/complete.json");
207+
208+
Inference inf = resp.getInference();
209+
assertNotNull(inf);
210+
assertEquals("12345678-1234-1234-1234-123456789abc", inf.getId());
211+
212+
InferenceFields f = inf.getResult().getFields();
213+
214+
SimpleField date = f.get("date").getSimpleField();
215+
assertEquals("2019-11-02", date.getValue());
216+
217+
ListField taxes = f.get("taxes").getListField();
218+
ObjectField firstTax = taxes.getItems().get(0).getObjectField();
219+
SimpleField baseTax = firstTax.getFields().get("base").getSimpleField();
220+
assertEquals(31.5, baseTax.getValue());
221+
222+
ObjectField customerAddr = f.get("customer_address").getObjectField();
223+
SimpleField city = customerAddr.getFields().get("city").getSimpleField();
224+
assertEquals("New York", city.getValue());
225+
226+
InferenceModel model = inf.getModel();
227+
assertNotNull(model);
228+
assertEquals("12345678-1234-1234-1234-123456789abc", model.getId());
229+
230+
InferenceFile file = inf.getFile();
231+
assertNotNull(file);
232+
assertEquals("complete.jpg", file.getName());
233+
assertNull(file.getAlias());
234+
235+
assertNull(inf.getResult().getOptions());
236+
}
237+
}
123238
}

0 commit comments

Comments
 (0)