From 6c25c7d0488aebbb96e40e8bf4bd45c33b734077 Mon Sep 17 00:00:00 2001 From: Pawel Czajka Date: Tue, 17 Dec 2024 19:08:44 +0100 Subject: [PATCH] last tests --- .../engine/extension/Conversion.scala | 2 +- .../engine/spel/SpelExpressionSpec.scala | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/extension/Conversion.scala b/scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/extension/Conversion.scala index a020c7ef978..916b705423f 100644 --- a/scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/extension/Conversion.scala +++ b/scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/extension/Conversion.scala @@ -69,7 +69,7 @@ object ToBigDecimalConversion extends ToNumericConversion[JBigDecimal] { override def convertEither(value: Any): Either[Throwable, JBigDecimal] = value match { - case v: JBigDecimal => Right(v) + case v: JBigDecimal => Right(v.setScale(Math.max(v.scale(), NumberUtilsConsts.DEFAULT_BIG_DECIMAL_SCALE))) case v: JBigInteger => Right(new JBigDecimal(v).setScale(NumberUtilsConsts.DEFAULT_BIG_DECIMAL_SCALE)) case v: Number => Try(NumberUtils.convertNumberToTargetClass(v, resultTypeClass)).toEither case v: String => diff --git a/scenario-compiler/src/test/scala/pl/touk/nussknacker/engine/spel/SpelExpressionSpec.scala b/scenario-compiler/src/test/scala/pl/touk/nussknacker/engine/spel/SpelExpressionSpec.scala index 1a68325f052..dc2192c095d 100644 --- a/scenario-compiler/src/test/scala/pl/touk/nussknacker/engine/spel/SpelExpressionSpec.scala +++ b/scenario-compiler/src/test/scala/pl/touk/nussknacker/engine/spel/SpelExpressionSpec.scala @@ -364,11 +364,26 @@ class SpelExpressionSpec extends AnyFunSuite with Matchers with ValidatedValuesD BigDecimal(result.asInstanceOf[java.math.BigDecimal]) shouldBe BigDecimal(0.5) +- BigDecimal(0.001) } - test("should set scale at least 18 when creating big double") { + test("should set scale at least default when creating big decimal from int") { val result = evaluate[Any]("""(1).toBigDecimal""".stripMargin) result.asInstanceOf[java.math.BigDecimal].scale() shouldBe NumberUtilsConsts.DEFAULT_BIG_DECIMAL_SCALE } + test("should set scale at least default when creating big decimal from big int") { + val result = evaluate[Any]("""#a.toBigDecimal""".stripMargin, Context("asd", Map("a" -> JBigInteger.ONE))) + result.asInstanceOf[java.math.BigDecimal].scale() shouldBe NumberUtilsConsts.DEFAULT_BIG_DECIMAL_SCALE + } + + test("should set scale at least default when creating big decimal from string with low scale") { + val result = evaluate[Any]("""("1.23").toBigDecimal""".stripMargin) + result.asInstanceOf[java.math.BigDecimal].scale() shouldBe NumberUtilsConsts.DEFAULT_BIG_DECIMAL_SCALE + } + + test("should set high scale when creating big decimal from string with high scale") { + val result = evaluate[Any]("""("1.345345345345345345345345345345").toBigDecimal""".stripMargin) + result.asInstanceOf[java.math.BigDecimal].scale() shouldBe 30 + } + test("indexer access on unknown - array like case") { parse[Any]( "#containerWithUnknownArray.value[0]"