forked from janus-idp/backstage-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(orchestrator): add column filters (janus-idp#2273)
* add filters to openapi spec * cleanup folders generated docs Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com> * update generated code * body example * model introspection query data Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com> * use colum filters Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com> * update unit test for colum filters Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com> * fix code duplication --------- Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
- Loading branch information
1 parent
7db423f
commit 544c2c7
Showing
31 changed files
with
1,711 additions
and
445 deletions.
There are no files selected for viewing
193 changes: 151 additions & 42 deletions
193
plugins/orchestrator-backend/src/helpers/queryBuilder.test.ts
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,58 +1,167 @@ | ||
import { | ||
FieldFilterOperatorEnum, | ||
Filter, | ||
IntrospectionField, | ||
TypeKind, | ||
TypeName, | ||
} from '@janus-idp/backstage-plugin-orchestrator-common'; | ||
|
||
import { Pagination } from '../types/pagination'; | ||
import { buildGraphQlQuery } from './queryBuilder'; | ||
import { buildFilterCondition, buildGraphQlQuery } from './queryBuilder'; | ||
|
||
describe('buildGraphQlQuery', () => { | ||
const queryBody = 'id status'; | ||
const type = 'ProcessInstances'; | ||
const offset = 0; | ||
const limit = 10; | ||
const order = 'asc'; | ||
const sortField = 'name'; | ||
const pagination: Pagination = { | ||
offset, | ||
limit, | ||
order, | ||
sortField, | ||
const defaultTestParams = { | ||
queryBody: 'id status', | ||
type: 'ProcessInstances' as | ||
| 'ProcessDefinitions' | ||
| 'ProcessInstances' | ||
| 'Jobs', | ||
pagination: { | ||
offset: 0, | ||
limit: 10, | ||
order: 'asc', | ||
sortField: 'name', | ||
} as Pagination | undefined, | ||
whereClause: 'version: "1.0"', | ||
}; | ||
|
||
const getPaginationString = (pagination: Pagination | undefined) => { | ||
const paginationOrder = pagination?.order | ||
? pagination.order.toUpperCase() | ||
: 'ASC'; | ||
if (pagination) { | ||
return `orderBy: {${pagination.sortField}: ${paginationOrder}}, pagination: {limit: ${pagination.limit}, offset: ${pagination.offset}})`; | ||
} | ||
return undefined; | ||
}; | ||
|
||
const paginationString = `orderBy: {${sortField}: ${order.toUpperCase()}}, pagination: {limit: ${limit}, offset: ${offset}})`; | ||
const whereClause = 'version: "1.0"'; | ||
type TestCase = { | ||
name: string; | ||
params: typeof defaultTestParams; | ||
expectedResult: string; | ||
}; | ||
|
||
const testCases: TestCase[] = [ | ||
{ | ||
name: 'should build a basic query without where clause and pagination', | ||
params: { | ||
type: defaultTestParams.type, | ||
queryBody: defaultTestParams.queryBody, | ||
whereClause: '', | ||
pagination: {}, | ||
}, | ||
expectedResult: `{${defaultTestParams.type} {${defaultTestParams.queryBody} } }`, | ||
}, | ||
{ | ||
name: 'should build a query with a where clause', | ||
params: { | ||
type: defaultTestParams.type, | ||
queryBody: defaultTestParams.queryBody, | ||
whereClause: defaultTestParams.whereClause, | ||
pagination: {}, | ||
}, | ||
expectedResult: `{${defaultTestParams.type} (where: {${defaultTestParams.whereClause}}) {${defaultTestParams.queryBody} } }`, | ||
}, | ||
{ | ||
name: 'should build a query with pagination', | ||
params: { | ||
type: defaultTestParams.type, | ||
queryBody: defaultTestParams.queryBody, | ||
whereClause: '', | ||
pagination: defaultTestParams.pagination, | ||
}, | ||
expectedResult: `{${defaultTestParams.type} (${getPaginationString(defaultTestParams.pagination)} {${defaultTestParams.queryBody} } }`, | ||
}, | ||
{ | ||
name: 'should build a query with both where clause and pagination', | ||
params: { | ||
...defaultTestParams, | ||
}, | ||
expectedResult: `{${defaultTestParams.type} (where: {${defaultTestParams.whereClause}}, ${getPaginationString(defaultTestParams.pagination)} {${defaultTestParams.queryBody} } }`, | ||
}, | ||
]; | ||
|
||
it('should build a basic query without where clause and pagination', () => { | ||
const result = buildGraphQlQuery({ | ||
type, | ||
queryBody, | ||
testCases.forEach(({ name, params, expectedResult }) => { | ||
it(`${name}`, () => { | ||
const result = buildGraphQlQuery(params); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
expect(result).toBe(`{${type} {${queryBody} } }`); | ||
}); | ||
}); | ||
|
||
it('should build a query with a where clause', () => { | ||
const result = buildGraphQlQuery({ | ||
type, | ||
queryBody, | ||
whereClause, | ||
}); | ||
expect(result).toBe(`{${type} (where: {${whereClause}}) {${queryBody} } }`); | ||
describe('column filters', () => { | ||
const createStringIntrospectionField = ( | ||
name: string, | ||
): IntrospectionField => ({ | ||
name, | ||
type: { | ||
name: TypeName.String, | ||
kind: TypeKind.InputObject, | ||
ofType: null, | ||
}, | ||
}); | ||
|
||
it('should build a query with pagination', () => { | ||
const result = buildGraphQlQuery({ | ||
type, | ||
queryBody, | ||
pagination, | ||
}); | ||
expect(result).toBe(`{${type} (${paginationString} {${queryBody} } }`); | ||
const createFieldFilter = ( | ||
field: string, | ||
operator: FieldFilterOperatorEnum, | ||
value: any, | ||
): Filter => ({ | ||
field, | ||
operator, | ||
value, | ||
}); | ||
|
||
it('should build a query with both where clause and pagination', () => { | ||
const result = buildGraphQlQuery({ | ||
type, | ||
queryBody, | ||
whereClause, | ||
pagination, | ||
type FilterTestCase = { | ||
name: string; | ||
introspectionFields: IntrospectionField[]; | ||
filter: Filter | undefined; | ||
expectedResult: string; | ||
}; | ||
|
||
const testCases: FilterTestCase[] = [ | ||
{ | ||
name: 'returns empty string when filters are null or undefined', | ||
introspectionFields: [], | ||
filter: undefined, | ||
expectedResult: '', | ||
}, | ||
{ | ||
name: 'one string field', | ||
introspectionFields: [createStringIntrospectionField('name')], | ||
filter: createFieldFilter( | ||
'name', | ||
FieldFilterOperatorEnum.Eq, | ||
'Hello World Workflow', | ||
), | ||
expectedResult: 'name: {equal: "Hello World Workflow"}', | ||
}, | ||
{ | ||
name: 'two string field with or', | ||
introspectionFields: [ | ||
createStringIntrospectionField('name'), | ||
createStringIntrospectionField('id'), | ||
], | ||
filter: { | ||
operator: 'OR', | ||
filters: [ | ||
createFieldFilter( | ||
'name', | ||
FieldFilterOperatorEnum.Eq, | ||
'Hello World Workflow', | ||
), | ||
createFieldFilter('id', FieldFilterOperatorEnum.Eq, 'yamlgreet'), | ||
], | ||
}, | ||
expectedResult: | ||
'or: {name: {equal: "Hello World Workflow"}, id: {equal: "yamlgreet"}}', | ||
}, | ||
// Add remaining test cases here | ||
]; | ||
|
||
testCases.forEach(({ name, introspectionFields, filter, expectedResult }) => { | ||
it(`${name}`, () => { | ||
const result = buildFilterCondition(introspectionFields, filter); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
expect(result).toBe( | ||
`{${type} (where: {${whereClause}}, ${paginationString} {${queryBody} } }`, | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.