WIP on KotlinX Json functions
This commit is contained in:
parent
a2b5d8f328
commit
a2fae8b679
@ -96,7 +96,6 @@ object Custom {
|
||||
* @param writer The writer to which the results should be written
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @return A JSON array of results for the given query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@ -116,7 +115,6 @@ object Custom {
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param writer The writer to which the results should be written
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @return A JSON array of results for the given query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
|
@ -2,6 +2,7 @@ package solutions.bitbadger.documents.kotlinx
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.Configuration
|
||||
import java.io.PrintWriter
|
||||
import solutions.bitbadger.documents.java.Custom as CoreCustom
|
||||
import java.sql.Connection
|
||||
import java.sql.ResultSet
|
||||
@ -70,6 +71,40 @@ object Custom {
|
||||
fun jsonArray(query: String, parameters: Collection<Parameter<*>> = listOf(), mapFunc: (ResultSet) -> String) =
|
||||
CoreCustom.jsonArray(query, parameters, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query, writing its JSON array of results to the given `PrintWriter`
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param writer The writer to which the results should be written
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
fun writeJsonArray(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
writer: PrintWriter,
|
||||
conn: Connection,
|
||||
mapFunc: (ResultSet) -> String
|
||||
) = CoreCustom.writeJsonArray(query, parameters, writer, conn, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query, writing its JSON array of results to the given `PrintWriter` (creates connection)
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param writer The writer to which the results should be written
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
fun writeJsonArray(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
writer: PrintWriter,
|
||||
mapFunc: (ResultSet) -> String
|
||||
) = CoreCustom.writeJsonArray(query, parameters, writer, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
*
|
||||
|
@ -1,12 +1,17 @@
|
||||
package solutions.bitbadger.documents.kotlinx.tests.integration
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.java.extensions.countAll
|
||||
import solutions.bitbadger.documents.java.extensions.insert
|
||||
import solutions.bitbadger.documents.java.extensions.writeCustomJsonArray
|
||||
import solutions.bitbadger.documents.kotlinx.Results
|
||||
import solutions.bitbadger.documents.kotlinx.extensions.*
|
||||
import solutions.bitbadger.documents.kotlinx.tests.ArrayDocument
|
||||
import solutions.bitbadger.documents.kotlinx.tests.JsonDocument
|
||||
import solutions.bitbadger.documents.kotlinx.tests.TEST_TABLE
|
||||
import solutions.bitbadger.documents.query.*
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
@ -59,6 +64,40 @@ object CustomFunctions {
|
||||
)
|
||||
}
|
||||
|
||||
fun writeJsonArrayEmpty(db: ThrowawayDatabase) {
|
||||
assertEquals(0L, db.conn.countAll(TEST_TABLE), "The test table should be empty")
|
||||
val output = StringWriter()
|
||||
val writer = PrintWriter(output)
|
||||
db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE), listOf(), writer, Results::jsonFromData)
|
||||
assertEquals("[]", output.toString(), "An empty list was not represented correctly")
|
||||
}
|
||||
|
||||
fun writeJsonArraySingle(db: ThrowawayDatabase) {
|
||||
db.conn.insert(TEST_TABLE, ArrayDocument("one", listOf("2", "3")))
|
||||
val output = StringWriter()
|
||||
val writer = PrintWriter(output)
|
||||
db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE), listOf(), writer, Results::jsonFromData)
|
||||
assertEquals(
|
||||
JsonFunctions.maybeJsonB("""[{"id":"one","values":["2","3"]}]"""),
|
||||
output.toString(),
|
||||
"A single document list was not represented correctly"
|
||||
)
|
||||
}
|
||||
|
||||
fun writeJsonArrayMany(db: ThrowawayDatabase) {
|
||||
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
|
||||
val output = StringWriter()
|
||||
val writer = PrintWriter(output)
|
||||
db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE) + orderBy(listOf(Field.named("id"))), listOf(), writer,
|
||||
Results::jsonFromData)
|
||||
assertEquals(
|
||||
JsonFunctions.maybeJsonB("""[{"id":"first","values":["a","b","c"]},"""
|
||||
+ """{"id":"second","values":["c","d","e"]},{"id":"third","values":["x","y","z"]}]"""),
|
||||
output.toString(),
|
||||
"A multiple document list was not represented correctly"
|
||||
)
|
||||
}
|
||||
|
||||
fun singleNone(db: ThrowawayDatabase) =
|
||||
assertNull(
|
||||
db.conn.customSingle(FindQuery.all(TEST_TABLE), mapFunc = Results::fromData),
|
||||
|
@ -6,6 +6,8 @@ import solutions.bitbadger.documents.Field
|
||||
import solutions.bitbadger.documents.FieldMatch
|
||||
import solutions.bitbadger.documents.kotlinx.extensions.*
|
||||
import solutions.bitbadger.documents.kotlinx.tests.*
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -42,9 +44,7 @@ object JsonFunctions {
|
||||
private fun docId(id: String) =
|
||||
maybeJsonB("{\"id\":\"$id\"")
|
||||
|
||||
fun allDefault(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val json = db.conn.jsonAll(TEST_TABLE)
|
||||
private fun checkAllDefault(json: String) {
|
||||
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
|
||||
when (Configuration.dialect()) {
|
||||
Dialect.SQLITE -> {
|
||||
@ -65,6 +65,19 @@ object JsonFunctions {
|
||||
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)")
|
||||
}
|
||||
|
||||
fun allDefault(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
checkAllDefault(db.conn.jsonAll(TEST_TABLE))
|
||||
}
|
||||
|
||||
// fun writeAllDefault(db: ThrowawayDatabase) {
|
||||
// JsonDocument.load(db)
|
||||
// val output = StringWriter()
|
||||
// val writer = PrintWriter(output)
|
||||
// db.conn.writeJsonAll(TEST_TABLE, writer)
|
||||
// checkAllDefault(output.toString())
|
||||
// }
|
||||
|
||||
fun allEmpty(db: ThrowawayDatabase) =
|
||||
assertEquals("[]", db.conn.jsonAll(TEST_TABLE), "There should have been no documents returned")
|
||||
|
||||
|
@ -35,6 +35,21 @@ class PostgreSQLCustomIT {
|
||||
fun jsonArrayMany() =
|
||||
PgDB().use(CustomFunctions::jsonArrayMany)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with empty array")
|
||||
fun writeJsonArrayEmpty() =
|
||||
PgDB().use(CustomFunctions::writeJsonArrayEmpty)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with a single-item array")
|
||||
fun writeJsonArraySingle() =
|
||||
PgDB().use(CustomFunctions::writeJsonArraySingle)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with a multi-item array")
|
||||
fun writeJsonArrayMany() =
|
||||
PgDB().use(CustomFunctions::writeJsonArrayMany)
|
||||
|
||||
@Test
|
||||
@DisplayName("single succeeds when document not found")
|
||||
fun singleNone() =
|
||||
|
@ -34,6 +34,21 @@ class SQLiteCustomIT {
|
||||
fun jsonArrayMany() =
|
||||
SQLiteDB().use(CustomFunctions::jsonArrayMany)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with empty array")
|
||||
fun writeJsonArrayEmpty() =
|
||||
SQLiteDB().use(CustomFunctions::writeJsonArrayEmpty)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with a single-item array")
|
||||
fun writeJsonArraySingle() =
|
||||
SQLiteDB().use(CustomFunctions::writeJsonArraySingle)
|
||||
|
||||
@Test
|
||||
@DisplayName("writeJsonArray succeeds with a multi-item array")
|
||||
fun writeJsonArrayMany() =
|
||||
SQLiteDB().use(CustomFunctions::writeJsonArrayMany)
|
||||
|
||||
@Test
|
||||
@DisplayName("single succeeds when document not found")
|
||||
fun singleNone() =
|
||||
|
Loading…
x
Reference in New Issue
Block a user