-
Notifications
You must be signed in to change notification settings - Fork 173
Add helper method to ensure benchmark folder existency #2011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a1b1edf
Add helper method to ensure benchmark folder
ravikiranvm d2cb675
Fix copilot comment about tests
ravikiranvm 3fdb21d
Fix folder ensuring method as pointed out by copilot
ravikiranvm f118103
Add new method to folder service for our use case
ravikiranvm 2fba669
Merge branch 'main' into ops-3752
ravikiranvm 0c79232
Fix type and tests
ravikiranvm 78832b4
Fix test case name
ravikiranvm dae30f5
Fix tests and make functions module level
ravikiranvm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/server/api/src/app/benchmark/create-benchmark.service.ts
This file contains hidden or 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,58 @@ | ||
| import { | ||
| BenchmarkCreationResult, | ||
| BenchmarkProviders, | ||
| ContentType, | ||
| Folder, | ||
| openOpsId, | ||
| } from '@openops/shared'; | ||
| import { flowFolderService } from '../flows/folder/folder.service'; | ||
| import { throwValidationError } from './errors'; | ||
|
|
||
| function getBenchmarkFolderDisplayName(provider: string): string { | ||
| const normalizedProvider = provider.toLowerCase(); | ||
| switch (normalizedProvider) { | ||
| case BenchmarkProviders.AWS: | ||
| return 'AWS Benchmark'; | ||
| default: | ||
| throwValidationError(`Unknown provider: ${provider}`); | ||
| } | ||
| } | ||
|
|
||
| async function ensureBenchmarkFolder( | ||
| projectId: string, | ||
| displayName: string, | ||
| ): Promise<Folder> { | ||
| return flowFolderService.getOrCreate({ | ||
| projectId, | ||
| request: { | ||
| displayName, | ||
| contentType: ContentType.WORKFLOW, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export async function createBenchmark(params: { | ||
| provider: string; | ||
| projectId: string; | ||
| }): Promise<BenchmarkCreationResult> { | ||
| const { provider, projectId } = params; | ||
|
|
||
| const benchmarkFolder = await ensureBenchmarkFolder( | ||
| projectId, | ||
| getBenchmarkFolderDisplayName(provider), | ||
| ); | ||
|
|
||
| return { | ||
| benchmarkId: openOpsId(), | ||
| folderId: benchmarkFolder.id, | ||
| provider, | ||
| workflows: [], | ||
| webhookPayload: { | ||
| webhookBaseUrl: '', | ||
| workflows: [], | ||
| cleanupWorkflows: [], | ||
| accounts: [], | ||
| regions: [], | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
packages/server/api/test/unit/benchmark/create-benchmark.service.test.ts
This file contains hidden or 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,88 @@ | ||
| import { ContentType, type Folder } from '@openops/shared'; | ||
| import { createBenchmark } from '../../../src/app/benchmark/create-benchmark.service'; | ||
| import { flowFolderService } from '../../../src/app/flows/folder/folder.service'; | ||
|
|
||
| jest.mock('../../../src/app/flows/folder/folder.service', () => ({ | ||
| flowFolderService: { | ||
| getOrCreate: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const flowFolderServiceMock = flowFolderService as jest.Mocked< | ||
| typeof flowFolderService | ||
| >; | ||
|
|
||
| describe('create-benchmark.service', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('createBenchmark with provider aws calls getOrCreate with displayName AWS Benchmark', async () => { | ||
| const projectId = 'project-1'; | ||
| const folder: Folder = { | ||
| id: 'folder-1', | ||
| projectId, | ||
| displayName: 'AWS Benchmark', | ||
| created: '', | ||
| updated: '', | ||
| contentType: ContentType.WORKFLOW, | ||
| }; | ||
| flowFolderServiceMock.getOrCreate.mockResolvedValue(folder); | ||
|
|
||
| await createBenchmark({ provider: 'aws', projectId }); | ||
|
|
||
| expect(flowFolderServiceMock.getOrCreate).toHaveBeenCalledWith({ | ||
| projectId, | ||
| request: { | ||
| displayName: 'AWS Benchmark', | ||
| contentType: ContentType.WORKFLOW, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('createBenchmark throws for unknown provider', async () => { | ||
| const projectId = 'project-1'; | ||
| await expect( | ||
| createBenchmark({ provider: 'gcp', projectId }), | ||
| ).rejects.toThrow('Unknown provider: gcp'); | ||
| expect(flowFolderServiceMock.getOrCreate).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('createBenchmark returns BenchmarkCreationResult', async () => { | ||
| const projectId = 'project-1'; | ||
| const folder: Folder = { | ||
| id: 'folder-2', | ||
| projectId, | ||
| displayName: 'AWS Benchmark', | ||
| created: '', | ||
| updated: '', | ||
| contentType: ContentType.WORKFLOW, | ||
| }; | ||
|
|
||
| flowFolderServiceMock.getOrCreate.mockResolvedValue(folder); | ||
|
|
||
| const result = await createBenchmark({ | ||
| provider: 'aws', | ||
| projectId, | ||
| }); | ||
|
|
||
| expect(flowFolderServiceMock.getOrCreate).toHaveBeenCalledWith({ | ||
| projectId, | ||
| request: { | ||
| displayName: 'AWS Benchmark', | ||
| contentType: ContentType.WORKFLOW, | ||
| }, | ||
| }); | ||
| expect(result.folderId).toBe(folder.id); | ||
| expect(result.workflows).toEqual([]); | ||
| expect(result.benchmarkId).toBeDefined(); | ||
| expect(result.provider).toBe('aws'); | ||
| expect(result.webhookPayload).toEqual({ | ||
| webhookBaseUrl: '', | ||
| workflows: [], | ||
| cleanupWorkflows: [], | ||
| accounts: [], | ||
| regions: [], | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.