|
7 | 7 | setupTestTables, |
8 | 8 | } from "../../test-setup"; |
9 | 9 | import { Repository } from "../../src/repository/repository"; |
10 | | -import { eq } from "../../src"; |
| 10 | +import { eq, like } from "../../src"; |
11 | 11 |
|
12 | 12 | describe("Repository Update", () => { |
13 | 13 | let postRepository: Repository<DomainPost>; |
@@ -57,48 +57,58 @@ describe("Repository Update", () => { |
57 | 57 | }); |
58 | 58 | }); |
59 | 59 |
|
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 | + }); |
104 | 114 | }); |
0 commit comments