|
1 | 1 | import { describe, expect, it } from '@jest/globals'; |
2 | | -import { generateOperationID } from '../../rulesets/functions/utils/operationIdGeneration'; |
| 2 | +import { |
| 3 | + generateOperationID, |
| 4 | + numberOfWords, |
| 5 | + shortenOperationId, |
| 6 | +} from '../../rulesets/functions/utils/operationIdGeneration'; |
3 | 7 |
|
4 | 8 | describe('tools/spectral/ipa/utils/operationIdGeneration.js', () => { |
5 | | - it('should singularize all nouns', () => { |
6 | | - expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster'); |
7 | | - expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster'); |
8 | | - expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster'); |
9 | | - expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster'); |
10 | | - expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster'); |
11 | | - }); |
| 9 | + describe('generateOperationID', () => { |
| 10 | + it('should singularize all nouns', () => { |
| 11 | + expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster'); |
| 12 | + expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster'); |
| 13 | + expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster'); |
| 14 | + expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster'); |
| 15 | + expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should leave the final noun as is', () => { |
| 19 | + expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters'); |
| 20 | + expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings'); |
| 21 | + expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings'); |
| 22 | + expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters'); |
| 23 | + expect( |
| 24 | + generateOperationID( |
| 25 | + 'get', |
| 26 | + '/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements' |
| 27 | + ) |
| 28 | + ).toEqual('getGroupClusterCollStatMeasurements'); |
| 29 | + expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should split camelCase method names', () => { |
| 33 | + expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode'); |
| 34 | + expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName'); |
| 35 | + expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual( |
| 36 | + 'exportGroupBackupBuckets' |
| 37 | + ); |
| 38 | + }); |
12 | 39 |
|
13 | | - it('should leave the final noun as is', () => { |
14 | | - expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters'); |
15 | | - expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings'); |
16 | | - expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings'); |
17 | | - expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters'); |
18 | | - expect( |
19 | | - generateOperationID( |
20 | | - 'get', |
21 | | - '/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements' |
22 | | - ) |
23 | | - ).toEqual('getGroupClusterCollStatMeasurements'); |
24 | | - expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess'); |
| 40 | + it('should accommodate legacy custom methods', () => { |
| 41 | + expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual( |
| 42 | + 'restartGroupClusterPrimaries' |
| 43 | + ); |
| 44 | + expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual( |
| 45 | + 'pauseGroupPipeline' |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return method when path is empty', () => { |
| 50 | + expect(generateOperationID('get', '')).toEqual('get'); |
| 51 | + expect(generateOperationID('getInfo', '')).toEqual('getInfo'); |
| 52 | + }); |
25 | 53 | }); |
26 | 54 |
|
27 | | - it('should split camelCase method names', () => { |
28 | | - expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode'); |
29 | | - expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName'); |
30 | | - expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual( |
31 | | - 'exportGroupBackupBuckets' |
32 | | - ); |
| 55 | + describe('numberOfWords', () => { |
| 56 | + it('should count the number of words in a camelCase string', () => { |
| 57 | + expect(numberOfWords('create')).toEqual(1); |
| 58 | + expect(numberOfWords('createGroup')).toEqual(2); |
| 59 | + expect(numberOfWords('createGroupCluster')).toEqual(3); |
| 60 | + expect(numberOfWords('createGroupClusterIndex')).toEqual(4); |
| 61 | + expect(numberOfWords('')).toEqual(0); |
| 62 | + }); |
33 | 63 | }); |
34 | 64 |
|
35 | | - it('should accommodate legacy custom methods', () => { |
36 | | - expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual( |
37 | | - 'restartGroupClusterPrimaries' |
38 | | - ); |
39 | | - expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual( |
40 | | - 'pauseGroupPipeline' |
41 | | - ); |
| 65 | + describe('shortenOperationId', () => { |
| 66 | + it('should shorten operation IDs correctly', () => { |
| 67 | + expect(shortenOperationId('createGroupClusterAutoScalingConfiguration')).toEqual( |
| 68 | + 'createAutoScalingConfiguration' |
| 69 | + ); |
| 70 | + expect(shortenOperationId('getFederationSettingConnectedOrgConfigRoleMapping')).toEqual('getConfigRoleMapping'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should make no change if the operation ID is <= 4 words long or undefined', () => { |
| 74 | + expect(shortenOperationId('createGroupClusterIndex')).toEqual('createGroupClusterIndex'); |
| 75 | + expect(shortenOperationId('create')).toEqual('create'); |
| 76 | + expect(shortenOperationId('')).toEqual(''); |
| 77 | + }); |
42 | 78 | }); |
43 | 79 | }); |
0 commit comments