2021 05 26 uint64 mapper (#3155)

* Add unit for u64 mapper

* Add more unit tests for u64 mapper
This commit is contained in:
Chris Stewart 2021-05-27 06:43:49 -05:00 committed by GitHub
parent 8432712dba
commit dee044eb4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 6 deletions

View file

@ -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)
}
}
}

View file

@ -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