Skip to content

Commit

Permalink
added data_types
Browse files Browse the repository at this point in the history
  • Loading branch information
andre1sk committed Jun 14, 2019
1 parent 3d46da6 commit f6dfefc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 65 deletions.
2 changes: 1 addition & 1 deletion test.ts
@@ -1,11 +1,11 @@
#! /usr/bin/env deno run --allow-net --allow-env test.ts
import { runTests } from "./deps.ts";

import "./tests/data_types.ts";
import "./tests/queries.ts";
import "./tests/connection_params.ts";
import "./tests/client.ts";
import "./tests/pool.ts";
import "./tests/utils.ts";
import "./tests/decode.ts";

runTests();
2 changes: 1 addition & 1 deletion tests/client.ts
Expand Up @@ -6,7 +6,7 @@ test(async function badAuthData() {
// TODO(bartlomieju): this fails on Travis because it trusts all connections to postgres
// figure out how to make it work
return;

const badConnectionData = { ...TEST_CONNECTION_PARAMS };
badConnectionData.password += "foobar";
const client = new Client(badConnectionData);
Expand Down
56 changes: 56 additions & 0 deletions tests/data_types.ts
@@ -0,0 +1,56 @@
import { assertEquals } from "../deps.ts";
import { Client } from "../mod.ts";
import { TEST_CONNECTION_PARAMS } from "./constants.ts";
import { getTestClient } from "./helpers.ts";

const SETUP = [
"DROP TABLE IF EXISTS data_types;",
`CREATE TABLE data_types(
inet_t inet,
macaddr_t macaddr,
cidr_t cidr
);`
];

const CLIENT = new Client(TEST_CONNECTION_PARAMS);

const testClient = getTestClient(CLIENT, SETUP);

testClient(async function inet() {
const inet = "127.0.0.1";
const insertRes = await CLIENT.query(
"INSERT INTO data_types (inet_t) VALUES($1)",
inet
);
const selectRes = await CLIENT.query(
"SELECT inet_t FROM data_types WHERE inet_t=$1",
inet
);
assertEquals(selectRes.rows, [[inet]]);
});

testClient(async function macaddr() {
const macaddr = "08:00:2b:01:02:03";
const insertRes = await CLIENT.query(
"INSERT INTO data_types (macaddr_t) VALUES($1)",
macaddr
);
const selectRes = await CLIENT.query(
"SELECT macaddr_t FROM data_types WHERE macaddr_t=$1",
macaddr
);
assertEquals(selectRes.rows, [[macaddr]]);
});

testClient(async function cidr() {
const cidr = "192.168.100.128/25";
const insertRes = await CLIENT.query(
"INSERT INTO data_types (cidr_t) VALUES($1)",
cidr
);
const selectRes = await CLIENT.query(
"SELECT cidr_t FROM data_types WHERE cidr_t=$1",
cidr
);
assertEquals(selectRes.rows, [[cidr]]);
});
48 changes: 0 additions & 48 deletions tests/decode.ts

This file was deleted.

22 changes: 22 additions & 0 deletions tests/helpers.ts
@@ -0,0 +1,22 @@
import { test, TestFunction } from "../deps.ts";

export function getTestClient(client, defSetupQueries) {
return async function testClient(
t: TestFunction,
setupQueries?: Array<string>
) {
const fn = async () => {
try {
await client.connect();
for (const q of setupQueries || defSetupQueries) {
await client.query(q);
}
await t();
} finally {
await client.end();
}
};
const name = t.name;
test({ fn, name });
};
}
17 changes: 2 additions & 15 deletions tests/queries.ts
@@ -1,24 +1,11 @@
import { test, assertEquals, TestFunction } from "../deps.ts";
import { Client } from "../mod.ts";
import { TEST_CONNECTION_PARAMS, DEFAULT_SETUP } from "./constants.ts";
import { getTestClient } from "./helpers.ts";

const CLIENT = new Client(TEST_CONNECTION_PARAMS);

async function testClient(t: TestFunction, setupQueries?: Array<string>) {
const fn = async () => {
try {
await CLIENT.connect();
for (const q of setupQueries || DEFAULT_SETUP) {
await CLIENT.query(q);
}
await t();
} finally {
await CLIENT.end();
}
};
const name = t.name;
test({ fn, name });
}
const testClient = getTestClient(CLIENT, DEFAULT_SETUP);

testClient(async function simpleQuery() {
const result = await CLIENT.query("SELECT * FROM ids;");
Expand Down

0 comments on commit f6dfefc

Please sign in to comment.