-
Notifications
You must be signed in to change notification settings - Fork 14
test(ipa): add missing tests for IPA utils functions #872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
tools/spectral/ipa/__tests__/metrics/metricCollectionUtils.test.js
This file contains hidden or 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,60 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { extractTeamOwnership, getSeverityPerRule } from '../../metrics/utils/metricCollectionUtils.js'; | ||
|
||
describe('tools/spectral/ipa/metrics/utils/metricCollectionUtils.js', () => { | ||
describe('getSeverityPerRule', () => { | ||
const testRuleSet = { | ||
rules: { | ||
rule1: { | ||
definition: { | ||
severity: 1, | ||
}, | ||
}, | ||
rule2: { | ||
definition: { | ||
severity: 2, | ||
}, | ||
}, | ||
}, | ||
}; | ||
it('maps the rule severities correctly', () => { | ||
expect(getSeverityPerRule(testRuleSet)).toEqual({ | ||
rule1: 1, | ||
rule2: 2, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('extractTeamOwnership', () => { | ||
const testOas = { | ||
paths: { | ||
'/resource1': { | ||
get: { | ||
description: 'get resource', | ||
'x-xgen-owner-team': 'team1', | ||
}, | ||
post: { | ||
description: 'create resource', | ||
'x-xgen-owner-team': 'team1', | ||
}, | ||
}, | ||
'/resource2': { | ||
get: { | ||
description: 'get resource', | ||
'x-xgen-owner-team': 'team2', | ||
}, | ||
post: { | ||
description: 'create resource', | ||
'x-xgen-owner-team': 'team2', | ||
}, | ||
}, | ||
}, | ||
}; | ||
it('maps the path ownership correctly', () => { | ||
expect(extractTeamOwnership(testOas)).toEqual({ | ||
'/resource1': 'team1', | ||
'/resource2': 'team2', | ||
}); | ||
}); | ||
}); | ||
}); |
149 changes: 149 additions & 0 deletions
149
tools/spectral/ipa/__tests__/utils/componentUtils.test.js
This file contains hidden or 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,149 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { | ||
isPathParam, | ||
pathIsForRequestVersion, | ||
pathIsForResponseVersion, | ||
resolveObject, | ||
} from '../../rulesets/functions/utils/componentUtils.js'; | ||
|
||
describe('tools/spectral/ipa/rulesets/functions/utils/componentUtils.js', () => { | ||
describe('isPathParam', () => { | ||
it('returns true if the string is a path param', () => { | ||
expect(isPathParam('{id}')).toEqual(true); | ||
}); | ||
it('returns true if the string is a path param with a custom method', () => { | ||
expect(isPathParam('{id}:custom')).toEqual(true); | ||
}); | ||
it('returns false if the string is not a path param', () => { | ||
expect(isPathParam('resource')).toEqual(false); | ||
expect(isPathParam('resource:custom')).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('resolveObject', () => { | ||
const testOas = { | ||
paths: { | ||
'/resource': { | ||
get: { | ||
description: 'get resource', | ||
}, | ||
}, | ||
}, | ||
components: { | ||
schemas: { | ||
MySchema: { | ||
properties: { | ||
fieldName: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('resolves the OAS component based on the path', () => { | ||
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema'])).toEqual( | ||
testOas.components.schemas.MySchema | ||
); | ||
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName'])).toEqual( | ||
testOas.components.schemas.MySchema.properties.fieldName | ||
); | ||
expect(resolveObject(testOas, ['paths', '/resource', 'get'])).toEqual(testOas.paths['/resource'].get); | ||
}); | ||
it('returns undefined if the OAS does not contain the component based on the path', () => { | ||
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema2'])).toEqual(undefined); | ||
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName2'])).toEqual( | ||
undefined | ||
); | ||
expect(resolveObject(testOas, ['paths', '/resource/{id}', 'get'])).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
describe('pathIsForResponseVersion', () => { | ||
it('returns true for a path for a response version', () => { | ||
expect( | ||
pathIsForResponseVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'responses', | ||
'200', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
]) | ||
).toEqual(true); | ||
}); | ||
it('returns true for a path for a schema in a response version', () => { | ||
expect( | ||
pathIsForResponseVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'responses', | ||
'200', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
'schema', | ||
]) | ||
).toEqual(true); | ||
}); | ||
it('returns false for a path for a schema in a request version', () => { | ||
expect( | ||
pathIsForResponseVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'requestBody', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
]) | ||
).toEqual(false); | ||
}); | ||
it('returns false for a path for a schema in components', () => { | ||
expect(pathIsForResponseVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('pathIsForRequestVersion', () => { | ||
it('returns true for a request version', () => { | ||
expect( | ||
pathIsForRequestVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'requestBody', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
]) | ||
).toEqual(true); | ||
}); | ||
it('returns true for a path for a schema in a request version', () => { | ||
expect( | ||
pathIsForRequestVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'requestBody', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
'schema', | ||
]) | ||
).toEqual(true); | ||
}); | ||
it('returns false for a path for a response version', () => { | ||
expect( | ||
pathIsForRequestVersion([ | ||
'paths', | ||
'/resource/{id}', | ||
'get', | ||
'responses', | ||
'200', | ||
'content', | ||
'application/vnd.atlas.2023-08-05+json', | ||
]) | ||
).toEqual(false); | ||
}); | ||
it('returns false for a path for a schema in components', () => { | ||
expect(pathIsForRequestVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.