mirror of https://github.com/ghostfolio/ghostfolio
4 changed files with 121 additions and 4 deletions
@ -0,0 +1,57 @@ |
|||||
|
import { plainToInstance } from 'class-transformer'; |
||||
|
import { validateSync } from 'class-validator'; |
||||
|
|
||||
|
import { HoldingDto } from './holding.dto'; |
||||
|
|
||||
|
describe('HoldingDto', () => { |
||||
|
const getInvalidProperties = (holding: object) => { |
||||
|
return validateSync(plainToInstance(HoldingDto, holding), { |
||||
|
forbidNonWhitelisted: true, |
||||
|
whitelist: true |
||||
|
}).map(({ property }) => { |
||||
|
return property; |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
it('Accepts the allocation as weight', () => { |
||||
|
expect(getInvalidProperties({ name: 'Apple', weight: 0.5 })).toEqual([]); |
||||
|
}); |
||||
|
|
||||
|
it('Accepts the allocation as allocationInPercentage', () => { |
||||
|
expect( |
||||
|
getInvalidProperties({ allocationInPercentage: 0.5, name: 'Apple' }) |
||||
|
).toEqual([]); |
||||
|
}); |
||||
|
|
||||
|
it('Rejects a holding without an allocation', () => { |
||||
|
expect(getInvalidProperties({ name: 'Apple' })).toEqual([ |
||||
|
'allocationInPercentage', |
||||
|
'weight' |
||||
|
]); |
||||
|
}); |
||||
|
|
||||
|
it('Rejects an allocation out of range', () => { |
||||
|
expect(getInvalidProperties({ name: 'Apple', weight: 1.5 })).toEqual([ |
||||
|
'weight' |
||||
|
]); |
||||
|
expect( |
||||
|
getInvalidProperties({ allocationInPercentage: -0.5, name: 'Apple' }) |
||||
|
).toEqual(['allocationInPercentage']); |
||||
|
}); |
||||
|
|
||||
|
it('Rejects an invalid allocation next to a valid one', () => { |
||||
|
expect( |
||||
|
getInvalidProperties({ |
||||
|
allocationInPercentage: 1.5, |
||||
|
name: 'Apple', |
||||
|
weight: 0.5 |
||||
|
}) |
||||
|
).toEqual(['allocationInPercentage']); |
||||
|
}); |
||||
|
|
||||
|
it('Rejects an unknown property', () => { |
||||
|
expect( |
||||
|
getInvalidProperties({ name: 'Apple', value: 100, weight: 0.5 }) |
||||
|
).toEqual(['value']); |
||||
|
}); |
||||
|
}); |
||||
@ -1,11 +1,26 @@ |
|||||
import { IsNumber, IsString, Max, Min } from 'class-validator'; |
import { IsNumber, IsString, Max, Min, ValidateIf } from 'class-validator'; |
||||
|
|
||||
|
/** |
||||
|
* Accepts the allocation as weight (format of the asset profile) or as |
||||
|
* allocationInPercentage (format of the export), at least one is required |
||||
|
*/ |
||||
export class HoldingDto { |
export class HoldingDto { |
||||
@IsNumber() |
@IsNumber() |
||||
@Max(1) |
@Max(1) |
||||
@Min(0) |
@Min(0) |
||||
allocationInPercentage: number; |
@ValidateIf(({ weight }, value) => { |
||||
|
return value !== undefined || weight === undefined; |
||||
|
}) |
||||
|
allocationInPercentage?: number; |
||||
|
|
||||
@IsString() |
@IsString() |
||||
name: string; |
name: string; |
||||
|
|
||||
|
@IsNumber() |
||||
|
@Max(1) |
||||
|
@Min(0) |
||||
|
@ValidateIf(({ allocationInPercentage }, value) => { |
||||
|
return value !== undefined || allocationInPercentage === undefined; |
||||
|
}) |
||||
|
weight?: number; |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue