mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 17:10:30 +00:00
fix failing JS tests
This commit is contained in:
parent
79566a7346
commit
79a4bb6bf5
1 changed files with 70 additions and 57 deletions
|
@ -1,30 +1,31 @@
|
||||||
|
import fs from "fs";
|
||||||
import { faker } from "@faker-js/faker";
|
import { faker } from "@faker-js/faker";
|
||||||
import { beforeAll, describe, expect, test } from "vitest";
|
import { beforeAll, describe, expect, test } from "vitest";
|
||||||
import { UserClient } from "../../user";
|
import { UserClient } from "../../user";
|
||||||
import { factories } from "../factories";
|
import { factories } from "../factories";
|
||||||
|
|
||||||
type ImportObj = {
|
type ImportObj = {
|
||||||
ImportRef: string;
|
[`HB.import_ref`]: string;
|
||||||
Location: string;
|
[`HB.location`]: string;
|
||||||
Labels: string;
|
[`HB.labels`]: string;
|
||||||
Quantity: string;
|
[`HB.quantity`]: number;
|
||||||
Name: string;
|
[`HB.name`]: string;
|
||||||
Description: string;
|
[`HB.description`]: string;
|
||||||
Insured: boolean;
|
[`HB.insured`]: boolean;
|
||||||
SerialNumber: string;
|
[`HB.serial_number`]: string;
|
||||||
ModelNumber: string;
|
[`HB.model_number`]: string;
|
||||||
Manufacturer: string;
|
[`HB.manufacturer`]: string;
|
||||||
Notes: string;
|
[`HB.notes`]: string;
|
||||||
PurchaseFrom: string;
|
[`HB.purchase_price`]: number;
|
||||||
PurchasedPrice: number;
|
[`HB.purchase_from`]: string;
|
||||||
PurchasedTime: string;
|
[`HB.purchase_time`]: string;
|
||||||
LifetimeWarranty: boolean;
|
[`HB.lifetime_warranty`]: boolean;
|
||||||
WarrantyExpires: string;
|
[`HB.warranty_expires`]: string;
|
||||||
WarrantyDetails: string;
|
[`HB.warranty_details`]: string;
|
||||||
SoldTo: string;
|
[`HB.sold_to`]: string;
|
||||||
SoldPrice: number;
|
[`HB.sold_price`]: number;
|
||||||
SoldTime: string;
|
[`HB.sold_time`]: string;
|
||||||
SoldNotes: string;
|
[`HB.sold_notes`]: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function toCsv(data: ImportObj[]): string {
|
function toCsv(data: ImportObj[]): string {
|
||||||
|
@ -36,7 +37,7 @@ function toCsv(data: ImportObj[]): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
function importFileGenerator(entries: number): ImportObj[] {
|
function importFileGenerator(entries: number): ImportObj[] {
|
||||||
const imports: ImportObj[] = [];
|
const imports: Partial<ImportObj>[] = [];
|
||||||
|
|
||||||
const pick = (arr: string[]) => arr[Math.floor(Math.random() * arr.length)];
|
const pick = (arr: string[]) => arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
|
||||||
|
@ -45,37 +46,41 @@ function importFileGenerator(entries: number): ImportObj[] {
|
||||||
|
|
||||||
const half = Math.floor(entries / 2);
|
const half = Math.floor(entries / 2);
|
||||||
|
|
||||||
|
// YYYY-MM-DD
|
||||||
|
const formatDate = (date: Date) => date.toISOString().split("T")[0];
|
||||||
|
|
||||||
for (let i = 0; i < entries; i++) {
|
for (let i = 0; i < entries; i++) {
|
||||||
imports.push({
|
imports.push({
|
||||||
ImportRef: faker.database.mongodbObjectId(),
|
[`HB.import_ref`]: faker.database.mongodbObjectId(),
|
||||||
Location: pick(locations),
|
[`HB.location`]: pick(locations),
|
||||||
Labels: labels,
|
[`HB.labels`]: labels,
|
||||||
Quantity: faker.random.numeric(1),
|
[`HB.quantity`]: Number(faker.random.numeric(2)),
|
||||||
Name: faker.random.words(3),
|
[`HB.name`]: faker.random.words(3),
|
||||||
Description: "",
|
[`HB.description`]: "",
|
||||||
Insured: faker.datatype.boolean(),
|
[`HB.insured`]: faker.datatype.boolean(),
|
||||||
SerialNumber: faker.random.alphaNumeric(5),
|
[`HB.serial_number`]: faker.random.alphaNumeric(5),
|
||||||
ModelNumber: faker.random.alphaNumeric(5),
|
[`HB.model_number`]: faker.random.alphaNumeric(5),
|
||||||
Manufacturer: faker.random.alphaNumeric(5),
|
[`HB.manufacturer`]: faker.random.alphaNumeric(5),
|
||||||
Notes: "",
|
[`HB.notes`]: "",
|
||||||
PurchaseFrom: faker.name.fullName(),
|
[`HB.purchase_from`]: faker.name.fullName(),
|
||||||
PurchasedPrice: faker.datatype.number(100),
|
[`HB.purchase_price`]: faker.datatype.number(100),
|
||||||
PurchasedTime: faker.date.past().toDateString(),
|
[`HB.purchase_time`]: faker.date.past().toDateString(),
|
||||||
LifetimeWarranty: half > i,
|
[`HB.lifetime_warranty`]: half > i,
|
||||||
WarrantyExpires: faker.date.future().toDateString(),
|
[`HB.warranty_details`]: "",
|
||||||
WarrantyDetails: "",
|
[`HB.sold_to`]: faker.name.fullName(),
|
||||||
SoldTo: faker.name.fullName(),
|
[`HB.sold_price`]: faker.datatype.number(100),
|
||||||
SoldPrice: faker.datatype.number(100),
|
[`HB.sold_time`]: formatDate(faker.date.past()),
|
||||||
SoldTime: faker.date.past().toDateString(),
|
[`HB.sold_notes`]: "",
|
||||||
SoldNotes: "",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return imports;
|
return imports as ImportObj[];
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("group related statistics tests", () => {
|
describe("group related statistics tests", () => {
|
||||||
const TOTAL_ITEMS = 30;
|
const TOTAL_ITEMS = 30;
|
||||||
|
const labelData: Record<string, number> = {};
|
||||||
|
const locationData: Record<string, number> = {};
|
||||||
|
|
||||||
let tAPI: UserClient | undefined;
|
let tAPI: UserClient | undefined;
|
||||||
const imports = importFileGenerator(TOTAL_ITEMS);
|
const imports = importFileGenerator(TOTAL_ITEMS);
|
||||||
|
@ -94,13 +99,32 @@ describe("group related statistics tests", () => {
|
||||||
|
|
||||||
const csv = toCsv(imports);
|
const csv = toCsv(imports);
|
||||||
|
|
||||||
|
// write to file system for debugging
|
||||||
|
fs.writeFileSync("test.csv", csv);
|
||||||
|
|
||||||
const setupResp = await client.items.import(new Blob([csv], { type: "text/csv" }));
|
const setupResp = await client.items.import(new Blob([csv], { type: "text/csv" }));
|
||||||
|
|
||||||
expect(setupResp.status).toBe(204);
|
expect(setupResp.status).toBe(204);
|
||||||
|
|
||||||
|
for (const item of imports) {
|
||||||
|
const labels = item[`HB.labels`].split(";");
|
||||||
|
for (const label of labels) {
|
||||||
|
if (labelData[label]) {
|
||||||
|
labelData[label] += item[`HB.purchase_price`];
|
||||||
|
} else {
|
||||||
|
labelData[label] = item[`HB.purchase_price`];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const location = item[`HB.location`];
|
||||||
|
if (locationData[location]) {
|
||||||
|
locationData[location] += item[`HB.purchase_price`];
|
||||||
|
} else {
|
||||||
|
locationData[location] = item[`HB.purchase_price`];
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Write to file system for debugging
|
|
||||||
// fs.writeFileSync("test.csv", csv);
|
|
||||||
test("Validate Group Statistics", async () => {
|
test("Validate Group Statistics", async () => {
|
||||||
const { status, data } = await api().stats.group();
|
const { status, data } = await api().stats.group();
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
|
@ -112,17 +136,6 @@ describe("group related statistics tests", () => {
|
||||||
expect(data.totalWithWarranty).toEqual(Math.floor(TOTAL_ITEMS / 2));
|
expect(data.totalWithWarranty).toEqual(Math.floor(TOTAL_ITEMS / 2));
|
||||||
});
|
});
|
||||||
|
|
||||||
const labelData: Record<string, number> = {};
|
|
||||||
const locationData: Record<string, number> = {};
|
|
||||||
|
|
||||||
for (const item of imports) {
|
|
||||||
for (const label of item.Labels.split(";")) {
|
|
||||||
labelData[label] = (labelData[label] || 0) + item.PurchasedPrice;
|
|
||||||
}
|
|
||||||
|
|
||||||
locationData[item.Location] = (locationData[item.Location] || 0) + item.PurchasedPrice;
|
|
||||||
}
|
|
||||||
|
|
||||||
test("Validate Labels Statistics", async () => {
|
test("Validate Labels Statistics", async () => {
|
||||||
const { status, data } = await api().stats.labels();
|
const { status, data } = await api().stats.labels();
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue