Skip to content

Commit

Permalink
optimize the code
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Nov 11, 2024
1 parent a86a3f7 commit 91fdbe6
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 749 deletions.
18 changes: 14 additions & 4 deletions src/core/server/saved_objects/import/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,16 @@ describe('findReferenceDataSourceForObject', () => {
type: 'non-data-source',
attributes: {},
},
{
id: '6',
references: [{ id: '7', type: 'index-pattern', name: '7' }],
type: 'non-data-source',
attributes: {},
},
];

const ObjectsMap = new Map(savedObjects.map((so) => [so.id, so]));

test('returns null if no references exist', () => {
expect(findReferenceDataSourceForObject(savedObjects[2], ObjectsMap)).toBeNull();
});

test('returns the data-source reference if it exists in the references', () => {
const result = findReferenceDataSourceForObject(savedObjects[0], ObjectsMap);
expect(result).toEqual({ id: '5', type: 'data-source', name: '5' });
Expand All @@ -387,11 +389,19 @@ describe('findReferenceDataSourceForObject', () => {
expect(findReferenceDataSourceForObject(savedObjects[1], ObjectsMap)).toBeNull();
});

test('returns null if no references exist', () => {
expect(findReferenceDataSourceForObject(savedObjects[2], ObjectsMap)).toBeNull();
});

test('returns nested data-source reference if found', () => {
const result = findReferenceDataSourceForObject(savedObjects[3], ObjectsMap);
expect(result).toEqual({ id: '5', type: 'data-source', name: '5' });
});

test('returns null if the nested references have no data-source reference', () => {
expect(findReferenceDataSourceForObject(savedObjects[4], ObjectsMap)).toBeNull();
});

test('returns null if circular reference', () => {
const circularAssets: Array<SavedObject<{ title?: string }>> = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import * as utilsExports from './utils';

describe('validateDataSources', () => {
const savedObjectsClient = savedObjectsClientMock.create();
const errorAccumulator: SavedObjectsImportError[] = [
{
id: '1',
type: 'dashboards',
meta: {},
error: { type: 'missing_references', references: [] },
},
];
const workspaces = ['workspace-1'];

beforeEach(() => {
jest.resetAllMocks();
});

it('returns empty array if no valid objects', async () => {
const errorAccumulator: SavedObjectsImportError[] = [
{
id: '1',
type: 'dashboards',
meta: {},
error: { type: 'missing_references', references: [] },
},
];
const savedObjects = [{ id: '1', type: 'dashboards', attributes: {}, references: [] }];
const result = await validateDataSources(
savedObjects,
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('validateDataSources', () => {
expect(result).toEqual([
{
error: {
dataSourceName: 'DataSource 2',
dataSource: 'data-source-2',
type: 'missing_data_source',
},
id: '1',
Expand Down
Loading

0 comments on commit 91fdbe6

Please sign in to comment.