-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into migrate-subtract-to-builtin
Signed-off-by: Joseph Cook <33655003+jmcook1186@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
613 additions
and
8 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
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
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
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,123 @@ | ||
import {Coefficient} from '../../../builtins/coefficient'; | ||
|
||
import {ERRORS} from '../../../util/errors'; | ||
|
||
const {InputValidationError, ConfigNotFoundError} = ERRORS; | ||
|
||
describe('builtins/coefficient: ', () => { | ||
describe('Coefficient: ', () => { | ||
const globalConfig = { | ||
'input-parameter': 'carbon', | ||
coefficient: 3, | ||
'output-parameter': 'carbon-product', | ||
}; | ||
const coefficient = Coefficient(globalConfig); | ||
|
||
describe('init: ', () => { | ||
it('successfully initalized.', () => { | ||
expect(coefficient).toHaveProperty('metadata'); | ||
expect(coefficient).toHaveProperty('execute'); | ||
}); | ||
}); | ||
|
||
describe('execute(): ', () => { | ||
it('successfully applies coefficient strategy to given input.', () => { | ||
expect.assertions(1); | ||
|
||
const expectedResult = [ | ||
{ | ||
duration: 3600, | ||
carbon: 3, | ||
'carbon-product': 9, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]; | ||
|
||
const result = coefficient.execute([ | ||
{ | ||
duration: 3600, | ||
carbon: 3, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
|
||
expect.assertions(1); | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it('throws an error when global config is not provided.', () => { | ||
const config = undefined; | ||
const coefficient = Coefficient(config!); | ||
const expectedMessage = 'Global config is not provided.'; | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
coefficient.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
carbon: 3, | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual(new ConfigNotFoundError(expectedMessage)); | ||
} | ||
}); | ||
|
||
it('throws an error on missing `input-parameter` param in input.', () => { | ||
const invalidConfig = { | ||
'input-parameter': '', | ||
coefficient: 3, | ||
'output-parameter': 'carbon-product', | ||
}; | ||
const coefficient = Coefficient(invalidConfig); | ||
const expectedMessage = | ||
'"input-parameter" parameter is string must contain at least 1 character(s). Error code: too_small.'; | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
coefficient.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
carbon: 3, | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError(expectedMessage) | ||
); | ||
} | ||
}); | ||
|
||
it('throws an error on missing `output-parameter` param in input.', () => { | ||
const invalidConfig = { | ||
'input-parameter': 'carbon', | ||
coefficient: 10, | ||
'output-parameter': '', | ||
}; | ||
const coefficient = Coefficient(invalidConfig); | ||
const expectedMessage = | ||
'"output-parameter" parameter is string must contain at least 1 character(s). Error code: too_small.'; | ||
|
||
expect.assertions(1); | ||
try { | ||
coefficient.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
carbon: 3, | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError(expectedMessage) | ||
); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
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,118 @@ | ||
import {Exponent} from '../../../builtins/exponent'; | ||
|
||
import {ERRORS} from '../../../util/errors'; | ||
|
||
const {InputValidationError} = ERRORS; | ||
|
||
describe('lib/exponent: ', () => { | ||
describe('Exponent: ', () => { | ||
const globalConfig = { | ||
'input-parameter': 'energy/base', | ||
exponent: 3, | ||
'output-parameter': 'energy', | ||
}; | ||
const exponent = Exponent(globalConfig); | ||
|
||
describe('init: ', () => { | ||
it('successfully initalized.', () => { | ||
expect(exponent).toHaveProperty('metadata'); | ||
expect(exponent).toHaveProperty('execute'); | ||
}); | ||
}); | ||
|
||
describe('execute(): ', () => { | ||
it('successfully applies Exponent strategy to given input.', async () => { | ||
expect.assertions(1); | ||
|
||
const expectedResult = [ | ||
{ | ||
duration: 3600, | ||
'energy/base': 2, | ||
energy: 8, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]; | ||
|
||
const result = await exponent.execute([ | ||
{ | ||
duration: 3600, | ||
'energy/base': 2, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it('throws an error on missing params in input.', async () => { | ||
const expectedMessage = | ||
'Exponent: energy/base is missing from the input array.'; | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
await exponent.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError(expectedMessage) | ||
); | ||
} | ||
}); | ||
|
||
it('throws an error on input param value not numeric.', async () => { | ||
const expectedMessage = 'Exponent: i-am-not-a-number is not numeric.'; | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
await exponent.execute([ | ||
{ | ||
duration: 3600, | ||
'energy/base': 'i-am-not-a-number', | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError(expectedMessage) | ||
); | ||
} | ||
}); | ||
|
||
it('returns a result with input params not related to energy.', async () => { | ||
expect.assertions(1); | ||
const newConfig = { | ||
'input-parameter': 'carbon/base', | ||
exponent: 4, | ||
'output-parameter': 'carbon', | ||
}; | ||
const exponent = Exponent(newConfig); | ||
|
||
const data = [ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
'carbon/base': 2, | ||
}, | ||
]; | ||
const response = await exponent.execute(data); | ||
|
||
const expectedResult = [ | ||
{ | ||
duration: 3600, | ||
'carbon/base': 2, | ||
carbon: 16, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]; | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.