Skip to content

Commit

Permalink
Add date in range generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Mar 24, 2024
1 parent 060d8e8 commit e0efa97
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/baseGenerators/date/DateTimeInRangeGenerator.ts
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
);
};
}
1 change: 1 addition & 0 deletions src/baseGenerators/date/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DateTimeInRangeGenerator';
9 changes: 8 additions & 1 deletion src/baseGenerators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { ObjectFromSchemaConfig, ObjectFromSchemaGenerator } from './object';
import { ContextAccessor, ContextAccessorConfig } from './other';
import { ProbableBooleanGenerator, ProbableBooleanGeneratorConfig } from './boolean';
import { DateTimeInRangeGenerator, DateTimeInRangeGeneratorConfig } from './date';

/**
* Declaration of the basic Value Generator names for type-hinting purposes
Expand All @@ -46,6 +47,7 @@ export type ValueGeneratorName =
| 'context-input'
| 'object-from-schema'
| 'probable-boolean'
| 'range-date-time'
);

/**
Expand Down Expand Up @@ -126,7 +128,7 @@ export const registerValueGenerator = <
GeneratorValueType extends GeneratedValue,
GivenConfig extends ValueGeneratorConfig,
>(
name: string,
name: ValueGeneratorName,
builder: ValueGeneratorBuilder<GeneratorValueType, GivenConfig>,
) => {
if (hasGenerator(name)) {
Expand Down Expand Up @@ -185,6 +187,10 @@ registerValueGenerator(
'probable-boolean',
(config: ProbableBooleanGeneratorConfig) => new ProbableBooleanGenerator(config)
);
registerValueGenerator(
'range-date-time',
(config: DateTimeInRangeGeneratorConfig) => new DateTimeInRangeGenerator(config)
);

export * from './ValueGenerator';
export * from './numeric';
Expand All @@ -193,3 +199,4 @@ export * from './array';
export * from './object';
export * from './other';
export * from './boolean';
export * from './date';
71 changes: 71 additions & 0 deletions test/baseGenerators/date/DateTimeInRange.test.ts
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();
});
});

0 comments on commit e0efa97

Please sign in to comment.