Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunghyunkang1111 committed Sep 18, 2024
1 parent e0ad523 commit 1f7236e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/Utils/QueryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as sinon from "sinon";
import * as DataModels from "../Contracts/DataModels";
import * as ViewModels from "../Contracts/ViewModels";
import * as QueryUtils from "./QueryUtils";
import { defaultQueryFields, extractPartitionKeyValues } from "./QueryUtils";
import { defaultQueryFields, extractPartitionKeyValues, getValueForPath } from "./QueryUtils";

describe("Query Utils", () => {
const generatePartitionKeyForPath = (path: string): DataModels.PartitionKey => {
Expand Down Expand Up @@ -111,6 +111,49 @@ describe("Query Utils", () => {
});
});

describe("getValueForPath", () => {
const documentContent = {
"Volcano Name": "Adams",
Country: "United States",
Region: "US-Washington",
Location: {
type: "Point",
coordinates: [-121.49, 46.206],
},
Elevation: 3742,
Type: "Stratovolcano",
Category: "",
Status: "Tephrochronology",
"Last Known Eruption": "Last known eruption from A.D. 1-1499, inclusive",
id: "9e3c494e-8367-3f50-1f56-8c6fcb961363",
_rid: "xzo0AJRYUxUFAAAAAAAAAA==",
_self: "dbs/xzo0AA==/colls/xzo0AJRYUxU=/docs/xzo0AJRYUxUFAAAAAAAAAA==/",
_etag: '"ce00fa43-0000-0100-0000-652840440000"',
_attachments: "attachments/",
_ts: 1697136708,
};
it("should return the correct value for a simple path", () => {
const pathSegments = ["Volcano Name"];
expect(getValueForPath(documentContent, pathSegments)).toBe("Adams");
});
it("should return the correct value for a nested path", () => {
const pathSegments = ["Location", "coordinates"];
expect(getValueForPath(documentContent, pathSegments)).toEqual([-121.49, 46.206]);
});
it("should return undefined for a non-existing path", () => {
const pathSegments = ["NonExistent", "Path"];
expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
});
it("should return undefined for an invalid path", () => {
const pathSegments = ["Location", "InvalidKey"];
expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
});
it("should return the root object if pathSegments is empty", () => {
const pathSegments: string[] = [];
expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
});
});

describe("extractPartitionKey", () => {
const documentContent = {
"Volcano Name": "Adams",
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/QueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export const queryPagesUntilContentPresent = async (

/* eslint-disable @typescript-eslint/no-explicit-any */
export const getValueForPath = (content: any, pathSegments: string[]): any => {
if (pathSegments.length === 0) {
return undefined;
}

let currentValue = content;

for (const segment of pathSegments) {
Expand Down

0 comments on commit 1f7236e

Please sign in to comment.