Skip to content

Commit 79c261b

Browse files
committed
Improve DbChange validation tests
1 parent bdb118e commit 79c261b

1 file changed

Lines changed: 84 additions & 18 deletions

File tree

packages/common/test/Evolu/Storage.test.ts

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { sha256 } from "@noble/hashes/sha2.js";
22
import { assert, expect, test } from "vitest";
3+
import { constTrue } from "../../src/Function.js";
34
import { ownerIdToOwnerIdBytes } from "../../src/local-first/Owner.js";
45
import {
56
BaseSqliteStorageDep,
@@ -12,7 +13,6 @@ import {
1213
InfiniteUpperBound,
1314
StorageInsertTimestampStrategy,
1415
timestampBytesToFingerprint,
15-
ValidDbChangeValues,
1616
} from "../../src/local-first/Storage.js";
1717
import {
1818
Counter,
@@ -22,7 +22,6 @@ import {
2222
TimestampBytes,
2323
timestampToTimestampBytes,
2424
} from "../../src/local-first/Timestamp.js";
25-
import { constTrue } from "../../src/Function.js";
2625
import { computeBalancedBuckets } from "../../src/Number.js";
2726
import { createRandom } from "../../src/Random.js";
2827
import { getOrThrow, ok } from "../../src/Result.js";
@@ -491,6 +490,7 @@ test.skip(
491490
test("DbChange", () => {
492491
const id = testCreateId();
493492

493+
// Valid
494494
expect(
495495
DbChange.is({
496496
table: "testTable",
@@ -501,22 +501,88 @@ test("DbChange", () => {
501501
}),
502502
).toBe(true);
503503

504-
// ValidDbChangeValues rejects system columns: createdAt, updatedAt, id
505-
expect(ValidDbChangeValues.is({ createdAt: "2024-01-01T00:00:00Z" })).toBe(
506-
false,
507-
);
508-
expect(ValidDbChangeValues.is({ updatedAt: "2024-01-01T00:00:00Z" })).toBe(
509-
false,
510-
);
511-
expect(ValidDbChangeValues.is({ id })).toBe(false);
504+
// Invalid: system columns in values
505+
expect(
506+
DbChange.is({
507+
table: "testTable",
508+
id,
509+
values: { createdAt: "2024-01-01T00:00:00Z" },
510+
isInsert: true,
511+
isDelete: false,
512+
}),
513+
).toBe(false);
512514

513-
expect(ValidDbChangeValues.is({ isDeleted: 1 })).toBe(false);
515+
expect(
516+
DbChange.is({
517+
table: "testTable",
518+
id,
519+
values: { updatedAt: "2024-01-01T00:00:00Z" },
520+
isInsert: true,
521+
isDelete: false,
522+
}),
523+
).toBe(false);
514524

515-
// ValidDbChangeValues allows valid column values
516-
expect(ValidDbChangeValues.is({ column1: "string", column2: 42 })).toBe(true);
517-
expect(ValidDbChangeValues.is({ data: new Uint8Array([1, 2, 3]) })).toBe(
518-
true,
519-
);
520-
expect(ValidDbChangeValues.is({ nullable: null })).toBe(true);
521-
expect(ValidDbChangeValues.is({})).toBe(true);
525+
expect(
526+
DbChange.is({
527+
table: "testTable",
528+
id,
529+
values: { id },
530+
isInsert: true,
531+
isDelete: false,
532+
}),
533+
).toBe(false);
534+
535+
expect(
536+
DbChange.is({
537+
table: "testTable",
538+
id,
539+
values: { isDeleted: 1 },
540+
isInsert: true,
541+
isDelete: false,
542+
}),
543+
).toBe(false);
544+
545+
// Invalid: invalid table
546+
expect(
547+
DbChange.is({
548+
table: 123,
549+
id,
550+
values: { column1: "value1" },
551+
isInsert: true,
552+
isDelete: false,
553+
}),
554+
).toBe(false);
555+
556+
// Invalid: invalid id
557+
expect(
558+
DbChange.is({
559+
table: "testTable",
560+
id: "invalid",
561+
values: { column1: "value1" },
562+
isInsert: true,
563+
isDelete: false,
564+
}),
565+
).toBe(false);
566+
567+
// Invalid: invalid isInsert
568+
expect(
569+
DbChange.is({
570+
table: "testTable",
571+
id,
572+
values: { column1: "value1" },
573+
isInsert: "true",
574+
isDelete: false,
575+
}),
576+
).toBe(false);
577+
578+
// Invalid: invalid isDelete
579+
expect(
580+
DbChange.is({
581+
table: "testTable",
582+
id,
583+
values: { column1: "value1" },
584+
isInsert: true,
585+
isDelete: "false",
586+
}),
587+
).toBe(false);
522588
});

0 commit comments

Comments
 (0)