Skip to content

Commit 6d2ad15

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

File tree

54 files changed

+476
-321
lines changed

Some content is hidden

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

54 files changed

+476
-321
lines changed

.github/workflows/_test-integrations.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
name: Run Integration Tests
1313
timeout-minutes: 30
1414
strategy:
15-
max-parallel: 4
1615
matrix:
1716
os:
1817
- "ubuntu-latest"

.github/workflows/_test-units.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ jobs:
1010
run-tests:
1111
name: Run Tests
1212
strategy:
13-
max-parallel: 4
1413
matrix:
1514
os:
1615
- "ubuntu-latest"

tests/data

Submodule data updated 302 files

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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");
9+
export const V2_PRODUCT_PATH = path.join(V2_RESOURCE_PATH, "products");

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/asyncResponse.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { RESOURCE_PATH } from "../../index";
1010
describe("MindeeV1 - Asynchronous API predict response", () => {
1111
it("should parse a successful enqueue", async () => {
1212
const jsonData = await fs.readFile(
13-
path.join(RESOURCE_PATH, "async/post_success.json")
13+
path.join(RESOURCE_PATH, "v1/async/post_success.json")
1414
);
1515
const httpResponse: StringDict = {
1616
data: JSON.parse(jsonData.toString()),
@@ -26,7 +26,7 @@ describe("MindeeV1 - Asynchronous API predict response", () => {
2626

2727
it("should parse a failed enqueue", async () => {
2828
const jsonData = await fs.readFile(
29-
path.join(RESOURCE_PATH, "async/post_fail_forbidden.json")
29+
path.join(RESOURCE_PATH, "v1/async/post_fail_forbidden.json")
3030
);
3131
const httpResponse: StringDict = {
3232
data: JSON.parse(jsonData.toString()),
@@ -36,7 +36,7 @@ describe("MindeeV1 - Asynchronous API predict response", () => {
3636

3737
it("should parse a failed job", async () => {
3838
const jsonData = await fs.readFile(
39-
path.join(RESOURCE_PATH, "async/get_failed_job_error.json")
39+
path.join(RESOURCE_PATH, "v1/async/get_failed_job_error.json")
4040
);
4141
const httpResponse: StringDict = {
4242
data: JSON.parse(jsonData.toString()),
@@ -46,7 +46,7 @@ describe("MindeeV1 - Asynchronous API predict response", () => {
4646

4747
it("should parse a job in progress", async () => {
4848
const jsonData = await fs.readFile(
49-
path.join(RESOURCE_PATH, "async/get_processing.json")
49+
path.join(RESOURCE_PATH, "v1/async/get_processing.json")
5050
);
5151
const httpResponse: StringDict = {
5252
data: JSON.parse(jsonData.toString()),
@@ -62,7 +62,7 @@ describe("MindeeV1 - Asynchronous API predict response", () => {
6262

6363
it("should parse a completed job", async () => {
6464
const jsonData = await fs.readFile(
65-
path.join(RESOURCE_PATH, "async/get_completed.json")
65+
path.join(RESOURCE_PATH, "v1/async/get_completed.json")
6666
);
6767
const httpResponse: StringDict = {
6868
data: JSON.parse(jsonData.toString()),

0 commit comments

Comments
 (0)