fix bad tests, fix invalid value object validations

This commit is contained in:
sbriat
2023-09-20 11:00:04 +02:00
parent 6b6a169dee
commit c9c682c1fc
16 changed files with 394 additions and 111 deletions

View File

@@ -22,7 +22,7 @@ export class Point extends ValueObject<PointProps> {
return this.props.lat;
}
protected validate(props: PointProps): void {
validate(props: PointProps): void {
if (props.lon > 180 || props.lon < -180)
throw new ArgumentOutOfRangeException('lon must be between -180 and 180');
if (props.lat > 90 || props.lat < -90)

View File

@@ -1,5 +1,5 @@
import { ArgumentInvalidException, ValueObject } from '@mobicoop/ddd-library';
import { PointProps } from './point.value-object';
import { Point, PointProps } from './point.value-object';
/** Note:
* Value Objects with multiple properties can contain
@@ -29,6 +29,11 @@ export class Step extends ValueObject<StepProps> {
}
protected validate(props: StepProps): void {
// validate point props
new Point({
lon: props.lon,
lat: props.lat,
});
if (props.duration < 0)
throw new ArgumentInvalidException(
'duration must be greater than or equal to 0',

View File

@@ -11,39 +11,31 @@ describe('Point value object', () => {
expect(pointVO.lon).toBe(6.17651);
});
it('should throw an exception if longitude is invalid', () => {
try {
expect(() => {
new Point({
lat: 48.689445,
lon: 186.17651,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
try {
}).toThrow(ArgumentOutOfRangeException);
expect(() => {
new Point({
lat: 48.689445,
lon: -186.17651,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
}).toThrow(ArgumentOutOfRangeException);
});
it('should throw an exception if latitude is invalid', () => {
try {
expect(() => {
new Point({
lat: 148.689445,
lon: 6.17651,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
try {
}).toThrow(ArgumentOutOfRangeException);
expect(() => {
new Point({
lat: -148.689445,
lon: 6.17651,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
}).toThrow(ArgumentOutOfRangeException);
});
});

View File

@@ -18,71 +18,59 @@ describe('Step value object', () => {
expect(stepVO.lon).toBe(6.17651);
});
it('should throw an exception if longitude is invalid', () => {
try {
expect(() => {
new Step({
lat: 48.689445,
lon: 186.17651,
duration: 150,
distance: 12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
try {
}).toThrow(ArgumentOutOfRangeException);
expect(() => {
new Step({
lon: 48.689445,
lat: -186.17651,
lat: 48.689445,
lon: -186.17651,
duration: 150,
distance: 12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
}).toThrow(ArgumentOutOfRangeException);
});
it('should throw an exception if latitude is invalid', () => {
try {
expect(() => {
new Step({
lat: 248.689445,
lon: 6.17651,
duration: 150,
distance: 12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
try {
}).toThrow(ArgumentOutOfRangeException);
expect(() => {
new Step({
lon: -148.689445,
lat: 6.17651,
lat: -148.689445,
lon: 6.17651,
duration: 150,
distance: 12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
}
}).toThrow(ArgumentOutOfRangeException);
});
it('should throw an exception if distance is invalid', () => {
try {
expect(() => {
new Step({
lat: 48.689445,
lon: 6.17651,
duration: 150,
distance: -12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentInvalidException);
}
}).toThrow(ArgumentInvalidException);
});
it('should throw an exception if duration is invalid', () => {
try {
expect(() => {
new Step({
lat: 48.689445,
lon: 6.17651,
duration: -150,
distance: 12000,
});
} catch (e: any) {
expect(e).toBeInstanceOf(ArgumentInvalidException);
}
}).toThrow(ArgumentInvalidException);
});
});