-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add compatibility check for TTL configs
- Loading branch information
Showing
5 changed files
with
199 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import gql from 'graphql-tag'; | ||
import { | ||
expectSingleCompatibilityIssue, | ||
expectToBeValid, | ||
} from '../implementation/validation-utils'; | ||
import { runCheck } from './utils'; | ||
import { Project } from '../../../src/project/project'; | ||
import { assertValidatorAcceptsAndDoesNotWarn } from '../../schema/ast-validation-modules/helpers'; | ||
|
||
describe('checkModel', () => { | ||
describe('ttl', () => { | ||
it('rejects if a type should have a TTL config', () => { | ||
const projectToCheck = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
]); | ||
expectToBeValid(projectToCheck); | ||
|
||
const baselineProject = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
{ | ||
name: 'ttl.json', | ||
body: JSON.stringify({ | ||
timeToLive: [ | ||
{ | ||
typeName: 'Test', | ||
dateField: 'dateField', | ||
expireAfterDays: 30, | ||
}, | ||
], | ||
}), | ||
}, | ||
]); | ||
expectToBeValid(baselineProject); | ||
|
||
const checkResult = projectToCheck.checkCompatibility(baselineProject); | ||
|
||
expectSingleCompatibilityIssue( | ||
checkResult, | ||
'There should be a timeToLive configuration for type "Test".', | ||
); | ||
}); | ||
|
||
it('rejects if a type should not have a TTL config', () => { | ||
const projectToCheck = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
{ | ||
name: 'ttl.json', | ||
body: JSON.stringify({ | ||
timeToLive: [ | ||
{ | ||
typeName: 'Test', | ||
dateField: 'dateField', | ||
expireAfterDays: 30, | ||
}, | ||
], | ||
}), | ||
}, | ||
]); | ||
expectToBeValid(projectToCheck); | ||
|
||
const baselineProject = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
]); | ||
expectToBeValid(baselineProject); | ||
|
||
const checkResult = projectToCheck.checkCompatibility(baselineProject); | ||
|
||
expectSingleCompatibilityIssue( | ||
checkResult, | ||
'There does not need to be a timeToLive configuration for type "Test". If the timeToLive configuration is intentional, suppress this message.', | ||
); | ||
}); | ||
|
||
it('accepts if there is a TTL config in both baseline and project to check', () => { | ||
const projectToCheck = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
{ | ||
name: 'ttl.json', | ||
body: JSON.stringify({ | ||
timeToLive: [ | ||
{ | ||
typeName: 'Test', | ||
dateField: 'dateField', | ||
expireAfterDays: 15, | ||
}, | ||
], | ||
}), | ||
}, | ||
]); | ||
expectToBeValid(projectToCheck); | ||
|
||
const baselineProject = new Project([ | ||
gql` | ||
type Test @rootEntity { | ||
dateField: DateTime | ||
} | ||
`.loc!.source, | ||
{ | ||
name: 'ttl.json', | ||
body: JSON.stringify({ | ||
timeToLive: [ | ||
{ | ||
typeName: 'Test', | ||
dateField: 'dateField', | ||
expireAfterDays: 30, | ||
}, | ||
], | ||
}), | ||
}, | ||
]); | ||
expectToBeValid(baselineProject); | ||
|
||
const checkResult = projectToCheck.checkCompatibility(baselineProject); | ||
|
||
expectToBeValid(checkResult); | ||
}); | ||
}); | ||
}); |
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,23 @@ | ||
import { ObjectType, RootEntityType } from '../implementation'; | ||
import { ValidationContext, ValidationMessage } from '../validation'; | ||
import { checkField } from './check-field'; | ||
import { getRequiredBySuffix } from './describe-module-specification'; | ||
|
||
export function checkBusinessObject( | ||
typeToCheck: RootEntityType, | ||
baselineType: RootEntityType, | ||
context: ValidationContext, | ||
) { | ||
if (baselineType.isBusinessObject && !typeToCheck.isBusinessObject) { | ||
context.addMessage( | ||
ValidationMessage.suppressableCompatibilityIssue( | ||
'BUSINESS_OBJECT', | ||
`Type "${ | ||
baselineType.name | ||
}" needs to be decorated with @businessObject${getRequiredBySuffix(baselineType)}.`, | ||
typeToCheck.astNode, | ||
{ location: typeToCheck.nameASTNode }, | ||
), | ||
); | ||
} | ||
} |
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,33 @@ | ||
import { ObjectType, RootEntityType } from '../implementation'; | ||
import { ValidationContext, ValidationMessage } from '../validation'; | ||
import { checkField } from './check-field'; | ||
import { getRequiredBySuffix } from './describe-module-specification'; | ||
|
||
export function checkTtl( | ||
typeToCheck: RootEntityType, | ||
baselineType: RootEntityType, | ||
context: ValidationContext, | ||
) { | ||
if (baselineType.timeToLiveTypes.length && !typeToCheck.timeToLiveTypes.length) { | ||
context.addMessage( | ||
ValidationMessage.suppressableCompatibilityIssue( | ||
'TTL', | ||
`There should be a timeToLive configuration for type "${baselineType.name}"${getRequiredBySuffix(baselineType)}.`, | ||
typeToCheck.astNode, | ||
{ location: typeToCheck.nameASTNode }, | ||
), | ||
); | ||
} | ||
if (!baselineType.timeToLiveTypes.length && typeToCheck.timeToLiveTypes.length) { | ||
// The @suppress needs to be specified on the root entity definition (because there is no @suppress for yaml/json files) | ||
// For this reason, we report the error on the type and not on the TTL config | ||
context.addMessage( | ||
ValidationMessage.suppressableCompatibilityIssue( | ||
'TTL', | ||
`There does not need to be a timeToLive configuration for type "${baselineType.name}". If the timeToLive configuration is intentional, suppress this message.`, | ||
typeToCheck.astNode, | ||
{ location: typeToCheck.nameASTNode }, | ||
), | ||
); | ||
} | ||
} |
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