-
Notifications
You must be signed in to change notification settings - Fork 10
Use Case for Getting Available Dataset Types #364
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
Closed
Closed
Changes from all commits
Commits
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
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,6 @@ | ||
| export interface DatasetType { | ||
| id: number | ||
| name: string | ||
| linkedMetadataBlocks?: string[] | ||
| availableLicenses?: string[] | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/datasetTypes/domain/repositories/IDatasetTypesRepository.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,5 @@ | ||
| import { DatasetType } from '../models/DatasetType' | ||
|
|
||
| export interface IDatasetTypesRepository { | ||
| getAvailableDatasetTypes(): Promise<DatasetType[]> | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/datasetTypes/domain/repositories/transformers/DatasetTypePayload.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,6 @@ | ||
| export interface DatasetTypePayload { | ||
| id: number | ||
| name: string | ||
| linkedMetadataBlocks?: string[] | ||
| availableLicenses?: string[] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/datasetTypes/domain/repositories/transformers/datasetTypeTransformers.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,14 @@ | ||
| import { AxiosResponse } from 'axios' | ||
| import { DatasetType } from '../../models/DatasetType' | ||
| import { DatasetTypePayload } from './DatasetTypePayload' | ||
|
|
||
| export const transformPayloadToDatasetType = (response: AxiosResponse): DatasetType[] => { | ||
| const payload = response.data.data as DatasetTypePayload[] | ||
|
|
||
| return payload.map((datasetType: DatasetTypePayload) => ({ | ||
| id: datasetType.id, | ||
| name: datasetType.name, | ||
| linkedMetadataBlocks: datasetType.linkedMetadataBlocks, | ||
| availableLicenses: datasetType.availableLicenses | ||
| })) | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/datasetTypes/domain/useCases/GetAvailableDatasetTypes.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,20 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { DatasetType } from '../models/DatasetType' | ||
| import { IDatasetTypesRepository } from '../repositories/IDatasetTypesRepository' | ||
|
|
||
| export class GetAvailableDatasetTypes implements UseCase<DatasetType[]> { | ||
| private datasetTypesRepository: IDatasetTypesRepository | ||
|
|
||
| constructor(datasetTypesRepository: IDatasetTypesRepository) { | ||
| this.datasetTypesRepository = datasetTypesRepository | ||
| } | ||
|
|
||
| /** | ||
| * Returns the list of available dataset types that can be selected for a dataset. | ||
| * | ||
| * @returns {Promise<DatasetType[]>} | ||
| */ | ||
| async execute(): Promise<DatasetType[]> { | ||
| return await this.datasetTypesRepository.getAvailableDatasetTypes() | ||
| } | ||
| } |
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,10 @@ | ||
| import { DatasetTypesRepository } from './infra/repositories/DatasetTypesRepository' | ||
| import { GetAvailableDatasetTypes } from './domain/useCases/GetAvailableDatasetTypes' | ||
|
|
||
| const datasetTypesRepository = new DatasetTypesRepository() | ||
|
|
||
| const getAvailableDatasetTypes = new GetAvailableDatasetTypes(datasetTypesRepository) | ||
|
|
||
| export { getAvailableDatasetTypes } | ||
|
|
||
| export { DatasetType } from './domain/models/DatasetType' |
15 changes: 15 additions & 0 deletions
15
src/datasetTypes/infra/repositories/DatasetTypesRepository.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,15 @@ | ||
| import { ApiRepository } from '../../../core/infra/repositories/ApiRepository' | ||
| import { IDatasetTypesRepository } from '../../domain/repositories/IDatasetTypesRepository' | ||
| import { DatasetType } from '../../domain/models/DatasetType' | ||
|
|
||
| export class DatasetTypesRepository extends ApiRepository implements IDatasetTypesRepository { | ||
| private readonly datasetTypesResourceName: string = 'datasets/datasetTypes' | ||
|
|
||
| public async getAvailableDatasetTypes(): Promise<DatasetType[]> { | ||
| return this.doGet(this.buildApiEndpoint(this.datasetTypesResourceName)) | ||
| .then((response) => response.data.data) | ||
| .catch((error) => { | ||
| throw error | ||
| }) | ||
| } | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
test/functional/datasetTypes/GetAvailableDatasetTypes.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,31 @@ | ||
| import { ApiConfig } from '../../../src' | ||
| import { getAvailableDatasetTypes } from '../../../src/datasetTypes' | ||
| import { DatasetType } from '../../../src/datasetTypes/domain/models/DatasetType' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
|
|
||
| describe('getAvailableDatasetTypes', () => { | ||
| describe('execute', () => { | ||
| beforeAll(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should return available dataset types', async () => { | ||
| const actualDatasetTypes: DatasetType[] = await getAvailableDatasetTypes.execute() | ||
| const expectedDatasetTypes = [ | ||
| { | ||
| id: 1, | ||
| name: 'dataset', | ||
| linkedMetadataBlocks: [], | ||
| availableLicenses: [] | ||
| } | ||
| ] | ||
|
|
||
| expect(actualDatasetTypes).toEqual(expectedDatasetTypes) | ||
| }) | ||
| }) | ||
| }) | ||
36 changes: 36 additions & 0 deletions
36
test/integration/datasetTypes/DatasetTypesRepository.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,36 @@ | ||
| import { | ||
| ApiConfig, | ||
| DataverseApiAuthMechanism | ||
| } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { DatasetTypesRepository } from '../../../src/datasetTypes/infra/repositories/DatasetTypesRepository' | ||
| // import { LicensesRepository } from '../../../src/licenses/infra/repositories/LicensesRepository' | ||
|
|
||
| describe('DatasetTypesRepository', () => { | ||
| const sut: DatasetTypesRepository = new DatasetTypesRepository() | ||
|
|
||
| describe('getAvailableDatasetTypes', () => { | ||
| beforeAll(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should return list of available dataset types', async () => { | ||
| const actual = await sut.getAvailableDatasetTypes() | ||
|
|
||
| const datasetTypes = [ | ||
| { | ||
| id: 1, | ||
| name: 'dataset', | ||
| linkedMetadataBlocks: [], | ||
| availableLicenses: [] | ||
| } | ||
| ] | ||
|
|
||
| expect(actual).toEqual(datasetTypes) | ||
| }) | ||
| }) | ||
| }) |
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,50 @@ | ||
| import { ReadError } from '../../../src' | ||
| // import { DatasetType } from '../../../src/datasetTypes/models/DatasetType' | ||
| import { DatasetType } from '../../../src/datasetTypes/domain/models/DatasetType' | ||
| import { IDatasetTypesRepository } from '../../../src/datasetTypes/domain/repositories/IDatasetTypesRepository' | ||
| import { GetAvailableDatasetTypes } from '../../../src/datasetTypes/domain/useCases/GetAvailableDatasetTypes' | ||
|
|
||
| describe('GetAvailableDatasetTypes', () => { | ||
| describe('execute', () => { | ||
| test('should return datasetTypes array on repository success', async () => { | ||
| const datasetTypesRepositoryStub: IDatasetTypesRepository = {} as IDatasetTypesRepository | ||
|
|
||
| const testDatasetTypes: DatasetType[] = [ | ||
| { | ||
| id: 1, | ||
| name: 'dataset', | ||
| linkedMetadataBlocks: [], | ||
| availableLicenses: [] | ||
| }, | ||
| { | ||
| id: 2, | ||
| name: 'software', | ||
| linkedMetadataBlocks: ['codeMeta20'], | ||
| availableLicenses: ['MIT', 'Apache-2.0'] | ||
| } | ||
| ] | ||
|
|
||
| datasetTypesRepositoryStub.getAvailableDatasetTypes = jest | ||
| .fn() | ||
| .mockResolvedValue(testDatasetTypes) | ||
| const sut = new GetAvailableDatasetTypes(datasetTypesRepositoryStub) | ||
|
|
||
| const actual = await sut.execute() | ||
|
|
||
| expect(actual).toEqual(testDatasetTypes) | ||
| expect(datasetTypesRepositoryStub.getAvailableDatasetTypes).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('should return error result on repository error', async () => { | ||
| const datasetTypesRepositoryStub: IDatasetTypesRepository = {} as IDatasetTypesRepository | ||
| const expectedError = new ReadError('Failed to fetch dataset types') | ||
| datasetTypesRepositoryStub.getAvailableDatasetTypes = jest | ||
| .fn() | ||
| .mockRejectedValue(expectedError) | ||
| const sut = new GetAvailableDatasetTypes(datasetTypesRepositoryStub) | ||
|
|
||
| await expect(sut.execute()).rejects.toThrow(ReadError) | ||
| expect(datasetTypesRepositoryStub.getAvailableDatasetTypes).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed at
dataverse-client-javascript/test/functional/licenses/GetAvailableStandardLicenses.test.ts
Line 1 in 47d2561
I'm not sure why this isn't working for me but perhaps it's related to the error I'm seeing in
src/index.tsin VS Code:I'm not sure what's going on. 🤷