From dee044eb4e24fa2f42aa6cba779c63b45e6a291d Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Thu, 27 May 2021 06:43:49 -0500 Subject: [PATCH] 2021 05 26 uint64 mapper (#3155) * Add unit for u64 mapper * Add more unit tests for u64 mapper --- .../org/bitcoins/db/UInt64MapperTest.scala | 50 +++++++++++++++++++ .../bitcoins/db/DbCommonsColumnMappers.scala | 16 +++--- 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 db-commons-test/src/test/scala/org/bitcoins/db/UInt64MapperTest.scala diff --git a/db-commons-test/src/test/scala/org/bitcoins/db/UInt64MapperTest.scala b/db-commons-test/src/test/scala/org/bitcoins/db/UInt64MapperTest.scala new file mode 100644 index 0000000000..d1d5742dfd --- /dev/null +++ b/db-commons-test/src/test/scala/org/bitcoins/db/UInt64MapperTest.scala @@ -0,0 +1,50 @@ +package org.bitcoins.db + +import org.bitcoins.core.number.UInt64 +import org.bitcoins.testkitcore.gen.NumberGenerator +import org.bitcoins.testkitcore.util.BitcoinSUnitTest +import slick.jdbc.{PostgresProfile, SQLiteProfile} + +class UInt64MapperTest extends BitcoinSUnitTest { + behavior of "UInt64Mapper" + + implicit override val generatorDrivenConfig: PropertyCheckConfiguration = { + generatorDrivenConfigNewCode + } + private val sqlite = new DbCommonsColumnMappers(SQLiteProfile) + + private val postgres = new DbCommonsColumnMappers(PostgresProfile) + + it must "map a large uint64 to bytes and back" in { + val u64 = UInt64.max + val sqliteHex = sqlite.uInt64ToHex(u64) + val sqliteu64 = UInt64.fromHex(sqliteHex) + assert(sqliteu64 == u64) + + val pgHex = postgres.uInt64ToHex(u64) + val pgu64 = UInt64.fromHex(pgHex) + assert(pgu64 == u64) + } + + it must "map UInt64.zero to bytes and back" in { + val u64 = UInt64.zero + val sqliteHex = sqlite.uInt64ToHex(u64) + val sqliteu64 = UInt64.fromHex(sqliteHex) + assert(sqliteu64 == u64) + + val pgHex = postgres.uInt64ToHex(u64) + val pgu64 = UInt64.fromHex(pgHex) + assert(pgu64 == u64) + } + + it must "map uint64 to bytes and back" in { + forAll(NumberGenerator.uInt64) { u64 => + val sqliteHex = sqlite.uInt64ToHex(u64) + val sqliteu64 = UInt64.fromHex(sqliteHex) + assert(sqliteu64 == u64) + val pgHex = postgres.uInt64ToHex(u64) + val pgu64 = UInt64.fromHex(pgHex) + assert(pgu64 == u64) + } + } +} diff --git a/db-commons/src/main/scala/org/bitcoins/db/DbCommonsColumnMappers.scala b/db-commons/src/main/scala/org/bitcoins/db/DbCommonsColumnMappers.scala index 8e1928c992..dda2c6e586 100644 --- a/db-commons/src/main/scala/org/bitcoins/db/DbCommonsColumnMappers.scala +++ b/db-commons/src/main/scala/org/bitcoins/db/DbCommonsColumnMappers.scala @@ -155,17 +155,21 @@ class DbCommonsColumnMappers(val profile: JdbcProfile) { implicit val uint64Mapper: BaseColumnType[UInt64] = { MappedColumnType.base[UInt64, String]( { u64: UInt64 => - val bytes = u64.bytes - val padded = if (bytes.length <= 8) { - bytes.padLeft(8) - } else bytes - - padded.toHex + uInt64ToHex(u64) }, UInt64.fromHex ) } + def uInt64ToHex(u64: UInt64): String = { + val bytes = u64.bytes + val padded = if (bytes.length <= 8) { + bytes.padLeft(8) + } else bytes + + padded.toHex + } + implicit val transactionOutPointMapper: BaseColumnType[ TransactionOutPoint] = { MappedColumnType