WIP on integration tests
This commit is contained in:
parent
448a9646d6
commit
93f4700613
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/documents.iml" filepath="$PROJECT_DIR$/documents.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
8
documents.iml
Normal file
8
documents.iml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="AdditionalModuleElements">
|
||||
<content url="file://$MODULE_DIR$" dumb="true">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/integration-test/kotlin" isTestSource="true" />
|
||||
</content>
|
||||
</component>
|
||||
</module>
|
43
src/integration-test/kotlin/CustomSQLiteIT.kt
Normal file
43
src/integration-test/kotlin/CustomSQLiteIT.kt
Normal file
@ -0,0 +1,43 @@
|
||||
package solutions.bitbadger.documents
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import solutions.bitbadger.documents.query.Definition
|
||||
import solutions.bitbadger.documents.query.Find
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
class CustomSQLiteIT {
|
||||
|
||||
private val tbl = "test_table";
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
Configuration.connectionString = "jdbc:sqlite:memory"
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the dialect
|
||||
*/
|
||||
@AfterEach
|
||||
fun cleanUp() {
|
||||
Configuration.dialectValue = null
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("list succeeds with empty list")
|
||||
fun listEmpty() {
|
||||
Configuration.dbConn().use { conn ->
|
||||
conn.customNonQuery(Definition.ensureTable(tbl), listOf())
|
||||
conn.customNonQuery(Definition.ensureKey(tbl, Dialect.SQLITE), listOf())
|
||||
val result = conn.customList<TestDocument>(Find.all(tbl), listOf(), Results::fromData)
|
||||
assertEquals(0, result.size, "There should have been no results")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TestDocument(val id: String)
|
@ -11,11 +11,9 @@ import java.sql.ResultSet
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
*/
|
||||
inline fun <reified TDoc> Connection.customList(query: String, parameters: Collection<Parameter<*>>,
|
||||
mapFunc: (ResultSet) -> TDoc): List<TDoc> =
|
||||
Parameters.apply(this, query, parameters).use { stmt ->
|
||||
Results.toCustomList(stmt, mapFunc)
|
||||
}
|
||||
inline fun <reified TDoc> Connection.customList(
|
||||
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
|
||||
) = Custom.list(query, parameters, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
@ -25,9 +23,9 @@ inline fun <reified TDoc> Connection.customList(query: String, parameters: Colle
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
*/
|
||||
inline fun <reified TDoc> Connection.customSingle(query: String, parameters: Collection<Parameter<*>>,
|
||||
mapFunc: (ResultSet) -> TDoc): TDoc? =
|
||||
this.customList("$query LIMIT 1", parameters, mapFunc).singleOrNull()
|
||||
inline fun <reified TDoc> Connection.customSingle(
|
||||
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
|
||||
) = Custom.single(query, parameters, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns no results
|
||||
@ -35,6 +33,19 @@ inline fun <reified TDoc> Connection.customSingle(query: String, parameters: Col
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
*/
|
||||
fun Connection.customNonQuery(query: String, parameters: Collection<Parameter<*>>) {
|
||||
Parameters.apply(this, query, parameters).use { it.executeUpdate() }
|
||||
}
|
||||
fun Connection.customNonQuery(query: String, parameters: Collection<Parameter<*>>) =
|
||||
Custom.nonQuery(query, parameters, this)
|
||||
|
||||
/**
|
||||
* Execute a query that returns a scalar result
|
||||
*
|
||||
* @param query The query to retrieve the result
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The scalar value from the query
|
||||
*/
|
||||
inline fun <reified T> Connection.customScalar(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>>,
|
||||
mapFunc: (ResultSet) -> T & Any
|
||||
) = Custom.scalar(query, parameters, this, mapFunc)
|
||||
|
110
src/main/kotlin/Custom.kt
Normal file
110
src/main/kotlin/Custom.kt
Normal file
@ -0,0 +1,110 @@
|
||||
package solutions.bitbadger.documents
|
||||
|
||||
import java.sql.Connection
|
||||
import java.sql.ResultSet
|
||||
|
||||
/**
|
||||
* Custom query execution functions
|
||||
*/
|
||||
object Custom {
|
||||
|
||||
/**
|
||||
* Execute a query that returns a list of results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
*/
|
||||
inline fun <reified TDoc> list(
|
||||
query: String, parameters: Collection<Parameter<*>>, conn: Connection, mapFunc: (ResultSet) -> TDoc
|
||||
) = Parameters.apply(conn, query, parameters).use { Results.toCustomList(it, mapFunc) }
|
||||
|
||||
/**
|
||||
* Execute a query that returns a list of results (creates connection)
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
*/
|
||||
inline fun <reified TDoc> list(
|
||||
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
|
||||
) = Configuration.dbConn().use { list(query, parameters, it, mapFunc) }
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
*/
|
||||
inline fun <reified TDoc> single(
|
||||
query: String, parameters: Collection<Parameter<*>>, conn: Connection, mapFunc: (ResultSet) -> TDoc
|
||||
) = list("$query LIMIT 1", parameters, conn, mapFunc).singleOrNull()
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
*/
|
||||
inline fun <reified TDoc> single(
|
||||
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
|
||||
) = Configuration.dbConn().use { single(query, parameters, it, mapFunc) }
|
||||
|
||||
/**
|
||||
* Execute a query that returns no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param parameters Parameters to use for the query
|
||||
*/
|
||||
fun nonQuery(query: String, parameters: Collection<Parameter<*>>, conn: Connection) {
|
||||
Parameters.apply(conn, query, parameters).use { it.executeUpdate() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
*/
|
||||
fun nonQuery(query: String, parameters: Collection<Parameter<*>>) =
|
||||
Configuration.dbConn().use { nonQuery(query, parameters, it) }
|
||||
|
||||
/**
|
||||
* Execute a query that returns a scalar result
|
||||
*
|
||||
* @param query The query to retrieve the result
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The scalar value from the query
|
||||
*/
|
||||
inline fun <reified T> scalar(
|
||||
query: String, parameters: Collection<Parameter<*>>, conn: Connection, mapFunc: (ResultSet) -> T & Any
|
||||
) = Parameters.apply(conn, query, parameters).use { stmt ->
|
||||
stmt.executeQuery().use { rs ->
|
||||
rs.next()
|
||||
mapFunc(rs)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns a scalar result
|
||||
*
|
||||
* @param query The query to retrieve the result
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The scalar value from the query
|
||||
*/
|
||||
inline fun <reified T> scalar(
|
||||
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> T & Any
|
||||
) = Configuration.dbConn().use { scalar(query, parameters, it, mapFunc) }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user