Skip to content

Commit a687c4d

Browse files
committed
Add tests for Repository insertOne method to validate record insertion and constraint violations
1 parent 5a47f44 commit a687c4d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {
2+
setupTestTables,
3+
cleanupTestData,
4+
executor,
5+
DomainUser,
6+
} from "../../test-setup";
7+
import { Repository } from "../../src/repository/repository";
8+
9+
describe("Repository - insertOne", () => {
10+
let repository: Repository<DomainUser>;
11+
12+
beforeAll(async () => {
13+
await setupTestTables();
14+
repository = new Repository("users", executor);
15+
});
16+
17+
afterEach(async () => {
18+
await cleanupTestData();
19+
});
20+
21+
it("should insert a single record into the database", async () => {
22+
const record = {
23+
name: "John Doe",
24+
25+
age: 30,
26+
};
27+
const result = await repository.insertOne(record);
28+
29+
expect(result).toMatchObject({
30+
name: "John Doe",
31+
32+
age: 30,
33+
});
34+
35+
const insertedRecord = await executor.executeSQL(
36+
"SELECT * FROM users WHERE email = $1",
37+
38+
);
39+
40+
expect(insertedRecord.rows[0]).toMatchObject(record);
41+
});
42+
43+
it("should throw an error if the record violates constraints", async () => {
44+
const record = { name: null, email: "[email protected]" }; // Assuming 'name' cannot be null
45+
46+
await expect(repository.insertOne(record)).rejects.toThrow();
47+
});
48+
});

0 commit comments

Comments
 (0)