From f1b4a396ec8b3646404e0338e83b05a3adef19c3 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 30 Jun 2025 21:19:24 +0100 Subject: [PATCH] feat: Implement EqualTo class and related tests for comparison functionality --- .../CodeExamples/DataTypes/EqualTo.md | 6 +- Common/Tests/Types/Database/EqualTo.test.ts | 75 +++++++++++++++++++ Common/Tests/Types/JSON.test.ts | 1 + Common/Types/BaseDatabase/EqualTo.ts | 31 ++++++++ Common/Types/JSON.ts | 4 + Common/Types/SerializableObjectDictionary.ts | 2 + 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 Common/Tests/Types/Database/EqualTo.test.ts create mode 100644 Common/Types/BaseDatabase/EqualTo.ts diff --git a/APIReference/CodeExamples/DataTypes/EqualTo.md b/APIReference/CodeExamples/DataTypes/EqualTo.md index 0beda73f07..abef1fe8fb 100644 --- a/APIReference/CodeExamples/DataTypes/EqualTo.md +++ b/APIReference/CodeExamples/DataTypes/EqualTo.md @@ -1,6 +1,8 @@ { "query": { - "name": "Hello", - // other filters + "age": { + "_type": "EqualTo", + value: 10 + } } } \ No newline at end of file diff --git a/Common/Tests/Types/Database/EqualTo.test.ts b/Common/Tests/Types/Database/EqualTo.test.ts new file mode 100644 index 0000000000..8a988f58fb --- /dev/null +++ b/Common/Tests/Types/Database/EqualTo.test.ts @@ -0,0 +1,75 @@ +import EqualTo from "../../../Types/BaseDatabase/EqualTo"; +import BadDataException from "../../../Types/Exception/BadDataException"; +import { JSONObject } from "../../../Types/JSON"; +import { describe, expect, it } from "@jest/globals"; + +describe("EqualTo", () => { + it("should create an EqualTo object with a valid value", () => { + const value: string = "oneuptime"; + const equalObj: EqualTo = new EqualTo(value); + expect(equalObj.value).toBe(value); + }); + + it("should get the value property of an EqualTo object", () => { + const value: string = "oneuptime"; + const equalObj: EqualTo = new EqualTo(value); + expect(equalObj.value).toBe(value); + }); + + it("should set the value property of an EqualTo object", () => { + const equalObj: EqualTo = new EqualTo("oldValue"); + equalObj.value = "newValue"; + expect(equalObj.value).toBe("newValue"); + }); + + it("should return the correct string representation using toString method", () => { + const equalObj: EqualTo = new EqualTo("oneuptime"); + expect(equalObj.toString()).toBe("oneuptime"); + }); + + it("should generate the correct JSON representation using toJSON method", () => { + const equalObj: EqualTo = new EqualTo("oneuptime"); + const expectedJSON: JSONObject = { + _type: "EqualTo", + value: "oneuptime", + }; + expect(equalObj.toJSON()).toEqual(expectedJSON); + }); + + it("should create an EqualTo object from valid JSON input", () => { + const jsonInput: JSONObject = { + _type: "EqualTo", + value: "oneuptime", + }; + const equalObj: EqualTo = EqualTo.fromJSON(jsonInput); + expect(equalObj.value).toBe("oneuptime"); + }); + + it("should throw a BadDataException when using invalid JSON input", () => { + const jsonInput: JSONObject = { + _type: "InvalidType", + value: "oneuptime", + }; + expect(() => { + return EqualTo.fromJSON(jsonInput); + }).toThrow(BadDataException); + }); + + it("should be a type of EqualTo", () => { + const equalObj: EqualTo = new EqualTo("oneuptime"); + expect(equalObj).toBeInstanceOf(EqualTo); + }); + + it("should handle numeric values", () => { + const value: number = 42; + const equalObj: EqualTo = new EqualTo(value); + expect(equalObj.value).toBe(value); + expect(equalObj.toString()).toBe("42"); + }); + + it("should handle date values", () => { + const value: Date = new Date("2023-01-01"); + const equalObj: EqualTo = new EqualTo(value); + expect(equalObj.value).toBe(value); + }); +}); diff --git a/Common/Tests/Types/JSON.test.ts b/Common/Tests/Types/JSON.test.ts index ca326a0791..c51e8eeec8 100644 --- a/Common/Tests/Types/JSON.test.ts +++ b/Common/Tests/Types/JSON.test.ts @@ -4,6 +4,7 @@ describe("ObjectType", () => { const expectedFields: Array = [ "ObjectID", "Name", + "EqualTo", "EqualToOrNull", "NotEqual", "Email", diff --git a/Common/Types/BaseDatabase/EqualTo.ts b/Common/Types/BaseDatabase/EqualTo.ts new file mode 100644 index 0000000000..1f90bd7890 --- /dev/null +++ b/Common/Types/BaseDatabase/EqualTo.ts @@ -0,0 +1,31 @@ +import CompareBase, { CompareType } from "../Database/CompareBase"; +import BadDataException from "../Exception/BadDataException"; +import { JSONObject, ObjectType } from "../JSON"; + +export default class EqualTo extends CompareBase { + public constructor(value: T) { + super(value); + this.value = value; + } + + public override toString(): string { + return this.value.toString(); + } + + public override toJSON(): JSONObject { + return { + _type: ObjectType.EqualTo, + value: (this as EqualTo).toString(), + }; + } + + public static override fromJSON( + json: JSONObject, + ): EqualTo { + if (json["_type"] === ObjectType.EqualTo) { + return new EqualTo(json["value"] as T); + } + + throw new BadDataException("Invalid JSON: " + JSON.stringify(json)); + } +} diff --git a/Common/Types/JSON.ts b/Common/Types/JSON.ts index cce77e6aef..a0a51f07f9 100644 --- a/Common/Types/JSON.ts +++ b/Common/Types/JSON.ts @@ -1,6 +1,7 @@ import Hostname from "./API/Hostname"; import Route from "./API/Route"; import URL from "./API/URL"; +import EqualTo from "./BaseDatabase/EqualTo"; import EqualToOrNull from "./BaseDatabase/EqualToOrNull"; import GreaterThan from "./BaseDatabase/GreaterThan"; import GreaterThanOrEqual from "./BaseDatabase/GreaterThanOrEqual"; @@ -35,6 +36,7 @@ export enum ObjectType { ObjectID = "ObjectID", Decimal = "Decimal", Name = "Name", + EqualTo = "EqualTo", EqualToOrNull = "EqualToOrNull", MonitorSteps = "MonitorSteps", MonitorStep = "MonitorStep", @@ -115,6 +117,8 @@ export type JSONValue = | Domain | Array | Array> + | EqualTo + | Array> | EqualToOrNull | Array> | NotEqual diff --git a/Common/Types/SerializableObjectDictionary.ts b/Common/Types/SerializableObjectDictionary.ts index b03b03849c..5de2d94c93 100644 --- a/Common/Types/SerializableObjectDictionary.ts +++ b/Common/Types/SerializableObjectDictionary.ts @@ -1,6 +1,7 @@ import Hostname from "./API/Hostname"; import Route from "./API/Route"; import URL from "./API/URL"; +import EqualTo from "./BaseDatabase/EqualTo"; import EqualToOrNull from "./BaseDatabase/EqualToOrNull"; import GreaterThan from "./BaseDatabase/GreaterThan"; import GreaterThanOrEqual from "./BaseDatabase/GreaterThanOrEqual"; @@ -38,6 +39,7 @@ const SerializableObjectDictionary: Dictionary = { [ObjectType.DateTime]: OneUptimeDate, [ObjectType.ObjectID]: ObjectID, [ObjectType.Name]: Name, + [ObjectType.EqualTo]: EqualTo, [ObjectType.EqualToOrNull]: EqualToOrNull, [ObjectType.MonitorSteps]: MonitorSteps, [ObjectType.MonitorStep]: MonitorStep,