Skip to content

Commit

Permalink
variable testcases updated
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Apr 27, 2021
1 parent 00adb2c commit 73b6267
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/app/InfinityQuery.ts → src/app/queryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getTemplateSrv } from '@grafana/runtime';
import { ScopedVars } from '@grafana/data';
import { InfinityQuery, InfinityQuerySources, InfinityQueryType, InfinityInstanceSettings } from './../types';
import { InfinityQuery, InfinityQuerySources, InfinityQueryType, InfinityInstanceSettings } from '../types';

export const replaceVariables = (query: InfinityQuery, scopedVars: ScopedVars): InfinityQuery => {
return {
Expand Down
47 changes: 47 additions & 0 deletions src/app/variablesQuery/Collection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SelectableValue } from '@grafana/data';
import { CollectionVariable } from './Collection';

const data: Array<[string, string, Array<SelectableValue<string>>]> = [
[
'it should return correct kv pairs and remove the variable keyword',
'Collection(prod,pd,nonprod,np)',
[
{ value: 'pd', text: 'prod' },
{ value: 'np', text: 'nonprod' },
],
],
[
'it should return correct kv pairs when even number of args passed',
'prod,pd,nonprod,np',
[
{ value: 'pd', text: 'prod' },
{ value: 'np', text: 'nonprod' },
],
],
[
'it should return correct kv pairs when value is missing',
'prod,pd,nonprod,',
[
{ value: 'pd', text: 'prod' },
{ value: 'nonprod', text: 'nonprod' },
],
],
[
'it should return correct kv pairs when odd number of args passed',
'prod,pd,nonprod',
[
{ value: 'pd', text: 'prod' },
{ value: 'nonprod', text: 'nonprod' },
],
],
];
describe('Collection', () => {
it.each(data)('%s', (name, input, output) => {
let got = CollectionVariable(input);
expect(got.length).toBe(output.length);
output.forEach((o, index) => {
expect(got[index].value).toBe(o.value);
expect(got[index].text).toBe(o.text);
});
});
});
4 changes: 3 additions & 1 deletion src/app/variablesQuery/Collection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import chunk from 'lodash/chunk';
import { SelectableValue } from '@grafana/data';
import { replaceTokenFromVariable } from './utils';

export const CollectionVariable = (query: string): Array<SelectableValue<string>> => {
query = replaceTokenFromVariable(query, 'Collection');
let out = chunk(query.split(','), 2).map(value => {
return {
text: value[0],
value: value[1],
value: value[1] || value[0],
};
});
return out;
Expand Down
32 changes: 32 additions & 0 deletions src/app/variablesQuery/CollectionLookup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { SelectableValue } from '@grafana/data';
import { CollectionLookupVariable } from './CollectionLookup';

const data: Array<[string, string, Array<SelectableValue<string>>]> = [
[
'it should return correct kv pairs and remove the variable keyword',
'CollectionLookup(prod,pd,nonprod,np,nonprod)',
[{ value: 'np', text: 'np' }],
],
[
'it should return correct kv pairs when even number of args passed',
'prod,pd,nonprod,np,nonprod',
[{ value: 'np', text: 'np' }],
],
[
'it should return correct kv pair when more than one arg matched',
'prod,pd,nonprod,np,nonprod,foo,nonprod',
[{ value: 'np', text: 'np' }],
],
['it should return empty array when even number of args passed', 'prod,pd,nonprod,np', []],
['it should return empty array when no args matched', 'prod,pd,nonprod,np,foo', []],
];
describe('CollectionLookup', () => {
it.each(data)('%s', (name, input, output) => {
let got = CollectionLookupVariable(input);
expect(got.length).toBe(output.length);
output.forEach((o, index) => {
expect(got[index].value).toBe(o.value);
expect(got[index].text).toBe(o.text);
});
});
});
2 changes: 2 additions & 0 deletions src/app/variablesQuery/CollectionLookup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import chunk from 'lodash/chunk';
import last from 'lodash/last';
import { SelectableValue } from '@grafana/data';
import { replaceTokenFromVariable } from './utils';

export const CollectionLookupVariable = (query: string): Array<SelectableValue<string>> => {
query = replaceTokenFromVariable(query, 'CollectionLookup');
let querySplit = query.split(',');
let chunkCollection = chunk(querySplit, 2);
let out = chunkCollection
Expand Down
18 changes: 18 additions & 0 deletions src/app/variablesQuery/Join.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { SelectableValue } from '@grafana/data';
import { JoinVariable } from './Join';

const data: Array<[string, string, Array<SelectableValue<string>>]> = [
['it should return correct kv pairs and remove the variable keyword', 'Join(a,b,c)', [{ value: 'abc', text: 'abc' }]],
['it should return correct kv pairs when no variable keyword passed', 'a,b,c', [{ value: 'abc', text: 'abc' }]],
['it should return empty array when no args passed', '', []],
];
describe('Join', () => {
it.each(data)('%s', (name, input, output) => {
let got = JoinVariable(input);
expect(got.length).toBe(output.length);
output.forEach((o, index) => {
expect(got[index].value).toBe(o.value);
expect(got[index].text).toBe(o.text);
});
});
});
16 changes: 10 additions & 6 deletions src/app/variablesQuery/Join.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { SelectableValue } from '@grafana/data';
import { replaceTokenFromVariable } from './utils';

export const JoinVariable = (query: string): Array<SelectableValue<string>> => {
query = replaceTokenFromVariable(query, 'Join');
let out = query.split(',').join('');
return [
{
value: out,
text: out,
},
];
return out
? [
{
value: out,
text: out,
},
]
: [];
};
33 changes: 33 additions & 0 deletions src/app/variablesQuery/Random.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SelectableValue } from '@grafana/data';
import { RandomVariable } from './Random';

const data: Array<[string, string, Array<SelectableValue<string>>]> = [
[
'it should return correct kv pairs and remove the variable keyword',
'Random(a,b,c)',
[
{ value: 'a', text: 'a' },
{ value: 'b', text: 'b' },
{ value: 'c', text: 'c' },
],
],
[
'it should return correct kv pairs and when no keyword passed',
'a,b,c',
[
{ value: 'a', text: 'a' },
{ value: 'b', text: 'b' },
{ value: 'c', text: 'c' },
],
],
['it should return empty array when no args passed', '', []],
];
describe('Random', () => {
it.each(data)('%s', (name, input, output) => {
let got = RandomVariable(input);
if (output.length > 0) {
expect(got.length).toBe(1);
expect(output).toContainEqual(got[0]);
}
});
});
2 changes: 2 additions & 0 deletions src/app/variablesQuery/Random.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sample from 'lodash/sample';
import { SelectableValue } from '@grafana/data';
import { replaceTokenFromVariable } from './utils';

export const RandomVariable = (query: string): Array<SelectableValue<string>> => {
query = replaceTokenFromVariable(query, 'Random');
let out = sample(query.split(','));
return [
{
Expand Down
30 changes: 4 additions & 26 deletions src/app/variablesQuery/VariableQuery.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import { replaceTokenFromVariable, migrateLegacyQuery } from './index';
import {
InfinityQueryType,
VariableQuery,
VariableQueryType,
VariableTokenLegacy,
DefaultInfinityQuery,
} from './../../types';

const data: Array<[string, VariableTokenLegacy, string]> = [
['Collection(A,a,B,b)', 'Collection', 'A,a,B,b'],
['CollectionLookup(A,a,B,b,A)', 'CollectionLookup', 'A,a,B,b,A'],
['Random(A,a,B,b,A)', 'Random', 'A,a,B,b,A'],
['Join(A,a,B,b,A)', 'Join', 'A,a,B,b,A'],
['Something(A,a,B,b,A)', 'Join', 'Something(A,a,B,b,A)'],
];

data.forEach((item, index) => {
describe('replaceTokenFromVariable', () => {
it(`replaceTokenFromVariable ${index + 1} ${item[1]}`, () => {
expect(replaceTokenFromVariable(item[0], item[1])).toBe(item[2]);
});
});
});
import { migrateLegacyQuery } from './index';
import { InfinityQueryType, VariableQuery, VariableQueryType, DefaultInfinityQuery } from './../../types';

describe('migrateLegacyQuery', () => {
it('Empty Query', () => {
Expand All @@ -36,7 +14,7 @@ describe('migrateLegacyQuery', () => {
expect(newQuery.queryType).toBe(VariableQueryType.Legacy);
expect(newQuery.query).toBe(originalQuery);
});
it('Empty Inifinity Query', () => {
it('Empty Infinity Query', () => {
let originalQuery: VariableQuery = {
queryType: VariableQueryType.Infinity,
query: '',
Expand All @@ -46,7 +24,7 @@ describe('migrateLegacyQuery', () => {
expect(newQuery.query).toBe(originalQuery.query);
expect(newQuery.infinityQuery?.type).toBe(InfinityQueryType.CSV);
});
it('Inifinity Query', () => {
it('Infinity Query', () => {
let originalQuery: VariableQuery = {
queryType: VariableQueryType.Infinity,
query: '',
Expand Down
19 changes: 5 additions & 14 deletions src/app/variablesQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { flatten, defaultsDeep } from 'lodash';
import { SelectableValue } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
import { InfinityProvider } from './../InfinityProvider';
import { IsValidInfinityQuery, replaceVariables } from './../InfinityQuery';
import { IsValidInfinityQuery, replaceVariables } from '../queryUtils';
import {
InfinityQuery,
VariableTokenLegacy,
InfinityInstanceSettings,
VariableQuery,
VariableQueryType,
Expand All @@ -16,10 +15,6 @@ import { CollectionLookupVariable } from './CollectionLookup';
import { JoinVariable } from './Join';
import { RandomVariable } from './Random';

export const replaceTokenFromVariable = (query: string, token: VariableTokenLegacy): string => {
return query.startsWith(`${token}(`) && query.endsWith(')') ? query.replace(`${token}(`, '').slice(0, -1) : query;
};

const getTemplateVariablesFromResult = (res: any): Array<SelectableValue<string>> => {
if (res.columns && res.columns.length > 0) {
if (res.columns.length === 2) {
Expand Down Expand Up @@ -108,17 +103,13 @@ export class LegacyVariableProvider implements VariableProvider {
query(): Promise<Array<SelectableValue<string>>> {
return new Promise(resolve => {
if (this.queryString.startsWith('Collection(') && this.queryString.endsWith(')')) {
let query = replaceTokenFromVariable(this.queryString, 'Collection');
resolve(CollectionVariable(query));
resolve(CollectionVariable(this.queryString));
} else if (this.queryString.startsWith('CollectionLookup(') && this.queryString.endsWith(')')) {
let query = replaceTokenFromVariable(this.queryString, 'CollectionLookup');
resolve(CollectionLookupVariable(query));
resolve(CollectionLookupVariable(this.queryString));
} else if (this.queryString.startsWith('Join(') && this.queryString.endsWith(')')) {
let query = replaceTokenFromVariable(this.queryString, 'Join');
resolve(JoinVariable(query));
resolve(JoinVariable(this.queryString));
} else if (this.queryString.startsWith('Random(') && this.queryString.endsWith(')')) {
let query = replaceTokenFromVariable(this.queryString, 'Random');
resolve(RandomVariable(query));
resolve(RandomVariable(this.queryString));
} else {
resolve([]);
}
Expand Down
18 changes: 18 additions & 0 deletions src/app/variablesQuery/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { VariableTokenLegacy } from './../../types';
import { replaceTokenFromVariable } from './utils';

const data: Array<[string, VariableTokenLegacy, string]> = [
['Collection(A,a,B,b)', 'Collection', 'A,a,B,b'],
['CollectionLookup(A,a,B,b,A)', 'CollectionLookup', 'A,a,B,b,A'],
['Random(A,a,B,b,A)', 'Random', 'A,a,B,b,A'],
['Join(A,a,B,b,A)', 'Join', 'A,a,B,b,A'],
['Something(A,a,B,b,A)', 'Join', 'Something(A,a,B,b,A)'],
];

data.forEach((item, index) => {
describe('replaceTokenFromVariable', () => {
it(`replaceTokenFromVariable ${index + 1} ${item[1]}`, () => {
expect(replaceTokenFromVariable(item[0], item[1])).toBe(item[2]);
});
});
});
5 changes: 5 additions & 0 deletions src/app/variablesQuery/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { VariableTokenLegacy } from './../../types';

export const replaceTokenFromVariable = (query: string, token: VariableTokenLegacy): string => {
return query.startsWith(`${token}(`) && query.endsWith(')') ? query.replace(`${token}(`, '').slice(0, -1) : query;
};
2 changes: 1 addition & 1 deletion src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DataQueryResponse, DataQueryRequest } from '@grafana/data';
import { DataSourceWithBackend } from '@grafana/runtime';
import { InfinityProvider } from './app/InfinityProvider';
import { SeriesProvider } from './app/SeriesProvider';
import { replaceVariables } from './app/InfinityQuery';
import { replaceVariables } from './app/queryUtils';
import { LegacyVariableProvider, InfinityVariableProvider, migrateLegacyQuery } from './app/variablesQuery';
import {
InfinityQuery,
Expand Down
2 changes: 1 addition & 1 deletion src/editors/query.editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defaultsDeep } from 'lodash';
import { QueryEditorProps } from '@grafana/data';
import { Datasource } from '../datasource';
import { InfinityQueryEditor } from './query/infinityQuery';
import { getDefaultGlobalQueryID } from './../app/InfinityQuery';
import { getDefaultGlobalQueryID } from '../app/queryUtils';
import { InfinityQuery, EditorMode, DefaultInfinityQuery } from '../types';

type EditorProps = QueryEditorProps<Datasource, InfinityQuery>;
Expand Down

0 comments on commit 73b6267

Please sign in to comment.