Skip to content

Commit

Permalink
chore: add encoding strategy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo committed Feb 8, 2024
1 parent 0cd109f commit b5b877f
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 292 deletions.
11 changes: 6 additions & 5 deletions tests/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientConfiguration } from "../connection/connection_params.ts";
import { ClientConfiguration, ClientOptions } from "../connection/connection_params.ts";
import config_file1 from "./config.json" with { type: "json" };

type TcpConfiguration = Omit<ClientConfiguration, "connection"> & {
Expand Down Expand Up @@ -67,17 +67,18 @@ export const getClearSocketConfiguration = (): SocketConfiguration => {
};

/** MD5 authenticated user with privileged access to the database */
export const getMainConfiguration = (): TcpConfiguration => {
export const getMainConfiguration = (_config?:ClientOptions ): TcpConfiguration => {
return {
applicationName: config.postgres_md5.applicationName,
database: config.postgres_md5.database,
hostname: config.postgres_md5.hostname,
host_type: "tcp",
options: {},
password: config.postgres_md5.password,
user: config.postgres_md5.users.main,
..._config,
options: {},
port: config.postgres_md5.port,
tls: enabled_tls,
user: config.postgres_md5.users.main,
host_type: "tcp",
};
};

Expand Down
125 changes: 101 additions & 24 deletions tests/decode_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Column, decode } from "../query/decode.ts";
import {
decodeBigint,
decodeBigintArray,
Expand All @@ -17,6 +18,7 @@ import {
decodeTid,
} from "../query/decoders.ts";
import { assertEquals, assertThrows } from "./test_deps.ts";
import { Oid } from "../query/oid.ts";

Deno.test("decodeBigint", function () {
assertEquals(decodeBigint("18014398509481984"), 18014398509481984n);
Expand All @@ -25,9 +27,9 @@ Deno.test("decodeBigint", function () {
Deno.test("decodeBigintArray", function () {
assertEquals(
decodeBigintArray(
"{17365398509481972,9007199254740992,-10414398509481984}",
"{17365398509481972,9007199254740992,-10414398509481984}"
),
[17365398509481972n, 9007199254740992n, -10414398509481984n],
[17365398509481972n, 9007199254740992n, -10414398509481984n]
);
});

Expand Down Expand Up @@ -62,25 +64,25 @@ Deno.test("decodeBox", function () {
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}". Box must have only 2 point, 1 given.`,
`Invalid Box: "${testValue}". Box must have only 2 point, 1 given.`
);
testValue = "(12.4,2),(123,123,123),(9303,33)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}". Box must have only 2 point, 3 given.`,
`Invalid Box: "${testValue}". Box must have only 2 point, 3 given.`
);
testValue = "(0,0),(123,123,123)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}" : Invalid Point: "(123,123,123)". Points must have only 2 coordinates, 3 given.`,
`Invalid Box: "${testValue}" : Invalid Point: "(123,123,123)". Points must have only 2 coordinates, 3 given.`
);
testValue = "(0,0),(100,r100)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}" : Invalid Point: "(100,r100)". Coordinate "r100" must be a valid number.`,
`Invalid Box: "${testValue}" : Invalid Point: "(100,r100)". Coordinate "r100" must be a valid number.`
);
});

Expand All @@ -93,13 +95,13 @@ Deno.test("decodeCircle", function () {
assertThrows(
() => decodeCircle(testValue),
Error,
`Invalid Circle: "${testValue}" : Invalid Point: "(c21 23,2)". Coordinate "c21 23" must be a valid number.`,
`Invalid Circle: "${testValue}" : Invalid Point: "(c21 23,2)". Coordinate "c21 23" must be a valid number.`
);
testValue = "<(33,2),mn23 3.5>";
assertThrows(
() => decodeCircle(testValue),
Error,
`Invalid Circle: "${testValue}". Circle radius "mn23 3.5" must be a valid number.`,
`Invalid Circle: "${testValue}". Circle radius "mn23 3.5" must be a valid number.`
);
});

Expand All @@ -110,11 +112,11 @@ Deno.test("decodeDate", function () {
Deno.test("decodeDatetime", function () {
assertEquals(
decodeDatetime("2021-08-01"),
new Date("2021-08-01 00:00:00-00"),
new Date("2021-08-01 00:00:00-00")
);
assertEquals(
decodeDatetime("1997-12-17 07:37:16-08"),
new Date("1997-12-17 07:37:16-08"),
new Date("1997-12-17 07:37:16-08")
);
});

Expand All @@ -131,14 +133,14 @@ Deno.test("decodeInt", function () {
Deno.test("decodeJson", function () {
assertEquals(
decodeJson(
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true, "subkey_2": ["1",2]}}',
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true, "subkey_2": ["1",2]}}'
),
{
key_1: "MY VALUE",
key_2: null,
key_3: 10,
key_4: { subkey_1: true, subkey_2: ["1", 2] },
},
}
);
assertThrows(() => decodeJson("{ 'eqw' ; ddd}"));
});
Expand All @@ -149,13 +151,13 @@ Deno.test("decodeLine", function () {
assertThrows(
() => decodeLine("{100,50,0,100}"),
Error,
`Invalid Line: "${testValue}". Line in linear equation format must have 3 constants, 4 given.`,
`Invalid Line: "${testValue}". Line in linear equation format must have 3 constants, 4 given.`
);
testValue = "{100,d3km,0}";
assertThrows(
() => decodeLine(testValue),
Error,
`Invalid Line: "${testValue}". Line constant "d3km" must be a valid number.`,
`Invalid Line: "${testValue}". Line constant "d3km" must be a valid number.`
);
});

Expand All @@ -168,25 +170,25 @@ Deno.test("decodeLineSegment", function () {
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}" : Invalid Point: "(r344,350)". Coordinate "r344" must be a valid number.`,
`Invalid Line Segment: "${testValue}" : Invalid Point: "(r344,350)". Coordinate "r344" must be a valid number.`
);
testValue = "((100),(r344,350))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}" : Invalid Point: "(100)". Points must have only 2 coordinates, 1 given.`,
`Invalid Line Segment: "${testValue}" : Invalid Point: "(100)". Points must have only 2 coordinates, 1 given.`
);
testValue = "((100,50))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 1 given.`,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 1 given.`
);
testValue = "((100,50),(350,350),(100,100))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 3 given.`,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 3 given.`
);
});

Expand All @@ -204,13 +206,13 @@ Deno.test("decodePath", function () {
assertThrows(
() => decodePath(testValue),
Error,
`Invalid Path: "${testValue}" : Invalid Point: "(350,kjf334)". Coordinate "kjf334" must be a valid number.`,
`Invalid Path: "${testValue}" : Invalid Point: "(350,kjf334)". Coordinate "kjf334" must be a valid number.`
);
testValue = "((100,50,9949))";
assertThrows(
() => decodePath(testValue),
Error,
`Invalid Path: "${testValue}" : Invalid Point: "(100,50,9949)". Points must have only 2 coordinates, 3 given.`,
`Invalid Path: "${testValue}" : Invalid Point: "(100,50,9949)". Points must have only 2 coordinates, 3 given.`
);
});

Expand All @@ -220,25 +222,25 @@ Deno.test("decodePoint", function () {
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 1 given.`,
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 1 given.`
);
testValue = "(100.100,50,350)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 3 given.`,
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 3 given.`
);
testValue = "(1,r344)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Coordinate "r344" must be a valid number.`,
`Invalid Point: "${testValue}". Coordinate "r344" must be a valid number.`
);
testValue = "(cd 213ee,100)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Coordinate "cd 213ee" must be a valid number.`,
`Invalid Point: "${testValue}". Coordinate "cd 213ee" must be a valid number.`
);
});

