Skip to content

Commit

Permalink
Test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
scull7 committed Jul 9, 2018
1 parent a026d00 commit 8854853
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 7 deletions.
226 changes: 226 additions & 0 deletions __tests__/Test_mysql2.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
open Jest;

let connect = () =>
MySql2.Connection.connect(
~host="127.0.0.1",
~port=3306,
~user="root",
~password="",
~database="test",
(),
);

let table_sql = {|
CREATE TABLE IF NOT EXISTS `test`.`mysql2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT
, `code` varchar(32) NOT NULL
, `display` varchar(140) DEFAULT NULL
, PRIMARY KEY(`id`)
)
|};

let seed_sql = {|
INSERT IGNORE INTO `test`.`mysql2`
(`id`, `code`, `display`)
VALUES
(1, "batman", "I am, Bat-man"),
(2, "superman", "Truth, Justice and the American Way"),
(3, "wonder_woman", "Suffering Sappho!")
|};

let mutationPromise = (db, sql, params) =>
Js.Promise.make((~resolve, ~reject) =>
MySql2.execute(
db,
sql,
params,
fun
| `Error(e) => reject(. e)
| `Select(_) => reject(. "unexpected_select_result" |. Failure)
| `Mutation(m) => resolve(. m),
)
|. ignore
);

