Tweak Scala code

This commit is contained in:
Daniel J. Summers 2025-04-01 14:01:56 -04:00
parent 40f8bded81
commit 515dbb2b1d
7 changed files with 19 additions and 29 deletions

View File

@ -23,13 +23,12 @@ object Parameters:
def nameFields(fields: Seq[Field[?]]): Seq[Field[?]] =
val name = ParameterName()
fields.map { it =>
if ((it.getParameterName == null || it.getParameterName.isEmpty)
&& !(Op.EXISTS :: Op.NOT_EXISTS :: Nil).contains(it.getComparison.getOp)) {
if (it.getParameterName == null || it.getParameterName.isEmpty)
&& !(Op.EXISTS :: Op.NOT_EXISTS :: Nil).contains(it.getComparison.getOp) then
it.withParameterName(name.derive(null))
} else {
else
it
}
}
/**
* Create a parameter by encoding a JSON object

View File

@ -25,10 +25,9 @@ class ConfigurationTest:
@Test
@DisplayName("Dialect is derived from connection string")
def dialectIsDerived(): Unit =
try {
try
assertThrows(classOf[DocumentException], () => Configuration.dialect())
Configuration.setConnectionString("jdbc:postgresql:db")
assertEquals(Dialect.POSTGRESQL, Configuration.dialect())
} finally {
finally
Configuration.setConnectionString(null)
}

View File

@ -22,13 +22,11 @@ class DialectTest:
@Test
@DisplayName("deriveFromConnectionString fails when the connection string is unknown")
def deriveFailsWhenUnknown(): Unit =
try {
try
Dialect.deriveFromConnectionString("SQL Server")
fail("Dialect derivation should have failed")
} catch {
catch
case ex: DocumentException =>
assertNotNull(ex.getMessage, "The exception message should not have been null")
assertTrue(ex.getMessage.contains("[SQL Server]"),
"The connection string should have been in the exception message")
}

View File

@ -64,7 +64,7 @@ class DocumentQueryTest:
@Test
@DisplayName("insert generates auto random string | PostgreSQL")
def insertAutoRandomPostgres(): Unit =
try {
try
ForceDialect.postgres()
Configuration.idStringLength = 8
val query = DocumentQuery.insert(TEST_TABLE, AutoId.RANDOM_STRING)
@ -74,9 +74,8 @@ class DocumentQueryTest:
assertEquals(8, query.replace(s"INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\"", "")
.replace("\"}')", "").length,
"Random string length incorrect")
} finally {
finally
Configuration.idStringLength = 16
}
@Test
@DisplayName("insert generates auto random string | SQLite")

View File

@ -23,7 +23,7 @@ object DocumentFunctions:
"Inserting a document with a duplicate key should have thrown an exception")
def insertNumAutoId(db: ThrowawayDatabase): Unit =
try {
try
Configuration.autoIdStrategy = AutoId.NUMBER
Configuration.idField = "key"
assertEquals(0L, db.conn.countAll(TEST_TABLE), "There should be no documents in the table")
@ -36,13 +36,12 @@ object DocumentFunctions:
val after = db.conn.findAll[NumIdDocument](TEST_TABLE, Field.named("key") :: Nil)
assertEquals(4, after.size, "There should have been 4 documents returned")
assertEquals("1|2|77|78", after.fold("") { (acc, item) => s"$acc|$item" }, "The IDs were not generated correctly")
} finally {
finally
Configuration.autoIdStrategy = AutoId.DISABLED
Configuration.idField = "id"
}
def insertUUIDAutoId(db: ThrowawayDatabase): Unit =
try {
try
Configuration.autoIdStrategy = AutoId.UUID
assertEquals(0L, db.conn.countAll(TEST_TABLE), "There should be no documents in the table")
@ -51,12 +50,11 @@ object DocumentFunctions:
val after = db.conn.findAll[JsonDocument](TEST_TABLE)
assertEquals(1, after.size, "There should have been 1 document returned")
assertEquals(32, after.head.id.length, "The ID was not generated correctly")
} finally {
finally
Configuration.autoIdStrategy = AutoId.DISABLED
}
def insertStringAutoId(db: ThrowawayDatabase): Unit =
try {
try
Configuration.autoIdStrategy = AutoId.RANDOM_STRING
assertEquals(0L, db.conn.countAll(TEST_TABLE), "There should be no documents in the table")
@ -69,10 +67,9 @@ object DocumentFunctions:
assertEquals(2, after.size, "There should have been 2 documents returned")
assertEquals(16, after.head.id.length, "The first document's ID was not generated correctly")
assertEquals(21, after(1).id.length, "The second document's ID was not generated correctly")
} finally {
finally
Configuration.autoIdStrategy = AutoId.DISABLED
Configuration.idStringLength = 16
}
def saveMatch(db: ThrowawayDatabase): Unit =
JsonDocument.load(db)

View File

@ -47,13 +47,12 @@ object FindFunctions:
def byIdNumber(db: ThrowawayDatabase): Unit =
Configuration.idField = "key"
try {
try
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy"))
val doc = db.conn.findById[Int, NumIdDocument](TEST_TABLE, 18)
assertTrue(doc.isDefined, "The document should have been returned")
} finally {
finally
Configuration.idField = "id"
}
def byIdNotFound(db: ThrowawayDatabase): Unit =
JsonDocument.load(db)

View File

@ -71,13 +71,12 @@ object JsonFunctions:
def byIdNumber (db: ThrowawayDatabase): Unit =
Configuration.idField = "key"
try {
try
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy"))
assertEquals(maybeJsonB("""{"key":18,"text":"howdy"}"""), db.conn.jsonById(TEST_TABLE, 18),
"The document should have been found by numeric ID")
} finally {
finally
Configuration.idField = "id"
}
def byIdNotFound (db: ThrowawayDatabase): Unit =
JsonDocument.load(db)