Skip to content

Commit

Permalink
include tests for additional query builder clear methods
Browse files Browse the repository at this point in the history
  • Loading branch information
garymathews committed Jan 11, 2024
1 parent 48a1ce8 commit 9e50ba8
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
133 changes: 133 additions & 0 deletions test/node/src/clear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,88 @@ for (const dialect of DIALECTS) {
})
})

it('InsertQueryBuilder should clear returning', () => {
const query = ctx.db
.insertInto('person')
.values({ first_name: 'Bruce', last_name: 'Willis', gender: 'male' })
.returning(['first_name'])
.clearReturning()

testSql(query, dialect, {
postgres: {
sql: `insert into "person" ("first_name", "last_name", "gender") values ($1, $2, $3)`,
parameters: ['Bruce', 'Willis', 'male'],
},
mysql: {
sql: 'insert into `person` (`first_name`, `last_name`, `gender`) values (?, ?, ?)',
parameters: ['Bruce', 'Willis', 'male'],
},
mssql: {
sql: `insert into "person" ("first_name", "last_name", "gender") values (@1, @2, @3)`,
parameters: ['Bruce', 'Willis', 'male'],
},
sqlite: {
sql: `insert into "person" ("first_name", "last_name", "gender") values (?, ?, ?)`,
parameters: ['Bruce', 'Willis', 'male'],
},
})
})

it('UpdateQueryBuilder should clear returning', () => {
const query = ctx.db
.updateTable('person')
.set({ marital_status: 'married' })
.where('first_name', '=', 'Bruce')
.returning(['first_name'])
.clearReturning()

testSql(query, dialect, {
postgres: {
sql: `update "person" set "marital_status" = $1 where "first_name" = $2`,
parameters: ['married', 'Bruce'],
},
mysql: {
sql: 'update `person` set `marital_status` = ? where `first_name` = ?',
parameters: ['married', 'Bruce'],
},
mssql: {
sql: `update "person" set "marital_status" = @1 where "first_name" = @2`,
parameters: ['married', 'Bruce'],
},
sqlite: {
sql: `update "person" set "marital_status" = ? where "first_name" = ?`,
parameters: ['married', 'Bruce'],
},
})
})

it('DeleteQueryBuilder should clear returning', () => {
const query = ctx.db
.deleteFrom('person')
.where('first_name', '=', 'James')
.returning(['first_name'])
.clearReturning()

testSql(query, dialect, {
postgres: {
sql: `delete from "person" where "first_name" = $1`,
parameters: ['James'],
},
mysql: {
sql: 'delete from `person` where `first_name` = ?',
parameters: ['James'],
},
mssql: {
sql: `delete from "person" where "first_name" = @1`,
parameters: ['James'],
},
sqlite: {
sql: `delete from "person" where "first_name" = ?`,
parameters: ['James'],
},
})
})

it('should clear orderBy', () => {
const query = ctx.db
.selectFrom('person')
Expand Down Expand Up @@ -209,6 +291,33 @@ for (const dialect of DIALECTS) {
})
})

it('DeleteQueryBuilder clear orderBy', () => {
const query = ctx.db
.deleteFrom('person')
.where('gender', '=', 'male')
.orderBy('id')
.clearOrderBy()

testSql(query, dialect, {
postgres: {
sql: `delete from "person" where "gender" = $1`,
parameters: ['male'],
},
mysql: {
sql: 'delete from `person` where `gender` = ?',
parameters: ['male'],
},
mssql: {
sql: `delete from "person" where "gender" = @1`,
parameters: ['male'],
},
sqlite: {
sql: `delete from "person" where "gender" = ?`,
parameters: ['male'],
},
})
})

if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'sqlite') {
it('should clear limit', () => {
const query = ctx.db
Expand All @@ -233,6 +342,30 @@ for (const dialect of DIALECTS) {
},
})
})

it('DeleteQueryBuilder should clear limit', () => {
const query = ctx.db
.deleteFrom('person')
.where('gender', '=', 'male')
.limit(1)
.clearLimit()

testSql(query, dialect, {
postgres: {
sql: `delete from "person" where "gender" = $1`,
parameters: ['male'],
},
mysql: {
sql: 'delete from `person` where `gender` = ?',
parameters: ['male'],
},
mssql: NOT_SUPPORTED,
sqlite: {
sql: `delete from "person" where "gender" = ?`,
parameters: ['male'],
},
})
})
}

it('should clear offset', () => {
Expand Down
68 changes: 68 additions & 0 deletions test/typings/test-d/clear.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,71 @@ async function testClearSelect(db: Kysely<Database>) {

expectType<{ age: number }>(r2)
}

async function testClearInsert(db: Kysely<Database>) {
const r1 = await db
.insertInto('person')
.values({ first_name: 'Bruce', last_name: 'Willis', age: 68, gender: 'male' })
.returning(['first_name', 'gender'])
.clearReturning()
.returning('id')
.executeTakeFirstOrThrow()

expectType<{ id: number }>(r1)

const r2 = await db
.insertInto('person')
.values({ first_name: 'Bruce', last_name: 'Willis', age: 68, gender: 'male' })
.returningAll()
.clearReturning()
.returning('age')
.executeTakeFirstOrThrow()

expectType<{ age: number }>(r2)
}

async function testClearUpdate(db: Kysely<Database>) {
const r1 = await db
.updateTable('person')
.set({ age: 76 })
.where('first_name', '=', 'Arnold')
.returning(['first_name', 'gender'])
.clearReturning()
.returning('id')
.executeTakeFirstOrThrow()

expectType<{ id: number }>(r1)

const r2 = await db
.updateTable('person')
.set({ age: 76 })
.where('first_name', '=', 'Arnold')
.returningAll()
.clearReturning()
.returning('age')
.executeTakeFirstOrThrow()

expectType<{ age: number }>(r2)
}

async function testClearDelete(db: Kysely<Database>) {
const r1 = await db
.deleteFrom('person')
.where('first_name', '=', 'Bruce')
.returning(['first_name', 'gender'])
.clearReturning()
.returning('id')
.executeTakeFirstOrThrow()

expectType<{ id: number }>(r1)

const r2 = await db
.deleteFrom('person')
.where('first_name', '=', 'Bruce')
.returningAll()
.clearReturning()
.returning('age')
.executeTakeFirstOrThrow()

expectType<{ age: number }>(r2)
}

0 comments on commit 9e50ba8

Please sign in to comment.