From f6dfefc192d03980ec0e88a74f6ad024ccf83c00 Mon Sep 17 00:00:00 2001 From: Andrei Skorokhod Date: Fri, 14 Jun 2019 07:32:03 -0400 Subject: [PATCH] added data_types --- test.ts | 2 +- tests/client.ts | 2 +- tests/data_types.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++ tests/decode.ts | 48 -------------------------------------- tests/helpers.ts | 22 ++++++++++++++++++ tests/queries.ts | 17 ++------------ 6 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 tests/data_types.ts delete mode 100644 tests/decode.ts create mode 100644 tests/helpers.ts diff --git a/test.ts b/test.ts index be6a6fa4..8f7420a2 100755 --- a/test.ts +++ b/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(); diff --git a/tests/client.ts b/tests/client.ts index 9425e4a7..46694ec3 100644 --- a/tests/client.ts +++ b/tests/client.ts @@ -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); diff --git a/tests/data_types.ts b/tests/data_types.ts new file mode 100644 index 00000000..dad892b0 --- /dev/null +++ b/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]]); +}); diff --git a/tests/decode.ts b/tests/decode.ts deleted file mode 100644 index 8d3f529c..00000000 --- a/tests/decode.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { test, assertEquals } from "../deps.ts"; -import { Column, Format } from "../connection.ts"; -import { Oid } from "../oid.ts"; -import { decode } from "../decode.ts"; - -const encoder = new TextEncoder(); - -test(function decodeInet() { - const column = new Column( - "inet_col", // name - 16410, // tableOid - 1, // index - Oid.inet, // dataTypeOid - -1, // columnLength - -1, // typeModifier - Format.TEXT // format - ); - const value = encoder.encode("127.0.0.1"); - assertEquals(decode(value, column), "127.0.0.1"); -}); - -test(function decodeMacaddr() { - const column = new Column( - "mac_col", // name - 16410, // tableOid - 2, // index - Oid.macaddr, // dataTypeOid - -1, // columnLength - -1, // typeModifier - Format.TEXT // format - ); - const value = encoder.encode("08:00:2b:01:02:03"); - assertEquals(decode(value, column), "08:00:2b:01:02:03"); -}); - -test(function decodeCidr() { - const column = new Column( - "cidr_col", // name - 16410, // tableOid - 2, // index - Oid.cidr, // dataTypeOid - -1, // columnLength - -1, // typeModifier - Format.TEXT // format - ); - const value = encoder.encode("192.168.100.128/25"); - assertEquals(decode(value, column), "192.168.100.128/25"); -}); diff --git a/tests/helpers.ts b/tests/helpers.ts new file mode 100644 index 00000000..fe3c0511 --- /dev/null +++ b/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 + ) { + 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 }); + }; +} diff --git a/tests/queries.ts b/tests/queries.ts index 6ac3ed7d..93f7a164 100644 --- a/tests/queries.ts +++ b/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) { - 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;");