Skip to content

Commit 90b7355

Browse files
committed
Refactor bulk insert tests in InsertQueryBuilder to use mock data and handle optional fields. Update User interface to make age optional and add error handling for empty bulk inserts.
1 parent 2aaf236 commit 90b7355

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

__tests__/insert-builder.test.ts

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface User {
1010
id: string;
1111
name: string;
1212
email: string;
13-
age: number;
13+
age?: number;
1414
}
1515

1616
describe("InsertQueryBuilder", () => {
@@ -64,58 +64,66 @@ describe("InsertQueryBuilder", () => {
6464
});
6565
});
6666

67-
// describe("Bulk Insert", () => {
68-
// it("should build correct SQL for bulk insertion", () => {
69-
// const data = [
70-
// {
71-
// firstName: "John",
72-
// lastName: "Doe",
73-
// age: 30,
74-
// },
75-
// {
76-
// firstName: "Jane",
77-
// lastName: "Smith",
78-
// age: 25,
79-
// },
80-
// ];
81-
82-
// const result = builder.values(data).build();
83-
84-
// expect(result.sql).toContain(
85-
// 'INSERT INTO users ("first_name", "last_name", "age")'
86-
// );
87-
// expect(result.sql).toContain("VALUES ($1, $2, $3), ($4, $5, $6)");
88-
// expect(result.values).toEqual(["John", "Doe", 30, "Jane", "Smith", 25]);
89-
// });
90-
91-
// it("should handle missing fields in some records", () => {
92-
// const data = [
93-
// {
94-
// firstName: "John",
95-
// lastName: "Doe",
96-
// age: 30,
97-
// },
98-
// {
99-
// firstName: "Jane",
100-
// lastName: "Smith",
101-
// },
102-
// ];
103-
104-
// const result = builder.values(data).build();
105-
106-
// expect(result.sql).toContain(
107-
// 'INSERT INTO users ("first_name", "last_name", "age")'
108-
// );
109-
// expect(result.sql).toContain("VALUES ($1, $2, $3), ($4, $5, $6)");
110-
// expect(result.values).toEqual(["John", "Doe", 30, "Jane", "Smith", null]);
111-
// });
67+
describe("Bulk Insert", () => {
68+
it("should build correct SQL for bulk insertion", () => {
69+
const mockUsers: Omit<User, "id">[] = [
70+
{
71+
name: "John",
72+
73+
age: 30,
74+
},
75+
{
76+
name: "Doe",
77+
78+
age: 25,
79+
},
80+
];
81+
const result = builder
82+
.values(mockUsers)
83+
.returning(["id", "name"])
84+
.build();
11285

113-
// it("should throw error for empty bulk insert", () => {
114-
// expect(() => {
115-
// builder.values([]).build();
116-
// }).toThrow("No data provided for bulk insert");
117-
// });
118-
// });
86+
expect(result.sql).toContain("INSERT INTO users (name, email, age)");
87+
expect(result.sql).toContain("VALUES ($1, $2, $3), ($4, $5, $6)");
88+
expect(result.values).toEqual([
89+
mockUsers[0].name,
90+
mockUsers[0].email,
91+
mockUsers[0].age,
92+
mockUsers[1].name,
93+
mockUsers[1].email,
94+
mockUsers[1].age,
95+
]);
96+
});
97+
it("should handle missing fields in some records", () => {
98+
const mockUsers: Omit<User, "id">[] = [
99+
{
100+
name: "John",
101+
102+
age: 30,
103+
},
104+
{
105+
name: "Doe",
106+
107+
},
108+
];
109+
const result = builder.values(mockUsers).build();
110+
expect(result.sql).toContain("INSERT INTO users (name, email, age)");
111+
expect(result.sql).toContain("VALUES ($1, $2, $3), ($4, $5, $6)");
112+
expect(result.values).toEqual([
113+
mockUsers[0].name,
114+
mockUsers[0].email,
115+
mockUsers[0].age,
116+
mockUsers[1].name,
117+
mockUsers[1].email,
118+
null,
119+
]);
120+
});
121+
it("should throw error for empty bulk insert", () => {
122+
expect(() => {
123+
builder.values([]).build();
124+
}).toThrow("No data provided for bulk insert");
125+
});
126+
});
119127

120128
// describe("Column Name Transformation", () => {
121129
// it("should convert camelCase to snake_case", () => {

0 commit comments

Comments
 (0)