From da548ac9b493a17fb19bdc2194767b5c6181df24 Mon Sep 17 00:00:00 2001 From: Nathan Sculli Date: Sun, 4 Feb 2018 13:40:34 -0800 Subject: [PATCH] test parameter substitution. --- __tests__/with_params.re | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 __tests__/with_params.re diff --git a/__tests__/with_params.re b/__tests__/with_params.re new file mode 100644 index 0000000..7f050d6 --- /dev/null +++ b/__tests__/with_params.re @@ -0,0 +1,53 @@ +open Jest; + +let connect = () => + MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); + +type result = {result: int}; + +describe("Test parameter interpolation", () => { + let conn = connect(); + let decoder = json => { + open! Json.Decode; + {result: json |> field("result", int)}; + }; + describe("Standard (positional) parameters", () => + testAsync("Expect parameters to be substituted properly", finish => + MySql.Query.with_params( + conn, "SELECT 1 + ? + ? AS result", [|5, 6|], results => + switch results { + | MySql.Response.Error(_) => fail("Unexpected exception") |> finish + | MySql.Response.Mutation(_) => + fail("unexpected_mutation_result") |> finish + | MySql.Response.Select(s) => + s.rows + |> Js.Array.map(decoder) + |> Js.Array.map(x => x.result) + |> Expect.expect + |> Expect.toBeSupersetOf([|12|]) + |> finish + } + ) + ) + ); + describe("Named parameters", () => + testAsync("Expect parameters to be substitude properly", finish => + MySql.Query.with_named_params( + conn, "SELECT :x + :y AS result", {"x": 1, "y": 2}, results => + switch results { + | MySql.Response.Error(_) => fail("Unexpected exception") |> finish + | MySql.Response.Mutation(_) => + fail("unexpected_mutation_result") |> finish + | MySql.Response.Select(s) => + s.rows + |> Js.Array.map(decoder) + |> Js.Array.map(x => x.result) + |> Expect.expect + |> Expect.toBeSupersetOf([|3|]) + |> finish + } + ) + ) + ); + afterAll(() => MySql.Connection.close(conn)); +});