fix partitionkey value fetching (#1972)

* fix partitionkey value fetching

* fix partitionkey value fetching

* added unit test

* Added some unit tests

* move the constant
This commit is contained in:
sunghyunkang1111 2024-09-19 13:09:09 -05:00 committed by GitHub
parent 42a1c6c319
commit 869d81dfbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 87 additions and 26 deletions

View File

@ -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)); .finally(() => setIsExecuting(false));
}, [ }, [
onExecutionErrorChange, onExecutionErrorChange,

View File

@ -4,7 +4,28 @@ import * as sinon from "sinon";
import * as DataModels from "../Contracts/DataModels"; import * as DataModels from "../Contracts/DataModels";
import * as ViewModels from "../Contracts/ViewModels"; import * as ViewModels from "../Contracts/ViewModels";
import * as QueryUtils from "./QueryUtils"; 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", () => { describe("Query Utils", () => {
const generatePartitionKeyForPath = (path: string): DataModels.PartitionKey => { const generatePartitionKeyForPath = (path: string): DataModels.PartitionKey => {
@ -111,28 +132,30 @@ describe("Query Utils", () => {
}); });
}); });
describe("extractPartitionKey", () => { describe("getValueForPath", () => {
const documentContent = { it("should return the correct value for a simple path", () => {
"Volcano Name": "Adams", const pathSegments = ["Volcano Name"];
Country: "United States", expect(getValueForPath(documentContent, pathSegments)).toBe("Adams");
Region: "US-Washington", });
Location: { it("should return the correct value for a nested path", () => {
type: "Point", const pathSegments = ["Location", "coordinates"];
coordinates: [-121.49, 46.206], expect(getValueForPath(documentContent, pathSegments)).toEqual([-121.49, 46.206]);
}, });
Elevation: 3742, it("should return undefined for a non-existing path", () => {
Type: "Stratovolcano", const pathSegments = ["NonExistent", "Path"];
Category: "", expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
Status: "Tephrochronology", });
"Last Known Eruption": "Last known eruption from A.D. 1-1499, inclusive", it("should return undefined for an invalid path", () => {
id: "9e3c494e-8367-3f50-1f56-8c6fcb961363", const pathSegments = ["Location", "InvalidKey"];
_rid: "xzo0AJRYUxUFAAAAAAAAAA==", expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
_self: "dbs/xzo0AA==/colls/xzo0AJRYUxU=/docs/xzo0AJRYUxUFAAAAAAAAAA==/", });
_etag: '"ce00fa43-0000-0100-0000-652840440000"', it("should return the root object if pathSegments is empty", () => {
_attachments: "attachments/", const pathSegments: string[] = [];
_ts: 1697136708, expect(getValueForPath(documentContent, pathSegments)).toBeUndefined();
}; });
});
describe("extractPartitionKey", () => {
it("should extract single partition key value", () => { it("should extract single partition key value", () => {
const singlePartitionKeyDefinition: PartitionKeyDefinition = { const singlePartitionKeyDefinition: PartitionKeyDefinition = {
kind: PartitionKeyKind.Hash, kind: PartitionKeyKind.Hash,
@ -189,5 +212,18 @@ describe("Query Utils", () => {
); );
expect(partitionKeyValues.length).toBe(0); 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"]);
});
}); });
}); });

View File

@ -95,6 +95,24 @@ export const queryPagesUntilContentPresent = async (
return await doRequest(firstItemIndex); 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 */ /* eslint-disable @typescript-eslint/no-explicit-any */
export const extractPartitionKeyValues = ( export const extractPartitionKeyValues = (
documentContent: any, documentContent: any,
@ -105,11 +123,15 @@ export const extractPartitionKeyValues = (
} }
const partitionKeyValues: PartitionKey[] = []; const partitionKeyValues: PartitionKey[] = [];
partitionKeyDefinition.paths.forEach((partitionKeyPath: string) => { partitionKeyDefinition.paths.forEach((partitionKeyPath: string) => {
const partitionKeyPathWithoutSlash: string = partitionKeyPath.substring(1); const pathSegments: string[] = partitionKeyPath.substring(1).split("/");
if (documentContent[partitionKeyPathWithoutSlash] !== undefined) { const value = getValueForPath(documentContent, pathSegments);
partitionKeyValues.push(documentContent[partitionKeyPathWithoutSlash]);
if (value !== undefined) {
partitionKeyValues.push(value);
} }
}); });
return partitionKeyValues; return partitionKeyValues;
}; };