101 lines
5.4 KiB
Scala

package solutions.bitbadger.documents.scala.tests
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.{AfterEach, DisplayName, Test}
import solutions.bitbadger.documents.{DocumentException, Field, Parameter, ParameterType}
import solutions.bitbadger.documents.scala.Parameters
@DisplayName("Scala | Parameters")
class ParametersTest:
/**
* Reset the dialect
*/
@AfterEach
def cleanUp(): Unit =
ForceDialect.none()
@Test
@DisplayName("nameFields works with no changes")
def nameFieldsNoChange(): Unit =
val fields = Field.equal("a", "", ":test") :: Field.exists("q") :: Field.equal("b", "", ":me") :: Nil
val named = Parameters.nameFields(fields).toList
assertEquals(fields.size, named.size, "There should have been 3 fields in the list")
assertSame(fields.head, named.head, "The first field should be the same")
assertSame(fields(1), named(1), "The second field should be the same")
assertSame(fields(2), named(2), "The third field should be the same")
@Test
@DisplayName("nameFields works when changing fields")
def nameFieldsChange(): Unit =
val fields = Field.equal("a", "") :: Field.equal("e", "", ":hi") :: Field.equal("b", "") ::
Field.notExists("z") :: Nil
val named = Parameters.nameFields(fields).toList
assertEquals(fields.size, named.size, "There should have been 4 fields in the list")
assertNotSame(fields.head, named.head, "The first field should not be the same")
assertEquals(":field0", named.head.getParameterName, "First parameter name incorrect")
assertSame(fields(1), named(1), "The second field should be the same")
assertNotSame(fields(2), named(2), "The third field should not be the same")
assertEquals(":field1", named(2).getParameterName, "Third parameter name incorrect")
assertSame(fields(3), named(3), "The fourth field should be the same")
@Test
@DisplayName("replaceNamesInQuery replaces successfully")
def replaceNamesInQuery(): Unit =
val parameters = Parameter(":data", ParameterType.JSON, "{}") ::
Parameter(":data_ext", ParameterType.STRING, "") :: Nil
val query = "SELECT data, data_ext FROM tbl WHERE data = :data AND data_ext = :data_ext AND more_data = :data"
assertEquals("SELECT data, data_ext FROM tbl WHERE data = ? AND data_ext = ? AND more_data = ?",
Parameters.replaceNamesInQuery(query, parameters), "Parameters not replaced correctly")
@Test
@DisplayName("fieldNames generates a single parameter (PostgreSQL)")
def fieldNamesSinglePostgres(): Unit =
ForceDialect.postgres()
val nameParams = Parameters.fieldNames("test" :: Nil).toList
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name", nameParams.head.getName, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams.head.getType, "The parameter type is incorrect")
assertEquals("{test}", nameParams.head.getValue, "The parameter value is incorrect")
@Test
@DisplayName("fieldNames generates multiple parameters (PostgreSQL)")
def fieldNamesMultiplePostgres(): Unit =
ForceDialect.postgres()
val nameParams = Parameters.fieldNames("test" :: "this" :: "today" :: Nil).toList
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name", nameParams.head.getName, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams.head.getType, "The parameter type is incorrect")
assertEquals("{test,this,today}", nameParams.head.getValue, "The parameter value is incorrect")
@Test
@DisplayName("fieldNames generates a single parameter (SQLite)")
def fieldNamesSingleSQLite(): Unit =
ForceDialect.sqlite()
val nameParams = Parameters.fieldNames("test" :: Nil).toList
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name0", nameParams.head.getName, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams.head.getType, "The parameter type is incorrect")
assertEquals("test", nameParams.head.getValue, "The parameter value is incorrect")
@Test
@DisplayName("fieldNames generates multiple parameters (SQLite)")
def fieldNamesMultipleSQLite(): Unit =
ForceDialect.sqlite()
val nameParams = Parameters.fieldNames("test" :: "this" :: "today" :: Nil).toList
assertEquals(3, nameParams.size, "There should be one name parameter")
assertEquals(":name0", nameParams.head.getName, "The first parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams.head.getType, "The first parameter type is incorrect")
assertEquals("test", nameParams.head.getValue, "The first parameter value is incorrect")
assertEquals(":name1", nameParams(1).getName, "The second parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams(1).getType, "The second parameter type is incorrect")
assertEquals("this", nameParams(1).getValue, "The second parameter value is incorrect")
assertEquals(":name2", nameParams(2).getName, "The third parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams(2).getType, "The third parameter type is incorrect")
assertEquals("today", nameParams(2).getValue, "The third parameter value is incorrect")
@Test
@DisplayName("fieldNames fails if dialect not set")
def fieldNamesFails(): Unit =
assertThrows(classOf[DocumentException], () => Parameters.fieldNames(List()))