nim-codex/tests/codex/helpers/mockreservations.nim
Adam Uhlíř e5df8c50d3
style: nph formatting (#1067)
* style: nph setup

* chore: formates codex/ and tests/ folder with nph 0.6.1
2025-01-21 20:54:46 +00:00

43 lines
1.3 KiB
Nim

import pkg/chronos
import pkg/codex/sales
import pkg/codex/stores
import pkg/questionable/results
type MockReservations* = ref object of Reservations
createReservationThrowBytesOutOfBoundsError: bool
createReservationThrowError: ?(ref CatchableError)
proc new*(T: type MockReservations, repo: RepoStore): MockReservations =
## Create a mock clock instance
MockReservations(availabilityLock: newAsyncLock(), repo: repo)
proc setCreateReservationThrowBytesOutOfBoundsError*(
self: MockReservations, flag: bool
) =
self.createReservationThrowBytesOutOfBoundsError = flag
proc setCreateReservationThrowError*(
self: MockReservations, error: ?(ref CatchableError)
) =
self.createReservationThrowError = error
method createReservation*(
self: MockReservations,
availabilityId: AvailabilityId,
slotSize: UInt256,
requestId: RequestId,
slotIndex: UInt256,
): Future[?!Reservation] {.async.} =
if self.createReservationThrowBytesOutOfBoundsError:
let error = newException(
BytesOutOfBoundsError,
"trying to reserve an amount of bytes that is greater than the total size of the Availability",
)
return failure(error)
elif error =? self.createReservationThrowError:
return failure(error)
return await procCall createReservation(
Reservations(self), availabilityId, slotSize, requestId, slotIndex
)