Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write two tests that catch a possible bug with SQLite + batchInsert #2116

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.jetbrains.exposed.sql.tests.shared.dml

import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.times
import org.jetbrains.exposed.sql.statements.ReturningStatement
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -157,4 +160,27 @@ class ReturningTests : DatabaseTestsBase() {
assertTrue { result3.all { it == 0.0 } }
}
}

object User : IdTable<String>("user") {
val username = varchar("username", 128)
override val primaryKey: PrimaryKey = PrimaryKey(username)
override val id: Column<EntityID<String>> = username.entityId()
}

@Test
fun testBatchInsertReturningCustomPrimaryKey() {
withTables(TestDB.enabledDialects() - returningSupportedDb, User) {
val result1 = User.batchInsert(listOf("batman")) { username ->
this[User.username] = username
}.single()
assertEquals("batman", result1[User.id].value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails, because result1[User.id].value is 1 on SQLite

assertEquals("batman", result1[User.username])

val result2 = User.batchInsert(listOf("superman")) { username ->
this[User.username] = username
}.single()
assertEquals("superman", result2[User.username])
assertEquals("superman", result2[User.id].value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails, because result2[User.username] caches the return value as a String, meaning result2[User.id].value produces a ClassCastException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably should be a separate PR, because this is not related to SQLite specifically - this fails on any database

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created another https://youtrack.jetbrains.com/issue/EXPOSED-409/Custom-primary-key.-Access-to-the-primary-key-fails-with-ClassCastException issue for the second problem. It's clear why it happens, I'll try to fix it a bit later.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,17 @@ class EntityTests : DatabaseTestsBase() {
}
}

@Test
fun testIdValueIsTheSameAsCustomPrimaryKeyColumn() {
withTables(RequestsTable) {
val request = Request.new {
requestId = "123"
}

assertEquals("123", request.id.value)
}
}

object CreditCards : IntIdTable("CreditCards") {
val number = varchar("number", 16)
val spendingLimit = ulong("spendingLimit").databaseGenerated()
Expand Down
Loading