Skip to content

Commit

Permalink
Tests for genericcoders
Browse files Browse the repository at this point in the history
  • Loading branch information
tbekas committed Jan 31, 2024
1 parent bb6095e commit ac896f9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 33 deletions.
9 changes: 4 additions & 5 deletions codex/slots/builder/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,10 @@ proc buildSlot*(
error "Failed to get proof for slot tree", err = err.msg
return failure(err)

# TODO
# if err =? (await self.store.putCidAndProof(
# treeCid, i, cellCid, encodableProof)).errorOption:
# error "Failed to store slot tree", err = err.msg
# return failure(err)
if err =? (await self.store.putCidAndProof(
treeCid, i, cellCid, encodableProof)).errorOption:
error "Failed to store slot tree", err = err.msg
return failure(err)

tree.root()

Expand Down
13 changes: 7 additions & 6 deletions codex/stores/repostore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,13 @@ method putCidAndProof*(
without res =? await self.putLeafMetadata(treeCid, index, blkCid, proof), err:
return failure(err)

if res == Stored:
if err =? (await self.updateBlockMetadata(blkCid, plusRefCount = 1)).errorOption:
return failure(err)
trace "Leaf metadata stored, block refCount incremented"
else:
trace "Leaf metadata already exists"
if blkCid.mcodec == BlockCodec:
if res == Stored:
if err =? (await self.updateBlockMetadata(blkCid, plusRefCount = 1)).errorOption:
return failure(err)
trace "Leaf metadata stored, block refCount incremented"
else:
trace "Leaf metadata already exists"

return success()

Expand Down
10 changes: 10 additions & 0 deletions codex/utils/genericcoders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ proc decode*(T: type NBytes, bytes: seq[byte]): ?!T = int.decode(bytes).map((ui:
proc encode*[T: enum](e: T): seq[byte] = e.ord().encode
proc decode*(T: typedesc[enum], bytes: seq[byte]): ?!T = int.decode(bytes).map((ui: int) => T(ui))

proc encode*(s: string): seq[byte] = s.toBytes
proc decode*(T: type string, bytes: seq[byte]): ?!T = success(string.fromBytes(bytes))

Check warning on line 39 in codex/utils/genericcoders.nim

View check run for this annotation

Codecov / codecov/patch

codex/utils/genericcoders.nim#L39

Added line #L39 was not covered by tests
proc encode*(b: bool): seq[byte] = (if b: @[byte 1] else: @[byte 0])
proc decode*(T: type bool, bytes: seq[byte]): ?!T =
if bytes.len >= 1:
success(not (bytes[0] == 0.byte))
else:

Check warning on line 44 in codex/utils/genericcoders.nim

View check run for this annotation

Codecov / codecov/patch

codex/utils/genericcoders.nim#L44

Added line #L44 was not covered by tests
failure("Not enought bytes to decode `bool`")

Check warning on line 46 in codex/utils/genericcoders.nim

View check run for this annotation

Codecov / codecov/patch

codex/utils/genericcoders.nim#L46

Added line #L46 was not covered by tests
proc autoencode*[T: tuple | object](tup: T): seq[byte] =
var
pb = initProtoBuffer(maxSize = MaxBufferSize)

Check warning on line 49 in codex/utils/genericcoders.nim

View check run for this annotation

Codecov / codecov/patch

codex/utils/genericcoders.nim#L49

Added line #L49 was not covered by tests
Expand Down
1 change: 1 addition & 0 deletions tests/codex/testutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ./utils/testoptionalcast
import ./utils/testkeyutils
import ./utils/testasyncstatemachine
import ./utils/testasynciter
import ./utils/testgenericcoders
import ./utils/testtimer
import ./utils/testthen
import ./utils/testtrackedfutures
Expand Down
24 changes: 2 additions & 22 deletions tests/codex/utils/testasynciter.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import std/sugar

import pkg/asynctest
import pkg/questionable
import pkg/chronos
import pkg/upraises
import pkg/codex/utils/asynciter

import ../../asynctest
import ../helpers

asyncchecksuite "Test AsyncIter":
Expand Down Expand Up @@ -54,24 +55,3 @@ asyncchecksuite "Test AsyncIter":

check:
iter.toSeq() == @[1, 3]

test "Should parallelize getting items using `prefetch`":
let
sleepDuration = [30, 10, 10, 10, 20].mapIt(it.millis)

let iter = Iter
.fromItems(sleepDuration)
.map(
proc (i: Duration): Future[void] {.async.} =
await sleepAsync(i)
)
.prefetch(3)

proc awaitAll(): Future[void] {.async.} =
for i in iter:
await i

let sleepTotal = sleepDuration.foldl(a + b)

# implicit assert - future should be completed in less than total
await awaitAll().wait(sleepTotal - 10.millis)
70 changes: 70 additions & 0 deletions tests/codex/utils/testgenericcoders.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import std/unittest
import pkg/questionable
import pkg/questionable/results
import pkg/codex/utils/genericcoders
import ../helpers

type
MyEnum = enum
MyEnumA = 1
MyEnumB = 2

MyObj = object
a: int
b: string
c: bool
d: MyEnum

MyTuple = (int, string, bool, MyEnum)

proc `==`*(a, b: MyObj): bool =
(a.a == b.a) and
(a.b == b.b) and
(a.c == b.c) and
(a.d == b.d)

proc `$`*(a: MyObj): string =
"a: " & $a.a &
", b: " & $a.b &
", c: " & $a.c &
", d: " & $a.d

checksuite "Test encode/decode":
proc coderTest(T: type, a: T) =
let bytes = a.encode

without decoded =? T.decode(bytes), err:
fail

check:
decoded == a

test "Should encode and decode primitive values":
coderTest(int, 123)
coderTest(int, -123)
coderTest(int64, 123.int64)
coderTest(int64, -123.int64)
coderTest(Natural, 123.Natural)
coderTest(NBytes, 123.KiBs)
coderTest(string, "")
coderTest(string, "123")
coderTest(string, "abcdefghij")
coderTest(bool, false)
coderTest(bool, true)
coderTest(MyEnum, MyEnumA)
coderTest(MyEnum, MyEnumB)

checksuite "Test autoencode/autodecode":

proc autocoderTest(T: type, a: T) =
let bytes = a.autoencode

without decoded =? T.autodecode(bytes), err:
fail

check:
decoded == a

test "Should encode and decode product values":
autocoderTest(MyObj, MyObj(a: 1, b: "abc", c: true, d: MyEnumA))
autocoderTest(MyTuple, (2, "def", false, MyEnumB))

0 comments on commit ac896f9

Please sign in to comment.