Add custom database mapping hooks
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import std/[options, unittest]
|
||||
|
||||
import fiber_orm/util
|
||||
|
||||
import ./custom_db_types
|
||||
|
||||
|
||||
type CustomModel = object
|
||||
id: ValidatedId
|
||||
optionalId: Option[ValidatedId]
|
||||
absentId: Option[ValidatedId]
|
||||
relatedIds: seq[ValidatedId]
|
||||
balance: Money
|
||||
|
||||
|
||||
suite "custom database mappings":
|
||||
|
||||
test "uses custom mappings for values nested in structural types":
|
||||
let model = rowToModel(CustomModel, @[
|
||||
"id_primary",
|
||||
"id_optional",
|
||||
"",
|
||||
"{id_first,id_second}",
|
||||
"1250",
|
||||
])
|
||||
|
||||
check:
|
||||
string(model.id) == "ID_PRIMARY"
|
||||
model.optionalId.isSome
|
||||
string(model.optionalId.get) == "ID_OPTIONAL"
|
||||
model.absentId.isNone
|
||||
model.relatedIds.len == 2
|
||||
string(model.relatedIds[0]) == "ID_FIRST"
|
||||
string(model.relatedIds[1]) == "ID_SECOND"
|
||||
model.balance.cents == 1_250
|
||||
|
||||
test "propagates errors from custom mappings without falling back":
|
||||
expect ValueError:
|
||||
discard rowToModel(CustomModel, @[
|
||||
"invalid",
|
||||
"",
|
||||
"",
|
||||
"{}",
|
||||
"1250",
|
||||
])
|
||||
Reference in New Issue
Block a user