(* JSONB codecs (jsonb.html, section 3).
Every expected value is the answer of the reference sqlite3, 3.54.1 as
Homebrew builds it, to the same call: the BLOBs as its hex() prints them,
so a header of the wrong width or an element of the wrong type shows. *)
module J = Sql.Jsonb
let hex s = String.uppercase_ascii (Hex.encode s)
let unhex = Hex.decode
(* The codecs read and write the bytes sqlite3 writes: jsonb('[1,"a",{"b":
null}]') and jsonb('{"a":[1,2],"a b":true,"c":-1.50}'), a number keeping
its text and a short payload its one-byte header. *)
let doc1 = unhex "8B133117613C176200"
let doc2 = unhex "CC1417614B1331133237612062011763552D312E3530"
let jvalue = Alcotest.testable J.Value.pp J.Value.equal
let test_codec () =
let v = J.Value.Array [ Int "1"; Text "a"; Object [ (Text "b", Null) ] ] in
Alcotest.check jvalue "of_string Value.t" v
(J.of_string_exn J.Codec.Value.t doc1);
Alcotest.(check string)
"to_string Value.t" (hex doc1)
(hex (J.to_string J.Codec.Value.t v));
let v2 =
J.Value.Object
[
(Text "a", Array [ Int "1"; Int "2" ]);
(Text "a b", True);
(Text "c", Float "-1.50");
]
in
Alcotest.check jvalue "a real keeps its text" v2
(J.of_string_exn J.Codec.Value.t doc2);
Alcotest.(check string)
"and writes it back" (hex doc2)
(hex (J.to_string J.Codec.Value.t v2));
Alcotest.(check (list string))
"array int" [ "1"; "2" ]
(J.of_string_exn (J.Codec.array J.Codec.int) (unhex "4B13311332"));
Alcotest.(check string)
"an INT5 reads as its text" "0x1F"
(J.of_string_exn J.Codec.int (unhex "4430783146"));
Alcotest.(check string)
"a TEXTJ reads unescaped" "A"
(J.of_string_exn J.Codec.text (unhex "685C7530303431"));
Alcotest.(check string)
"a string writes TEXTRAW" "1A78"
(hex (J.to_string J.Codec.text "x"));
Alcotest.check jvalue "decode then encode"
(J.Value.Object [ (Textraw "k", Int "7") ])
(J.encode (J.Codec.members J.Codec.int) [ ("k", "7") ]);
Alcotest.(check (list (pair string string)))
"members"
[ ("k", "7") ]
(J.decode_exn
(J.Codec.members J.Codec.int)
( J.Value.Object [ (Textj "\\u006b", Int "7") ] |> fun _ ->
J.Value.Object [ (Text "k", Int "7") ] ));
Alcotest.(check bool)
"a truncated document is refused" true
(Result.is_error (J.of_string J.Codec.ignore (unhex "CB0513")))
(* [any] picks its codec by element type, INT and INT5 alike, and writes
with the one [enc] picks; a missing case is a sort error naming the
codec. *)
let test_any () =
let c =
J.Codec.any ~kind:"number or text"
~int:
(J.Codec.map
~dec:(fun s -> `I s)
~enc:(function `I s -> s | _ -> "")
J.Codec.int)
~text:
(J.Codec.map
~dec:(fun s -> `T s)
~enc:(function `T s -> s | _ -> "")
J.Codec.text)
~enc:(function
| `I _ ->
J.Codec.map
~dec:(fun s -> `I s)
~enc:(function `I s -> s | _ -> "")
J.Codec.int
| `T _ ->
J.Codec.map
~dec:(fun s -> `T s)
~enc:(function `T s -> s | _ -> "")
J.Codec.text)
()
in
Alcotest.(check bool)
"an INT5" true
(J.of_string_exn c (unhex "4430783146") = `I "0x1F");
Alcotest.(check bool) "a TEXT" true (J.of_string_exn c (unhex "1761") = `T "a");
Alcotest.(check string) "writes the int" "1337" (hex (J.to_string c (`I "7")));
Alcotest.check_raises "a NULL"
(Catalog.Error
(Catalog.Error.Runtime "JSONB: expected number or text, found null"))
(fun () -> ignore (J.of_string_exn c "\000"))
(* [json] crosses to nox-json's value and back: the numbers keep their text
([-1.50]), and the labels, read as strings, write back TEXTRAW. *)
let test_json () =
let v = J.of_string_exn J.Codec.json doc2 in
(match v with
| Json.Object ([ _; _; (_, Json.Number (n, _)) ], _) ->
Alcotest.(check (option string))
"the real's text" (Some "-1.50")
(Json.Number.source_lexeme n)
| _ -> Alcotest.fail "not a three-member object");
Alcotest.(check string)
"written back" "CC141A614B133113323A612062011A63552D312E3530"
(hex (J.to_string J.Codec.json v))
let suite =
( "jsonb_codec",
[
Alcotest.test_case "codec" `Quick test_codec;
Alcotest.test_case "any" `Quick test_any;
Alcotest.test_case "json" `Quick test_json;
] )