Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better version catalog support for dialects (#3435)
* 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
Showing
7 changed files
with
214 additions
and
1 deletion.
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
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
27 changes: 27 additions & 0 deletions
27
sqldelight-gradle-plugin/src/test/integration-catalog/build.gradle
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,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 | ||
} |
17 changes: 17 additions & 0 deletions
17
sqldelight-gradle-plugin/src/test/integration-catalog/settings.gradle
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,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") | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...st/integration-catalog/src/main/sqldelight/app/cash/sqldelight/mysql/integration/Dates.sq
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,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; | ||
} |
19 changes: 19 additions & 0 deletions
19
...test/integration-catalog/src/main/sqldelight/app/cash/sqldelight/mysql/integration/Dog.sq
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,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 ?; |
128 changes: 128 additions & 0 deletions
128
...st/integration-catalog/src/test/kotlin/app/cash/sqldelight/mysql/integration/MySqlTest.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,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) | ||
} |