Skip to content

Commit

Permalink
Add tests for preferring Kotlin types
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Aug 11, 2023
1 parent 23ca781 commit 98d6c65
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ class QueriesTypeTest {
|SELECT *
|FROM soupView
|WHERE soup_token = ?;
|
|maxSoupBroth:
|SELECT MAX(soup_broth)
|FROM soupView;
""".trimMargin(),
temporaryFolder,
fileName = "MyView.sq",
Expand Down Expand Up @@ -798,6 +802,22 @@ class QueriesTypeTest {
| )
| }
|
| public fun <T : Any> maxSoupBroth(mapper: (MAX: ChickenSoupBase.Broth?) -> T): Query<T> =
| Query(-1_892_940_684, arrayOf("soupBase", "soup"), driver, "MyView.sq", "maxSoupBroth", ""${'"'}
| |SELECT MAX(soup_broth)
| |FROM soupView
| ""${'"'}.trimMargin()) { cursor ->
| mapper(
| cursor.getBytes(0)?.let { soupBaseAdapter.soup_brothAdapter.decode(it) }
| )
| }
|
| public fun maxSoupBroth(): Query<MaxSoupBroth> = maxSoupBroth { MAX ->
| MaxSoupBroth(
| MAX
| )
| }
|
| private inner class ForSoupTokenQuery<out T : Any>(
| public val soup_token: String,
| mapper: (SqlCursor) -> T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import app.cash.sqldelight.core.dialects.textType
import app.cash.sqldelight.test.util.FixtureCompiler
import com.google.common.truth.Truth.assertThat
import com.squareup.burst.BurstJUnit4
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DOUBLE
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.asClassName
import org.junit.Assume.assumeTrue
import org.junit.Rule
Expand Down Expand Up @@ -376,6 +378,79 @@ class ExpressionTest {
).inOrder()
}

@Test fun `coalesce returns the kotlin type for given homogeneous argument types`(dialect: TestDialect) {
val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| bar INTEGER AS Foo
|);
|
|someSelect:
|SELECT coalesce(foo, bar),
| coalesce(bar, bar),
| coalesce(foo, bar)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
ClassName("com.example", "Foo"),
ClassName("com.example", "Foo").copy(nullable = true),
ClassName("com.example", "Foo"),
).inOrder()
}

@Test fun `coalesce returns the dialect type for given heterogeneous argument types`(dialect: TestDialect) {
val dialectTextType = when {
dialect.dialect.isSqlite -> "TEXT"
else -> "VARCHAR(1)"
}

val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|import com.example.Bar;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| foo2 REAL AS Foo NOT NULL,
| bar INTEGER AS Bar,
| baz $dialectTextType
|);
|
|someSelect:
|SELECT coalesce(foo, bar),
| coalesce(foo, foo2),
| coalesce(foo, baz),
| coalesce(bar, baz),
| coalesce(foo, bar, baz)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val integerKotlinType = when (dialect) {
POSTGRESQL, HSQL, MYSQL -> INT
else -> LONG
}

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
integerKotlinType,
DOUBLE,
STRING,
STRING.copy(nullable = true),
STRING,
).inOrder()
}

@Test fun `max takes the proper type`(dialect: TestDialect) {
val file = FixtureCompiler.parseSql(
"""
Expand Down Expand Up @@ -414,6 +489,76 @@ class ExpressionTest {
).inOrder()
}

@Test fun `max returns the kotlin type for given heterogeneous argument types`(dialect: TestDialect) {
val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| bar INTEGER AS Foo
|);
|
|someSelect:
|SELECT max(foo),
| max(foo, bar)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
ClassName("com.example", "Foo").copy(nullable = true),
ClassName("com.example", "Foo").copy(nullable = true),
).inOrder()
}

