123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- import {
- DataQuery,
- DataSourceApi,
- DataSourceWithQueryExportSupport,
- DataSourceWithQueryImportSupport,
- } from '@grafana/data';
- import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
- import { updateQueries } from './updateQueries';
- const oldUidDS = {
- uid: 'old-uid',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- } as DataSourceApi;
- const mixedDS = {
- uid: 'mixed',
- meta: {
- id: 'mixed',
- mixed: true,
- },
- } as DataSourceApi;
- const newUidDS = {
- uid: 'new-uid',
- type: 'new-type',
- meta: {
- id: 'new-type',
- },
- } as DataSourceApi;
- const newUidSameTypeDS = {
- uid: 'new-uid-same-type',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- } as DataSourceApi;
- describe('updateQueries', () => {
- it('Should update all queries except expression query when changing data source with same type', async () => {
- const updated = await updateQueries(
- newUidSameTypeDS,
- 'new-uid-same-type',
- [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: ExpressionDatasourceRef,
- },
- ],
- oldUidDS
- );
- expect(updated[0].datasource).toEqual({ type: 'old-type', uid: 'new-uid-same-type' });
- expect(updated[1].datasource).toEqual(ExpressionDatasourceRef);
- });
- it('Should update all to uid string passed in even when different from real current ds uid', async () => {
- const updated = await updateQueries(
- newUidSameTypeDS,
- '${ds}',
- [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- ],
- oldUidDS
- );
- expect(updated[0].datasource).toEqual({ type: 'old-type', uid: '${ds}' });
- });
- it('Should clear queries when changing type', async () => {
- const updated = await updateQueries(
- newUidDS,
- 'new-uid',
- [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- ],
- oldUidDS
- );
- expect(updated.length).toEqual(1);
- expect(updated[0].datasource).toEqual({ type: 'new-type', uid: 'new-uid' });
- });
- it('Should preserve query data source when changing to mixed', async () => {
- const updated = await updateQueries(
- mixedDS,
- 'mixed',
- [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ],
- oldUidDS
- );
- expect(updated[0].datasource).toEqual({ type: 'old-type', uid: 'old-uid' });
- expect(updated[1].datasource).toEqual({ type: 'other-type', uid: 'other-uid' });
- });
- it('should change nothing mixed updated to mixed', async () => {
- const updated = await updateQueries(
- mixedDS,
- 'mixed',
- [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ],
- mixedDS
- );
- expect(updated[0].datasource).toEqual({ type: 'old-type', uid: 'old-uid' });
- expect(updated[1].datasource).toEqual({ type: 'other-type', uid: 'other-uid' });
- });
- });
- describe('updateQueries with import', () => {
- describe('abstract queries support', () => {
- it('should migrate abstract queries', async () => {
- const exportSpy = jest.fn();
- const importSpy = jest.fn();
- const newUidDSWithAbstract = {
- uid: 'new-uid',
- type: 'new-type',
- meta: {
- id: 'new-type',
- },
- importFromAbstractQueries: (queries) => {
- importSpy(queries);
- const importedQueries = queries.map((q) => ({ ...q, imported: true }));
- return Promise.resolve(importedQueries);
- },
- } as DataSourceWithQueryImportSupport<any>;
- const oldUidDSWithAbstract = {
- uid: 'old-uid',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- exportToAbstractQueries: (queries) => {
- exportSpy(queries);
- const exportedQueries = queries.map((q) => ({ ...q, exported: true }));
- return Promise.resolve(exportedQueries);
- },
- } as DataSourceWithQueryExportSupport<any>;
- const queries = [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ];
- const updated = await updateQueries(
- newUidDSWithAbstract as any,
- (newUidDSWithAbstract as any).uid,
- queries,
- oldUidDSWithAbstract as any
- );
- expect(exportSpy).toBeCalledWith(queries);
- expect(importSpy).toBeCalledWith(queries.map((q) => ({ ...q, exported: true })));
- expect(updated).toMatchInlineSnapshot(`
- Array [
- Object {
- "datasource": Object {
- "type": "new-type",
- "uid": "new-uid",
- },
- "exported": true,
- "imported": true,
- "refId": "A",
- },
- Object {
- "datasource": Object {
- "type": "new-type",
- "uid": "new-uid",
- },
- "exported": true,
- "imported": true,
- "refId": "B",
- },
- ]
- `);
- });
- it('should clear queries when no queries were imported', async () => {
- const newUidDSWithAbstract = {
- uid: 'new-uid',
- type: 'new-type',
- meta: {
- id: 'new-type',
- },
- importFromAbstractQueries: () => {
- return Promise.resolve([]);
- },
- } as DataSourceWithQueryImportSupport<any>;
- const oldUidDSWithAbstract = {
- uid: 'old-uid',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- exportToAbstractQueries: (queries) => {
- const exportedQueries = queries.map((q) => ({ ...q, exported: true }));
- return Promise.resolve(exportedQueries);
- },
- } as DataSourceWithQueryExportSupport<any>;
- const queries = [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ];
- const updated = await updateQueries(
- newUidDSWithAbstract as any,
- (newUidDSWithAbstract as any).uid,
- queries,
- oldUidDSWithAbstract as any
- );
- expect(updated.length).toEqual(1);
- expect(updated[0].datasource).toEqual({ type: 'new-type', uid: 'new-uid' });
- });
- });
- describe('importQueries support', () => {
- it('should import queries when abstract queries are not supported by datasources', async () => {
- const importSpy = jest.fn();
- const newUidDSWithImport = {
- uid: 'new-uid',
- type: 'new-type',
- meta: {
- id: 'new-type',
- },
- importQueries: (queries, origin) => {
- importSpy(queries, origin);
- const importedQueries = queries.map((q) => ({ ...q, imported: true }));
- return Promise.resolve(importedQueries);
- },
- } as DataSourceApi<any>;
- const oldUidDS = {
- uid: 'old-uid',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- } as DataSourceApi;
- const queries = [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ];
- const updated = await updateQueries(newUidDSWithImport, newUidDSWithImport.uid, queries, oldUidDS);
- expect(importSpy).toBeCalledWith(queries, { uid: 'old-uid', type: 'old-type', meta: { id: 'old-type' } });
- expect(updated).toMatchInlineSnapshot(`
- Array [
- Object {
- "datasource": Object {
- "type": "new-type",
- "uid": "new-uid",
- },
- "imported": true,
- "refId": "A",
- },
- Object {
- "datasource": Object {
- "type": "new-type",
- "uid": "new-uid",
- },
- "imported": true,
- "refId": "B",
- },
- ]
- `);
- });
- it('should clear queries when no queries were imported', async () => {
- const newUidDSWithImport = {
- uid: 'new-uid',
- type: 'new-type',
- meta: {
- id: 'new-type',
- },
- importQueries: (queries, origin) => {
- return Promise.resolve([] as DataQuery[]);
- },
- } as DataSourceApi<any>;
- const oldUidDS = {
- uid: 'old-uid',
- type: 'old-type',
- meta: {
- id: 'old-type',
- },
- } as DataSourceApi;
- const queries = [
- {
- refId: 'A',
- datasource: {
- uid: 'old-uid',
- type: 'old-type',
- },
- },
- {
- refId: 'B',
- datasource: {
- uid: 'other-uid',
- type: 'other-type',
- },
- },
- ];
- const updated = await updateQueries(newUidDSWithImport, 'new-uid', queries, oldUidDS);
- expect(updated.length).toEqual(1);
- expect(updated[0].datasource).toEqual({ type: 'new-type', uid: 'new-uid' });
- });
- });
- });
|