From b17ef73ff8ae04d4c96f8694c56a58e83d1a2a9f Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 10 Mar 2025 23:04:34 -0400 Subject: [PATCH] WIP on Java integration test shell --- src/main/kotlin/Count.kt | 10 +++ src/main/kotlin/Document.kt | 6 ++ .../java/integration/common/Count.java | 20 +++++ .../documents/java/testDocs/JsonDocument.java | 81 +++++++++++++++++++ .../documents/java/testDocs/SubDocument.java | 32 ++++++++ 5 files changed, 149 insertions(+) create mode 100644 src/test/java/solutions/bitbadger/documents/java/integration/common/Count.java create mode 100644 src/test/java/solutions/bitbadger/documents/java/testDocs/JsonDocument.java create mode 100644 src/test/java/solutions/bitbadger/documents/java/testDocs/SubDocument.java diff --git a/src/main/kotlin/Count.kt b/src/main/kotlin/Count.kt index 970c865..3d4d26f 100644 --- a/src/main/kotlin/Count.kt +++ b/src/main/kotlin/Count.kt @@ -15,6 +15,7 @@ object Count { * @param conn The connection over which documents should be counted * @return A count of the documents in the table */ + @JvmStatic fun all(tableName: String, conn: Connection) = conn.customScalar(Count.all(tableName), mapFunc = Results::toCount) @@ -24,6 +25,7 @@ object Count { * @param tableName The name of the table in which documents should be counted * @return A count of the documents in the table */ + @JvmStatic fun all(tableName: String) = Configuration.dbConn().use { all(tableName, it) } @@ -36,6 +38,8 @@ object Count { * @param conn The connection on which the deletion should be executed * @return A count of the matching documents in the table */ + @JvmStatic + @JvmOverloads fun byFields( tableName: String, fields: Collection>, @@ -58,6 +62,8 @@ object Count { * @param howMatched How the fields should be matched * @return A count of the matching documents in the table */ + @JvmStatic + @JvmOverloads fun byFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null) = Configuration.dbConn().use { byFields(tableName, fields, howMatched, it) } @@ -70,6 +76,7 @@ object Count { * @return A count of the matching documents in the table * @throws DocumentException If called on a SQLite connection */ + @JvmStatic inline fun byContains(tableName: String, criteria: TContains, conn: Connection) = conn.customScalar(Count.byContains(tableName), listOf(Parameters.json(":criteria", criteria)), Results::toCount) @@ -81,6 +88,7 @@ object Count { * @return A count of the matching documents in the table * @throws DocumentException If called on a SQLite connection */ + @JvmStatic inline fun byContains(tableName: String, criteria: TContains) = Configuration.dbConn().use { byContains(tableName, criteria, it) } @@ -93,6 +101,7 @@ object Count { * @return A count of the matching documents in the table * @throws DocumentException If called on a SQLite connection */ + @JvmStatic fun byJsonPath(tableName: String, path: String, conn: Connection) = conn.customScalar( Count.byJsonPath(tableName), @@ -108,6 +117,7 @@ object Count { * @return A count of the matching documents in the table * @throws DocumentException If called on a SQLite connection */ + @JvmStatic fun byJsonPath(tableName: String, path: String) = Configuration.dbConn().use { byJsonPath(tableName, path, it) } } diff --git a/src/main/kotlin/Document.kt b/src/main/kotlin/Document.kt index fff4a3a..da06089 100644 --- a/src/main/kotlin/Document.kt +++ b/src/main/kotlin/Document.kt @@ -17,6 +17,7 @@ object Document { * @param document The document to be inserted * @param conn The connection on which the query should be executed */ + @JvmStatic inline fun insert(tableName: String, document: TDoc, conn: Connection) { val strategy = Configuration.autoIdStrategy val query = if (strategy == AutoId.DISABLED) { @@ -58,6 +59,7 @@ object Document { * @param tableName The table into which the document should be inserted (may include schema) * @param document The document to be inserted */ + @JvmStatic inline fun insert(tableName: String, document: TDoc) = Configuration.dbConn().use { insert(tableName, document, it) } @@ -68,6 +70,7 @@ object Document { * @param document The document to be saved * @param conn The connection on which the query should be executed */ + @JvmStatic inline fun save(tableName: String, document: TDoc, conn: Connection) = conn.customNonQuery(Document.save(tableName), listOf(Parameters.json(":data", document))) @@ -77,6 +80,7 @@ object Document { * @param tableName The table in which the document should be saved (may include schema) * @param document The document to be saved */ + @JvmStatic inline fun save(tableName: String, document: TDoc) = Configuration.dbConn().use { save(tableName, document, it) } @@ -88,6 +92,7 @@ object Document { * @param document The document to be replaced * @param conn The connection on which the query should be executed */ + @JvmStatic inline fun update(tableName: String, docId: TKey, document: TDoc, conn: Connection) = conn.customNonQuery( statementWhere(Document.update(tableName), Where.byId(":id", docId)), @@ -104,6 +109,7 @@ object Document { * @param docId The ID of the document to be replaced * @param document The document to be replaced */ + @JvmStatic inline fun update(tableName: String, docId: TKey, document: TDoc) = Configuration.dbConn().use { update(tableName, docId, document, it) } } diff --git a/src/test/java/solutions/bitbadger/documents/java/integration/common/Count.java b/src/test/java/solutions/bitbadger/documents/java/integration/common/Count.java new file mode 100644 index 0000000..8258386 --- /dev/null +++ b/src/test/java/solutions/bitbadger/documents/java/integration/common/Count.java @@ -0,0 +1,20 @@ +package solutions.bitbadger.documents.java.integration.common; + +import solutions.bitbadger.documents.integration.ThrowawayDatabase; +import solutions.bitbadger.documents.java.testDocs.JsonDocument; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static solutions.bitbadger.documents.integration.TypesKt.TEST_TABLE; + +final public class Count { + + public static void all(ThrowawayDatabase db) { + JsonDocument.load(db); + assertEquals(5L, solutions.bitbadger.documents.Count.all(TEST_TABLE, db.getConn()), + "There should have been 5 documents in the table"); + } + + + private Count() { + } +} diff --git a/src/test/java/solutions/bitbadger/documents/java/testDocs/JsonDocument.java b/src/test/java/solutions/bitbadger/documents/java/testDocs/JsonDocument.java new file mode 100644 index 0000000..be54944 --- /dev/null +++ b/src/test/java/solutions/bitbadger/documents/java/testDocs/JsonDocument.java @@ -0,0 +1,81 @@ +package solutions.bitbadger.documents.java.testDocs; + +import solutions.bitbadger.documents.Document; +import solutions.bitbadger.documents.integration.ThrowawayDatabase; + +import java.util.List; + +import static solutions.bitbadger.documents.integration.TypesKt.TEST_TABLE; + +public class JsonDocument { + + private String id; + private String value; + private int numValue; + private SubDocument sub; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public int getNumValue() { + return numValue; + } + + public void setNumValue(int numValue) { + this.numValue = numValue; + } + + public SubDocument getSub() { + return sub; + } + + public void setSub(SubDocument sub) { + this.sub = sub; + } + + public JsonDocument(String id, String value, int numValue, SubDocument sub) { + this.id = id; + this.value = value; + this.numValue = numValue; + this.sub = sub; + } + + public JsonDocument(String id, String value, int numValue) { + this(id, value, numValue, null); + } + + public JsonDocument(String id) { + this(id, "", 0, null); + } + + private static final List testDocuments = List.of( + new JsonDocument("one", "FIRST!", 0), + new JsonDocument("two", "another", 10, new SubDocument("green", "blue")), + new JsonDocument("three", "", 4), + new JsonDocument("four", "purple", 17, new SubDocument("green", "red")), + new JsonDocument("five", "purple", 18)); + + public static void load(ThrowawayDatabase db, String tableName) { + for (JsonDocument doc : testDocuments) { + // TODO: inline reified generics cannot be called from Java :( + // Document.insert(tableName, doc, db.getConn()); + } + } + + public static void load(ThrowawayDatabase db) { + load(db, TEST_TABLE); + } +} diff --git a/src/test/java/solutions/bitbadger/documents/java/testDocs/SubDocument.java b/src/test/java/solutions/bitbadger/documents/java/testDocs/SubDocument.java new file mode 100644 index 0000000..f0b46ec --- /dev/null +++ b/src/test/java/solutions/bitbadger/documents/java/testDocs/SubDocument.java @@ -0,0 +1,32 @@ +package solutions.bitbadger.documents.java.testDocs; + +public class SubDocument { + + private String foo; + private String bar; + + public String getFoo() { + return foo; + } + + public void setFoo(String foo) { + this.foo = foo; + } + + public String getBar() { + return bar; + } + + public void setBar(String bar) { + this.bar = bar; + } + + public SubDocument(String foo, String bar) { + this.foo = foo; + this.bar = bar; + } + + public SubDocument() { + this("", ""); + } +}