Skip to content

Commit 6e2c5ac

Browse files
committed
Added tests and corrections
1 parent 2ec495a commit 6e2c5ac

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

spra-play-server/src/main/scala/net/wiringbits/spra/admin/repositories/daos/DatabaseTablesDAO.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ object DatabaseTablesDAO {
8282
List("int", "serial").exists(columnType.contains)
8383
}
8484

85+
private def columnTypeIsDate(columnType: String): Boolean = {
86+
List("date", "time").exists(columnType.contains)
87+
}
88+
8589
private def isUUID(value: String, columnType: String): Boolean = {
8690
Try(UUID.fromString(value)) match {
8791
case Success(_) => columnType == "uuid"
@@ -123,9 +127,8 @@ object DatabaseTablesDAO {
123127
case None => throw Exception(s"Column with name '$filterField' not found.")
124128
}
125129
filterValue match {
126-
case dateRegex(_, _, _) if columnType == "date" =>
130+
case dateRegex(_, _, _) if columnTypeIsDate(columnType) =>
127131
s"DATE($filterField) = ?"
128-
129132
case _ =>
130133
if (isNumberOrUUID(filterValue, columnType))
131134
s"$filterField = ?"
@@ -153,7 +156,7 @@ object DatabaseTablesDAO {
153156
case None => throw Exception(s"Column with name '$filterField' not found.")
154157
}
155158
filterValue match {
156-
case dateRegex(year, month, day) =>
159+
case dateRegex(year, month, day) if columnTypeIsDate(columnType) =>
157160
val parsedDate = LocalDate.of(year.toInt, month.toInt, day.toInt)
158161
preparedStatement.setDate(sqlIndex, Date.valueOf(parsedDate))
159162

spra-play-server/src/test/scala/controllers/AdminControllerSpec.scala

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,106 @@ class AdminControllerSpec extends PlayPostgresSpec with AdminUtils {
532532
.futureValue
533533
response.headOption.isEmpty must be(true)
534534
}
535+
536+
"don't fail when the column is citext or similar, but the value can be interpreted as Int." in withApiClient {
537+
client =>
538+
val name = "wiringbits"
539+
val email = "[email protected]"
540+
val request = AdminCreateTable.Request(
541+
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
542+
)
543+
client.createItem(usersSettings.tableName, request).futureValue
544+
545+
val response = client
546+
.getTableMetadata(
547+
usersSettings.tableName,
548+
List("email", "ASC"),
549+
List(0, 9),
550+
"""{"email":"17"}"""
551+
)
552+
.futureValue
553+
val head = response.headOption.value
554+
val nameValue = head.find(_._1 == "name").value._2
555+
val emailValue = head.find(_._1 == "email").value._2
556+
response.size must be(1)
557+
name must be(nameValue)
558+
email must be(emailValue)
559+
}
560+
561+
"don't fail when the column is citext or similar, but the value can be interpreted as Decimal." in withApiClient {
562+
client =>
563+
val name = "wiringbits"
564+
val email = "[email protected]"
565+
val request = AdminCreateTable.Request(
566+
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
567+
)
568+
client.createItem(usersSettings.tableName, request).futureValue
569+
570+
val response = client
571+
.getTableMetadata(
572+
usersSettings.tableName,
573+
List("email", "ASC"),
574+
List(0, 9),
575+
"""{"email":"17.10"}"""
576+
)
577+
.futureValue
578+
val head = response.headOption.value
579+
val nameValue = head.find(_._1 == "name").value._2
580+
val emailValue = head.find(_._1 == "email").value._2
581+
response.size must be(1)
582+
name must be(nameValue)
583+
email must be(emailValue)
584+
}
585+
586+
"don't fail when the column is citext or similar, but the value can be interpreted as Date." in withApiClient {
587+
client =>
588+
val name = "wiringbits"
589+
val email = "[email protected]"
590+
val request = AdminCreateTable.Request(
591+
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
592+
)
593+
client.createItem(usersSettings.tableName, request).futureValue
594+
595+
val response = client
596+
.getTableMetadata(
597+
usersSettings.tableName,
598+
List("email", "ASC"),
599+
List(0, 9),
600+
"""{"email":"2024-06"}"""
601+
)
602+
.futureValue
603+
val head = response.headOption.value
604+
val nameValue = head.find(_._1 == "name").value._2
605+
val emailValue = head.find(_._1 == "email").value._2
606+
response.size must be(1)
607+
name must be(nameValue)
608+
email must be(emailValue)
609+
}
610+
611+
"don't fail when the column is citext or similar, but the value can be interpreted as UUID." in withApiClient {
612+
client =>
613+
val name = "wiringbits"
614+
val email = "[email protected]"
615+
val request = AdminCreateTable.Request(
616+
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
617+
)
618+
client.createItem(usersSettings.tableName, request).futureValue
619+
620+
val response = client
621+
.getTableMetadata(
622+
usersSettings.tableName,
623+
List("name", "ASC"),
624+
List(0, 9),
625+
"""{"email":"8c861a28-e384-4a9b-b7c2-a0367aa3f3e8"}"""
626+
)
627+
.futureValue
628+
val head = response.headOption.value
629+
val nameValue = head.find(_._1 == "name").value._2
630+
val emailValue = head.find(_._1 == "email").value._2
631+
response.size must be(1)
632+
name must be(nameValue)
633+
email must be(emailValue)
634+
}
535635
}
536636

537637
"GET /admin/tables/:tableName/:primaryKey" should {
@@ -680,7 +780,7 @@ class AdminControllerSpec extends PlayPostgresSpec with AdminUtils {
680780

681781
responseMetadata.head.nonEmpty mustBe true
682782
}
683-
783+
684784
"return new user id" in withApiClient { implicit client =>
685785
val user = createUser.futureValue
686786
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue

0 commit comments

Comments
 (0)