Skip to content

Commit

Permalink
Added passing test for Postgres INSERT DO UPDATE
Browse files Browse the repository at this point in the history
To help with diagnosis of issue #2791.
  • Loading branch information
Satook authored and AlecKazakova committed Apr 6, 2022
1 parent 88d3fc6 commit a879a30
Showing 1 changed file with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PgInsertOnConflictTest {

Truth.assertThat(generator.function().toString()).isEqualTo(
"""
|public fun upsert2Cols(
|public fun upsertCols(
| id: kotlin.Int?,
| c1: kotlin.String?
|): kotlin.Unit {
Expand All @@ -55,6 +55,57 @@ class PgInsertOnConflictTest {
)
}

@Test
fun `postgres INSERT DO UPDATE works with 2 columns`() {
val file = FixtureCompiler.parseSql(
"""
|CREATE TABLE data (
| id INTEGER NOT NULL PRIMARY KEY,
| col1 TEXT DEFAULT '',
| col2 TEXT DEFAULT '',
| col3 TEXT DEFAULT ''
|);
|
|upsertCols:
|INSERT INTO data
|VALUES (:id, :c1, :c2, :c3)
|ON CONFLICT (id) DO UPDATE SET col1 = :c1, col2 = :c2;
""".trimMargin(),
tempFolder, dialectPreset = DialectPreset.POSTGRESQL
)

val insert = file.namedMutators.first()
val generator = MutatorQueryGenerator(insert)

Truth.assertThat(generator.function().toString()).isEqualTo(
"""
|public fun upsertCols(
| id: kotlin.Int?,
| c1: kotlin.String?,
| c2: kotlin.String?,
| c3: kotlin.String?
|): kotlin.Unit {
| driver.execute(${insert.id}, ""${'"'}
| |INSERT INTO data
| |VALUES (?, ?, ?, ?)
| |ON CONFLICT (id) DO UPDATE SET col1 = ?, col2 = ?
| ""${'"'}.trimMargin(), 6) {
| bindLong(1, id?.let { it.toLong() })
| bindString(2, c1)
| bindString(3, c2)
| bindString(4, c3)
| bindString(5, c1)
| bindString(6, c2)
| }
| notifyQueries(${insert.id}) { emit ->
| emit("data")
| }
|}
|
""".trimMargin()
)
}

@Test
fun `postgres INSERT DO UPDATE works with 3 columns`() {
val file = FixtureCompiler.parseSql(
Expand All @@ -79,7 +130,7 @@ class PgInsertOnConflictTest {

Truth.assertThat(generator.function().toString()).isEqualTo(
"""
|public fun upsert2Cols(
|public fun upsertCols(
| id: kotlin.Int?,
| c1: kotlin.String?,
| c2: kotlin.String?,
Expand Down

0 comments on commit a879a30

Please sign in to comment.