Skip to main content
Module

x/postgres/tests/data_types_test.ts

PostgreSQL driver for Deno
Extremely Popular
Latest
File
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
import { assertEquals, base64, formatDate, parseDate } from "./test_deps.ts";import { getMainConfiguration } from "./config.ts";import { generateSimpleClientTest } from "./helpers.ts";import type { Box, Circle, // Float4, Float8, Line, LineSegment, Path, Point, Polygon, TID, Timestamp,} from "../query/types.ts";
// TODO// Find out how to test char types
/** * This will generate a random number with a precision of 2 */function generateRandomNumber(max_value: number) { return Math.round((Math.random() * max_value + Number.EPSILON) * 100) / 100;}
function generateRandomPoint(max_value = 100): Point { return { x: String(generateRandomNumber(max_value)) as Float8, y: String(generateRandomNumber(max_value)) as Float8, };}
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";function randomBase64(): string { return base64.encodeBase64( Array.from( { length: Math.ceil(Math.random() * 256) }, () => CHARS[Math.floor(Math.random() * CHARS.length)], ).join(""), );}
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;const timezone_utc = new Date().toTimeString().slice(12, 17);
const testClient = generateSimpleClientTest(getMainConfiguration());
Deno.test( "inet", testClient(async (client) => { const url = "127.0.0.1"; const selectRes = await client.queryArray( "SELECT $1::INET", [url], ); assertEquals(selectRes.rows[0], [url]); }),);
Deno.test( "inet array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( "SELECT '{ 127.0.0.1, 192.168.178.0/24 }'::inet[]", ); assertEquals(result_1[0], [["127.0.0.1", "192.168.178.0/24"]]);
const { rows: result_2 } = await client.queryArray( "SELECT '{{127.0.0.1},{192.168.178.0/24}}'::inet[]", ); assertEquals(result_2[0], [[["127.0.0.1"], ["192.168.178.0/24"]]]); }),);
Deno.test( "macaddr", testClient(async (client) => { const address = "08:00:2b:01:02:03";
const { rows } = await client.queryArray( "SELECT $1::MACADDR", [address], ); assertEquals(rows[0], [address]); }),);
Deno.test( "macaddr array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( "SELECT '{ 08:00:2b:01:02:03, 09:00:2b:01:02:04 }'::macaddr[]", ); assertEquals(result_1[0], [[ "08:00:2b:01:02:03", "09:00:2b:01:02:04", ]]);
const { rows: result_2 } = await client.queryArray( "SELECT '{{08:00:2b:01:02:03},{09:00:2b:01:02:04}}'::macaddr[]", ); assertEquals( result_2[0], [[["08:00:2b:01:02:03"], ["09:00:2b:01:02:04"]]], ); }),);
Deno.test( "cidr", testClient(async (client) => { const host = "192.168.100.128/25";
const { rows } = await client.queryArray( "SELECT $1::CIDR", [host], ); assertEquals(rows[0], [host]); }),);
Deno.test( "cidr array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( "SELECT '{ 10.1.0.0/16, 11.11.11.0/24 }'::cidr[]", ); assertEquals(result_1[0], [["10.1.0.0/16", "11.11.11.0/24"]]);
const { rows: result_2 } = await client.queryArray( "SELECT '{{10.1.0.0/16},{11.11.11.0/24}}'::cidr[]", ); assertEquals(result_2[0], [[["10.1.0.0/16"], ["11.11.11.0/24"]]]); }),);
Deno.test( "name", testClient(async (client) => { const name = "some"; const result = await client.queryArray(`SELECT $1::name`, [name]); assertEquals(result.rows[0], [name]); }),);
Deno.test( "name array", testClient(async (client) => { const result = await client.queryArray( `SELECT ARRAY['some'::name, 'none']`, ); assertEquals(result.rows[0], [["some", "none"]]); }),);
Deno.test( "oid", testClient(async (client) => { const result = await client.queryArray(`SELECT 1::oid`); assertEquals(result.rows[0][0], "1"); }),);
Deno.test( "oid array", testClient(async (client) => { const result = await client.queryArray(`SELECT ARRAY[1::oid, 452, 1023]`); assertEquals(result.rows[0][0], ["1", "452", "1023"]); }),);
Deno.test( "regproc", testClient(async (client) => { const result = await client.queryArray(`SELECT 'now'::regproc`); assertEquals(result.rows[0][0], "now"); }),);
Deno.test( "regproc array", testClient(async (client) => { const result = await client.queryArray( `SELECT ARRAY['now'::regproc, 'timeofday']`, ); assertEquals(result.rows[0][0], ["now", "timeofday"]); }),);
Deno.test( "regprocedure", testClient(async (client) => { const result = await client.queryArray( `SELECT 'sum(integer)'::regprocedure`, ); assertEquals(result.rows[0][0], "sum(integer)"); }),);
Deno.test( "regprocedure array", testClient(async (client) => { const result = await client.queryArray( `SELECT ARRAY['sum(integer)'::regprocedure, 'max(integer)']`, ); assertEquals(result.rows[0][0], ["sum(integer)", "max(integer)"]); }),);
Deno.test( "regoper", testClient(async (client) => { const operator = "!!";
const { rows } = await client.queryObject({ args: [operator], fields: ["result"], text: "SELECT $1::regoper", });
assertEquals(rows[0], { result: operator }); }),);
Deno.test( "regoper array", testClient(async (client) => { const operator_1 = "!!"; const operator_2 = "|/";
const { rows } = await client.queryObject({ args: [operator_1, operator_2], fields: ["result"], text: "SELECT ARRAY[$1::regoper, $2]", });
assertEquals(rows[0], { result: [operator_1, operator_2] }); }),);
Deno.test( "regoperator", testClient(async (client) => { const regoperator = "-(NONE,integer)";
const { rows } = await client.queryObject({ args: [regoperator], fields: ["result"], text: "SELECT $1::regoperator", });
assertEquals(rows[0], { result: regoperator }); }),);
Deno.test( "regoperator array", testClient(async (client) => { const regoperator_1 = "-(NONE,integer)"; const regoperator_2 = "*(integer,integer)";
const { rows } = await client.queryObject({ args: [regoperator_1, regoperator_2], fields: ["result"], text: "SELECT ARRAY[$1::regoperator, $2]", });
assertEquals(rows[0], { result: [regoperator_1, regoperator_2] }); }),);
Deno.test( "regclass", testClient(async (client) => { const object_name = "TEST_REGCLASS";
await client.queryArray(`CREATE TEMP TABLE ${object_name} (X INT)`);
const result = await client.queryObject<{ table_name: string }>({ args: [object_name], fields: ["table_name"], text: "SELECT $1::REGCLASS", });
assertEquals(result.rows.length, 1); // Objects in postgres are case insensitive unless indicated otherwise assertEquals( result.rows[0].table_name.toLowerCase(), object_name.toLowerCase(), ); }),);
Deno.test( "regclass array", testClient(async (client) => { const object_1 = "TEST_REGCLASS_1"; const object_2 = "TEST_REGCLASS_2";
await client.queryArray(`CREATE TEMP TABLE ${object_1} (X INT)`); await client.queryArray(`CREATE TEMP TABLE ${object_2} (X INT)`);
const { rows: result } = await client.queryObject< { tables: [string, string] } >({ args: [object_1, object_2], fields: ["tables"], text: "SELECT ARRAY[$1::REGCLASS, $2]", });
assertEquals(result.length, 1); assertEquals(result[0].tables.length, 2); // Objects in postgres are case insensitive unless indicated otherwise assertEquals( result[0].tables.map((x) => x.toLowerCase()), [object_1, object_2].map((x) => x.toLowerCase()), ); }),);
Deno.test( "regtype", testClient(async (client) => { const result = await client.queryArray(`SELECT 'integer'::regtype`); assertEquals(result.rows[0][0], "integer"); }),);
Deno.test( "regtype array", testClient(async (client) => { const result = await client.queryArray( `SELECT ARRAY['integer'::regtype, 'bigint']`, ); assertEquals(result.rows[0][0], ["integer", "bigint"]); }),);
// TODO// Refactor test to look for users directly in the database instead// of relying on configDeno.test( "regrole", testClient(async (client) => { const user = getMainConfiguration().user;
const result = await client.queryArray( `SELECT ($1)::regrole`, [user], );
assertEquals(result.rows[0][0], user); }),);
Deno.test( "regrole array", testClient(async (client) => { const user = getMainConfiguration().user;
const result = await client.queryArray( `SELECT ARRAY[($1)::regrole]`, [user], );
assertEquals(result.rows[0][0], [user]); }),);
Deno.test( "regnamespace", testClient(async (client) => { const result = await client.queryArray(`SELECT 'public'::regnamespace;`); assertEquals(result.rows[0][0], "public"); }),);
Deno.test( "regnamespace array", testClient(async (client) => { const result = await client.queryArray( `SELECT ARRAY['public'::regnamespace, 'pg_catalog'];`, ); assertEquals(result.rows[0][0], ["public", "pg_catalog"]); }),);
Deno.test( "regconfig", testClient(async (client) => { const result = await client.queryArray(`SElECT 'english'::regconfig`); assertEquals(result.rows, [["english"]]); }),);
Deno.test( "regconfig array", testClient(async (client) => { const result = await client.queryArray( `SElECT ARRAY['english'::regconfig, 'spanish']`, ); assertEquals(result.rows[0][0], ["english", "spanish"]); }),);
Deno.test( "regdictionary", testClient(async (client) => { const result = await client.queryArray("SELECT 'simple'::regdictionary"); assertEquals(result.rows[0][0], "simple"); }),);
Deno.test( "regdictionary array", testClient(async (client) => { const result = await client.queryArray( "SELECT ARRAY['simple'::regdictionary]", ); assertEquals(result.rows[0][0], ["simple"]); }),);
Deno.test( "bigint", testClient(async (client) => { const result = await client.queryArray("SELECT 9223372036854775807"); assertEquals(result.rows[0][0], 9223372036854775807n); }),);
Deno.test( "bigint array", testClient(async (client) => { const result = await client.queryArray( "SELECT ARRAY[9223372036854775807, 789141]", ); assertEquals(result.rows[0][0], [9223372036854775807n, 789141n]); }),);
Deno.test( "numeric", testClient(async (client) => { const number = "1234567890.1234567890"; const result = await client.queryArray(`SELECT $1::numeric`, [number]); assertEquals(result.rows[0][0], number); }),);
Deno.test( "numeric array", testClient(async (client) => { const numeric = ["1234567890.1234567890", "6107693.123123124"]; const result = await client.queryArray( `SELECT ARRAY[$1::numeric, $2]`, [numeric[0], numeric[1]], ); assertEquals(result.rows[0][0], numeric); }),);
Deno.test( "integer", testClient(async (client) => { const int = 17;
const { rows: result } = await client.queryObject({ args: [int], fields: ["result"], text: "SELECT $1::INTEGER", });
assertEquals(result[0], { result: int }); }),);
Deno.test( "integer array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( "SELECT '{1,100}'::int[]", ); assertEquals(result_1[0], [[1, 100]]);
const { rows: result_2 } = await client.queryArray( "SELECT '{{1},{100}}'::int[]", ); assertEquals(result_2[0], [[[1], [100]]]); }),);
Deno.test( "char", testClient(async (client) => { await client.queryArray( `CREATE TEMP TABLE CHAR_TEST (X CHARACTER(2));`, ); await client.queryArray( `INSERT INTO CHAR_TEST (X) VALUES ('A');`, ); const result = await client.queryArray( `SELECT X FROM CHAR_TEST`, ); assertEquals(result.rows[0][0], "A "); }),);
Deno.test( "char array", testClient(async (client) => { const result = await client.queryArray( `SELECT '{"x","Y"}'::char[]`, ); assertEquals(result.rows[0][0], ["x", "Y"]); }),);
Deno.test( "text", testClient(async (client) => { const result = await client.queryArray( `SELECT 'ABCD'::text`, ); assertEquals(result.rows[0], ["ABCD"]); }),);
Deno.test( "text array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( `SELECT '{"(ZYX)-123-456","(ABC)-987-654"}'::text[]`, ); assertEquals(result_1[0], [["(ZYX)-123-456", "(ABC)-987-654"]]);
const { rows: result_2 } = await client.queryArray( `SELECT '{{"(ZYX)-123-456"},{"(ABC)-987-654"}}'::text[]`, ); assertEquals(result_2[0], [[["(ZYX)-123-456"], ["(ABC)-987-654"]]]); }),);
Deno.test( "varchar", testClient(async (client) => { const result = await client.queryArray( `SELECT 'ABC'::varchar`, ); assertEquals(result.rows[0][0], "ABC"); }),);
Deno.test( "varchar array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( `SELECT '{"(ZYX)-(PQR)-456","(ABC)-987-(?=+)"}'::varchar[]`, ); assertEquals(result_1[0], [["(ZYX)-(PQR)-456", "(ABC)-987-(?=+)"]]);
const { rows: result_2 } = await client.queryArray( `SELECT '{{"(ZYX)-(PQR)-456"},{"(ABC)-987-(?=+)"}}'::varchar[]`, ); assertEquals(result_2[0], [[["(ZYX)-(PQR)-456"], ["(ABC)-987-(?=+)"]]]); }),);
Deno.test( "uuid", testClient(async (client) => { const uuid_text = "c4792ecb-c00a-43a2-bd74-5b0ed551c599"; const result = await client.queryArray(`SELECT $1::uuid`, [uuid_text]); assertEquals(result.rows[0][0], uuid_text); }),);
Deno.test( "uuid array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( `SELECT '{"c4792ecb-c00a-43a2-bd74-5b0ed551c599", "c9dd159e-d3d7-4bdf-b0ea-e51831c28e9b"}'::uuid[]`, ); assertEquals( result_1[0], [[ "c4792ecb-c00a-43a2-bd74-5b0ed551c599", "c9dd159e-d3d7-4bdf-b0ea-e51831c28e9b", ]], );
const { rows: result_2 } = await client.queryArray( `SELECT '{{"c4792ecb-c00a-43a2-bd74-5b0ed551c599"}, {"c9dd159e-d3d7-4bdf-b0ea-e51831c28e9b"}}'::uuid[]`, ); assertEquals( result_2[0], [[ ["c4792ecb-c00a-43a2-bd74-5b0ed551c599"], ["c9dd159e-d3d7-4bdf-b0ea-e51831c28e9b"], ]], ); }),);
Deno.test( "void", testClient(async (client) => { const result = await client.queryArray`SELECT PG_SLEEP(0.01)`; // `pg_sleep()` returns void. assertEquals(result.rows, [[""]]); }),);
Deno.test( "bpchar", testClient(async (client) => { const result = await client.queryArray( "SELECT cast('U7DV6WQ26D7X2IILX5L4LTYMZUKJ5F3CEDDQV3ZSLQVYNRPX2WUA' as char(52));", ); assertEquals( result.rows, [["U7DV6WQ26D7X2IILX5L4LTYMZUKJ5F3CEDDQV3ZSLQVYNRPX2WUA"]], ); }),);
Deno.test( "bpchar array", testClient(async (client) => { const { rows: result_1 } = await client.queryArray( `SELECT '{"AB1234","4321BA"}'::bpchar[]`, ); assertEquals(result_1[0], [["AB1234", "4321BA"]]);
const { rows: result_2 } = await client.queryArray( `SELECT '{{"AB1234"},{"4321BA"}}'::bpchar[]`, ); assertEquals(result_2[0], [[["AB1234"], ["4321BA"]]]); }),);
Deno.test( "bool", testClient(async (client) => { const result = await client.queryArray( `SELECT bool('y')`, ); assertEquals(result.rows[0][0], true); }),);
Deno.test( "bool array", testClient(async (client) => { const result = await client.queryArray( `SELECT array[bool('y'), bool('n'), bool('1'), bool('0')]`, ); assertEquals(result.rows[0][0], [true, false, true, false]); }),);
Deno.test( "bytea", testClient(async (client) => { const base64_string = randomBase64();
const result = await client.queryArray( `SELECT decode('${base64_string}','base64')`, );
assertEquals(result.rows[0][0], base64.decodeBase64(base64_string)); }),);
Deno.test( "bytea array", testClient(async (client) => { const strings = Array.from( { length: Math.ceil(Math.random() * 10) }, randomBase64, );
const result = await client.queryArray( `SELECT array[ ${ strings.map((x) => `decode('${x}', 'base64')`).join(", ") } ]`, );
assertEquals( result.rows[0][0], strings.map(base64.decodeBase64), ); }),);
Deno.test( "point", testClient(async (client) => { const selectRes = await client.queryArray<[Point]>( "SELECT point(1, 2.5)", ); assertEquals(selectRes.rows, [[{ x: "1", y: "2.5" }]]); }),);
Deno.test( "point array", testClient(async (client) => { const result1 = await client.queryArray( `SELECT '{"(1, 2)","(3.5, 4.1)"}'::point[]`, ); assertEquals(result1.rows, [ [[{ x: "1", y: "2" }, { x: "3.5", y: "4.1" }]], ]);
const result2 = await client.queryArray( `SELECT array[ array[ point(1,2), point(3.5, 4.1) ], array[ point(25, 50), point(-10, -17.5) ] ]`, ); assertEquals(result2.rows[0], [ [ [{ x: "1", y: "2" }, { x: "3.5", y: "4.1" }], [{ x: "25", y: "50" }, { x: "-10", y: "-17.5" }], ], ]); }),);
Deno.test( "time", testClient(async (client) => { const result = await client.queryArray("SELECT '01:01:01'::TIME");
assertEquals(result.rows[0][0], "01:01:01"); }),);
Deno.test( "time array", testClient(async (client) => { const result = await client.queryArray("SELECT ARRAY['01:01:01'::TIME]");
assertEquals(result.rows[0][0], ["01:01:01"]); }),);
Deno.test( "timestamp", testClient(async (client) => { const date = "1999-01-08 04:05:06"; const result = await client.queryArray<[Timestamp]>( "SELECT $1::TIMESTAMP, 'INFINITY'::TIMESTAMP", [date], );
assertEquals(result.rows[0], [new Date(date), Infinity]); }),);
Deno.test( "timestamp array", testClient(async (client) => { const timestamps = [ "2011-10-05T14:48:00.00", new Date().toISOString().slice(0, -1), ];
const { rows: result } = await client.queryArray<[[Date, Date]]>( "SELECT ARRAY[$1::TIMESTAMP, $2]", timestamps, );
assertEquals(result[0][0], timestamps.map((x) => new Date(x))); }),);
Deno.test( "timestamptz", testClient(async (client) => { const timestamp = "1999-01-08 04:05:06+02"; const result = await client.queryArray<[Timestamp]>( "SELECT $1::TIMESTAMPTZ, 'INFINITY'::TIMESTAMPTZ", [timestamp], );
assertEquals(result.rows[0], [new Date(timestamp), Infinity]); }),);
Deno.test( "timestamptz array", testClient(async (client) => { const timestamps = [ "2012/04/10 10:10:30 +0000", new Date().toISOString(), ];
const result = await client.queryArray<[[Timestamp, Timestamp]]>( `SELECT ARRAY[$1::TIMESTAMPTZ, $2]`, timestamps, );
assertEquals(result.rows[0][0], [ new Date(timestamps[0]), new Date(timestamps[1]), ]); }),);
Deno.test( "timetz", testClient(async (client) => { const result = await client.queryArray<[string]>( `SELECT '01:01:01${timezone_utc}'::TIMETZ`, );
assertEquals(result.rows[0][0].slice(0, 8), "01:01:01"); }),);
Deno.test( "timetz array", testClient(async (client) => { const result = await client.queryArray<[string]>( `SELECT ARRAY['01:01:01${timezone_utc}'::TIMETZ]`, );
assertEquals(typeof result.rows[0][0][0], "string");
assertEquals(result.rows[0][0][0].slice(0, 8), "01:01:01"); }),);
Deno.test( "xid", testClient(async (client) => { const result = await client.queryArray("SELECT '1'::xid");
assertEquals(result.rows[0][0], 1); }),);
Deno.test( "xid array", testClient(async (client) => { const result = await client.queryArray( "SELECT ARRAY['12'::xid, '4789'::xid]", );
assertEquals(result.rows[0][0], [12, 4789]); }),);
Deno.test( "float4", testClient(async (client) => { const result = await client.queryArray<[number, number]>( "SELECT '1'::FLOAT4, '17.89'::FLOAT4", );
assertEquals(result.rows[0], [1, 17.89]); }),);
Deno.test( "float4 array", testClient(async (client) => { const result = await client.queryArray<[[number, number]]>( "SELECT ARRAY['12.25'::FLOAT4, '4789']", );
assertEquals(result.rows[0][0], [12.25, 4789]); }),);
Deno.test( "float8", testClient(async (client) => { const result = await client.queryArray<[Float8, Float8]>( "SELECT '1'::FLOAT8, '17.89'::FLOAT8", );
assertEquals(result.rows[0], ["1", "17.89"]); }),);
Deno.test( "float8 array", testClient(async (client) => { const result = await client.queryArray<[[Float8, Float8]]>( "SELECT ARRAY['12.25'::FLOAT8, '4789']", );
assertEquals(result.rows[0][0], ["12.25", "4789"]); }),);
Deno.test( "tid", testClient(async (client) => { const result = await client.queryArray<[TID, TID]>( "SELECT '(1, 19)'::TID, '(23, 17)'::TID", );
assertEquals(result.rows[0], [[1n, 19n], [23n, 17n]]); }),);
Deno.test( "tid array", testClient(async (client) => { const result = await client.queryArray<[[TID, TID]]>( "SELECT ARRAY['(4681, 1869)'::TID, '(0, 17476)']", );
assertEquals(result.rows[0][0], [[4681n, 1869n], [0n, 17476n]]); }),);
Deno.test( "date", testClient(async (client) => { await client.queryArray(`SET SESSION TIMEZONE TO '${timezone}'`); const date_text = "2020-01-01";
const result = await client.queryArray<[Timestamp, Timestamp]>( "SELECT $1::DATE, 'Infinity'::Date", [date_text], );
assertEquals(result.rows[0], [ parseDate(date_text, "yyyy-MM-dd"), Infinity, ]); }),);
Deno.test( "date array", testClient(async (client) => { await client.queryArray(`SET SESSION TIMEZONE TO '${timezone}'`); const dates = ["2020-01-01", formatDate(new Date(), "yyyy-MM-dd")];
const { rows: result } = await client.queryArray<[[Date, Date]]>( "SELECT ARRAY[$1::DATE, $2]", dates, );
assertEquals( result[0][0], dates.map((d) => parseDate(d, "yyyy-MM-dd")), ); }),);
Deno.test( "line", testClient(async (client) => { const result = await client.queryArray<[Line]>( "SELECT '[(1, 2), (3, 4)]'::LINE", );
assertEquals(result.rows[0][0], { a: "1", b: "-1", c: "1" }); }),);
Deno.test( "line array", testClient(async (client) => { const result = await client.queryArray<[[Line, Line]]>( "SELECT ARRAY['[(1, 2), (3, 4)]'::LINE, '41, 1, -9, 25.5']", );
assertEquals(result.rows[0][0], [ { a: "1", b: "-1", c: "1" }, { a: "-0.49", b: "-1", c: "21.09", }, ]); }),);
Deno.test( "line segment", testClient(async (client) => { const result = await client.queryArray<[LineSegment]>( "SELECT '[(1, 2), (3, 4)]'::LSEG", );
assertEquals(result.rows[0][0], { a: { x: "1", y: "2" }, b: { x: "3", y: "4" }, }); }),);
Deno.test( "line segment array", testClient(async (client) => { const result = await client.queryArray<[[LineSegment, LineSegment]]>( "SELECT ARRAY['[(1, 2), (3, 4)]'::LSEG, '41, 1, -9, 25.5']", );
assertEquals(result.rows[0][0], [ { a: { x: "1", y: "2" }, b: { x: "3", y: "4" }, }, { a: { x: "41", y: "1" }, b: { x: "-9", y: "25.5" }, }, ]); }),);
Deno.test( "box", testClient(async (client) => { const result = await client.queryArray<[Box]>( "SELECT '((1, 2), (3, 4))'::BOX", );
assertEquals(result.rows[0][0], { a: { x: "3", y: "4" }, b: { x: "1", y: "2" }, }); }),);
Deno.test( "box array", testClient(async (client) => { const result = await client.queryArray<[[Box, Box]]>( "SELECT ARRAY['(1, 2), (3, 4)'::BOX, '41, 1, -9, 25.5']", );
assertEquals(result.rows[0][0], [ { a: { x: "3", y: "4" }, b: { x: "1", y: "2" }, }, { a: { x: "41", y: "25.5" }, b: { x: "-9", y: "1" }, }, ]); }),);
Deno.test( "path", testClient(async (client) => { const points = Array.from( { length: Math.floor((Math.random() + 1) * 10) }, generateRandomPoint, );
const selectRes = await client.queryArray<[Path]>( `SELECT '(${points.map(({ x, y }) => `(${x},${y})`).join(",")})'::PATH`, );
assertEquals(selectRes.rows[0][0], points); }),);
Deno.test( "path array", testClient(async (client) => { const points = Array.from( { length: Math.floor((Math.random() + 1) * 10) }, generateRandomPoint, );
const selectRes = await client.queryArray<[[Path]]>( `SELECT ARRAY['(${ points.map(({ x, y }) => `(${x},${y})`).join(",") })'::PATH]`, );
assertEquals(selectRes.rows[0][0][0], points); }),);
Deno.test( "polygon", testClient(async (client) => { const points = Array.from( { length: Math.floor((Math.random() + 1) * 10) }, generateRandomPoint, );
const selectRes = await client.queryArray<[Polygon]>( `SELECT '(${ points.map(({ x, y }) => `(${x},${y})`).join(",") })'::POLYGON`, );
assertEquals(selectRes.rows[0][0], points); }),);
Deno.test( "polygon array", testClient(async (client) => { const points = Array.from( { length: Math.floor((Math.random() + 1) * 10) }, generateRandomPoint, );
const selectRes = await client.queryArray<[[Polygon]]>( `SELECT ARRAY['(${ points.map(({ x, y }) => `(${x},${y})`).join(",") })'::POLYGON]`, );
assertEquals(selectRes.rows[0][0][0], points); }),);
Deno.test( "circle", testClient(async (client) => { const point = generateRandomPoint(); const radius = String(generateRandomNumber(100));
const { rows } = await client.queryArray<[Circle]>( `SELECT '<(${point.x},${point.y}), ${radius}>'::CIRCLE`, );
assertEquals(rows[0][0], { point, radius }); }),);
Deno.test( "circle array", testClient(async (client) => { const point = generateRandomPoint(); const radius = String(generateRandomNumber(100));
const { rows } = await client.queryArray<[[Circle]]>( `SELECT ARRAY['<(${point.x},${point.y}), ${radius}>'::CIRCLE]`, );
assertEquals(rows[0][0][0], { point, radius }); }),);
Deno.test( "unhandled type", testClient(async (client) => { const { rows: exists } = await client.queryArray( "SELECT EXISTS (SELECT TRUE FROM PG_TYPE WHERE UPPER(TYPNAME) = 'DIRECTION')", ); if (exists[0][0]) { await client.queryArray("DROP TYPE DIRECTION;"); } await client.queryArray( "CREATE TYPE DIRECTION AS ENUM ( 'LEFT', 'RIGHT' )", ); const { rows: result } = await client.queryArray( "SELECT 'LEFT'::DIRECTION;", ); await client.queryArray("DROP TYPE DIRECTION;");
assertEquals(result[0][0], "LEFT"); }),);
Deno.test( "json", testClient(async (client) => { const result = await client .queryArray`SELECT JSON_BUILD_OBJECT( 'X', '1' )`;
assertEquals(result.rows[0], [{ X: "1" }]); }),);
Deno.test( "json array", testClient(async (client) => { const json_array = await client.queryArray( `SELECT ARRAY_AGG(A) FROM ( SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A UNION ALL SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A ) A`, );
assertEquals(json_array.rows[0][0], [{ X: "1" }, { Y: "2" }]);
const jsonArrayNested = await client.queryArray( `SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM ( SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A UNION ALL SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A ) A`, );
assertEquals( jsonArrayNested.rows[0][0], [ [ [{ X: "1" }, { Y: "2" }], [{ X: "1" }, { Y: "2" }], ], [ [{ X: "1" }, { Y: "2" }], [{ X: "1" }, { Y: "2" }], ], ], ); }),);