let selectPromise = (db, sql, params) =>
Js.Promise.make((~resolve, ~reject) =>
MySql2.execute(
db,
sql,
params,
fun
| `Error(e) => reject(. e)
| `Mutation(_) => reject(. "unexpected_mutation_result" |. Failure)
| `Select(select) => resolve(. select),
)
|. ignore
);

describe("MySql2", () => {
let db = ref(None);

let getDb = () => db^ |. Belt.Option.getExn;

beforeAllPromise(() => {
db := connect() |. Some;

mutationPromise(getDb(), "DROP TABLE IF EXISTS `test`.`mysql2`", None)
|> Js.Promise.then_(_ => mutationPromise(getDb(), table_sql, None))
|> Js.Promise.then_(_ => mutationPromise(getDb(), seed_sql, None));
});

afterAll(() => getDb() |. MySql2.Connection.close);

describe("Mutation", () => {
let result = ref(None);

let getResult = () => result^ |. Belt.Option.getExn;

beforeAllPromise(() => {
let sql = {|
INSERT INTO `test`.`mysql2`
(`id`, `code`, `display`)
VALUES
(4, "flash", "I am the fastest man alive")
|};

mutationPromise(getDb(), sql, None)
|> Js.Promise.then_(mutation => {
result := Some(mutation);
Js.Promise.resolve(mutation);
});
});

let makeTest = (label, fn, expected) =>
test(label, () =>
getResult() |. fn |. Expect.expect |> Expect.toBe(expected)
);

describe("fieldCount", () =>
makeTest("should return zero", MySql2.Mutation.fieldCount, 0)
);

describe("info", () =>
makeTest("should return empty string", MySql2.Mutation.info, "")
);

describe("serverStatus", () =>
makeTest("should return 2", MySql2.Mutation.serverStatus, 2)
);

describe("warningStatus", () =>
makeTest("should return 0", MySql2.Mutation.warningStatus, 0)
);
});

describe("Select", () => {
let result = ref(None);

let getResult = () => result^ |. Belt.Option.getExn;

beforeAllPromise(() =>
selectPromise(getDb(), "SELECT * FROM `test`.`mysql2`", None)
|> Js.Promise.then_(select => {
result := Some(select);
Js.Promise.resolve(select);
})
);

let makeMetaTest = (label, fn, expected) =>
test(label, () =>
getResult()
|. MySql2.Select.meta
|. Belt.Array.map(fn)
|. Expect.expect
|> Expect.toEqual(expected)
);

describe("catalog", () =>
makeMetaTest(
"should return the string 'def'",
MySql2.Select.Meta.catalog,
[|"def", "def", "def"|],
)
);

describe("schema", () =>
makeMetaTest(
"should return the string 'test'",
MySql2.Select.Meta.schema,
[|"test", "test", "test"|],
)
);

describe("name", () =>
makeMetaTest(
"should return the column name string",
MySql2.Select.Meta.name,
[|"id", "code", "display"|],
)
);

describe("orgName", () =>
makeMetaTest(
"should return the schema defined column name string",
MySql2.Select.Meta.orgName,
[|"id", "code", "display"|],
)
);

describe("table", () =>
makeMetaTest(
"should return the table name string",
MySql2.Select.Meta.table,
[|"mysql2", "mysql2", "mysql2"|],
)
);

describe("orgTable", () =>
makeMetaTest(
"should return the schema defined table name string",
MySql2.Select.Meta.orgTable,
[|"mysql2", "mysql2", "mysql2"|],
)
);

describe("characterSet", () =>
makeMetaTest(
"should return the column character sets",
MySql2.Select.Meta.characterSet,
[|63, 224, 224|],
)
);

describe("columnLength", () =>
makeMetaTest(
"should return the length of each column",
MySql2.Select.Meta.columnLength,
[|20, 128, 560|],
)
);

describe("columnType", () =>
makeMetaTest(
"should return the internal type code of each column",
MySql2.Select.Meta.columnType,
[|8, 253, 253|],
)
);

describe("flags", () =>
makeMetaTest(
"should return the flag set for each column",
MySql2.Select.Meta.flags,
[|16899, 4097, 0|],
)
);

describe("decimals", () =>
makeMetaTest(
"should return the decimal count of each column",
MySql2.Select.Meta.decimals,
[|0, 0, 0|],
)
);
});
});
4 changes: 4 additions & 0 deletions src/MySql2.rei
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module Select: {

let table: t => string;

let orgTable: t => string;

let characterSet: t => int;

let columnLength: t => int;
Expand All @@ -78,6 +80,8 @@ module Select: {
let decimals: t => int;
};

let meta: t => array(Meta.t);

let count: t => int;

let flatMap: (t, (Js.Json.t, array(Meta.t)) => 'a) => array('a);
Expand Down
27 changes: 20 additions & 7 deletions src/MySql2_response.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module Select = {
/* https://mariadb.com/kb/en/library/packet_resultset/ */
/**
* There will be a meta data packet for each column within the
* response.
* @TODO - determine the meaning of the characterSet field
* @TODO - determine the meaning of the flags field.
*/
module Meta = {
[@bs.deriving abstract]
type t = {
Expand Down Expand Up @@ -33,23 +40,29 @@ module Select = {
);
};

[@bs.deriving abstract]
type t = {
rows: array(Js.Json.t),
meta: array(Meta.t),
};

let make = (rows, meta) => {
rows,
meta: meta |. Belt.Array.map(Meta.make),
};
let make = (rows, meta) =>
t(~rows, ~meta=meta |. Belt.Array.map(Meta.make));

let flatMap = (t, fn) => Belt.Array.map(t.rows, row => fn(row, t.meta));
let flatMap = (t, fn) =>
Belt.Array.map(t |. rows, row => fn(row, t |. meta));

let mapDecoder = (t, decoder) => Belt.Array.map(t.rows, decoder);
let mapDecoder = (t, decoder) => Belt.Array.map(t |. rows, decoder);

let count = t => Belt.Array.length(t.rows);
let count = t => Belt.Array.length(t |. rows);
};

/* https://github.com/mysqljs/mysql#getting-the-number-of-changed-rows */
/*
* @TODO - Add changed rows
* @TODO - determine the meaning of fieldCount
* @TODO - determine the meaning of info
*/
module Mutation = {
[@bs.deriving abstract]
type t = {
Expand Down

0 comments on commit 8854853

Please sign in to comment.