Skip to content

Commit

Permalink
fix partitionkey value fetching (#1972)
Browse files Browse the repository at this point in the history
* fix partitionkey value fetching

* fix partitionkey value fetching

* added unit test

* Added some unit tests

* move the constant
  • Loading branch information
sunghyunkang1111 authored Sep 19, 2024
1 parent 42a1c6c commit 869d81d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,10 @@ export const DocumentsTabComponent: React.FunctionComponent<IDocumentsTabCompone
);
},
)
.then(() => setSelectedRows(new Set([documentIds.length - 1])))
.then(() => {
setSelectedRows(new Set([documentIds.length - 1]));
setClickedRowIndex(documentIds.length - 1);
})
.finally(() => setIsExecuting(false));
}, [
onExecutionErrorChange,
Expand Down
80 changes: 58 additions & 22 deletions src/Utils/QueryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,28 @@ 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";

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,
};

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

describe("extractPartitionKey", () => {
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,
};
describe("getValueForPath", () => {
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", () => {
it("should extract single partition key value", () => {
const singlePartitionKeyDefinition: PartitionKeyDefinition = {
kind: PartitionKeyKind.Hash,
Expand Down Expand Up @@ -189,5 +212,18 @@ describe("Query Utils", () => {
);
expect(partitionKeyValues.length).toBe(0);
});

it("should extract all partition key values for hierarchical and nested partition keys", () => {
const mixedPartitionKeyDefinition: PartitionKeyDefinition = {
kind: PartitionKeyKind.MultiHash,
paths: ["/Country", "/Location/type"],
};
const partitionKeyValues: PartitionKey[] = extractPartitionKeyValues(
documentContent,
mixedPartitionKeyDefinition,
);
expect(partitionKeyValues.length).toBe(2);
expect(partitionKeyValues).toEqual(["United States", "Point"]);
});
});
});
28 changes: 25 additions & 3 deletions src/Utils/QueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ export const queryPagesUntilContentPresent = async (
return await doRequest(firstItemIndex);
};

/* 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) {
if (!currentValue || currentValue[segment] === undefined) {
return undefined;
}
currentValue = currentValue[segment];
}

return currentValue;
};

/* eslint-disable @typescript-eslint/no-explicit-any */
export const extractPartitionKeyValues = (
documentContent: any,
Expand All @@ -105,11 +123,15 @@ export const extractPartitionKeyValues = (
}

const partitionKeyValues: PartitionKey[] = [];

partitionKeyDefinition.paths.forEach((partitionKeyPath: string) => {
const partitionKeyPathWithoutSlash: string = partitionKeyPath.substring(1);
if (documentContent[partitionKeyPathWithoutSlash] !== undefined) {
partitionKeyValues.push(documentContent[partitionKeyPathWithoutSlash]);
const pathSegments: string[] = partitionKeyPath.substring(1).split("/");
const value = getValueForPath(documentContent, pathSegments);

if (value !== undefined) {
partitionKeyValues.push(value);
}
});

return partitionKeyValues;
};

0 comments on commit 869d81d

Please sign in to comment.