-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
060d8e8
commit e0efa97
Showing
4 changed files
with
140 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
GeneratedValue, | ||
ValueGenerator, | ||
ValueGeneratorConfig | ||
} from '../ValueGenerator'; | ||
import { randomDateTime } from '../../utils'; | ||
|
||
/** | ||
* For easier access, it has to be either: | ||
* <ul> | ||
* <li>instance of Date</li> | ||
* <li>string representing the date time (in standard format)</li> | ||
* <li>number representing the date time (in milliseconds)</li> | ||
* </ul> | ||
*/ | ||
export type DateTimeDeclaration = Date | string | number | ||
|
||
export type DateTimeInRangeGeneratorConfig = { | ||
from: DateTimeDeclaration; | ||
to: DateTimeDeclaration; | ||
} & ValueGeneratorConfig; | ||
|
||
|
||
/** | ||
* This class generates a random integer within a given range. | ||
*/ | ||
export class DateTimeInRangeGenerator extends ValueGenerator< | ||
GeneratedValue, | ||
DateTimeInRangeGeneratorConfig | ||
> { | ||
constructor(config: DateTimeInRangeGeneratorConfig) { | ||
|
||
if (!config.from) { | ||
throw new Error(`Property 'from' is required`); | ||
} else if (!config.to) { | ||
throw new Error(`Property 'to' is required`); | ||
} | ||
|
||
super(config); | ||
|
||
this.config.from = this.buildDate(config.from); | ||
this.config.to = this.buildDate(config.to); | ||
|
||
if (this.config.from.getTime() > this.config.to.getTime()) { | ||
throw new Error(`Given date 'from' is after given date 'to' (${this.config.from} > ${this.config.to})`); | ||
} | ||
} | ||
|
||
/** Shorthand for building the date */ | ||
private buildDate = (value: DateTimeDeclaration): Date => { | ||
return new Date(value); | ||
}; | ||
|
||
get = (): GeneratedValue => { | ||
return randomDateTime( | ||
this.config.from as Date, | ||
this.config.to as Date | ||
); | ||
}; | ||
} |
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 @@ | ||
export * from './DateTimeInRangeGenerator'; |
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,71 @@ | ||
import { getValueGenerator } from '../../../src'; | ||
|
||
const generatorName = 'range-date-time'; | ||
|
||
describe('DateTimeInRange generator', () => { | ||
it('should be able to be found by name', () => { | ||
|
||
const config = { | ||
from: new Date('2000-01-01'), | ||
to: new Date('2999-01-01') | ||
} | ||
|
||
expect(() => getValueGenerator(generatorName, config)).not.toThrow() | ||
}) | ||
|
||
it('should return a date', () => { | ||
|
||
const config = { | ||
from: new Date('2000-01-01'), | ||
to: new Date('2999-01-01') | ||
} | ||
|
||
const generator = getValueGenerator(generatorName, config); | ||
|
||
expect(generator.get({})).toBeInstanceOf(Date) | ||
}) | ||
|
||
it('should return a date within the given range', () => { | ||
|
||
const config = { | ||
from: new Date('2000-01-01'), | ||
to: new Date('2999-01-01') | ||
} | ||
|
||
const generator = getValueGenerator(generatorName, config); | ||
const value = generator.get({}) as Date; | ||
|
||
expect(value.getTime()).toBeGreaterThanOrEqual(config.from.getTime()) | ||
expect(value.getTime()).toBeLessThanOrEqual(config.to.getTime()) | ||
}); | ||
|
||
it('should throw on from > to', () => { | ||
|
||
const config = { | ||
from: new Date('2999-01-01'), | ||
to: new Date('2000-01-01') | ||
} | ||
|
||
expect(() => getValueGenerator(generatorName, config)).toThrow(); | ||
}); | ||
|
||
it('should accept strings', () => { | ||
|
||
const config = { | ||
from: '2000-01-01', | ||
to: '2999-01-01' | ||
} | ||
|
||
expect(() => getValueGenerator(generatorName, config)).not.toThrow(); | ||
}); | ||
|
||
it('should accept numbers (timestamps)', () => { | ||
|
||
const config = { | ||
from: 123456, | ||
to: 654321 | ||
} | ||
|
||
expect(() => getValueGenerator(generatorName, config)).not.toThrow(); | ||
}); | ||
}); |