-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00adb2c
commit 73b6267
Showing
15 changed files
with
182 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
] | ||
: []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters