Skip to content

Commit

Permalink
Add BigNumeric tests and fix arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Nov 19, 2024
1 parent 90861ac commit f74c7f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class ConverterProviderSpec

import Schemas._

def arbBigDecimal(precision: Int, scale: Int): Arbitrary[BigDecimal] = Arbitrary {
val max = BigInt(10).pow(precision) - 1
Gen.choose(-max, max).map(BigDecimal(_, scale))
}

implicit val arbByteArray: Arbitrary[Array[Byte]] = Arbitrary(Gen.alphaStr.map(_.getBytes))
implicit val arbByteString: Arbitrary[ByteString] = Arbitrary(
Gen.alphaStr.map(ByteString.copyFromUtf8)
Expand All @@ -46,17 +51,26 @@ final class ConverterProviderSpec
implicit val arbDate: Arbitrary[LocalDate] = Arbitrary(Gen.const(LocalDate.now()))
implicit val arbTime: Arbitrary[LocalTime] = Arbitrary(Gen.const(LocalTime.now()))
implicit val arbDatetime: Arbitrary[LocalDateTime] = Arbitrary(Gen.const(LocalDateTime.now()))
implicit val arbNumericBigDecimal: Arbitrary[BigDecimal] = Arbitrary {
Arbitrary.arbBigDecimal.arbitrary
.retryUntil(_.precision <= Numeric.MaxNumericPrecision)
.map(Numeric.apply)
}
implicit val arbNumericBigDecimal: Arbitrary[BigDecimal] =
arbBigDecimal(Numeric.MaxNumericPrecision, Numeric.MaxNumericScale)
implicit val arbGeography: Arbitrary[Geography] = Arbitrary(
for {
x <- Gen.numChar
y <- Gen.numChar
} yield Geography(s"POINT($x $y)")
)
implicit val arbJson: Arbitrary[Json] = Arbitrary(
for {
key <- Gen.alphaStr
value <- Gen.alphaStr
} yield Json(s"""{"$key":"$value"}""")
)
implicit val arbBigNumeric: Arbitrary[BigNumeric] = Arbitrary {
// Precision: 76.76 (the 77th digit is partial)
arbBigDecimal(BigNumeric.MaxNumericPrecision - 1, BigNumeric.MaxNumericScale).arbitrary
.map(BigNumeric.apply)
}

implicit val eqByteArrays: Eq[Array[Byte]] = Eq.instance[Array[Byte]](_.toList == _.toList)
implicit val eqByteString: Eq[ByteString] = Eq.instance[ByteString](_ == _)
implicit val eqInstant: Eq[Instant] = Eq.instance[Instant](_ == _)
Expand Down Expand Up @@ -111,6 +125,7 @@ final class ConverterProviderSpec
o.bigDecimalF.isDefined shouldBe r.containsKey("bigDecimalF")
o.geographyF.isDefined shouldBe r.containsKey("geographyF")
o.jsonF.isDefined shouldBe r.containsKey("jsonF")
o.bigNumericF.isDefined shouldBe r.containsKey("bigNumericF")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class SchemaProviderTest extends AnyFlatSpec with Matchers {
| {"mode": "$mode", "name": "datetimeF", "type": "DATETIME"},
| {"mode": "$mode", "name": "bigDecimalF", "type": "NUMERIC"},
| {"mode": "$mode", "name": "geographyF", "type": "GEOGRAPHY"},
| {"mode": "$mode", "name": "jsonF", "type": "JSON"}
| {"mode": "$mode", "name": "jsonF", "type": "JSON"},
| {"mode": "$mode", "name": "bigNumericF", "type": "BIGNUMERIC"}
|]
|""".stripMargin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ object Schemas {
datetimeF: LocalDateTime,
bigDecimalF: BigDecimal,
geographyF: Geography,
jsonF: Json
jsonF: Json,
bigNumericF: BigNumeric
)
case class Optional(
boolF: Option[Boolean],
Expand All @@ -54,7 +55,8 @@ object Schemas {
datetimeF: Option[LocalDateTime],
bigDecimalF: Option[BigDecimal],
geographyF: Option[Geography],
jsonF: Option[Json]
jsonF: Option[Json],
bigNumericF: Option[BigNumeric]
)
case class Repeated(
boolF: List[Boolean],
Expand All @@ -71,7 +73,8 @@ object Schemas {
datetimeF: List[LocalDateTime],
bigDecimalF: List[BigDecimal],
geographyF: List[Geography],
jsonF: List[Json]
jsonF: List[Json],
bigNumericF: List[BigNumeric]
)

// records
Expand Down

0 comments on commit f74c7f4

Please sign in to comment.