From ed7efb67036d842d8a017ca1a03b83d642019f66 Mon Sep 17 00:00:00 2001 From: Brendan Long Date: Mon, 22 Oct 2018 14:31:41 -0400 Subject: [PATCH] Add test for #38 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This tests that a string with ä in it round-trips properly --- pgx/src/pgx_value.ml | 2 +- pgx_test/src/pgx_test.ml | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/pgx/src/pgx_value.ml b/pgx/src/pgx_value.ml index 53fe175..ed5de08 100644 --- a/pgx/src/pgx_value.ml +++ b/pgx/src/pgx_value.ml @@ -268,8 +268,8 @@ let to_point = Option.map to_point' let of_string t = Some t -let to_string_exn = required (fun t -> t) let to_string t = t +let to_string_exn = required to_string let unit = Some "" diff --git a/pgx_test/src/pgx_test.ml b/pgx_test/src/pgx_test.ml index 19ba36a..e34c9c0 100644 --- a/pgx_test/src/pgx_test.ml +++ b/pgx_test/src/pgx_test.ml @@ -469,6 +469,82 @@ module Make_tests (IO : Pgx.IO) = struct | _ -> assert false)) >>| List.iter (fun () -> ()) ) + ; "UTF-8 partial round-trip 1", (fun () -> + (* Select a literal string *) + let expect = "test-ä-test" in + with_conn (fun db -> + simple_query db {| + CREATE TEMPORARY TABLE this_test (id text); + INSERT INTO this_test (id) VALUES ('test-ä-test') + |} + >>= fun _ -> + execute db "SELECT id FROM this_test" + >>| function + | [[ result ]] -> assert_equal (Some expect) (Pgx.Value.to_string result) + | _ -> assert false + ) + ) + ; "UTF-8 partial round-trip 1 with where", (fun () -> + (* Select a literal string *) + let expect = "test-ä-test" in + with_conn (fun db -> + simple_query db {| + CREATE TEMPORARY TABLE this_test (id text); + INSERT INTO this_test (id) VALUES ('test-ä-test') + |} + >>= fun _ -> + execute db ~params:[ Pgx.Value.of_string expect ] + "SELECT id FROM this_test WHERE id = $1" + >>| function + | [[ result ]] -> assert_equal (Some expect) (Pgx.Value.to_string result) + | [] -> assert_failure "Expected one row but got zero" + | _ -> assert false + ) + ) + ; "UTF-8 partial round-trip 2", (fun () -> + (* Insert string as a param, then select back the contents of + the table *) + let expect = "test-ä-test" in + with_conn (fun db -> + simple_query db "CREATE TEMPORARY TABLE this_test (id text)" + >>= fun _ -> + execute db ~params:[ Pgx.Value.of_string expect] + "INSERT INTO this_test (id) VALUES ($1)" + >>= fun _ -> + execute db "SELECT id FROM this_test" + >>| function + | [[ result ]] -> assert_equal (Some expect) (Pgx.Value.to_string result) + | _ -> assert false + ) + ) + ; "UTF-8 round-trip", (fun () -> + (* Select the contents of a param *) + let expect = "test-ä-test" in + with_conn (fun db -> + execute db ~params:[ Pgx.Value.of_string expect ] + "SELECT $1::VARCHAR" + >>| function + | [[ result ]] -> assert_equal (Some expect) (Pgx.Value.to_string result) + | _ -> assert false + ) + ) + ; "UTF-8 round-trip where", (fun () -> + (* Insert string as a param, then select back the contents of + the table using a WHERE *) + let expect = "test-ä-test" in + with_conn (fun db -> + simple_query db "CREATE TEMPORARY TABLE this_test (id text)" + >>= fun _ -> + execute db ~params:[ Pgx.Value.of_string expect] + "INSERT INTO this_test (id) VALUES ($1)" + >>= fun _ -> + execute db ~params:[ Pgx.Value.of_string expect] + "SELECT id FROM this_test WHERE id = $1" + >>| function + | [[ result ]] -> assert_equal (Some expect) (Pgx.Value.to_string result) + | _ -> assert false + ) + ) ] in make_tests "pgx_async" tests >>| run_test_tt_main ~exit