Skip to content

Commit

Permalink
Add json-rpc response test
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Sep 12, 2024
1 parent b633620 commit d954d93
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/tests/src/core/S_union_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,60 @@ module CrazyUnion = {
)
})
}

test("json-rpc response", t => {
let jsonRpcSchema = (okSchema, errorSchema) =>
S.union([
S.object(s => Ok(s.field("result", okSchema))),
S.object(s => Error(s.field("error", errorSchema))),
])

let getLogsResponseSchema = jsonRpcSchema(
S.array(S.string),
S.union([
S.object(s => {
s.tag("message", "NotFound")
#LogsNotFound
}),
S.object(s => {
s.tag("message", "Invalid")
#InvalidData(s.field("data", S.string))
}),
]),
)

t->Assert.deepEqual(
%raw(`{
"jsonrpc": "2.0",
"id": 1,
"result": ["foo", "bar"]
}`)->S.parseOrRaiseWith(getLogsResponseSchema),
Ok(["foo", "bar"]),
(),
)

t->Assert.deepEqual(
%raw(`{
"jsonrpc": "2.0",
"id": 1,
"error": {
"message": "NotFound"
}
}`)->S.parseOrRaiseWith(getLogsResponseSchema),
Error(#LogsNotFound),
(),
)

t->Assert.deepEqual(
%raw(`{
"jsonrpc": "2.0",
"id": 1,
"error": {
"message": "Invalid",
"data": "foo"
}
}`)->S.parseOrRaiseWith(getLogsResponseSchema),
Error(#InvalidData("foo")),
(),
)
})

1 comment on commit d954d93

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: d954d93 Previous: 83f51d8 Ratio
Parse string 818208927 ops/sec (±0.16%) 815414023 ops/sec (±0.49%) 1.00
Serialize string 820285228 ops/sec (±0.07%) 817783951 ops/sec (±0.11%) 1.00
Advanced object schema factory 474557 ops/sec (±0.71%) 483577 ops/sec (±0.73%) 1.02
Parse advanced object 56786303 ops/sec (±0.75%) 57257765 ops/sec (±0.43%) 1.01
Assert advanced object 173578315 ops/sec (±0.19%) 173214107 ops/sec (±0.13%) 1.00
Create and parse advanced object 94718 ops/sec (±0.20%) 94633 ops/sec (±0.16%) 1.00
Parse advanced strict object 25385689 ops/sec (±0.48%) 25373330 ops/sec (±0.31%) 1.00
Assert advanced strict object 30934364 ops/sec (±0.18%) 30917166 ops/sec (±0.16%) 1.00
Serialize advanced object 75883035 ops/sec (±0.22%) 76333117 ops/sec (±0.06%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.