forked from kamranahmedse/aws-cost-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from route06/issue-9-aggregation-unit
Allow users to choose aggregation unit for `Breakdown by service`
- Loading branch information
Showing
9 changed files
with
250 additions
and
98 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,78 @@ | ||
import AWS from 'aws-sdk'; | ||
import { AWSConfig } from './config'; | ||
import { formatServiceBreakdown } from './slack'; | ||
import { generateMockedCostByService } from '../testUtils'; | ||
import { getTotalCosts } from '../cost'; | ||
import AWSMock from 'aws-sdk-mock'; | ||
|
||
const costDataLength = 65; | ||
const fixedToday = '2024-05-11'; // cost of 'this month' will be sum of 10 days from May 1 to May 10 ('today' is omitted because its cost is incomplete) | ||
|
||
const awsConfig: AWSConfig = { | ||
credentials: { | ||
accessKeyId: 'testAccessKeyId', | ||
secretAccessKey: 'testSecretAccessKey', | ||
sessionToken: 'testSessionToken', | ||
}, | ||
region: 'us-east-1', | ||
}; | ||
|
||
const mockedCostByService = generateMockedCostByService( | ||
fixedToday, | ||
costDataLength, | ||
); | ||
|
||
beforeAll(() => { | ||
AWSMock.setSDKInstance(AWS); | ||
}); | ||
|
||
afterAll(() => { | ||
AWSMock.restore(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers('modern'); | ||
jest.setSystemTime(new Date(fixedToday).getTime()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
AWSMock.mock('CostExplorer', 'getCostAndUsage', (params, callback) => { | ||
callback(null, mockedCostByService); | ||
}); | ||
|
||
describe('formatServiceBreakdown', () => { | ||
it('should return service breakdown of "yesterday"', async () => { | ||
const totalCosts = await getTotalCosts(awsConfig); | ||
const result = formatServiceBreakdown(totalCosts, 'yesterday'); | ||
|
||
// cost value is defined in generateMockedCostByService | ||
expect(result).toEqual('> service2: `$110.00`\n' + '> service1: `$1.10`'); | ||
}); | ||
|
||
it('should return service breakdown of "Last 7 days"', async () => { | ||
const totalCosts = await getTotalCosts(awsConfig); | ||
const result = formatServiceBreakdown(totalCosts, 'last7Days'); | ||
|
||
// cost value is defined in generateMockedCostByService | ||
expect(result).toEqual('> service2: `$770.00`\n' + '> service1: `$7.70`'); | ||
}); | ||
|
||
it('should return service breakdown of "This Month"', async () => { | ||
const totalCosts = await getTotalCosts(awsConfig); | ||
const result = formatServiceBreakdown(totalCosts, 'thisMonth'); | ||
|
||
// cost value is defined in generateMockedCostByService | ||
expect(result).toEqual('> service2: `$1100.00`\n' + '> service1: `$11.00`'); | ||
}); | ||
|
||
it('should return service breakdown of "Last Month"', async () => { | ||
const totalCosts = await getTotalCosts(awsConfig); | ||
const result = formatServiceBreakdown(totalCosts, 'lastMonth'); | ||
|
||
// cost value is defined in generateMockedCostByService | ||
expect(result).toEqual('> service2: `$3000.00`\n' + '> service1: `$30.00`'); | ||
}); | ||
}); |
Oops, something went wrong.