-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the SQL Server default keyword as default value for a column (#…
…1207) / Move test to a separate file
- Loading branch information
Showing
2 changed files
with
60 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
exposed-java-time/src/test/kotlin/org/jetbrains/exposed/sqlserver/SQLServerDefaultsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.jetbrains.exposed.sqlserver | ||
|
||
import org.hamcrest.CoreMatchers.`is` | ||
import org.hamcrest.CoreMatchers.notNullValue | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.`java-time`.* | ||
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase | ||
import org.jetbrains.exposed.sql.tests.TestDB | ||
import org.junit.Test | ||
import java.time.* | ||
|
||
class SQLServerDefaultsTest : DatabaseTestsBase() { | ||
|
||
@Test | ||
fun testDefaultExpressionsForTemporalTable() { | ||
|
||
fun databaseGeneratedTimestamp() = object : ExpressionWithColumnType<LocalDateTime>() { | ||
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { +"DEFAULT" } | ||
override val columnType: IColumnType = JavaLocalDateTimeColumnType() | ||
} | ||
|
||
val temporalTable = object : UUIDTable("TemporalTable") { | ||
val name = text("name") | ||
val sysStart = datetime("sysStart").defaultExpression(databaseGeneratedTimestamp()) | ||
val sysEnd = datetime("sysEnd").defaultExpression(databaseGeneratedTimestamp()) | ||
} | ||
|
||
withDb(TestDB.SQLSERVER) { | ||
try { | ||
exec( | ||
""" | ||
CREATE TABLE TemporalTable | ||
( | ||
id uniqueidentifier PRIMARY KEY, | ||
"name" VARCHAR(100) NOT NULL, | ||
sysStart DATETIME2 GENERATED ALWAYS AS ROW START, | ||
sysEnd DATETIME2 GENERATED ALWAYS AS ROW END, | ||
PERIOD FOR SYSTEM_TIME ([sysStart], [sysEnd]) | ||
) | ||
""".trimIndent() | ||
) | ||
|
||
val names = listOf("name") | ||
val batchInsert: List<ResultRow> = | ||
temporalTable.batchInsert(names, shouldReturnGeneratedValues = true) { name -> | ||
this[temporalTable.name] = "name" | ||
} | ||
val id = batchInsert.first()[temporalTable.id] | ||
val result = temporalTable.select { temporalTable.id eq id }.single() | ||
assertThat(result[temporalTable.name], `is`("name")) | ||
assertThat(result[temporalTable.sysStart], notNullValue()) | ||
assertThat(result[temporalTable.sysEnd], notNullValue()) | ||
} finally { | ||
SchemaUtils.drop(temporalTable) | ||
} | ||
} | ||
} | ||
} |