Skip to content

Commit

Permalink
Json Integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova committed Apr 16, 2022
1 parent 48e8976 commit b15a196
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.gradle
/.idea
**/.idea
sqldelight-gradle-plugin/**/gradle
sqldelight-gradle-plugin/**/gradlew
sqldelight-gradle-plugin/**/gradlew.bat
*.iml
build
local.properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import com.intellij.psi.PsiElement
internal abstract class TableOrSubqueryMixin(node: ASTNode?) : SqlTableOrSubqueryImpl(node), SqliteJsonTableOrSubquery {
private val queryExposed = ModifiableFileLazy lazy@{
if (jsonFunctionName != null) {
return@lazy listOf(QueryResult(
table = jsonFunctionName!!,
columns = emptyList(),
synthesizedColumns = listOf(
SynthesizedColumn(jsonFunctionName!!, acceptableValues = listOf("key", "value", "type", "atom", "id", "parent", "fullkey", "path", "json", "root"))
return@lazy listOf(
QueryResult(
table = jsonFunctionName!!,
columns = emptyList(),
synthesizedColumns = listOf(
SynthesizedColumn(jsonFunctionName!!, acceptableValues = listOf("key", "value", "type", "atom", "id", "parent", "fullkey", "path", "json", "root"))
)
)
))
)
}
super.queryExposed()
}
Expand All @@ -42,9 +44,9 @@ internal abstract class TableOrSubqueryMixin(node: ASTNode?) : SqlTableOrSubquer
}
}

internal abstract class JsonFunctionNameMixin(node: ASTNode): SqlNamedElementImpl(node), SqlTableName, ExposableType {
internal abstract class JsonFunctionNameMixin(node: ASTNode) : SqlNamedElementImpl(node), SqlTableName, ExposableType {
override fun getId(): PsiElement? = null
override fun getString(): PsiElement? = null
override val parseRule: (PsiBuilder, Int) -> Boolean = JsonParser::json_function_name_real
override fun type() = IntermediateType(PrimitiveType.TEXT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class JsonModuleTest(name: String, fixtureRoot: File) : FixturesTest(name, fixtu
.map { arrayOf(it.name, it) }
} + ansiFixtures
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import com.alecstrong.sql.psi.core.psi.SqlAnnotatedElement

interface ExposableType : SqlAnnotatedElement {
fun type(): IntermediateType
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.cash.sqldelight.core

import app.cash.sqldelight.core.lang.types.typeResolver
import app.cash.sqldelight.core.lang.util.argumentType
import app.cash.sqldelight.core.lang.util.findChildrenOfType
import app.cash.sqldelight.core.lang.util.isArrayParameter
import app.cash.sqldelight.dialect.api.PrimitiveType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
buildscript {
apply from: "${projectDir.absolutePath}/../buildscript.gradle"
}

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

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

sqldelight {
QueryWrapper {
packageName = "app.cash.sqldelight.integration"
dialect("app.cash.sqldelight:sqlite-3-18-dialect:${app.cash.sqldelight.VersionKt.VERSION}")
module("app.cash.sqldelight:sqlite-json-module:${app.cash.sqldelight.VersionKt.VERSION}")
}
}

dependencies {
implementation deps.sqliteJdbc
implementation "app.cash.sqldelight:sqlite-driver:${app.cash.sqldelight.VersionKt.VERSION}"
implementation deps.truth
implementation("com.squareup.moshi:moshi:1.13.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apply from: "../settings.gradle"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE user(
name TEXT NOT NULL PRIMARY KEY,
phone TEXT
);

insertUser:
INSERT INTO user
VALUES (?, ?);

byAreaCode:
SELECT DISTINCT user.name
FROM user, json_each(user.phone)
WHERE json_each.value LIKE :areaCode || '-%';

byAreaCode2:
SELECT name FROM user WHERE phone LIKE :areaCode || '-%'
UNION
SELECT user.name
FROM user, json_each(user.phone)
WHERE json_valid(user.phone)
AND json_each.value LIKE :areaCode || '-%';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app.cash.sqldelight.integration

import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver.Companion.IN_MEMORY
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.adapter
import org.junit.Before
import org.junit.Test

class IntegrationTests {
private val moshi = Moshi.Builder().build()

private lateinit var queryWrapper: QueryWrapper
private lateinit var jsonQueries: JsonTableQueries

@Before fun before() {
val database = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
QueryWrapper.Schema.create(database)

queryWrapper = QueryWrapper(database)
jsonQueries = queryWrapper.jsonTableQueries
}

@Test fun jsonArray() {
with(jsonQueries) {
insertUser("user1", jsonPhones("704-555-5555", "705-555-5555"))
insertUser("user2", jsonPhones("604-555-5555", "605-555-5555"))
assertThat(byAreaCode(areaCode = "704").executeAsList()).containsExactly(
"user1"
)
}
}

@Test fun jsonArrayOrLiteral() {
with(jsonQueries) {
insertUser("user1", jsonPhones("704-555-5555", "705-555-5555"))
insertUser("user2", jsonPhones("604-555-5555", "605-555-5555"))
insertUser("user3", "704-666-6666")
assertThat(byAreaCode2(areaCode = "704").executeAsList()).containsExactly(
"user1",
"user3"
)
}
}

private fun jsonPhones(vararg phoneNumbers: String): String {
val adapter =
moshi.adapter<List<String>>(Types.newParameterizedType(List::class.java, String::class.java))
return adapter.toJson(phoneNumbers.toList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class IntegrationTest {
assertThat(result.output).contains("BUILD SUCCESSFUL")
}

@Test fun integrationTestsSqliteJsonModule() {
val runner = GradleRunner.create()
.withCommonConfiguration(File("src/test/integration-sqlite-json"))
.withArguments("clean", "check", "--stacktrace")

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

@Test fun migrationCallbackIntegrationTests() {
val runner = GradleRunner.create()
.withCommonConfiguration(File("src/test/integration-migration-callbacks"))
Expand Down

0 comments on commit b15a196

Please sign in to comment.