Skip to content

Commit fc31b41

Browse files
committed
Fix comments - #1537 (review)
- Add unit tests for profile/explain commands generator - Use arrow functions
1 parent 3e20c4f commit fc31b41

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

redisinsight/ui/src/components/query-card/QueryCardHeader/QueryCardHeader.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const QueryCardHeader = (props: Props) => {
245245
<div className={cx(styles.dropdownOption, styles.dropdownProfileOption)}>
246246
<EuiIcon
247247
className={styles.iconDropdownOption}
248-
type={'visTagCloud'}
248+
type="visTagCloud"
249249
data-testid={`view-type-selected-${value}-${id}`}
250250
/>
251251
</div>
@@ -259,7 +259,7 @@ const QueryCardHeader = (props: Props) => {
259259
}
260260
})
261261

262-
const canCommandProfile = isCommandAllowedForProfile(query.split(' ')[0].toLowerCase())
262+
const canCommandProfile = isCommandAllowedForProfile(query)
263263

264264
const indexForSeparator = findIndex(pluginsOptions, (option) => !option.internal)
265265
if (indexForSeparator > -1) {
@@ -349,7 +349,7 @@ const QueryCardHeader = (props: Props) => {
349349
className={cx(styles.buttonIcon, styles.viewTypeIcon)}
350350
onClick={onDropDownViewClick}
351351
>
352-
{isOpen && profileOptions.length > 1 && canCommandProfile && !summaryText && (
352+
{isOpen && canCommandProfile && !summaryText && (
353353
<div className={styles.dropdownWrapper}>
354354
<div className={styles.dropdown}>
355355
<EuiSuperSelect

redisinsight/ui/src/pages/workbench/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export const GRAPH_COMMANDS = ['graph.query']
3333

3434
const ALLOWED_PROFILE_COMMANDS = [...SEARCH_COMMANDS, ...GRAPH_COMMANDS]
3535

36-
export function isCommandAllowedForProfile(cmd: string) {
37-
return ALLOWED_PROFILE_COMMANDS.includes(cmd)
36+
export const isCommandAllowedForProfile = (query: string) => {
37+
return ALLOWED_PROFILE_COMMANDS.includes(query.split(' ')[0].toLowerCase())
3838
}
3939

4040
export enum ProfileQueryType {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { ProfileQueryType, SEARCH_COMMANDS, GRAPH_COMMANDS } from './constants'
2+
3+
import {
4+
generateGraphProfileQuery,
5+
generateSearchProfileQuery,
6+
generateProfileQueryForCommand,
7+
} from './utils'
8+
9+
10+
const generateGraphProfileQueryTests: Record<string, any>[] = [
11+
{ input: 'GRAPH.QUERY key "MATCH (n) RETURN n"', output: 'graph.profile key "MATCH (n) RETURN n"', type: ProfileQueryType.Profile },
12+
{ input: 'GRAPH.QUERY key "MATCH (n) RETURN n"', output: 'graph.explain key "MATCH (n) RETURN n"', type: ProfileQueryType.Explain },
13+
{ input: 'graph.query key "MATCH (n) RETURN n"', output: 'graph.profile key "MATCH (n) RETURN n"', type: ProfileQueryType.Profile },
14+
{ input: 'graph.query key "MATCH (n) RETURN n"', output: 'graph.explain key "MATCH (n) RETURN n"', type: ProfileQueryType.Explain },
15+
]
16+
17+
describe('generateGraphProfileQuery', () => {
18+
19+
generateGraphProfileQueryTests.forEach(test => {
20+
it(`should be output: ${test.output} for input: ${test.input} and type: ${test.type}`, () => {
21+
const result = generateGraphProfileQuery(test.input, test.type);
22+
23+
expect(result).toEqual(test.output);
24+
});
25+
})
26+
});
27+
28+
29+
const generateSearchProfileQueryTests: Record<string, any>[] = [
30+
{ input: 'FT.SEARCH index tomatoes', output: 'ft.profile index SEARCH QUERY tomatoes', type: ProfileQueryType.Profile },
31+
{ input: 'FT.AGGREGATE index tomatoes', output: 'ft.profile index AGGREGATE QUERY tomatoes', type: ProfileQueryType.Profile },
32+
{ input: 'FT.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
33+
{ input: 'FT.AGGREGATE index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
34+
{ input: 'ft.search index tomatoes', output: 'ft.profile index search QUERY tomatoes', type: ProfileQueryType.Profile },
35+
{ input: 'ft.aggregate index tomatoes', output: 'ft.profile index aggregate QUERY tomatoes', type: ProfileQueryType.Profile },
36+
{ input: 'ft.search index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
37+
{ input: 'ft.aggregate index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
38+
{ input: 'ft.SEARCH index tomatoes', output: 'ft.profile index SEARCH QUERY tomatoes', type: ProfileQueryType.Profile },
39+
{ input: 'ft.AGGREGATE index tomatoes', output: 'ft.profile index AGGREGATE QUERY tomatoes', type: ProfileQueryType.Profile },
40+
{ input: 'ft.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
41+
{ input: 'ft.AGGREGATE index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
42+
]
43+
44+
describe('generateSearchProfileQuery', () => {
45+
46+
generateSearchProfileQueryTests.forEach(test => {
47+
it(`should be output: ${test.output} for input: ${test.input} and type: ${test.type}`, () => {
48+
const result = generateSearchProfileQuery(test.input, test.type);
49+
50+
expect(result).toEqual(test.output);
51+
});
52+
})
53+
});
54+
55+
const generateProfileQueryForCommandTests: Record<string, any>[] = [
56+
...generateGraphProfileQueryTests,
57+
...generateSearchProfileQueryTests,
58+
{ input: 'GRAPH.LIST', output: null, type: ProfileQueryType.Profile },
59+
{ input: 'GRAPH.LIST', output: null, type: ProfileQueryType.Explain },
60+
{ input: 'GRAPH.PROFILE key "MATCH (n) RETURN n"', output: null, type: ProfileQueryType.Profile },
61+
{ input: 'GRAPH.PROFILE key "MATCH (n) RETURN n"', output: null, type: ProfileQueryType.Explain },
62+
{ input: 'GRAPH.EXPLAIN key "MATCH (n) RETURN n"', output: null, type: ProfileQueryType.Profile },
63+
{ input: 'GRAPH.EXPLAIN key "MATCH (n) RETURN n"', output: null, type: ProfileQueryType.Explain },
64+
{ input: 'ft._LIST', output: null, type: ProfileQueryType.Profile },
65+
{ input: 'ft._LIST', output: null, type: ProfileQueryType.Explain },
66+
{ input: 'ft.profile index SEARCH QUERY tomatoes', output: null, type: ProfileQueryType.Profile },
67+
{ input: 'ft.profile index AGGREGATE QUERY tomatoes', output: null, type: ProfileQueryType.Explain },
68+
{ input: 'ft.explain index tomatoes', output: null, type: ProfileQueryType.Profile },
69+
{ input: 'ft.explain index tomatoes', output: null, type: ProfileQueryType.Explain },
70+
]
71+
describe('generateProfileQueryForCommand', () => {
72+
generateProfileQueryForCommandTests.forEach(test => {
73+
it(`should be output: ${test.output} for input: ${test.input} and type: ${test.type}`, () => {
74+
const result = generateProfileQueryForCommand(test.input, test.type);
75+
76+
expect(result).toEqual(test.output);
77+
});
78+
})
79+
});

redisinsight/ui/src/pages/workbench/utils.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { ProfileQueryType, SEARCH_COMMANDS, GRAPH_COMMANDS } from './constants'
22

3-
function generateGraphProfileQuery(query: string, type: ProfileQueryType) {
4-
return [`graph.${type}`, ...query.split(' ').slice(1)].join(' ')
3+
export function generateGraphProfileQuery(query: string, type: ProfileQueryType) {
4+
return [`graph.${type.toLowerCase()}`, ...query.split(' ').slice(1)].join(' ')
55
}
66

7-
function generateSearchProfileQuery(query: string, type: ProfileQueryType) {
7+
export function generateSearchProfileQuery(query: string, type: ProfileQueryType) {
88
const commandSplit = query.split(' ')
9-
const key = commandSplit[0].toLowerCase()
9+
const cmd = commandSplit[0]
1010

1111
if (type === ProfileQueryType.Explain) {
12-
return [`ft.${type}`, ...commandSplit.slice(1)].join(' ')
12+
return [`ft.${type.toLowerCase()}`, ...commandSplit.slice(1)].join(' ')
1313
} else {
14-
const index = commandSplit[1]
15-
const queryType = key.split('.')[1] // SEARCH / AGGREGATE
16-
return [`ft.${type}`, index, queryType, 'QUERY', ...commandSplit.slice(2)].join(' ')
14+
let index = commandSplit[1]
15+
16+
const queryType = cmd.split('.')[1] // SEARCH / AGGREGATE
17+
return [`ft.${type.toLowerCase()}`, index, queryType, 'QUERY', ...commandSplit.slice(2)].join(' ')
1718
}
1819
}
1920

0 commit comments

Comments
 (0)