From 8dc3c76ea21fe4d298cb5f202607608284cc14f5 Mon Sep 17 00:00:00 2001 From: manushak Date: Mon, 12 Aug 2024 17:29:27 +0400 Subject: [PATCH] test(builtins): update test accorgind to changes --- .../if-run/builtins/coefficient.test.ts | 3 +- .../if-run/builtins/copy-param.test.ts | 2 +- .../if-run/builtins/csv-lookup.test.ts | 5 +- src/__tests__/if-run/builtins/divide.test.ts | 2 +- .../if-run/builtins/exponent.test.ts | 7 ++- .../if-run/builtins/interpolation.test.ts | 14 +++++ .../if-run/builtins/mock-observations.test.ts | 2 +- .../if-run/builtins/multiply.test.ts | 10 +++- src/__tests__/if-run/builtins/regex.test.ts | 2 +- .../if-run/builtins/sci-embodied.test.ts | 13 ++-- src/__tests__/if-run/builtins/sci.test.ts | 4 +- src/__tests__/if-run/builtins/shell.test.ts | 59 +------------------ .../if-run/builtins/subtract.test.ts | 6 +- src/__tests__/if-run/builtins/sum.test.ts | 9 ++- .../if-run/builtins/time-sync.test.ts | 10 +++- 15 files changed, 65 insertions(+), 83 deletions(-) diff --git a/src/__tests__/if-run/builtins/coefficient.test.ts b/src/__tests__/if-run/builtins/coefficient.test.ts index 9a0db46d1..40bc2d2cf 100644 --- a/src/__tests__/if-run/builtins/coefficient.test.ts +++ b/src/__tests__/if-run/builtins/coefficient.test.ts @@ -57,6 +57,7 @@ describe('builtins/coefficient: ', () => { const mapping = { carbon: 'carbon-for-production', }; + const coefficient = Coefficient( globalConfig, parametersMetadata, @@ -76,7 +77,7 @@ describe('builtins/coefficient: ', () => { const result = coefficient.execute([ { duration: 3600, - carbon: 3, + 'carbon-for-production': 3, timestamp: '2021-01-01T00:00:00Z', }, ]); diff --git a/src/__tests__/if-run/builtins/copy-param.test.ts b/src/__tests__/if-run/builtins/copy-param.test.ts index 9f64539c2..4b893b60b 100644 --- a/src/__tests__/if-run/builtins/copy-param.test.ts +++ b/src/__tests__/if-run/builtins/copy-param.test.ts @@ -72,7 +72,7 @@ describe('builtins/copy: ', () => { { timestamp: '2021-01-01T00:00:00Z', duration: 3600, - original: 'hello', + from: 'hello', }, ]); diff --git a/src/__tests__/if-run/builtins/csv-lookup.test.ts b/src/__tests__/if-run/builtins/csv-lookup.test.ts index 52198de47..546771e71 100644 --- a/src/__tests__/if-run/builtins/csv-lookup.test.ts +++ b/src/__tests__/if-run/builtins/csv-lookup.test.ts @@ -133,7 +133,6 @@ describe('builtins/CSVLookup: ', () => { }; const parameterMetadata = {inputs: {}, outputs: {}}; const mapping = { - tdp: 'cpu/tdp', 'cpu/utilized': 'cpu/util', }; const csvLookup = CSVLookup(globalConfig, parameterMetadata, mapping); @@ -142,7 +141,7 @@ describe('builtins/CSVLookup: ', () => { { timestamp: '2024-03-01', 'cpu/available': 16, - 'cpu/utilized': 16, + 'cpu/util': 16, 'cpu/manufacturer': 'AWS', }, ]); @@ -152,7 +151,7 @@ describe('builtins/CSVLookup: ', () => { 'cpu/available': 16, 'cpu/util': 16, 'cpu/manufacturer': 'AWS', - 'cpu/tdp': 150, + tdp: 150, }, ]; diff --git a/src/__tests__/if-run/builtins/divide.test.ts b/src/__tests__/if-run/builtins/divide.test.ts index 1d616f7ab..895259dc2 100644 --- a/src/__tests__/if-run/builtins/divide.test.ts +++ b/src/__tests__/if-run/builtins/divide.test.ts @@ -71,7 +71,7 @@ describe('builtins/divide: ', () => { const result = await divide.execute([ { duration: 3600, - 'vcpus-allocated': 24, + 'vcpus-distributed': 24, timestamp: '2021-01-01T00:00:00Z', }, ]); diff --git a/src/__tests__/if-run/builtins/exponent.test.ts b/src/__tests__/if-run/builtins/exponent.test.ts index 3eef64c1e..9af6c0e4e 100644 --- a/src/__tests__/if-run/builtins/exponent.test.ts +++ b/src/__tests__/if-run/builtins/exponent.test.ts @@ -53,6 +53,11 @@ describe('builtins/exponent: ', () => { const mapping = { 'energy/base': 'energy/main', }; + const globalConfig = { + 'input-parameter': 'energy/base', + exponent: 3, + 'output-parameter': 'energy', + }; const exponent = Exponent(globalConfig, parametersMetadata, mapping); const expectedResult = [ { @@ -66,7 +71,7 @@ describe('builtins/exponent: ', () => { const result = await exponent.execute([ { duration: 3600, - 'energy/base': 2, + 'energy/main': 2, timestamp: '2021-01-01T00:00:00Z', }, ]); diff --git a/src/__tests__/if-run/builtins/interpolation.test.ts b/src/__tests__/if-run/builtins/interpolation.test.ts index f5f1b7f27..f48ef8dd3 100644 --- a/src/__tests__/if-run/builtins/interpolation.test.ts +++ b/src/__tests__/if-run/builtins/interpolation.test.ts @@ -61,6 +61,20 @@ describe('builtins/interpolation: ', () => { 'cpu/utilization': 'cpu/util', 'interpolation-result': 'result', }; + const globalConfig = { + method: Method.LINEAR, + x: [0, 10, 50, 100], + y: [0.12, 0.32, 0.75, 1.02], + 'input-parameter': 'cpu/utilization', + 'output-parameter': 'interpolation-result', + }; + const inputs = [ + { + timestamp: '2023-07-06T00:00', + duration: 3600, + 'cpu/util': 45, + }, + ]; const plugin = Interpolation(globalConfig, parametersMetadata, mapping); const outputs = [ { diff --git a/src/__tests__/if-run/builtins/mock-observations.test.ts b/src/__tests__/if-run/builtins/mock-observations.test.ts index 3c43040e7..e548a7d46 100644 --- a/src/__tests__/if-run/builtins/mock-observations.test.ts +++ b/src/__tests__/if-run/builtins/mock-observations.test.ts @@ -111,7 +111,7 @@ describe('builtins/mock-observations: ', () => { 'common-key': 'common-val', }, randint: { - 'cpu/utilization': {min: 10, max: 11}, + 'cpu/util': {min: 10, max: 11}, }, }, }; diff --git a/src/__tests__/if-run/builtins/multiply.test.ts b/src/__tests__/if-run/builtins/multiply.test.ts index ec6571863..8e2eab6af 100644 --- a/src/__tests__/if-run/builtins/multiply.test.ts +++ b/src/__tests__/if-run/builtins/multiply.test.ts @@ -58,6 +58,10 @@ describe('builtins/multiply: ', () => { 'network/energy': 'energy-from-network', 'memory/energy': 'energy-from-memory', }; + const globalConfig = { + 'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'], + 'output-parameter': 'energy', + }; const multiply = Multiply(globalConfig, parametersMetadata, mapping); const expectedResult = [ @@ -75,9 +79,9 @@ describe('builtins/multiply: ', () => { { timestamp: '2021-01-01T00:00:00Z', duration: 3600, - 'cpu/energy': 2, - 'network/energy': 2, - 'memory/energy': 2, + 'energy-from-cpu': 2, + 'energy-from-network': 2, + 'energy-from-memory': 2, }, ]); diff --git a/src/__tests__/if-run/builtins/regex.test.ts b/src/__tests__/if-run/builtins/regex.test.ts index 1fc5070a8..a139a5635 100644 --- a/src/__tests__/if-run/builtins/regex.test.ts +++ b/src/__tests__/if-run/builtins/regex.test.ts @@ -104,7 +104,7 @@ describe('builtins/regex: ', () => { { timestamp: '2023-08-06T00:00', duration: 3600, - 'cloud/instance-type': 'Standard_DS1_v2', + 'instance-type': 'Standard_DS1_v2', }, ]); diff --git a/src/__tests__/if-run/builtins/sci-embodied.test.ts b/src/__tests__/if-run/builtins/sci-embodied.test.ts index 47cc028d5..bf3ebfb1e 100644 --- a/src/__tests__/if-run/builtins/sci-embodied.test.ts +++ b/src/__tests__/if-run/builtins/sci-embodied.test.ts @@ -13,7 +13,7 @@ describe('builtins/sci-embodied:', () => { inputs: {}, outputs: {}, }; - const sciEmbodied = SciEmbodied(parametersMetadata, {}); + const sciEmbodied = SciEmbodied(undefined, parametersMetadata, {}); describe('init: ', () => { it('successfully initalized.', () => { @@ -72,14 +72,13 @@ describe('builtins/sci-embodied:', () => { it('executes when `mapping` has valid data.', async () => { const mapping = { 'device/emissions-embodied': 'device/carbon-footprint', - 'carbon-embodied': 'carbon-footprint', }; - const sciEmbodied = SciEmbodied(parametersMetadata, mapping); + const sciEmbodied = SciEmbodied(undefined, parametersMetadata, mapping); const inputs = [ { timestamp: '2021-01-01T00:00:00Z', duration: 60 * 60 * 24 * 30, - 'device/emissions-embodied': 200, + 'device/carbon-footprint': 200, 'device/expected-lifespan': 60 * 60 * 24 * 365 * 4, 'resources-reserved': 1, 'resources-total': 1, @@ -87,7 +86,7 @@ describe('builtins/sci-embodied:', () => { { timestamp: '2021-01-01T00:00:00Z', duration: 60 * 60 * 24 * 30 * 2, - 'device/emissions-embodied': 200, + 'device/carbon-footprint': 200, 'device/expected-lifespan': 60 * 60 * 24 * 365 * 4, 'resources-reserved': 1, 'resources-total': 1, @@ -106,7 +105,7 @@ describe('builtins/sci-embodied:', () => { 'device/expected-lifespan': 60 * 60 * 24 * 365 * 4, 'resources-reserved': 1, 'resources-total': 1, - 'carbon-footprint': 4.10958904109589, + 'carbon-embodied': 4.10958904109589, }, { timestamp: '2021-01-01T00:00:00Z', @@ -115,7 +114,7 @@ describe('builtins/sci-embodied:', () => { 'device/expected-lifespan': 60 * 60 * 24 * 365 * 4, 'resources-reserved': 1, 'resources-total': 1, - 'carbon-footprint': 4.10958904109589 * 2, + 'carbon-embodied': 4.10958904109589 * 2, }, ]); }); diff --git a/src/__tests__/if-run/builtins/sci.test.ts b/src/__tests__/if-run/builtins/sci.test.ts index 37826ecc8..143d34757 100644 --- a/src/__tests__/if-run/builtins/sci.test.ts +++ b/src/__tests__/if-run/builtins/sci.test.ts @@ -48,7 +48,7 @@ describe('builtins/sci:', () => { it('successfully executes when `mapping` has valid data.', async () => { const mapping = { - 'carbon-embodied': 'carbon-footprint', + 'carbon-footprint': 'carbon-embodied', }; const sci = Sci(config, parametersMetadata, mapping); const inputs = [ @@ -69,7 +69,7 @@ describe('builtins/sci:', () => { { timestamp: '2021-01-01T00:00:00Z', 'carbon-operational': 0.02, - 'carbon-footprint': 5, + 'carbon-embodied': 5, carbon: 5.02, users: 100, duration: 1, diff --git a/src/__tests__/if-run/builtins/shell.test.ts b/src/__tests__/if-run/builtins/shell.test.ts index 4febe9f93..125595ced 100644 --- a/src/__tests__/if-run/builtins/shell.test.ts +++ b/src/__tests__/if-run/builtins/shell.test.ts @@ -16,7 +16,7 @@ describe('builtins/shell', () => { inputs: {}, outputs: {}, }; - const shell = Shell(globalConfig, parametersMetadata, {}); + const shell = Shell(globalConfig, parametersMetadata); describe('init: ', () => { it('successfully initalized.', () => { @@ -56,61 +56,8 @@ describe('builtins/shell', () => { expect(mockLoadAll).toHaveBeenCalledWith('mocked stdout'); }); - it('executes when `mapping` is provided.', async () => { - const mapping = { - 'cpu/energy': 'energy-for-cpu', - }; - const shell = Shell(globalConfig, parametersMetadata, mapping); - const mockSpawnSync = spawnSync as jest.MockedFunction< - typeof spawnSync - >; - mockSpawnSync.mockReturnValueOnce({stdout: 'mocked stdout'} as any); - - const mockLoadAll = loadAll as jest.MockedFunction; - mockLoadAll.mockReturnValueOnce([ - { - timestamp: '2023-11-02T10:35:31.820Z', - duration: 3600, - 'energy-for-cpu': 0.002, - 'memory/energy': 0.000005, - energy: 1, - }, - ] as any); - - const inputs = [ - { - timestamp: '2023-11-02T10:35:31.820Z', - duration: 3600, - 'cpu/energy': 0.002, - 'memory/energy': 0.000005, - }, - ]; - - expect.assertions(3); - - const result = await shell.execute(inputs); - expect(result).toEqual([ - { - timestamp: '2023-11-02T10:35:31.820Z', - duration: 3600, - 'energy-for-cpu': 0.002, - 'memory/energy': 0.000005, - energy: 1, - }, - ]); - - expect(mockSpawnSync).toHaveBeenCalledWith( - 'python3', - ['/path/to/script.py'], - { - encoding: 'utf8', - } - ); - expect(mockLoadAll).toHaveBeenCalledWith('mocked stdout'); - }); - it('throws an error if validation fails.', async () => { - const shell = Shell({}, parametersMetadata, {}); + const shell = Shell({}, parametersMetadata); const invalidInputs = [ {duration: 3600, timestamp: '2022-01-01T00:00:00Z', command: 123}, ]; @@ -129,7 +76,7 @@ describe('builtins/shell', () => { }); it('throws an error when shell could not run command.', async () => { - const shell = Shell(globalConfig, parametersMetadata, {}); + const shell = Shell(globalConfig, parametersMetadata); (spawnSync as jest.Mock).mockImplementation(() => { throw new InputValidationError('Could not run the command'); }); diff --git a/src/__tests__/if-run/builtins/subtract.test.ts b/src/__tests__/if-run/builtins/subtract.test.ts index eef766867..751d1a820 100644 --- a/src/__tests__/if-run/builtins/subtract.test.ts +++ b/src/__tests__/if-run/builtins/subtract.test.ts @@ -55,6 +55,10 @@ describe('builtins/subtract: ', () => { const mapping = { 'cpu/energy': 'energy-for-cpu', }; + const globalConfig = { + 'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'], + 'output-parameter': 'energy/diff', + }; const subtract = Subtract(globalConfig, parametersMetadata, mapping); expect.assertions(1); @@ -72,7 +76,7 @@ describe('builtins/subtract: ', () => { const result = await subtract.execute([ { duration: 3600, - 'cpu/energy': 4, + 'energy-for-cpu': 4, 'network/energy': 2, 'memory/energy': 1, timestamp: '2021-01-01T00:00:00Z', diff --git a/src/__tests__/if-run/builtins/sum.test.ts b/src/__tests__/if-run/builtins/sum.test.ts index b377ca3b4..1baf2acac 100644 --- a/src/__tests__/if-run/builtins/sum.test.ts +++ b/src/__tests__/if-run/builtins/sum.test.ts @@ -56,10 +56,15 @@ describe('builtins/sum: ', () => { it('successfully executes when `mapping` has valid data.', () => { expect.assertions(1); + const mapping = { 'cpu/energy': 'energy-from-cpu', 'network/energy': 'energy-from-network', }; + const globalConfig = { + 'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'], + 'output-parameter': 'energy', + }; const sum = Sum(globalConfig, parametersMetadata, mapping); @@ -78,8 +83,8 @@ describe('builtins/sum: ', () => { { timestamp: '2021-01-01T00:00:00Z', duration: 3600, - 'cpu/energy': 1, - 'network/energy': 1, + 'energy-from-cpu': 1, + 'energy-from-network': 1, 'memory/energy': 1, }, ]); diff --git a/src/__tests__/if-run/builtins/time-sync.test.ts b/src/__tests__/if-run/builtins/time-sync.test.ts index b21ef03c8..9a3507599 100644 --- a/src/__tests__/if-run/builtins/time-sync.test.ts +++ b/src/__tests__/if-run/builtins/time-sync.test.ts @@ -632,7 +632,7 @@ describe('builtins/time-sync:', () => { expect(result).toStrictEqual(expectedResult); }); - it.skip('returns a result when `mapping` has valid data.', async () => { + it('returns a result when `mapping` has valid data.', async () => { const basicConfig = { 'start-time': '2023-12-12T00:00:00.000Z', 'end-time': '2023-12-12T00:00:09.000Z', @@ -642,19 +642,23 @@ describe('builtins/time-sync:', () => { const mapping = { 'time-reserved': 'time-allocated', }; + + storeAggregationMetrics({'time-allocated': 'avg'}); + storeAggregationMetrics({'resources-total': 'sum'}); + const timeModel = TimeSync(basicConfig, parametersMetadata, mapping); const result = await timeModel.execute([ { timestamp: '2023-12-12T00:00:00.000Z', duration: 3, - 'time-reserved': 5, + 'time-allocated': 5, 'resources-total': 10, }, { timestamp: '2023-12-12T00:00:05.000Z', duration: 3, - 'time-reserved': 5, + 'time-allocated': 5, 'resources-total': 10, }, ]);