Skip to content

Commit b93e97c

Browse files
committed
♻️ rework tests structure
1 parent 85af66a commit b93e97c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+363
-231
lines changed

tests/data

Submodule data updated 290 files

tests/imageOperations/invoiceSplitter.integration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from "chai";
44
import { levenshteinRatio } from "../testingUtilities";
55
import { promises as fs } from "fs";
66
import path from "path";
7-
import { RESOURCE_PATH } from "../index";
7+
import { V1_PRODUCT_PATH } from "../index";
88

99
describe("Given a PDF", async () => {
1010
let client: mindee.Client;
@@ -15,7 +15,7 @@ describe("Given a PDF", async () => {
1515

1616
it("should extract invoices in strict mode.", async () => {
1717
const sample = client.docFromPath(
18-
path.join(RESOURCE_PATH, "products/invoice_splitter/default_sample.pdf")
18+
path.join(V1_PRODUCT_PATH, "invoice_splitter/default_sample.pdf")
1919
);
2020
await sample.init();
2121

@@ -34,7 +34,7 @@ describe("Given a PDF", async () => {
3434

3535
const invoiceResult = await client.parse(mindee.product.InvoiceV4, invoices[0].asSource());
3636
const testStringRstInvoice = await fs.readFile(
37-
path.join(RESOURCE_PATH, "products/invoices/response_v4/summary_full_invoice_p1.rst")
37+
path.join(V1_PRODUCT_PATH, "invoices/response_v4/summary_full_invoice_p1.rst")
3838
);
3939

4040
expect(

tests/imageOperations/invoiceSplitterExtractor.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import path from "path";
44
import { InvoiceSplitterV1 } from "../../src/product";
55
import { extractInvoices } from "../../src/imageOperations";
66
import { PathInput } from "../../src";
7-
import { RESOURCE_PATH } from "../index";
7+
import { V1_PRODUCT_PATH } from "../index";
88

99
const dataPath = {
10-
complete: path.join(RESOURCE_PATH, "products/invoice_splitter/response_v1/complete.json"),
11-
fileSample: path.join(RESOURCE_PATH, "products/invoice_splitter/invoice_5p.pdf"),
10+
complete: path.join(V1_PRODUCT_PATH, "invoice_splitter/response_v1/complete.json"),
11+
fileSample: path.join(V1_PRODUCT_PATH, "invoice_splitter/invoice_5p.pdf"),
1212
};
1313

1414
describe("A multi-page invoice document", () => {

tests/imageOperations/multiReceiptsExtractor.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import path from "path";
44
import { MultiReceiptsDetectorV1 } from "../../src/product";
55
import { extractReceipts } from "../../src/imageOperations";
66
import { PathInput } from "../../src";
7-
import { RESOURCE_PATH } from "../index";
7+
import { V1_PRODUCT_PATH } from "../index";
88

99
const dataPath = {
10-
complete: path.join(RESOURCE_PATH, "products/multi_receipts_detector/response_v1/complete.json"),
11-
fileSample: path.join(RESOURCE_PATH, "products/multi_receipts_detector/default_sample.jpg"),
12-
completeMultiPage: path.join(RESOURCE_PATH, "products/multi_receipts_detector/response_v1/multipage_sample.json"),
13-
multiPageSample: path.join(RESOURCE_PATH, "products/multi_receipts_detector/multipage_sample.pdf"),
10+
complete: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/response_v1/complete.json"),
11+
fileSample: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/default_sample.jpg"),
12+
completeMultiPage: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/response_v1/multipage_sample.json"),
13+
multiPageSample: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/multipage_sample.pdf"),
1414
};
1515

1616
describe("A single-page multi-receipts document", () => {

tests/in.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const axios = require("axios");
4+
const FormData = require("form-data");
5+
6+
async function sendFileWithPolling(
7+
filePath,
8+
modelId,
9+
apiKey,
10+
maxRetries = 30,
11+
pollingInterval = 2
12+
) {
13+
const fileName = path.basename(filePath);
14+
const headers = {
15+
"Authorization": apiKey
16+
};
17+
18+
const formData = new FormData();
19+
formData.append("model_id", modelId);
20+
formData.append("rag", "false");
21+
formData.append("file", fs.createReadStream(filePath), {
22+
filename: fileName,
23+
contentType: "application/octet-stream"
24+
});
25+
26+
console.log(`Enqueuing file: ${filePath}`);
27+
const response = await axios.post(
28+
"https://api-v2.mindee.net/v2/inferences/enqueue",
29+
formData,
30+
{headers: { ...headers, ...formData.getHeaders() }}
31+
);
32+
33+
const jobData = response.data.job;
34+
const pollingUrl = jobData.polling_url;
35+
36+
await new Promise(resolve => setTimeout(resolve, 3000));
37+
38+
for (let attempt = 0; attempt < maxRetries; attempt++) {
39+
console.log(`Polling on: ${pollingUrl}`);
40+
41+
const pollResponse = await axios.get(pollingUrl, {
42+
headers,
43+
maxRedirects: 0,
44+
validateStatus: status => status >= 200 && status < 400
45+
});
46+
47+
const pollData = pollResponse.data;
48+
const jobStatus = pollData.job?.status;
49+
50+
if (pollResponse.status === 302 || jobStatus === "Processed") {
51+
const resultUrl = pollData.job?.result_url;
52+
console.log(`Get result from: ${resultUrl}`);
53+
54+
const resultResponse = await axios.get(resultUrl, { headers });
55+
return resultResponse.data;
56+
}
57+
58+
await new Promise(resolve => setTimeout(resolve, pollingInterval * 1000));
59+
}
60+
61+
throw new Error(`Polling timed out after ${maxRetries} attempts`);
62+
}
63+
64+
async function main() {
65+
try {
66+
const result = await sendFileWithPolling(
67+
"/home/ianardee/Downloads/Invoice-5Z69WRLL-0003.pdf",
68+
"3b87142f-0eb4-4e6d-92a8-1ed01f097052",
69+
"md_xcygjjg8ehfaf2jfeyzomxghvhzh4kzs"
70+
);
71+
console.log(result);
72+
} catch (error) {
73+
console.error("Error:", error.message);
74+
}
75+
}
76+
77+
main();

tests/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import path from "node:path";
22

33
export const RESOURCE_PATH = path.join(__dirname, "data");
4+
45
export const V1_RESOURCE_PATH = path.join(RESOURCE_PATH, "v1");
6+
export const V1_PRODUCT_PATH = path.join(V1_RESOURCE_PATH, "products");
7+
58
export const V2_RESOURCE_PATH = path.join(RESOURCE_PATH, "v2");

tests/input/localResponse.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { expect } from "chai";
44
import { Client, PredictResponse, AsyncPredictResponse, InferenceResponse } from "../../src";
55
import { InternationalIdV2, InvoiceV4, MultiReceiptsDetectorV1 } from "../../src/product";
66
import path from "path";
7-
import { RESOURCE_PATH } from "../index";
7+
import { RESOURCE_PATH, V1_PRODUCT_PATH } from "../index";
88

99
const signature: string = "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0";
1010
const dummySecretKey: string = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
1111
const filePath: string = path.join(RESOURCE_PATH, "/async/get_completed_empty.json");
1212
const multiReceiptsDetectorPath: string = path.join(
13-
RESOURCE_PATH, "products/multi_receipts_detector/response_v1/complete.json"
13+
V1_PRODUCT_PATH, "multi_receipts_detector/response_v1/complete.json"
1414
);
1515
const failedPath: string = path.join(RESOURCE_PATH, "async/get_failed_job_error.json");
1616
const internationalIdPath: string = path.join(
17-
RESOURCE_PATH, "products/international_id/response_v2/complete.json"
17+
V1_PRODUCT_PATH, "international_id/response_v2/complete.json"
1818
);
1919

2020
describe("A valid local response", () => {

tests/input/sources.spec.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { compressImage } from "../../src/imageOperations";
1919
import { compressPdf } from "../../src/pdf";
2020
import { extractTextFromPdf } from "../../src/pdf/pdfUtils";
2121
import { logger } from "../../src/logger";
22-
import { RESOURCE_PATH } from "../index";
22+
import { RESOURCE_PATH, V1_PRODUCT_PATH } from "../index";
2323

2424
describe("Test different types of input", () => {
2525
const outputPath = path.join(RESOURCE_PATH, "output");
@@ -55,12 +55,12 @@ describe("Test different types of input", () => {
5555

5656
it("should accept JPEG files from a path", async () => {
5757
const inputSource = new PathInput({
58-
inputPath: path.join(RESOURCE_PATH, "products/expense_receipts/default_sample.jpg"),
58+
inputPath: path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg"),
5959
});
6060
await inputSource.init();
6161

6262
const expectedResult = await fs.promises.readFile(
63-
path.join(RESOURCE_PATH, "products/expense_receipts/default_sample.jpg")
63+
path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg")
6464
);
6565
expect(inputSource.inputType).to.equals(INPUT_TYPE_PATH);
6666
expect(inputSource.filename).to.equals("default_sample.jpg");
@@ -103,7 +103,7 @@ describe("Test different types of input", () => {
103103
});
104104

105105
it("should accept read streams", async () => {
106-
const filePath = path.join(RESOURCE_PATH, "products/expense_receipts/default_sample.jpg");
106+
const filePath = path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg");
107107
const stream = fs.createReadStream(filePath);
108108
const filename = "default_sample.jpg";
109109
const inputSource = new StreamInput({
@@ -121,7 +121,7 @@ describe("Test different types of input", () => {
121121
});
122122

123123
it("should accept raw bytes", async () => {
124-
const filePath = path.join(RESOURCE_PATH, "products/expense_receipts/default_sample.jpg");
124+
const filePath = path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg");
125125
const inputBytes = await fs.promises.readFile(filePath);
126126
// don't provide an extension to see if we can detect MIME
127127
// type based on contents
@@ -144,7 +144,7 @@ describe("Test different types of input", () => {
144144
const filename = "invoice_01.pdf";
145145
const buffer = Buffer.from(
146146
await fs.promises.readFile(
147-
path.join(RESOURCE_PATH, "products/invoices/invoice_10p.pdf")
147+
path.join(V1_PRODUCT_PATH, "invoices/invoice_10p.pdf")
148148
)
149149
);
150150
const inputSource = new BufferInput({
@@ -257,7 +257,7 @@ describe("Test different types of input", () => {
257257

258258
it("PDF Compress From InputSource", async () => {
259259
const pdfResizeInput = new PathInput(
260-
{ inputPath: path.join(RESOURCE_PATH, "products/invoice_splitter/default_sample.pdf") }
260+
{ inputPath: path.join(V1_PRODUCT_PATH, "invoice_splitter/default_sample.pdf") }
261261
);
262262
await pdfResizeInput.init();
263263

@@ -267,10 +267,7 @@ describe("Test different types of input", () => {
267267
await fs.promises.writeFile(path.join(outputPath, "resize_indirect.pdf"), compressedPdf);
268268

269269
const initialFileStats = await fs.promises.stat(
270-
path.join(
271-
RESOURCE_PATH,
272-
"products/invoice_splitter/default_sample.pdf"
273-
)
270+
path.join(V1_PRODUCT_PATH, "invoice_splitter/default_sample.pdf")
274271
);
275272
const renderedFileStats = await fs.promises.stat(
276273
path.join(outputPath, "resize_indirect.pdf")
@@ -280,7 +277,7 @@ describe("Test different types of input", () => {
280277

281278
it("PDF Compress From Compressor", async () => {
282279
const pdfResizeInput = new PathInput(
283-
{ inputPath: path.join(RESOURCE_PATH, "products/invoice_splitter/default_sample.pdf") }
280+
{ inputPath: path.join(V1_PRODUCT_PATH, "invoice_splitter/default_sample.pdf") }
284281
);
285282
await pdfResizeInput.init();
286283

@@ -297,7 +294,7 @@ describe("Test different types of input", () => {
297294
}
298295

299296
const initialFileStats = await fs.promises.stat(
300-
path.join(RESOURCE_PATH, "products/invoice_splitter/default_sample.pdf")
297+
path.join(V1_PRODUCT_PATH, "invoice_splitter/default_sample.pdf")
301298
);
302299
const renderedFileStats = await Promise.all(
303300
fileNames.map(fileName => fs.promises.stat(path.join(outputPath, fileName)))

tests/pdf/pdfTypes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as pdf from "../../src/pdf";
55
import { PageOptions } from "../../src/input";
66
import { PageOptionsOperation } from "../../src";
77
import * as fs from "node:fs";
8-
import {RESOURCE_PATH} from "../index";
8+
import { RESOURCE_PATH } from "../index";
99

1010
describe("Test pdf lib", () => {
1111
let client: mindee.Client;

tests/v1/api/feedbackResponse.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import path from "path";
22
import { expect } from "chai";
33
import { promises as fs } from "fs";
44
import { FeedbackResponse } from "../../../src/parsing/common";
5-
import { RESOURCE_PATH } from "../../index";
5+
import { V1_PRODUCT_PATH } from "../../index";
66

77
describe("MindeeV1 - Feedback response", () => {
88
it("should load an empty feedback response", async () => {
99
const jsonData = await fs.readFile(
10-
path.join(RESOURCE_PATH, "products/invoices/feedback_response/empty.json")
10+
path.join(V1_PRODUCT_PATH, "invoices/feedback_response/empty.json")
1111
);
1212
const feedbackResponse: FeedbackResponse = new FeedbackResponse(JSON.parse(jsonData.toString()));
1313
expect(feedbackResponse.feedback).to.not.be.undefined;

0 commit comments

Comments
 (0)