@Test fun `max returns the kotlin type for given homogeneous argument types`(dialect: TestDialect) {
val dialectTextType = when {
dialect.dialect.isSqlite -> "TEXT"
else -> "VARCHAR(1)"
}
val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|import com.example.Bar;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| foo2 REAL AS Foo NOT NULL,
| bar INTEGER AS Bar,
| baz $dialectTextType
|);
|
|someSelect:
|SELECT max(foo, bar),
| max(foo, foo2),
| max(foo, baz),
| max(bar, baz),
| max(foo, bar, baz)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val integerKotlinType = when (dialect) {
POSTGRESQL, HSQL, MYSQL -> INT
else -> LONG
}

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
integerKotlinType.copy(nullable = true),
DOUBLE.copy(nullable = true),
STRING.copy(nullable = true),
STRING.copy(nullable = true),
STRING.copy(nullable = true),
).inOrder()
}

@Test fun `case expression part of limit infers type`() {
val file = FixtureCompiler.parseSql(
"""
Expand Down Expand Up @@ -472,6 +617,76 @@ class ExpressionTest {
).inOrder()
}

@Test fun `min returns the kotlin type for given heterogeneous argument types`(dialect: TestDialect) {
val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| bar INTEGER AS Foo
|);
|
|someSelect:
|SELECT min(foo),
| min(foo, bar)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
ClassName("com.example", "Foo").copy(nullable = true),
ClassName("com.example", "Foo").copy(nullable = true),
).inOrder()
}

@Test fun `min returns the kotlin type for given homogeneous argument types`(dialect: TestDialect) {
val dialectTextType = when {
dialect.dialect.isSqlite -> "TEXT"
else -> "VARCHAR(1)"
}
val file = FixtureCompiler.parseSql(
"""
|import com.example.Foo;
|import com.example.Bar;
|
|CREATE TABLE test (
| foo INTEGER AS Foo NOT NULL,
| foo2 REAL AS Foo NOT NULL,
| bar INTEGER AS Bar,
| baz $dialectTextType
|);
|
|someSelect:
|SELECT min(foo, bar),
| min(foo, foo2),
| min(foo, baz),
| min(bar, baz),
| min(foo, bar, baz)
|FROM test;
""".trimMargin(),
tempFolder,
dialect = dialect.dialect,
)

val integerKotlinType = when (dialect) {
POSTGRESQL, HSQL, MYSQL -> INT
else -> LONG
}

val query = file.namedQueries.first()
assertThat(query.resultColumns.map { it.javaType }).containsExactly(
integerKotlinType.copy(nullable = true),
DOUBLE.copy(nullable = true),
integerKotlinType.copy(nullable = true),
integerKotlinType.copy(nullable = true),
integerKotlinType.copy(nullable = true),
).inOrder()
}

@Test fun `arithmetic on nullable type`() {
val file = FixtureCompiler.parseSql(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.List;
import kotlin.Boolean;
import com.sample.Person;
import com.squareup.Redacted;

CREATE TABLE people (
_id INTEGER NOT NULL PRIMARY KEY,
person1 BLOB AS Person NOT NULL,
person2 BLOB AS Person
);

preferKotlinType:
SELECT MAX(person1, person2) AS max_person,
MIN(person1, person2) AS min_person,
COALESCE(person2, person1) AS coalesce_person,
IFNULL(person2, person1) AS if_null_person
FROM people;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sample

import com.sample.person

public data class PreferKotlinType(
public val max_person: Person?,
public val min_person: Person?,
public val coalesce_person: Person,
public val if_null_person: Person,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ FROM person;
CREATE VIEW personCool AS
SELECT *, CASE is_cool WHEN 1 THEN "cool" ELSE "lame" END AS how_cool
FROM person;

CREATE VIEW personAndMaxFriends AS
SELECT *, MAX( friends ) AS max_friends
FROM person;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sample

import com.squareup.Redacted
import java.util.List
import kotlin.Boolean
import kotlin.Long
import kotlin.String

public data class PersonAndMaxFriends(
public val _id: Long,
public val name: String,
public val last_name: String?,
public val is_cool: Boolean,
public val friends: List<Person>?,
@Redacted
public val shhh_its_secret: String,
public val how_cool: String,
public val max_friends: List<Person>?,
)

0 comments on commit 98d6c65

Please sign in to comment.