Skip to content

Commit

Permalink
Add better version catalog support for dialects (#3435)
Browse files Browse the repository at this point in the history
* Add better version catalog support for dialects

* Use better api to add dialect dependency

Add integration test for version catalogs

* Move catalog test into dockerTest

* Spotless apply

* Correctly resolve dialect module in test
  • Loading branch information
dellisd committed Aug 12, 2022
1 parent f57c07b commit 66d432c
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 1 deletion.
Expand Up @@ -33,6 +33,15 @@ class DialectIntegrationTests {
val result = runner.build()
Truth.assertThat(result.output).contains("BUILD SUCCESSFUL")
}

@Test fun `dialect accepts version catalog dependency`() {
val runner = GradleRunner.create()
.withCommonConfiguration(File("src/test/integration-catalog"))
.withArguments("clean", "check", "--stacktrace")

val result = runner.build()
Truth.assertThat(result.output).contains("BUILD SUCCESSFUL")
}
}

private fun GradleRunner.withCommonConfiguration(projectRoot: File): GradleRunner {
Expand Down
Expand Up @@ -41,7 +41,7 @@ class SqlDelightDatabase(

fun dialect(dialect: Any) {
if (addedDialect) throw IllegalStateException("Can only set a single dialect.")
configuration.dependencies.add(project.dependencies.create(dialect))
project.dependencies.add(configuration.name, dialect)
addedDialect = true
}

Expand Down
27 changes: 27 additions & 0 deletions sqldelight-gradle-plugin/src/test/integration-catalog/build.gradle
@@ -0,0 +1,27 @@
buildscript {
apply from: "${projectDir.absolutePath}/../buildscript.gradle"
}

apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'app.cash.sqldelight'

sqldelight {
MyDatabase {
packageName = "app.cash.sqldelight.mysql.integration"
dialect(libs.sqldelight.mysql)
}
}

repositories {
maven {
url "file://${projectDir.absolutePath}/../../../../build/localMaven"
}
mavenCentral()
}

dependencies {
implementation deps.mysqlJdbc
implementation "org.testcontainers:mysql:1.16.2"
implementation "app.cash.sqldelight:jdbc-driver:${app.cash.sqldelight.VersionKt.VERSION}"
implementation deps.truth
}
@@ -0,0 +1,17 @@
apply from: "../settings.gradle"

rootProject.name = 'sqldelight-mysql-integration'

dependencyResolutionManagement {
versionCatalogs {
libs {
library('sqldelight-mysql', 'app.cash.sqldelight', 'mysql-dialect').withoutVersion()
}
}
}

includeBuild("../../../../") {
dependencySubstitution {
substitute module("app.cash.sqldelight:mysql-dialect") using project(":dialects:mysql")
}
}
@@ -0,0 +1,13 @@
CREATE TABLE dates(
time TIME NOT NULL,
date DATE NOT NULL,
datetime DATETIME NOT NULL,
timestamp TIMESTAMP NOT NULL,
year YEAR NOT NULL
);

insertDate {
INSERT INTO dates VALUES (?, ?, ?, ?, ?);

SELECT * FROM dates LIMIT 1;
}
@@ -0,0 +1,19 @@
CREATE TABLE dog (
name VARCHAR(8) NOT NULL,
breed TEXT NOT NULL,
is_good BOOLEAN NOT NULL DEFAULT 1
);

insertDog:
INSERT INTO dog
VALUES (?, ?, ?);

selectDogs:
SELECT *
FROM dog;

selectDogsByBreedAndNames:
SELECT *
FROM dog
WHERE breed = ?
AND name IN ?;
@@ -0,0 +1,128 @@
package app.cash.sqldelight.mysql.integration

import app.cash.sqldelight.Query
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.JdbcDriver
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.sql.Connection
import java.sql.DriverManager
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

class MySqlTest {
lateinit var connection: Connection
lateinit var dogQueries: DogQueries
lateinit var datesQueries: DatesQueries
lateinit var driver: JdbcDriver

@Before
fun before() {
connection = DriverManager.getConnection("jdbc:tc:mysql:///myDb")
driver = object : JdbcDriver() {
override fun getConnection() = connection
override fun closeConnection(connection: Connection) = Unit
override fun addListener(listener: Query.Listener, queryKeys: Array<String>) = Unit
override fun removeListener(listener: Query.Listener, queryKeys: Array<String>) = Unit
override fun notifyListeners(queryKeys: Array<String>) = Unit
}
val database = MyDatabase(driver)

MyDatabase.Schema.create(driver)
dogQueries = database.dogQueries
datesQueries = database.datesQueries
}

@After
fun after() {
connection.close()
}

@Test fun simpleSelect() {
dogQueries.insertDog("Tilda", "Pomeranian", true)
assertThat(dogQueries.selectDogs().executeAsOne())
.isEqualTo(
Dog(
name = "Tilda",
breed = "Pomeranian",
is_good = true,
),
)
}

@Test
fun simpleSelectWithIn() {
dogQueries.insertDog("Tilda", "Pomeranian", true)
dogQueries.insertDog("Tucker", "Portuguese Water Dog", true)
dogQueries.insertDog("Cujo", "Pomeranian", false)
dogQueries.insertDog("Buddy", "Pomeranian", true)
assertThat(
dogQueries.selectDogsByBreedAndNames(
breed = "Pomeranian",
name = listOf("Tilda", "Buddy"),
).executeAsList(),
)
.containsExactly(
Dog(
name = "Tilda",
breed = "Pomeranian",
is_good = true,
),
Dog(
name = "Buddy",
breed = "Pomeranian",
is_good = true,
),
)
}

@Test
fun testDates() {
with(
datesQueries.insertDate(
date = LocalDate.of(2020, 1, 1),
time = LocalTime.of(21, 30, 59),
datetime = LocalDateTime.of(2020, 1, 1, 21, 30, 59),
timestamp = OffsetDateTime.of(1980, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)),
year = "2022",
).executeAsOne(),
) {
assertThat(date).isEqualTo(LocalDate.of(2020, 1, 1))
assertThat(time).isEqualTo(LocalTime.of(21, 30, 59))
assertThat(datetime).isEqualTo(LocalDateTime.of(2020, 1, 1, 21, 30, 59))

assertThat(timestamp.format(DateTimeFormatter.ISO_LOCAL_DATE))
.isEqualTo(OffsetDateTime.of(1980, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)).format(DateTimeFormatter.ISO_LOCAL_DATE))
assertThat(year).isEqualTo("2022-01-01")
}
}

@Test
fun transactionCrashRollsBack() {
val transacter = SqlDriverTransacter(driver)

try {
transacter.transaction {
driver.execute(null, "CREATE TABLE throw_test(some Text)", 0, null)
afterRollback { driver.execute(null, "DROP TABLE throw_test", 0, null) }
throw ExpectedException()
}
Assert.fail()
} catch (_: ExpectedException) {
transacter.transaction {
driver.execute(null, "CREATE TABLE throw_test(some Text)", 0, null)
}
}
}

private class ExpectedException : Exception()
private class SqlDriverTransacter(driver: SqlDriver) : TransacterImpl(driver)
}

0 comments on commit 66d432c

Please sign in to comment.