Skip to content

Commit

Permalink
test-d when not matched by source.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Nov 2, 2023
1 parent 79b23f8 commit 7e7deb7
Showing 1 changed file with 113 additions and 5 deletions.
118 changes: 113 additions & 5 deletions test/typings/test-d/merge.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
ExpressionBuilder,
JoinBuilder,
Kysely,
MatchedThenableMergeQueryBuilder,
MergeQueryBuilder,
MergeResult,
NotMatchedThenableMergeQueryBuilder,
WheneableMergeQueryBuilder,
sql,
} from '..'
Expand All @@ -22,6 +25,10 @@ async function testMergeInto(db: Kysely<Database>) {
eb.selectFrom('person').selectAll().as('person')
)
)

expectType<MergeQueryBuilder<Database, 'person', MergeResult>>(
db.mergeInto('person')
)
}

async function testUsing(db: Kysely<Database>) {
Expand All @@ -46,9 +53,9 @@ async function testUsing(db: Kysely<Database>) {
return join.onTrue()
})

const baseQuery = db
.mergeInto('person')
.using('pet', 'pet.owner_id', 'person.id')
expectType<
WheneableMergeQueryBuilder<Database, 'person', 'pet', MergeResult>
>(db.mergeInto('person').using('pet', 'pet.owner_id', 'person.id'))

expectType<MergeResult>(
await db
Expand Down Expand Up @@ -93,6 +100,19 @@ async function testWhenMatched(
baseQuery.whenMatchedAndRef('age', '>', sql`person.age`)
baseQuery.whenMatchedAndRef('age', sql`>`, 'person.age')
expectError(baseQuery.whenMatchedAndRef('age', 'NO_SUCH_OPERATOR', 'age'))

type ExpectedReturnType = MatchedThenableMergeQueryBuilder<
Database,
'person',
'pet',
'person' | 'pet',
MergeResult
>
expectType<ExpectedReturnType>(baseQuery.whenMatched())
expectType<ExpectedReturnType>(baseQuery.whenMatchedAnd('age', '>', 2))
expectType<ExpectedReturnType>(
baseQuery.whenMatchedAndRef('pet.name', '>', 'person.age')
)
}

async function testWhenNotMatched(
Expand Down Expand Up @@ -133,11 +153,99 @@ async function testWhenNotMatched(
expectError(baseQuery.whenNotMatchedAndRef('species', '>', 2))
baseQuery.whenNotMatchedAndRef('pet.name', '>', 'pet.species')
// when not matched can only reference the source table's columns.
baseQuery.whenNotMatchedAndRef('pet.name', '>', 'person.first_name')
baseQuery.whenNotMatchedAndRef('person.first_name', '>', 'pet.species')
expectError(
baseQuery.whenNotMatchedAndRef('pet.name', '>', 'person.first_name')
)
expectError(
baseQuery.whenNotMatchedAndRef('person.first_name', '>', 'pet.species')
)
baseQuery.whenNotMatchedAndRef('species', '>', sql`person.age`)
baseQuery.whenNotMatchedAndRef('species', sql`>`, 'pet.species')
expectError(
baseQuery.whenNotMatchedAndRef('species', 'NO_SUCH_OPERATOR', 'name')
)

type ExpectedReturnType = NotMatchedThenableMergeQueryBuilder<
Database,
'person',
'pet',
MergeResult
>
expectType<ExpectedReturnType>(baseQuery.whenNotMatched())
expectType<ExpectedReturnType>(
baseQuery.whenNotMatchedAnd('species', '>', 'dog')
)
expectType<ExpectedReturnType>(
baseQuery.whenNotMatchedAndRef('pet.name', '>', 'pet.species')
)
}

async function testWhenNotMatchedBySource(
baseQuery: WheneableMergeQueryBuilder<Database, 'person', 'pet', MergeResult>
) {
baseQuery.whenNotMatchedBySource()
expectError(baseQuery.whenNotMatchedBySource('age'))
expectError(baseQuery.whenNotMatchedBySourceAnd('age'))
expectError(baseQuery.whenNotMatchedBySourceAnd('NO_SUCH_COLUMN'))
expectError(baseQuery.whenNotMatchedBySourceAnd('age', '>'))
expectError(baseQuery.whenNotMatchedBySourceAnd('age', '>', 'string'))
baseQuery.whenNotMatchedBySourceAnd('age', '>', 2)
expectError(
baseQuery.whenNotMatchedBySourceAnd('age', 'NOT_SUCH_OPERATOR', 'dog')
)
// when not matched by source can only reference the target table's columns.
expectError(baseQuery.whenNotMatchedBySourceAnd('species', '>', 'dog'))
baseQuery.whenNotMatchedBySourceAnd('age', sql`>`, 2)
baseQuery.whenNotMatchedBySourceAnd('person.age', '>', sql`2`)
baseQuery.whenNotMatchedBySourceAnd('age', '>', (eb) => {
// already tested in many places
expectType<ExpressionBuilder<Database, 'person'>>(eb)
return eb.ref('person.age')
})
expectError(
baseQuery.whenNotMatchedBySourceAnd('age', '>', (eb) =>
eb.ref('person.gender')
)
)
baseQuery.whenNotMatchedBySourceAnd((eb) => {
// already tested in many places
expectType<ExpressionBuilder<Database, 'person'>>(eb)
return eb.and([])
})
expectError(baseQuery.whenNotMatchedBySourceAndRef('age'))
expectError(baseQuery.whenNotMatchedBySourceAndRef('NO_SUCH_COLUMN'))
expectError(baseQuery.whenNotMatchedBySourceAndRef('age', '>'))
expectError(baseQuery.whenNotMatchedBySourceAndRef('age', '>', 'string'))
expectError(baseQuery.whenNotMatchedBySourceAndRef('age', '>', 2))
baseQuery.whenNotMatchedBySourceAndRef(
'person.first_name',
'>',
'person.last_name'
)
// when not matched by source can only reference the target table's columns.
expectError(
baseQuery.whenNotMatchedBySourceAndRef('person.first_name', '>', 'pet.name')
)
expectError(
baseQuery.whenNotMatchedBySourceAndRef('pet.name', '>', 'person.first_name')
)

type ExpectedReturnType = MatchedThenableMergeQueryBuilder<
Database,
'person',
'pet',
'person',
MergeResult
>
expectType<ExpectedReturnType>(baseQuery.whenNotMatchedBySource())
expectType<ExpectedReturnType>(
baseQuery.whenNotMatchedBySourceAnd('age', '>', 2)
)
expectType<ExpectedReturnType>(
baseQuery.whenNotMatchedBySourceAndRef(
'person.first_name',
'>',
'person.last_name'
)
)
}

0 comments on commit 7e7deb7

Please sign in to comment.