Expand All @@ -248,3 +250,78 @@ Deno.test("decodeTid", function () {
29383838509481984n,
]);
});

Deno.test("decode_strategy", function () {
const testValues = [
{
value: "40",
column: new Column("test", 0, 0, Oid.int4, 0, 0, 0),
parsed: 40,
},
{
value: "my_value",
column: new Column("test", 0, 0, Oid.text, 0, 0, 0),
parsed: "my_value",
},
{
value: "[(100,50),(350,350)]",
column: new Column("test", 0, 0, Oid.path, 0, 0, 0),
parsed: [
{ x: "100", y: "50" },
{ x: "350", y: "350" },
],
},
{
value: '{"value_1","value_2","value_3"}',
column: new Column("test", 0, 0, Oid.text_array, 0, 0, 0),
parsed: ["value_1", "value_2", "value_3"],
},
{
value: "1997-12-17 07:37:16-08",
column: new Column("test", 0, 0, Oid.timestamp, 0, 0, 0),
parsed: new Date("1997-12-17 07:37:16-08"),
},
{
value: "Yes",
column: new Column("test", 0, 0, Oid.bool, 0, 0, 0),
parsed: true,
},
{
value: "<(12.4,2),3.5>",
column: new Column("test", 0, 0, Oid.circle, 0, 0, 0),
parsed: { point: { x: "12.4", y: "2" }, radius: "3.5" },
},
{
value: '{"test":1,"val":"foo","example":[1,2,false]}',
column: new Column("test", 0, 0, Oid.jsonb, 0, 0, 0),
parsed: { test: 1, val: "foo", example: [1, 2, false] },
},
{
value: "18014398509481984",
column: new Column("test", 0, 0, Oid.int8, 0, 0, 0),
parsed: 18014398509481984n,
},
{
value: "{3.14,1.11,0.43,200}",
column: new Column("test", 0, 0, Oid.float4_array, 0, 0, 0),
parsed: [3.14, 1.11, 0.43, 200],
},
];

for (const testValue of testValues) {
const encodedValue = new TextEncoder().encode(testValue.value);

// check default behavior
assertEquals(decode(encodedValue, testValue.column), testValue.parsed);
// check 'auto' behavior
assertEquals(
decode(encodedValue, testValue.column, { decode_strategy: "auto" }),
testValue.parsed
);
// check 'string' behavior
assertEquals(
decode(encodedValue, testValue.column, { decode_strategy: "string" }),
testValue.value
);
}
});
Loading

0 comments on commit b5b877f

Please sign in to comment.