@@ -27,7 +27,9 @@ export class InMemoryCatalogClient implements CatalogApi {
|
||||
// (undocumented)
|
||||
addLocation(_location: AddLocationRequest): Promise<AddLocationResponse>;
|
||||
// (undocumented)
|
||||
getEntities(_request?: GetEntitiesRequest): Promise<GetEntitiesResponse>;
|
||||
getEntities(
|
||||
request?: GetEntitiesRequest | undefined,
|
||||
): Promise<GetEntitiesResponse>;
|
||||
// (undocumented)
|
||||
getEntitiesByRefs(
|
||||
request: GetEntitiesByRefsRequest,
|
||||
@@ -42,7 +44,7 @@ export class InMemoryCatalogClient implements CatalogApi {
|
||||
): Promise<Entity | undefined>;
|
||||
// (undocumented)
|
||||
getEntityFacets(
|
||||
_request: GetEntityFacetsRequest,
|
||||
request: GetEntityFacetsRequest,
|
||||
): Promise<GetEntityFacetsResponse>;
|
||||
// (undocumented)
|
||||
getLocationByEntity(
|
||||
@@ -54,7 +56,7 @@ export class InMemoryCatalogClient implements CatalogApi {
|
||||
getLocationByRef(_locationRef: string): Promise<Location_2 | undefined>;
|
||||
// (undocumented)
|
||||
queryEntities(
|
||||
_request?: QueryEntitiesRequest,
|
||||
request?: QueryEntitiesRequest | undefined,
|
||||
): Promise<QueryEntitiesResponse>;
|
||||
// (undocumented)
|
||||
refreshEntity(_entityRef: string): Promise<void>;
|
||||
|
||||
@@ -43,6 +43,9 @@ describe('InMemoryCatalogClient', () => {
|
||||
it('getEntities', async () => {
|
||||
const client = new InMemoryCatalogClient({ entities });
|
||||
await expect(client.getEntities()).resolves.toEqual({ items: entities });
|
||||
await expect(
|
||||
client.getEntities({ filter: { 'metadata.uid': 'u2' } }),
|
||||
).resolves.toEqual({ items: [entity2] });
|
||||
});
|
||||
|
||||
it('getEntitiesByRefs', async () => {
|
||||
@@ -56,6 +59,16 @@ describe('InMemoryCatalogClient', () => {
|
||||
],
|
||||
}),
|
||||
).resolves.toEqual({ items: [entity2, undefined, entity1] });
|
||||
await expect(
|
||||
client.getEntitiesByRefs({
|
||||
entityRefs: [
|
||||
'customkind:default/e2',
|
||||
'customkind:missing/missing',
|
||||
'customkind:default/e1',
|
||||
],
|
||||
filter: { 'metadata.uid': 'u1' },
|
||||
}),
|
||||
).resolves.toEqual({ items: [undefined, undefined, entity1] });
|
||||
});
|
||||
|
||||
it('queryEntities', async () => {
|
||||
@@ -65,6 +78,13 @@ describe('InMemoryCatalogClient', () => {
|
||||
totalItems: 2,
|
||||
pageInfo: {},
|
||||
});
|
||||
await expect(
|
||||
client.queryEntities({ filter: { 'metadata.uid': 'u2' } }),
|
||||
).resolves.toEqual({
|
||||
items: [entity2],
|
||||
totalItems: 1,
|
||||
pageInfo: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('getEntityAncestors', async () => {
|
||||
|
||||
@@ -46,17 +46,17 @@ function pick(obj: JsonObject, key: string): JsonValue | undefined {
|
||||
const parts = key.split('.');
|
||||
|
||||
return parts.reduce((acc, part, index) => {
|
||||
if (!acc) {
|
||||
if (acc === undefined) {
|
||||
return acc;
|
||||
}
|
||||
if (typeof acc === 'object' && acc !== null && !Array.isArray(acc)) {
|
||||
const value = acc[part];
|
||||
if (value) {
|
||||
if (value !== undefined) {
|
||||
return value;
|
||||
}
|
||||
const rest = parts.slice(index).join('.');
|
||||
const restValue = acc[rest];
|
||||
if (restValue) {
|
||||
if (restValue !== undefined) {
|
||||
return restValue;
|
||||
}
|
||||
}
|
||||
@@ -80,9 +80,8 @@ function createFilter(
|
||||
if (!filterOrFilters) {
|
||||
return () => true;
|
||||
}
|
||||
const filters = Array.isArray(filterOrFilters)
|
||||
? filterOrFilters
|
||||
: [filterOrFilters];
|
||||
|
||||
const filters = [filterOrFilters].flat();
|
||||
|
||||
return entity => {
|
||||
return filters.some(filter => {
|
||||
@@ -133,9 +132,13 @@ export class InMemoryCatalogClient implements CatalogApi {
|
||||
): Promise<GetEntitiesByRefsResponse> {
|
||||
const filter = createFilter(request.filter);
|
||||
return {
|
||||
items: request.entityRefs
|
||||
.map(ref => this.#entitiesByRef.get(ref))
|
||||
.filter(e => (e ? filter(e) : true)),
|
||||
items: request.entityRefs.map(ref => {
|
||||
const entity = this.#entitiesByRef.get(ref);
|
||||
if (entity && filter(entity)) {
|
||||
return entity;
|
||||
}
|
||||
return undefined;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -146,11 +149,12 @@ export class InMemoryCatalogClient implements CatalogApi {
|
||||
return { items: [], pageInfo: {}, totalItems: 0 };
|
||||
}
|
||||
const filter = createFilter(request?.filter);
|
||||
const items = this.#entities.filter(filter);
|
||||
// TODO(Rugvip): Pagination
|
||||
return {
|
||||
items: this.#entities.filter(filter),
|
||||
items,
|
||||
pageInfo: {},
|
||||
totalItems: this.#entities.length,
|
||||
totalItems: items.length,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import { Entity } from '@backstage/catalog-model';
|
||||
import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha';
|
||||
// eslint-disable-next-line @backstage/no-undeclared-imports
|
||||
import { ServiceMock } from '@backstage/backend-test-utils';
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
|
||||
/** @internal */
|
||||
function simpleMock<TService>(
|
||||
@@ -57,7 +58,9 @@ function simpleMock<TService>(
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function catalogServiceMock(options?: { entities?: Entity[] }) {
|
||||
export function catalogServiceMock(options?: {
|
||||
entities?: Entity[];
|
||||
}): CatalogApi {
|
||||
return new InMemoryCatalogClient(options);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user