85 lines
1.9 KiB
Nim
85 lines
1.9 KiB
Nim
import std/[options, times, unittest]
|
|
|
|
import uuids
|
|
|
|
import fiber_orm/util
|
|
|
|
|
|
type
|
|
StringId = distinct string
|
|
NestedStringId = distinct StringId
|
|
UuidId = distinct UUID
|
|
Count = distinct int
|
|
Ratio = distinct float
|
|
Enabled = distinct bool
|
|
|
|
State = enum
|
|
pending
|
|
complete
|
|
|
|
DistinctState = distinct State
|
|
|
|
DistinctModel = object
|
|
id: StringId
|
|
nestedId: NestedStringId
|
|
userId: UuidId
|
|
count: Count
|
|
ratio: Ratio
|
|
enabled: Enabled
|
|
state: DistinctState
|
|
optionalId: Option[StringId]
|
|
absentId: Option[StringId]
|
|
relatedIds: seq[StringId]
|
|
occurredAt: DateTime
|
|
|
|
|
|
suite "distinct type row mapping":
|
|
|
|
test "parses each distinct type through its backing type":
|
|
let model = rowToModel(DistinctModel, @[
|
|
"item_123",
|
|
"nested_123",
|
|
"07e268ed-a3c1-4952-bc45-778b3000b76c",
|
|
"42",
|
|
"1.25",
|
|
"true",
|
|
"complete",
|
|
"optional_123",
|
|
"",
|
|
"{related_1,related_2}",
|
|
"2026-08-26 14:32:10.123457+00",
|
|
])
|
|
|
|
check:
|
|
string(model.id) == "item_123"
|
|
string(StringId(model.nestedId)) == "nested_123"
|
|
UUID(model.userId) ==
|
|
parseUUID("07e268ed-a3c1-4952-bc45-778b3000b76c")
|
|
int(model.count) == 42
|
|
float(model.ratio) == 1.25
|
|
bool(model.enabled)
|
|
State(model.state) == complete
|
|
model.optionalId.isSome
|
|
string(model.optionalId.get) == "optional_123"
|
|
model.absentId.isNone
|
|
model.relatedIds.len == 2
|
|
string(model.relatedIds[0]) == "related_1"
|
|
string(model.relatedIds[1]) == "related_2"
|
|
model.occurredAt.nanosecond == 123_457_000
|
|
|
|
test "preserves backing-type parse failures":
|
|
expect ValueError:
|
|
discard rowToModel(DistinctModel, @[
|
|
"item_123",
|
|
"nested_123",
|
|
"not-a-uuid",
|
|
"42",
|
|
"1.25",
|
|
"true",
|
|
"complete",
|
|
"",
|
|
"",
|
|
"{}",
|
|
"2026-08-26 14:32:10+00",
|
|
])
|