mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2024-11-24 14:36:54 +00:00
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:
parent
42a1c6c319
commit
869d81dfbc
@ -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,
|
||||
|
@ -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 => {
|
||||
@ -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,
|
||||
@ -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"]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user