Skip to content

Commit

Permalink
Merge pull request #55 from Robert-M-Lucas/dev-test-fix
Browse files Browse the repository at this point in the history
Fixed tests
  • Loading branch information
Robert-M-Lucas authored May 2, 2024
2 parents 4ab93e1 + 5ade715 commit 1c3104a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/utils/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,17 @@ describe("Firestore Transaction Tests", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await overwriteTransactionsBatched(user, transactions_with_doc.map((t) => t.forceGetDocName()), transactions_with_doc);


const sleep = (milliseconds: number) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
};
await sleep(1000);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const overwritten_transactions = await getTransactionsFilterOrderBy(user, where("name", "==", new_transaction_name));

expect(overwritten_transactions.reduce((curr, t) => curr && (t.name == new_transaction_name), true)).toBeTruthy();
expect(overwritten_transactions.length).toBe(transactions.length);

await t.cleanup();
}, 40_000);
Expand Down
8 changes: 4 additions & 4 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export async function overwriteTransaction(user: User, docName: string, transact

// Writes a batch of transactions to Firestore
export async function writeNewTransactionsBatched(user: User, transactions: Transaction[]): Promise<void> {
for (let i = 0; i < transactions.length; i+=500) {
for (let i = 0; i < transactions.length; i += MAX_BATCH_SIZE) {
const batch = writeBatch(db);
const chunk = transactions.slice(i, i + MAX_BATCH_SIZE);
chunk.forEach((transaction) => {
Expand All @@ -198,14 +198,14 @@ export async function writeNewTransactionsBatched(user: User, transactions: Tran

// Overwrites a batch of existing transactions in Firestore
export async function overwriteTransactionsBatched(user: User, docName: string[], transactions: Transaction[]): Promise<void> {
for (let i = 0; i < transactions.length; i += 500) {
for (let i = 0; i < transactions.length; i += MAX_BATCH_SIZE) {
const batch = writeBatch(db);
const chunk = transactions.slice(i, i + MAX_BATCH_SIZE);
chunk.forEach((transaction) => {
chunk.forEach((transaction, j) => {
if (user.uid != transaction.uid) {
throw Error(`Current user is '${user.uid}' however transaction is '${transaction.uid}'`);
}
const newTransactionRef = doc(collection(db, "Transactions"), docName[i]);
const newTransactionRef = doc(collection(db, "Transactions"), docName[i + j]);
batch.set(newTransactionRef, transaction.toSendObject());
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/user_prefs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {setTestDBContext} from "./firebase.ts";

describe("Firestore UserPrefs Tests", () => {
test("Category Integrity Test", () => {
expect(_.isEqual(new Set(Object.keys(Categories)), TransactionCategories), "Emoji keys do not match transaction categories").toBeTruthy();
expect(_.isEqual(new Set(Object.keys(Categories)), TransactionCategories), "Category keys do not match transaction categories").toBeTruthy();
});
test("Read/Write/Default Value Test", async () => {
const t = await getTestEnv();
Expand Down

0 comments on commit 1c3104a

Please sign in to comment.