From cedde08dd1046fdacd21fa4a074ad51f6ea939bd Mon Sep 17 00:00:00 2001 From: manushak Date: Mon, 29 Jul 2024 20:10:54 +0400 Subject: [PATCH] test(builtins): add tests for the plugin --- .../if-run/builtins/time-converter.test.ts | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/__tests__/if-run/builtins/time-converter.test.ts diff --git a/src/__tests__/if-run/builtins/time-converter.test.ts b/src/__tests__/if-run/builtins/time-converter.test.ts new file mode 100644 index 000000000..ae55daf5d --- /dev/null +++ b/src/__tests__/if-run/builtins/time-converter.test.ts @@ -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); + }); + }); + }); +});