Skip to content

Commit

Permalink
refactor: replace random int functions with Faker
Browse files Browse the repository at this point in the history
This way, there should be less duplication.

Related to #83
  • Loading branch information
JoosepAlviste committed Feb 8, 2024
1 parent d9438cd commit 0a8ee62
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 20 deletions.
1 change: 0 additions & 1 deletion apps/api/src/features/series/series.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const findOne = async ({
)
.$dynamic()
if (episodeId) {
// TODO: Does this work?
query = query
.innerJoin(season, eq(series.id, season.seriesId))
.innerJoin(episode, eq(season.id, episode.seasonId))
Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/utils/generateRandomInt.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core-db-factories/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@faker-js/faker": "^8.4.0",
"@serieslist/core-db": "workspace:*",
"@serieslist/core-logger": "workspace:*",
"fishery": "^2.2.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/core-db-factories/src/episode.factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { faker } from '@faker-js/faker'
import { episode, type Episode } from '@serieslist/core-db'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'

import { db } from './lib/db'
import { seasonFactory } from './season.factory'
import { generateRandomInt } from './utils/generateRandomInt'

export const episodeFactory = Factory.define<Episode>(
({ sequence, onCreate, params }) => {
Expand All @@ -22,7 +22,7 @@ export const episodeFactory = Factory.define<Episode>(

return {
id: sequence,
tmdbId: generateRandomInt(1, 9999999),
tmdbId: faker.number.int(9999999),
title: 'Test Episode',
imdbId: `tt${nanoid(12)}`,
number: sequence,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-db-factories/src/season.factory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { faker } from '@faker-js/faker'
import { season, type Season } from '@serieslist/core-db'
import { Factory } from 'fishery'

import { db } from './lib/db'
import { seriesFactory } from './series.factory'
import { generateRandomInt } from './utils/generateRandomInt'

export const seasonFactory = Factory.define<Season>(
({ sequence, onCreate, params }) => {
Expand All @@ -21,7 +21,7 @@ export const seasonFactory = Factory.define<Season>(

return {
id: sequence,
tmdbId: generateRandomInt(1, 9999999),
tmdbId: faker.number.int(9999999),
title: `Season ${sequence}`,
number: sequence,
seriesId: params.seriesId ?? seriesFactory.build().id,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-db-factories/src/series.factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { faker } from '@faker-js/faker'
import { series } from '@serieslist/core-db'
import type { Episode, Season, Series } from '@serieslist/core-db'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'

import { createArrayOfLength } from './lib/createArrayOfLength'
import { db } from './lib/db'
import { generateRandomInt } from './utils/generateRandomInt'

import { episodeFactory, seasonFactory } from '.'

Expand All @@ -21,7 +21,7 @@ export const seriesFactory = Factory.define<Series>(

return {
id: sequence,
tmdbId: generateRandomInt(1, 9999999),
tmdbId: faker.number.int(9999999),
imdbId: `tt${nanoid(12)}`,
title: 'Testing Series',
startYear: 2020,
Expand Down
3 changes: 0 additions & 3 deletions packages/core-db-factories/src/utils/generateRandomInt.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/feature-tmdb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"zod": "^3.22.3"
},
"devDependencies": {
"@faker-js/faker": "^8.4.0",
"@serieslist/eslint-config-base": "workspace:*",
"@serieslist/prettier-config": "workspace:*",
"@serieslist/typescript-config-base": "workspace:*",
Expand Down
11 changes: 4 additions & 7 deletions packages/feature-tmdb/src/tmdbSeriesDetails.factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { faker } from '@faker-js/faker'
import { subDays } from 'date-fns'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'
Expand All @@ -6,12 +7,8 @@ import type { z } from 'zod'
import type { TMDBSeason, TMDBEpisode, TMDBSeries } from './tmdb.types'
import type { tmdbNotFoundSchema } from './tmdbRequests.schema'

const generateRandomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min)
}

export const tmdbSeriesDetailsFactory = Factory.define<TMDBSeries>(() => ({
id: generateRandomInt(1, 9999999),
id: faker.number.int(9999999),
name: 'Test Series',
poster_path: 'foo.jpg',
overview: 'Test plot.',
Expand All @@ -30,7 +27,7 @@ export const tmdbSeriesDetailsFactory = Factory.define<TMDBSeries>(() => ({

export const tmdbEpisodeFactory = Factory.define<TMDBEpisode>(
({ sequence }) => ({
id: generateRandomInt(1, 9999999),
id: faker.number.int(9999999),
episode_number: sequence,
name: 'Test Episode',
air_date: subDays(new Date(Date.now()), 2),
Expand All @@ -39,7 +36,7 @@ export const tmdbEpisodeFactory = Factory.define<TMDBEpisode>(

export const tmdbSeasonFactory = Factory.define<TMDBSeason>(
({ sequence, params }) => ({
id: generateRandomInt(1, 9999999),
id: faker.number.int(9999999),
season_number: sequence,
name: `Season ${params.season_number ?? sequence}`,
air_date: new Date(Date.now()),
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a8ee62

Please sign in to comment.