Skip to content

Commit

Permalink
Add test for #38
Browse files Browse the repository at this point in the history
This tests that a string with ä in it round-trips properly
  • Loading branch information
brendanlong committed Nov 11, 2018
1 parent 2eae60a commit fbf168d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgx/src/pgx_value.ml
Expand Up @@ -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 ""

Expand Down
59 changes: 59 additions & 0 deletions pgx_test/src/pgx_test.ml
Expand Up @@ -469,6 +469,65 @@ 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 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
Expand Down

0 comments on commit fbf168d

Please sign in to comment.