From 036079411592dd4aaa9513bfe9aecd66ea81b91d Mon Sep 17 00:00:00 2001 From: Manfred Zingl Date: Fri, 2 Aug 2024 11:28:14 +0200 Subject: [PATCH] add test and modify test helper --- .../api/structure/ChangeStructureTest.scala | 14 +++++ .../api/structure/CreateColumnTest.scala | 51 ++++++++++++++++++- .../tableaux/testtools/RequestCreation.scala | 13 ++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala b/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala index 7c90b3ef..b46eb2a3 100644 --- a/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala @@ -92,6 +92,20 @@ class ChangeStructureTest extends TableauxTestBase { } } + @Test + def changeColumnDecimalDigits(implicit c: TestContext): Unit = okTest { + val postJson = Json.obj("decimalDigits" -> 4) + + for { + _ <- createDefaultTable() + resultPost <- sendRequest("POST", "/tables/1/columns/2", postJson) + resultGet <- sendRequest("GET", "/tables/1/columns/2") + } yield { + assertEquals(4, resultGet.getInteger("decimalDigits")) + assertEquals(resultPost, resultGet) + } + } + @Test def changeColumnKindWhichShouldFail(implicit c: TestContext): Unit = { okTest { diff --git a/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala b/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala index e20d8464..e6ae8294 100644 --- a/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala @@ -24,7 +24,8 @@ class CreateColumnTest extends TableauxTestBase { def createRichTextColumnJson(name: String) = RequestCreation.Columns().add(RequestCreation.RichTextCol(name)).getJson - def createNumberColumnJson(name: String) = RequestCreation.Columns().add(RequestCreation.NumericCol(name)).getJson + def createNumberColumnJson(name: String, decimalDigits: Option[Int] = None) = + RequestCreation.Columns().add(RequestCreation.NumericCol(name, decimalDigits)).getJson def createIntegerColumnJson(name: String) = RequestCreation.Columns().add(RequestCreation.IntegerCol(name)).getJson @@ -223,6 +224,54 @@ class CreateColumnTest extends TableauxTestBase { } } + @Test + def createNumberColumnWithCustomDecimalDigits(implicit c: TestContext): Unit = { + okTest { + val createColumn1 = createNumberColumnJson("column1", None) + val createColumn2 = createNumberColumnJson("column2", Some(5)) + val expectedJson = Json.obj( + "status" -> "ok", + "columns" -> Json.arr( + Json + .obj( + "id" -> 1, + "ordering" -> 1, + "multilanguage" -> false, + "identifier" -> false, + "displayName" -> Json.obj(), + "description" -> Json.obj() + ) + .mergeIn(createColumn1.getJsonArray("columns").getJsonObject(0)) + ) + ) + val expectedJson2 = Json.obj( + "status" -> "ok", + "columns" -> Json.arr( + Json + .obj( + "id" -> 2, + "ordering" -> 2, + "multilanguage" -> false, + "identifier" -> false, + "displayName" -> Json.obj(), + "description" -> Json.obj() + ) + .mergeIn(createColumn2.getJsonArray("columns").getJsonObject(0)) + ) + ) + + for { + _ <- sendRequest("POST", "/tables", createTableJson) + test1 <- sendRequest("POST", "/tables/1/columns", createColumn1) + test2 <- sendRequest("POST", "/tables/1/columns", createColumn2) + } yield { + assertJSONEquals(expectedJson, test1) + assertNull(test1.getJsonArray("columns").getJsonObject(0).getInteger("decimalDigits")) + assertJSONEquals(expectedJson2, test2) + } + } + } + @Test def createIntegerColumn(implicit c: TestContext): Unit = { okTest { diff --git a/src/test/scala/com/campudus/tableaux/testtools/RequestCreation.scala b/src/test/scala/com/campudus/tableaux/testtools/RequestCreation.scala index 24bb7536..f11b4240 100644 --- a/src/test/scala/com/campudus/tableaux/testtools/RequestCreation.scala +++ b/src/test/scala/com/campudus/tableaux/testtools/RequestCreation.scala @@ -62,7 +62,18 @@ object RequestCreation { case class RichTextCol(name: String) extends ColumnType("richtext") - case class NumericCol(name: String) extends ColumnType("numeric") + case class NumericCol(override val name: String, decimalDigits: Option[Int] = None) extends ColumnType("numeric") { + + override def getJson: JsonObject = { + super.getJson + .mergeIn( + decimalDigits match { + case Some(digits) => Json.obj("decimalDigits" -> digits) + case None => Json.emptyObj() + } + ) + } + } case class IntegerCol(name: String) extends ColumnType("integer")