Skip to content

Commit

Permalink
Port EventFactory code from #181
Browse files Browse the repository at this point in the history
Code from #181 adds ability to fake events in the past, future or present.
Taken verbatim from commit 18a952c.
  • Loading branch information
Matei-Alexandru Gardus authored and StormFireFox1 committed Jul 17, 2021
1 parent da3210a commit 747b44d
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions tests/data/EventFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class EventFactory {
return substitutes.map((sub) => EventModel.merge(EventFactory.fake(), sub));
}

public static fake(substitute?: Partial<EventModel>): EventModel {
public static fake(): EventModel {
const [start, end] = EventFactory.randomTime();
const fake = EventModel.create({
return EventModel.create({
uuid: uuid(),
organization: FactoryUtils.pickRandomValue(EventFactory.ORGS),
title: faker.datatype.hexaDecimal(10),
Expand All @@ -37,19 +37,56 @@ export class EventFactory {
requiresStaff: FactoryUtils.getRandomBoolean(),
staffPointBonus: EventFactory.randomPointValue(),
});
return EventModel.merge(fake, substitute);
}

public static fakePastEvent(daysAgo = 1): EventModel {
const [start, end] = EventFactory.randomPastTime(daysAgo);
return EventFactory.with({ start, end })[0];
}

public static fakeOngoingEvent(): EventModel {
const [start, end] = EventFactory.randomOngoingTime();
return EventFactory.with({ start, end })[0];
}

public static fakeFutureEvent(daysAhead = 1): EventModel {
const [start, end] = EventFactory.randomFutureTime(daysAhead);
return EventFactory.with({ start, end })[0];
}

private static randomTime(): [Date, Date] {
// between last and next week
const days = FactoryUtils.getRandomNumber(-7, 7);
// between 8 AM and 6 PM
const hour = FactoryUtils.getRandomNumber(9, 19);
// between 0.5 and 2.5 hours long, rounded to the half hour
const duration = FactoryUtils.getRandomNumber(30, 150, 30);
const start = moment().subtract(days, 'days').hour(hour);
// random day between last and next week
const day = FactoryUtils.getRandomNumber(-7, 7);
return EventFactory.randomIntervalInDay(day);
}

private static randomPastTime(daysAgo: number): [Date, Date] {
// random day between daysAgo and a week before daysAgo
const day = FactoryUtils.getRandomNumber(-daysAgo - 7, -daysAgo);
return EventFactory.randomIntervalInDay(day);
}

private static randomOngoingTime(): [Date, Date] {
// 0, 30 mins, or 1 hour before now
const currentHour = moment().hour();
const hour = FactoryUtils.getRandomNumber(currentHour - 1, currentHour, 0.5);
return EventFactory.randomIntervalInDay(0, hour);
}

private static randomFutureTime(daysAhead: number): [Date, Date] {
// random day between daysAhead and a week after daysAhead
const day = FactoryUtils.getRandomNumber(daysAhead, daysAhead + 7);
return EventFactory.randomIntervalInDay(day);
}

private static randomIntervalInDay(day: number, hour?: number): [Date, Date] {
// default between 8 AM and 6 PM
if (!hour) hour = FactoryUtils.getRandomNumber(9, 19);
// between 1 and 2.5 hours long, rounded to the half hour
const duration = FactoryUtils.getRandomNumber(60, 150, 30);
const start = moment().add(day, 'days').hour(hour);
const end = moment(start.valueOf()).add(duration, 'minutes');
return [new Date(start.valueOf()), new Date(end.valueOf())];
return [start.toDate(), end.toDate()];
}

private static randomPointValue(): number {
Expand Down

0 comments on commit 747b44d

Please sign in to comment.