-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(builtins): add tests for the plugin
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 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,126 @@ | ||
import {ERRORS} from '@grnsft/if-core/utils'; | ||
|
||
import {TimeConverter} from '../../../if-run/builtins/time-converter'; | ||
|
||
import {STRINGS} from '../../../if-run/config'; | ||
|
||
const {GlobalConfigError, InputValidationError} = ERRORS; | ||
const {MISSING_GLOBAL_CONFIG} = STRINGS; | ||
|
||
describe('builtins/time-converter: ', () => { | ||
describe('TimeConverter: ', () => { | ||
const globalConfig = { | ||
'input-parameter': 'energy-per-year', | ||
'original-time-unit': 'year', | ||
'new-time-unit': 'duration', | ||
'output-parameter': 'energy-per-duration', | ||
}; | ||
const parametersMetadata = { | ||
inputs: {}, | ||
outputs: {}, | ||
}; | ||
const timeConverter = TimeConverter(globalConfig, parametersMetadata); | ||
|
||
describe('init: ', () => { | ||
it('successfully initalized.', () => { | ||
expect(timeConverter).toHaveProperty('metadata'); | ||
expect(timeConverter).toHaveProperty('execute'); | ||
}); | ||
}); | ||
|
||
describe('execute(): ', () => { | ||
it('successfully applies TimeConverter strategy to given input.', () => { | ||
expect.assertions(1); | ||
|
||
const expectedResult = [ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
'energy-per-year': 10000, | ||
'energy-per-duration': 1.140795, | ||
}, | ||
]; | ||
|
||
const result = timeConverter.execute([ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
'energy-per-year': 10000, | ||
}, | ||
]); | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it('throws an error when global config is not provided.', () => { | ||
const config = undefined; | ||
const timeConverter = TimeConverter(config!, parametersMetadata); | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
timeConverter.execute([ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
'energy-per-year': 10000, | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new GlobalConfigError(MISSING_GLOBAL_CONFIG) | ||
); | ||
} | ||
}); | ||
|
||
it('throws an error on missing params in input.', () => { | ||
expect.assertions(1); | ||
|
||
try { | ||
timeConverter.execute([ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError( | ||
'"energy-per-year" parameter is required. Error code: invalid_type.' | ||
) | ||
); | ||
} | ||
}); | ||
|
||
it('returns a result when `new-time-unit` is a different time unit than `duration`.', () => { | ||
expect.assertions(1); | ||
const newConfig = { | ||
'input-parameter': 'energy-per-year', | ||
'original-time-unit': 'year', | ||
'new-time-unit': 'month', | ||
'output-parameter': 'energy-per-duration', | ||
}; | ||
const timeConverter = TimeConverter(newConfig, parametersMetadata); | ||
|
||
const data = [ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
'energy-per-year': 10000, | ||
}, | ||
]; | ||
const response = timeConverter.execute(data); | ||
const expectedResult = [ | ||
{ | ||
timestamp: '2021-01-01T00:00:00Z', | ||
duration: 3600, | ||
'energy-per-year': 10000, | ||
'energy-per-duration': 832.886522, | ||
}, | ||
]; | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); | ||
}); |