From 32eaf0152d7bee51997a63d357cac146bb1ad8fc Mon Sep 17 00:00:00 2001 From: j3lte Date: Wed, 6 Dec 2023 23:26:31 +0100 Subject: [PATCH] Improve testing --- src/SelectImpl.ts | 3 ++- test/Select.test.ts | 23 ++++++++++++++++++++++- test/utils.test.ts | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/SelectImpl.ts b/src/SelectImpl.ts index db1f369..5a39838 100644 --- a/src/SelectImpl.ts +++ b/src/SelectImpl.ts @@ -74,7 +74,8 @@ export class SelectImpl { } get fieldName(): string { - return this.field ?? this.fieldObj?.name ?? "*"; + // this.field is only null when fieldObj is not null + return this.field ?? this.fieldObj!.name; } get value(): string { diff --git a/test/Select.test.ts b/test/Select.test.ts index 693e266..ef091e8 100644 --- a/test/Select.test.ts +++ b/test/Select.test.ts @@ -10,6 +10,7 @@ import { } from "../src/Select.ts"; import { DataType } from "../src/types.ts"; import { Field } from "../src/Field.ts"; +import { SelectFunction } from "../src/SelectImpl.ts"; const createField = (type: T, name = "test") => Field(name, type); @@ -22,10 +23,26 @@ Deno.test("Select (empty)", () => { }); Deno.test("Select (error)", () => { - // @ts-ignore: This is a pure test, wouldn't fly in Typescript, but we got to test it for Javascript + // @ts-expect-error - This is a pure test, wouldn't fly in Typescript, but we got to test it for Javascript assertThrows(() => Select(1)); }); +Deno.test("Select (setFunc)", () => { + const select = Select("test"); + + select.setFunc(SelectFunction.Log, "test"); + + assertEquals(select.fieldName, "test"); + assertEquals(select.value, "ln(test, test)"); + assertEquals(select.toString(), "ln(test, test)"); + + select.setFunc(SelectFunction.Log); + + assertEquals(select.fieldName, "test"); + assertEquals(select.value, "ln(test)"); + assertEquals(select.toString(), "ln(test)"); +}); + Deno.test("Select (basic string field)", () => { const select = Select("test"); @@ -44,6 +61,7 @@ Deno.test("Select (Field obj)", () => { Deno.test("Select (count)", () => { const select = Select(createField(DataType.Number)).count(); + assertEquals(select.fieldName, "test"); assertEquals(select.value, "count(test)"); }); @@ -144,6 +162,9 @@ Deno.test("Select (Text functions", () => { assertEquals(createSelect().pad(1, "a", "RIGHT").value, "pad_right(test, 1, 'a')"); assertThrows(() => createSelect(DataType.Number).pad(1, "a", "RIGHT")); + + // @ts-expect-error - testing invalid types + assertThrows(() => createSelect().pad(1, "a", "CENTER")); }); Deno.test("Select (Spatial functions)", () => { diff --git a/test/utils.test.ts b/test/utils.test.ts index a03f846..a200914 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -60,6 +60,12 @@ Deno.test("Utils::params::replaceParams", () => { }); Deno.test("Utils::qs::toQS", () => { + assertEquals(toQS({ test: "test" }), "test=test"); + assertEquals(toQS([{ test: "test" }, { test2: "test2" }]), "0[test]=test&1[test2]=test2"); + assertEquals( + toQS([{ test: "test" }, { test2: { "test1": "test1" } }]), + "0[test]=test&1[test2][test1]=test1", + ); assertEquals(toQS({ test: "test" }), "test=test"); assertEquals(toQS({ test: "test", test2: "test3" }), "test=test&test2=test3"); assertEquals(toQS({ test: "test", test2: "test3", test3: 1 }), "test=test&test2=test3&test3=1");