|
1 | | -import { SqlExecutor, QueryResult } from "../../src"; |
| 1 | +import { |
| 2 | + setupTestTables, |
| 3 | + cleanupTestData, |
| 4 | + executor, |
| 5 | + DomainUser, |
| 6 | +} from "../../test-setup"; |
2 | 7 | import { Repository } from "../../src/repository/repository"; |
3 | | -import {DomainUser} from "../../test-setup"; |
4 | 8 |
|
5 | | -// Mock SqlExecutor |
6 | | -const mockExecutor: jest.Mocked<SqlExecutor> = { |
7 | | - executeSQL: jest.fn(), |
8 | | -}; |
9 | | - |
10 | | - |
11 | | -describe("Repository insertMany", () => { |
| 9 | +describe("Repository - insertMany", () => { |
12 | 10 | let repository: Repository<DomainUser>; |
13 | 11 |
|
14 | | - beforeEach(() => { |
15 | | - repository = new Repository("users", mockExecutor); |
16 | | - jest.clearAllMocks(); |
| 12 | + beforeAll(async () => { |
| 13 | + await setupTestTables(); |
| 14 | + repository = new Repository("users", executor); |
17 | 15 | }); |
18 | 16 |
|
19 | | - it("should insert multiple records and return them", async () => { |
20 | | - const mockUsers: Partial<DomainUser>[] = [ |
21 | | - { |
22 | | - name: "John Doe", |
23 | | - |
24 | | - age: 30, |
25 | | - }, |
26 | | - { |
27 | | - name: "Jane Doe", |
28 | | - |
29 | | - age: 25, |
30 | | - }, |
31 | | - ]; |
32 | | - |
33 | | - const mockResult: QueryResult = { |
34 | | - rows: [ |
35 | | - { |
36 | | - id: "1", |
37 | | - ...mockUsers[0], |
38 | | - }, |
39 | | - { |
40 | | - id: "2", |
41 | | - ...mockUsers[1], |
42 | | - }, |
43 | | - ], |
44 | | - }; |
45 | | - |
46 | | - mockExecutor.executeSQL.mockResolvedValueOnce(mockResult); |
47 | | - |
48 | | - const result = await repository.insertMany(mockUsers); |
49 | | - |
50 | | - expect(mockExecutor.executeSQL).toHaveBeenCalledTimes(1); |
51 | | - expect(result).toEqual(mockResult.rows); |
| 17 | + afterEach(async () => { |
| 18 | + await cleanupTestData(); |
52 | 19 | }); |
53 | 20 |
|
54 | | - it("should insert records with specific returning columns", async () => { |
55 | | - const mockUsers: Partial<DomainUser>[] = [ |
56 | | - { |
57 | | - name: "John Doe", |
58 | | - |
59 | | - }, |
60 | | - { |
61 | | - name: "Jane Doe", |
62 | | - |
63 | | - }, |
| 21 | + it("should insert multiple records into the database", async () => { |
| 22 | + const records = [ |
| 23 | + { name: "John Doe", email: "[email protected]", age: 30 }, |
| 24 | + { name: "Jane Smith", email: "[email protected]", age: 25 }, |
64 | 25 | ]; |
| 26 | + const result = await repository.insertMany(records); |
65 | 27 |
|
66 | | - const mockResult: QueryResult = { |
67 | | - rows: [ |
68 | | - { |
69 | | - id: "1", |
70 | | - name: mockUsers[0].name, |
71 | | - }, |
72 | | - { |
73 | | - id: "2", |
74 | | - name: mockUsers[1].name, |
75 | | - }, |
76 | | - ], |
77 | | - }; |
78 | | - |
79 | | - mockExecutor.executeSQL.mockResolvedValueOnce(mockResult); |
80 | | - |
81 | | - const result = await repository.insertMany(mockUsers, ["id", "name"]); |
| 28 | + expect(result).toHaveLength(2); |
| 29 | + expect(result[0]).toMatchObject(records[0]); |
| 30 | + expect(result[1]).toMatchObject(records[1]); |
82 | 31 |
|
83 | | - expect(mockExecutor.executeSQL).toHaveBeenCalledTimes(1); |
84 | | - expect(result).toEqual(mockResult.rows); |
85 | | - }); |
86 | | - |
87 | | - it("should handle null values", async () => { |
88 | | - const mockUsers: Partial<DomainUser>[] = [ |
89 | | - { |
90 | | - name: "John Doe", |
91 | | - email: null as any, |
92 | | - age: null as any, |
93 | | - }, |
94 | | - { |
95 | | - name: "Jane Doe", |
96 | | - email: null as any, |
97 | | - age: null as any, |
98 | | - }, |
99 | | - ]; |
100 | | - |
101 | | - const mockResult: QueryResult = { |
102 | | - rows: [ |
103 | | - { |
104 | | - id: "1", |
105 | | - ...mockUsers[0], |
106 | | - }, |
107 | | - { |
108 | | - id: "2", |
109 | | - ...mockUsers[1], |
110 | | - }, |
111 | | - ], |
112 | | - }; |
113 | | - |
114 | | - mockExecutor.executeSQL.mockResolvedValueOnce(mockResult); |
115 | | - |
116 | | - const result = await repository.insertMany(mockUsers); |
117 | | - |
118 | | - expect(mockExecutor.executeSQL).toHaveBeenCalledTimes(1); |
119 | | - expect(result).toEqual(mockResult.rows); |
120 | | - }); |
121 | | - |
122 | | - it("should handle undefined values", async () => { |
123 | | - const mockUsers: Partial<DomainUser>[] = [ |
124 | | - { |
125 | | - name: "John Doe", |
126 | | - |
127 | | - age: undefined, |
128 | | - }, |
129 | | - { |
130 | | - name: "Jane Doe", |
131 | | - |
132 | | - age: undefined, |
133 | | - }, |
134 | | - ]; |
135 | | - |
136 | | - const mockResult: QueryResult = { |
137 | | - rows: [ |
138 | | - { |
139 | | - id: "1", |
140 | | - name: mockUsers[0].name, |
141 | | - email: mockUsers[0].email, |
142 | | - age: null, |
143 | | - }, |
144 | | - { |
145 | | - id: "2", |
146 | | - name: mockUsers[1].name, |
147 | | - email: mockUsers[1].email, |
148 | | - age: null, |
149 | | - }, |
150 | | - ], |
151 | | - }; |
152 | | - |
153 | | - mockExecutor.executeSQL.mockResolvedValueOnce(mockResult); |
154 | | - |
155 | | - const result = await repository.insertMany(mockUsers); |
156 | | - |
157 | | - expect(mockExecutor.executeSQL).toHaveBeenCalledTimes(1); |
158 | | - expect(result).toEqual(mockResult.rows); |
159 | | - }); |
160 | | - |
161 | | - it("should handle different column sets", async () => { |
162 | | - const mockUsers: Partial<DomainUser>[] = [ |
163 | | - { |
164 | | - name: "John Doe", |
165 | | - |
166 | | - }, |
167 | | - { |
168 | | - name: "Jane Doe", |
169 | | - |
170 | | - age: 25, |
171 | | - }, |
172 | | - ]; |
173 | | - |
174 | | - const mockResult: QueryResult = { |
175 | | - rows: [ |
176 | | - { |
177 | | - id: "1", |
178 | | - name: mockUsers[0].name, |
179 | | - email: mockUsers[0].email, |
180 | | - age: null, |
181 | | - }, |
182 | | - { |
183 | | - id: "2", |
184 | | - name: mockUsers[1].name, |
185 | | - email: mockUsers[1].email, |
186 | | - age: mockUsers[1].age, |
187 | | - }, |
188 | | - ], |
189 | | - }; |
190 | | - |
191 | | - mockExecutor.executeSQL.mockResolvedValueOnce(mockResult); |
192 | | - |
193 | | - const result = await repository.insertMany(mockUsers); |
| 32 | + const insertedRecords = await executor.executeSQL( |
| 33 | + "SELECT * FROM users WHERE email IN ($1, $2)", |
| 34 | + |
| 35 | + ); |
194 | 36 |
|
195 | | - expect(mockExecutor.executeSQL).toHaveBeenCalledTimes(1); |
196 | | - expect(result).toEqual(mockResult.rows); |
| 37 | + expect(insertedRecords.rows).toHaveLength(2); |
| 38 | + expect(insertedRecords.rows).toEqual( |
| 39 | + expect.arrayContaining([ |
| 40 | + expect.objectContaining(records[0]), |
| 41 | + expect.objectContaining(records[1]), |
| 42 | + ]) |
| 43 | + ); |
197 | 44 | }); |
198 | 45 |
|
199 | | - it("should handle errors during execution", async () => { |
200 | | - const mockUsers: Partial<DomainUser>[] = [ |
201 | | - { |
202 | | - name: "John Doe", |
203 | | - |
204 | | - }, |
| 46 | + it("should throw an error if one of the records violates constraints", async () => { |
| 47 | + const records = [ |
| 48 | + { name: "Valid User", email: "[email protected]", age: 40 }, |
| 49 | + { name: null, email: "[email protected]" }, // Assuming 'name' cannot be null |
205 | 50 | ]; |
206 | 51 |
|
207 | | - const error = new Error("Database error"); |
208 | | - mockExecutor.executeSQL.mockRejectedValueOnce(error); |
209 | | - |
210 | | - await expect(repository.insertMany(mockUsers)).rejects.toThrow( |
211 | | - "Database error" |
212 | | - ); |
| 52 | + // @ts-ignore |
| 53 | + await expect(repository.insertMany(records)).rejects.toThrow(); |
213 | 54 | }); |
214 | 55 | }); |
0 commit comments