From 6080fcd785157ed8a6c5623f53616e43b1c2e049 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Fri, 1 Dec 2023 15:10:25 +0530 Subject: [PATCH] tests: organize tests into proper descriptions and cases Signed-off-by: Gnanakeethan Balasubramaniam --- src/__tests__/unit/lib/boavizta/index.test.ts | 93 ++++---- .../unit/lib/watt-time/index.test.ts | 204 ++++++++++-------- 2 files changed, 152 insertions(+), 145 deletions(-) diff --git a/src/__tests__/unit/lib/boavizta/index.test.ts b/src/__tests__/unit/lib/boavizta/index.test.ts index efaab9d..dd64e9e 100644 --- a/src/__tests__/unit/lib/boavizta/index.test.ts +++ b/src/__tests__/unit/lib/boavizta/index.test.ts @@ -383,7 +383,7 @@ describe('cpu:configure test', () => { test('initialize without params throws error for parameter and call execute without params throws error for input', async () => { const outputModel = new BoaviztaCpuOutputModel(); const outputModelConfigFail = new BoaviztaCpuOutputModel(); - await expect(outputModel.authenticate({})).resolves.toBe(undefined); + await expect(outputModel.authenticate({})).resolves.toBe(undefined); // authenticate does not have any params / output await expect( outputModelConfigFail.execute([ { @@ -442,6 +442,7 @@ describe('cpu:initialize with params', () => { }); test('initialize with params and call multiple usages in IMPL format:verbose', async () => { const outputModel = new BoaviztaCpuOutputModel(); + // test configuration with verbose false await expect( outputModel.configure({ 'physical-processor': 'Intel Xeon Gold 6138f', @@ -450,6 +451,7 @@ describe('cpu:initialize with params', () => { verbose: false, }) ).resolves.toBeInstanceOf(BoaviztaCpuOutputModel); + // test configuration with verbose true await expect( outputModel.configure({ 'physical-processor': 'Intel Xeon Gold 6138f', @@ -459,7 +461,7 @@ describe('cpu:initialize with params', () => { }) ).resolves.toBeInstanceOf(BoaviztaCpuOutputModel); - // configure without static params will cause improper configure error + // verbose still results in same output await expect( outputModel.execute([ { @@ -480,15 +482,6 @@ describe('cpu:initialize with params', () => { describe('cloud:initialize with params', () => { test('initialize with params and call usage in RAW Format', async () => { const outputModel = new BoaviztaCloudOutputModel(); - await expect( - outputModel.validateLocation({location: 'SomethingFail'}) - ).rejects.toThrow(); - await expect( - outputModel.validateInstanceType({'instance-type': 'SomethingFail'}) - ).rejects.toThrow(); - await expect( - outputModel.validateProvider({provider: 'SomethingFail'}) - ).rejects.toThrow(); await expect( outputModel.configure({ 'instance-type': 't2.micro', @@ -519,22 +512,55 @@ describe('cloud:initialize with params', () => { // configure without static params will cause improper configure error }); - - test("correct 'instance-type': initialize with params and call usage in IMPL Format", async () => { + test('invalid input for location throws error', async () => { + const outputModel = new BoaviztaCloudOutputModel(); + await expect( + outputModel.validateLocation({location: 'SomethingFail'}) + ).rejects.toThrow(); + }); + test('invalid input for instance type throws error', async () => { + const outputModel = new BoaviztaCloudOutputModel(); + await expect( + outputModel.validateInstanceType({'instance-type': 'SomethingFail'}) + ).rejects.toThrow(); + }); + test('invalid input for provider throws error', async () => { + const outputModel = new BoaviztaCloudOutputModel(); + await expect( + outputModel.validateProvider({provider: 'SomethingFail'}) + ).rejects.toThrow(); + }); + test('missing provider throws error', async () => { const outputModel = new BoaviztaCloudOutputModel(); - await expect( outputModel.configure({ 'instance-type': 't2.micro', location: 'USA', }) ).rejects.toThrow(); + }); + test('missing `instance-type` throws error', async () => { + const outputModel = new BoaviztaCloudOutputModel(); await expect( outputModel.configure({ provider: 'aws', location: 'USA', }) ).rejects.toThrow(); + }); + + test('wrong `instance-type` throws error', async () => { + const outputModel = new BoaviztaCloudOutputModel(); + await expect( + outputModel.configure({ + 'instance-type': 't5.micro', + location: 'USA', + provider: 'aws', + }) + ).rejects.toThrow(); + }); + test("correct 'instance-type': initialize with params and call usage in IMPL Format", async () => { + const outputModel = new BoaviztaCloudOutputModel(); await expect( outputModel.configure({ 'instance-type': 't2.micro', @@ -542,8 +568,6 @@ describe('cloud:initialize with params', () => { provider: 'aws', }) ).resolves.toBeInstanceOf(BoaviztaCloudOutputModel); - - // mockAxios.get.mockResolvedValue({data: {}}); await expect( outputModel.execute([ { @@ -568,43 +592,6 @@ describe('cloud:initialize with params', () => { ).rejects.toThrow(); }); - test('wrong "instance-type": initialize with params and call usage in IMPL Format throws error', async () => { - const outputModel = new BoaviztaCloudOutputModel(); - - await expect( - outputModel.configure({ - 'instance-type': 't5.micro', - location: 'USA', - provider: 'aws', - }) - ).rejects.toThrow(); - - // configure without static params will cause improper configure error - await expect( - outputModel.execute([ - { - timestamp: '2021-01-01T00:00:00Z', - duration: 15, - 'cpu-util': 34, - }, - { - timestamp: '2021-01-01T00:00:15Z', - duration: 15, - 'cpu-util': 12, - }, - { - timestamp: '2021-01-01T00:00:30Z', - duration: 15, - 'cpu-util': 1, - }, - { - timestamp: '2021-01-01T00:00:45Z', - duration: 15, - 'cpu-util': 78, - }, - ]) - ).rejects.toThrow(); - }); test('without "instance-type": initialize with params and call usage in IMPL Format throws error', async () => { const outputModel = new BoaviztaCloudOutputModel(); diff --git a/src/__tests__/unit/lib/watt-time/index.test.ts b/src/__tests__/unit/lib/watt-time/index.test.ts index f253661..c19cac0 100644 --- a/src/__tests__/unit/lib/watt-time/index.test.ts +++ b/src/__tests__/unit/lib/watt-time/index.test.ts @@ -87,20 +87,20 @@ mockAxios.get.mockImplementation((url, data) => { } }); describe('watt-time:configure test', () => { - test('initialize and test', async () => { + test('initialize without configurations throws error', async () => { await expect( new WattTimeGridEmissions().configure(undefined) ).rejects.toThrow(); - const model = await new WattTimeGridEmissions().configure({ - username: 'test1', - password: 'test2', - }); + }); + test('initialize with wrong credentials throw error', async () => { await expect( new WattTimeGridEmissions().configure({ username: 'test1', password: 'test1', }) ).rejects.toThrow(); + }); + test('initialize without either username / password throws error', async () => { await expect( new WattTimeGridEmissions().configure({ password: 'test1', @@ -108,80 +108,11 @@ describe('watt-time:configure test', () => { ).rejects.toThrow(); await expect( new WattTimeGridEmissions().configure({ - username: 'ENV_WATT_USERNAME', - password: 'ENV_WATT_PASSWORD', - }) - ).rejects.toThrow(); - await expect( - new WattTimeGridEmissions().configure({ - token: 'ENV_WATT_TOKEN', + username: 'test1', }) ).rejects.toThrow(); - expect(model).toBeInstanceOf(WattTimeGridEmissions); - await expect( - model.execute([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 1200, - }, - ]) - ).resolves.toStrictEqual([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 1200, - 'grid-carbon-intensity': 2185.332173907599, - }, - ]); - await expect( - model.execute([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 120, - }, - ]) - ).resolves.toStrictEqual([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 120, - 'grid-carbon-intensity': 2198.0087539832293, - }, - ]); - await expect( - model.execute([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 300, - }, - ]) - ).resolves.toStrictEqual([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 300, - 'grid-carbon-intensity': 2198.0087539832293, - }, - ]); - await expect( - model.execute([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 360, - }, - ]) - ).resolves.toStrictEqual([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 360, - 'grid-carbon-intensity': 2193.5995087395318, - }, - ]); + }); + test('initialize with wrong username / password throws error', async () => { const modelFail = await new WattTimeGridEmissions().configure({ baseUrl: 'https://apifail.watttime.org/v2', username: 'test1', @@ -196,21 +127,28 @@ describe('watt-time:configure test', () => { }, ]) ).rejects.toThrow(); - + }); + test('initialize with undefined environment variables throw error', async () => { await expect( - model.execute([ - { - location: '37.7749,-122.4194', - timestamp: '2021-01-01T00:00:00Z', - duration: 3600, - }, - { - location: '37.7749,-122.4194', - timestamp: '2021-01-02T01:00:00Z', - duration: 3600, - }, - ]) + new WattTimeGridEmissions().configure({ + username: 'ENV_WATT_USERNAME', + password: 'ENV_WATT_PASSWORD', + }) + ).rejects.toThrow(); + await expect( + new WattTimeGridEmissions().configure({ + token: 'ENV_WATT_TOKEN', + }) ).rejects.toThrow(); + }); + + test('throws error if watttime api returns wrong data', async () => { + const model = await new WattTimeGridEmissions().configure({ + username: 'test1', + password: 'test2', + }); + + // data is not enough to proceed with computation await expect( model.execute([ { @@ -220,11 +158,17 @@ describe('watt-time:configure test', () => { }, { location: '37.7749,-122.4194', - timestamp: '2022-01-02T01:00:00Z', + timestamp: '2021-01-02T01:00:00Z', duration: 3600, }, ]) ).rejects.toThrow(); + }); + test('throws error if wrong location is provided', async () => { + const model = await new WattTimeGridEmissions().configure({ + username: 'test1', + password: 'test2', + }); await expect( model.execute([ { @@ -261,7 +205,8 @@ describe('watt-time:configure test', () => { }, ]) ).rejects.toThrow(); - + }); + test('throws error if no data is returned by API', async () => { const modelFail2 = await new WattTimeGridEmissions().configure({ username: 'test1', password: 'test2', @@ -281,6 +226,9 @@ describe('watt-time:configure test', () => { }, ]) ).rejects.toThrow(); + }); + test('throws error if unauthorized error occurs during data fetch', async () => { + const modelFail3 = await new WattTimeGridEmissions().configure({ username: 'test1', password: 'test2', @@ -320,4 +268,76 @@ describe('watt-time:configure test', () => { ]) ).rejects.toThrow(); }); + + test('proper initialization and test', async () => { + const model = await new WattTimeGridEmissions().configure({ + username: 'test1', + password: 'test2', + }); + expect(model).toBeInstanceOf(WattTimeGridEmissions); + await expect( + model.execute([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 1200, + }, + ]) + ).resolves.toStrictEqual([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 1200, + 'grid-carbon-intensity': 2185.332173907599, + }, + ]); + await expect( + model.execute([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 120, + }, + ]) + ).resolves.toStrictEqual([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 120, + 'grid-carbon-intensity': 2198.0087539832293, + }, + ]); + await expect( + model.execute([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 300, + }, + ]) + ).resolves.toStrictEqual([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 300, + 'grid-carbon-intensity': 2198.0087539832293, + }, + ]); + await expect( + model.execute([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 360, + }, + ]) + ).resolves.toStrictEqual([ + { + location: '37.7749,-122.4194', + timestamp: '2021-01-01T00:00:00Z', + duration: 360, + 'grid-carbon-intensity': 2193.5995087395318, + }, + ]); + }); });