Skip to content

Commit 242e576

Browse files
committed
Refactor update tests to restore and enhance user field updates, add validation for non-existent records, and ensure unchanged fields remain intact.
1 parent e4bc4e7 commit 242e576

File tree

2 files changed

+55
-61
lines changed

2 files changed

+55
-61
lines changed

__tests__/repository/paginate.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
setupTestTables,
88
} from "../../test-setup";
99
import { Repository } from "../../src/repository/repository";
10-
import { desc } from "../../src";
1110

1211
describe("Repository Pagination", () => {
1312
let postRepository: Repository<DomainPost>;
@@ -38,21 +37,6 @@ describe("Repository Pagination", () => {
3837
expect(page.nodes).toHaveLength(0);
3938
});
4039

41-
it("should handle custom sorting for pagination", async () => {
42-
const page = await userRepository.paginate({
43-
page: 1,
44-
limit: 5,
45-
orderBy: [desc("age")],
46-
});
47-
48-
expect(page.nodes).toHaveLength(5);
49-
for (let i = 1; i < page.nodes.length; i++) {
50-
expect(
51-
page.nodes[i - 1]?.title?.localeCompare(page.nodes[i]?.title)
52-
).toBeGreaterThanOrEqual(0);
53-
}
54-
});
55-
5640
it("should paginate posts correctly without related authors", async () => {
5741
const page = await postRepository.paginate({
5842
page: 1,

__tests__/repository/update.test.ts

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
setupTestTables,
88
} from "../../test-setup";
99
import { Repository } from "../../src/repository/repository";
10-
import { eq } from "../../src";
10+
import { eq, like } from "../../src";
1111

1212
describe("Repository Update", () => {
1313
let postRepository: Repository<DomainPost>;
@@ -57,48 +57,58 @@ describe("Repository Update", () => {
5757
});
5858
});
5959

60-
// it("should update multiple fields of a user correctly", async () => {
61-
// const targetUser = users[0];
62-
// const updatedUser = await userRepository.update({
63-
// where: eq("id", targetUser.id),
64-
// data: {
65-
// name: "Updated Name",
66-
// age: 30,
67-
// },
68-
// });
69-
// expect(updatedUser).toBeDefined();
70-
// expect(updatedUser.name).toBe("Updated Name");
71-
// expect(updatedUser.age).toBe(30);
72-
//
73-
// const fetchedUser = await userRepository.findById(targetUser.id);
74-
// expect(fetchedUser).toBeDefined();
75-
// expect(fetchedUser?.name).toBe("Updated Name");
76-
// expect(fetchedUser?.age).toBe(30);
77-
// });
78-
//
79-
// it("should return null if the record to update does not exist", async () => {
80-
// const result = await postRepository.update({
81-
// where: eq("id", 999),
82-
// data: {
83-
// title: "Non-existent Post",
84-
// },
85-
// });
86-
// expect(result).toBeNull();
87-
// });
88-
//
89-
// it("should not update fields that are not provided", async () => {
90-
// const targetUser = users[1];
91-
// const originalUser = await userRepository.findById(targetUser.id);
92-
// expect(originalUser).toBeDefined();
93-
//
94-
// const updatedUser = await userRepository.update({
95-
// where: eq("id", targetUser.id),
96-
// data: {
97-
// name: "Partially Updated Name",
98-
// },
99-
// });
100-
// expect(updatedUser).toBeDefined();
101-
// expect(updatedUser.name).toBe("Partially Updated Name");
102-
// expect(updatedUser?.age).toBe(originalUser?.age); // Age should remain unchanged
103-
// });
60+
it("should update multiple fields of a user correctly", async () => {
61+
const targetUser = users[0];
62+
const updatedUser = await userRepository.update({
63+
where: eq("id", targetUser.id),
64+
data: {
65+
name: "Updated Name",
66+
age: 30,
67+
},
68+
});
69+
expect(updatedUser).toBeDefined();
70+
expect(updatedUser.name).toBe("Updated Name");
71+
expect(updatedUser.age).toBe(30);
72+
73+
// Make sure also updated in the database
74+
const fetchedUser = await executor.executeSQL<DomainUser>(
75+
`SELECT * from users WHERE id = $1`,
76+
[targetUser.id],
77+
);
78+
expect(fetchedUser).toBeDefined();
79+
expect(fetchedUser?.rows[0]).toMatchObject({
80+
name: "Updated Name",
81+
age: 30,
82+
});
83+
});
84+
85+
it("should return null if the record to update does not exist", async () => {
86+
const result = await postRepository.update({
87+
where: like("title", "%post-not-exists%"),
88+
data: {
89+
title: "Non-existent Post",
90+
},
91+
});
92+
expect(result).toBeNull();
93+
});
94+
95+
it("should not update fields that are not provided", async () => {
96+
const targetUser = users[1];
97+
const fetchedUsers = await executor.executeSQL<DomainUser>(
98+
`SELECT * FROM users WHERE id = $1`,
99+
[targetUser.id],
100+
);
101+
const originalUser = fetchedUsers.rows[0];
102+
expect(originalUser).toBeDefined();
103+
104+
const updatedUser = await userRepository.update({
105+
where: eq("id", targetUser.id),
106+
data: {
107+
name: "Partially Updated Name",
108+
},
109+
});
110+
expect(updatedUser).toBeDefined();
111+
expect(updatedUser.name).toBe("Partially Updated Name");
112+
expect(updatedUser?.age).toBe(originalUser?.age); // Age should remain unchanged
113+
});
104114
});

0 commit comments

Comments
 (0)