Skip to content

Commit 67480f7

Browse files
authored
✅ add unit tests for receipt v4, invoice v4 (#110)
1 parent ea9205f commit 67480f7

File tree

2 files changed

+73
-26
lines changed

2 files changed

+73
-26
lines changed

src/test/java/com/mindee/product/invoice/InvoiceV4Test.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,61 @@
22

33
import com.fasterxml.jackson.databind.JavaType;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.mindee.parsing.common.Document;
6+
import com.mindee.parsing.common.Page;
57
import com.mindee.parsing.common.PredictResponse;
68
import org.junit.jupiter.api.Assertions;
79
import org.junit.jupiter.api.Test;
8-
9-
import java.io.*;
10+
import java.io.File;
11+
import java.io.IOException;
1012
import java.nio.file.Files;
1113
import java.nio.file.Paths;
1214
import java.util.List;
1315

14-
class InvoiceV4Test {
15-
16-
@Test
17-
void givenAnInvoiceV4_whenDeserialized_MustHaveAValidSummary() throws IOException {
16+
/**
17+
* Unit tests for InvoiceV4.
18+
*/
19+
public class InvoiceV4Test {
1820

21+
protected PredictResponse<InvoiceV4> getPrediction() throws IOException {
1922
ObjectMapper objectMapper = new ObjectMapper();
2023
objectMapper.findAndRegisterModules();
2124

22-
JavaType type = objectMapper.getTypeFactory().constructParametricType(PredictResponse.class,
23-
InvoiceV4.class);
24-
PredictResponse<InvoiceV4> invoiceV4Prediction = objectMapper.readValue(
25-
new File("src/test/resources/invoice/response_v4/complete.json"),
26-
type);
25+
JavaType type = objectMapper.getTypeFactory().constructParametricType(
26+
PredictResponse.class,
27+
InvoiceV4.class
28+
);
29+
return objectMapper.readValue(
30+
new File("src/test/resources/invoice/response_v4/complete.json"),
31+
type
32+
);
33+
}
2734

28-
String[] actualLines = invoiceV4Prediction.getDocument().toString().split(System.lineSeparator());
29-
List<String> expectedLines = Files
30-
.readAllLines(Paths.get("src/test/resources/invoice/response_v4/summary_full.rst"));
35+
@Test
36+
void whenCompleteDeserialized_mustHaveValidDocumentSummary() throws IOException {
37+
PredictResponse<InvoiceV4> prediction = getPrediction();
38+
Document<InvoiceV4> doc = prediction.getDocument();
39+
String[] actualLines = doc.toString().split(System.lineSeparator());
40+
List<String> expectedLines = Files.readAllLines(
41+
Paths.get("src/test/resources/invoice/response_v4/summary_full.rst")
42+
);
3143
String expectedSummary = String.join(String.format("%n"), expectedLines);
3244
String actualSummary = String.join(String.format("%n"), actualLines);
3345

3446
Assertions.assertEquals(expectedSummary, actualSummary);
3547
}
3648

49+
@Test
50+
void whenCompleteDeserialized_mustHaveValidPage0Summary() throws IOException {
51+
PredictResponse<InvoiceV4> prediction = getPrediction();
52+
Page<InvoiceV4Document> page = prediction.getDocument().getInference().getPages().get(0);
53+
String[] actualLines = page.toString().split(System.lineSeparator());
54+
List<String> expectedLines = Files.readAllLines(
55+
Paths.get("src/test/resources/invoice/response_v4/summary_page0.rst")
56+
);
57+
String expectedSummary = String.join(String.format("%n"), expectedLines);
58+
String actualSummary = String.join(String.format("%n"), actualLines);
59+
60+
Assertions.assertEquals(expectedSummary, actualSummary);
61+
}
3762
}

src/test/java/com/mindee/product/receipt/ReceiptV4Test.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.fasterxml.jackson.databind.JavaType;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.mindee.parsing.common.Document;
6+
import com.mindee.parsing.common.Page;
57
import com.mindee.parsing.common.PredictResponse;
68
import org.junit.jupiter.api.Assertions;
79
import org.junit.jupiter.api.Test;
@@ -14,27 +16,47 @@
1416
/**
1517
* Unit tests for ReceiptV4.
1618
*/
17-
class ReceiptV4Test {
18-
19-
@Test
20-
void givenAReceiptV4_whenDeserialized_MustHaveAValidSummary() throws IOException {
19+
public class ReceiptV4Test {
2120

21+
protected PredictResponse<ReceiptV4> getPrediction() throws IOException {
2222
ObjectMapper objectMapper = new ObjectMapper();
2323
objectMapper.findAndRegisterModules();
2424

25-
JavaType type = objectMapper.getTypeFactory().constructParametricType(PredictResponse.class,
26-
ReceiptV4.class);
27-
PredictResponse<ReceiptV4> prediction = objectMapper.readValue(
28-
new File("src/test/resources/receipt/response_v4/complete.json"),
29-
type);
25+
JavaType type = objectMapper.getTypeFactory().constructParametricType(
26+
PredictResponse.class,
27+
ReceiptV4.class
28+
);
29+
return objectMapper.readValue(
30+
new File("src/test/resources/receipt/response_v4/complete.json"),
31+
type
32+
);
33+
}
3034

31-
String[] actualLines = prediction.getDocument().toString().split(System.lineSeparator());
32-
List<String> expectedLines = Files
33-
.readAllLines(Paths.get("src/test/resources/receipt/response_v4/summary_full.rst"));
35+
@Test
36+
void whenCompleteDeserialized_mustHaveValidDocumentSummary() throws IOException {
37+
PredictResponse<ReceiptV4> prediction = getPrediction();
38+
Document<ReceiptV4> doc = prediction.getDocument();
39+
String[] actualLines = doc.toString().split(System.lineSeparator());
40+
List<String> expectedLines = Files.readAllLines(
41+
Paths.get("src/test/resources/receipt/response_v4/summary_full.rst")
42+
);
3443
String expectedSummary = String.join(String.format("%n"), expectedLines);
3544
String actualSummary = String.join(String.format("%n"), actualLines);
3645

3746
Assertions.assertEquals(expectedSummary, actualSummary);
3847
}
3948

49+
@Test
50+
void whenCompleteDeserialized_mustHaveValidPage0Summary() throws IOException {
51+
PredictResponse<ReceiptV4> prediction = getPrediction();
52+
Page<ReceiptV4Document> page = prediction.getDocument().getInference().getPages().get(0);
53+
String[] actualLines = page.toString().split(System.lineSeparator());
54+
List<String> expectedLines = Files.readAllLines(
55+
Paths.get("src/test/resources/receipt/response_v4/summary_page0.rst")
56+
);
57+
String expectedSummary = String.join(String.format("%n"), expectedLines);
58+
String actualSummary = String.join(String.format("%n"), actualLines);
59+
60+
Assertions.assertEquals(expectedSummary, actualSummary);
61+
}
4062
}

0 commit comments

Comments
 (0)