Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162(* JSONB cursors over SQLite's paths (json1.html, section 3.1). 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.decodelet doc2 = unhex "CC1417614B1331133237612062011763552D312E3530"
(* Paths as json.c reads them (json1.html, section 3.1): the result the cursor reaches and the bytes an edit leaves are sqlite3's. *)let test_cursor () = let path = Alcotest.testable (Fmt.of_to_string (function | J.Error.Bad_path -> "bad path" | Not_array_element -> "not an array element" | Path_too_deep -> "too deep" | Malformed -> "malformed")) ( = ) in let seek = Alcotest.(result bool path) in let c = J.Cursor.of_string doc2 in Alcotest.check seek "$.a[1]" (Ok true) (J.Cursor.seek "$.a[1]" c); Alcotest.(check string) "its value" "2" (J.Cursor.get J.Codec.int c); Alcotest.(check string) "its path" "$.a[1]" (J.Cursor.to_path c); Alcotest.check seek "$.\"a b\"" (Ok true) (J.Cursor.seek {|$."a b"|} c); Alcotest.(check bool) "a TRUE" true (J.Cursor.sort c = Some J.Sort.True); Alcotest.check seek "$.a[#-1]" (Ok true) (J.Cursor.seek "$.a[#-1]" c); Alcotest.(check string) "the last element" "2" (J.Cursor.get J.Codec.int c); Alcotest.check seek "$.a" (Ok true) (J.Cursor.seek "$.a" c); Alcotest.(check int) "its length" 2 (J.Cursor.length c); Alcotest.check seek "nothing there" (Ok false) (J.Cursor.seek "$.zz" c); Alcotest.(check string) "the focus stays" "$.a" (J.Cursor.to_path c); Alcotest.check seek "no $" (Error J.Error.Bad_path) (J.Cursor.seek "a" c); Alcotest.check seek "an unclosed index" (Error J.Error.Bad_path) (J.Cursor.seek "$.a[" c); let edit what expected f doc = let c = J.Cursor.of_string (unhex doc) in Alcotest.(check (result unit path)) what (Ok ()) (f c); Alcotest.(check string) what expected (hex (J.Cursor.to_string c)) in edit "jsonb_set($.a[#])" "9C17616B133113321333" (J.Cursor.set "$.a[#]" J.Codec.int "3") "7C17614B13311332"; edit "jsonb_insert($.b.c)" "CC0E17614B133113321A624C1A631A78" (J.Cursor.insert "$.b.c" J.Codec.text "x") "7C17614B13311332"; edit "jsonb_remove($.a[0])" "5C17612B1332" (J.Cursor.remove "$.a[0]") "7C17614B13311332"; edit "jsonb_array_insert($[1])" "6B133113391332" (J.Cursor.array_insert "$[1]" J.Codec.int "9") "4B13311332"; let c = J.Cursor.of_string (unhex "4C17611331") in Alcotest.(check (result unit path)) "json_array_insert on a member" (Error J.Error.Not_array_element) (J.Cursor.array_insert "$.a" J.Codec.int "2" c)
let suite = ("jsonb_cursor", [ Alcotest.test_case "cursor" `Quick test_cursor ])