From 0a8ee623298e39f23538509057d7d0da91de219f Mon Sep 17 00:00:00 2001 From: Joosep Alviste Date: Thu, 8 Feb 2024 20:38:19 +0200 Subject: [PATCH] refactor: replace random int functions with Faker This way, there should be less duplication. Related to #83 --- apps/api/src/features/series/series.repository.ts | 1 - apps/api/src/utils/generateRandomInt.ts | 3 --- packages/core-db-factories/package.json | 1 + packages/core-db-factories/src/episode.factory.ts | 4 ++-- packages/core-db-factories/src/season.factory.ts | 4 ++-- packages/core-db-factories/src/series.factory.ts | 4 ++-- .../core-db-factories/src/utils/generateRandomInt.ts | 3 --- packages/feature-tmdb/package.json | 1 + .../feature-tmdb/src/tmdbSeriesDetails.factory.ts | 11 ++++------- pnpm-lock.yaml | 10 ++++++++++ 10 files changed, 22 insertions(+), 20 deletions(-) delete mode 100644 apps/api/src/utils/generateRandomInt.ts delete mode 100644 packages/core-db-factories/src/utils/generateRandomInt.ts diff --git a/apps/api/src/features/series/series.repository.ts b/apps/api/src/features/series/series.repository.ts index a08facb7..3f90bb3e 100644 --- a/apps/api/src/features/series/series.repository.ts +++ b/apps/api/src/features/series/series.repository.ts @@ -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)) diff --git a/apps/api/src/utils/generateRandomInt.ts b/apps/api/src/utils/generateRandomInt.ts deleted file mode 100644 index e410cdc0..00000000 --- a/apps/api/src/utils/generateRandomInt.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const generateRandomInt = (min: number, max: number) => { - return Math.floor(Math.random() * (max - min + 1) + min) -} diff --git a/packages/core-db-factories/package.json b/packages/core-db-factories/package.json index fdbd6c93..dc8fd0d8 100644 --- a/packages/core-db-factories/package.json +++ b/packages/core-db-factories/package.json @@ -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", diff --git a/packages/core-db-factories/src/episode.factory.ts b/packages/core-db-factories/src/episode.factory.ts index c6ee8479..93dfe052 100644 --- a/packages/core-db-factories/src/episode.factory.ts +++ b/packages/core-db-factories/src/episode.factory.ts @@ -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( ({ sequence, onCreate, params }) => { @@ -22,7 +22,7 @@ export const episodeFactory = Factory.define( return { id: sequence, - tmdbId: generateRandomInt(1, 9999999), + tmdbId: faker.number.int(9999999), title: 'Test Episode', imdbId: `tt${nanoid(12)}`, number: sequence, diff --git a/packages/core-db-factories/src/season.factory.ts b/packages/core-db-factories/src/season.factory.ts index 991c8519..dacc47ec 100644 --- a/packages/core-db-factories/src/season.factory.ts +++ b/packages/core-db-factories/src/season.factory.ts @@ -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( ({ sequence, onCreate, params }) => { @@ -21,7 +21,7 @@ export const seasonFactory = Factory.define( return { id: sequence, - tmdbId: generateRandomInt(1, 9999999), + tmdbId: faker.number.int(9999999), title: `Season ${sequence}`, number: sequence, seriesId: params.seriesId ?? seriesFactory.build().id, diff --git a/packages/core-db-factories/src/series.factory.ts b/packages/core-db-factories/src/series.factory.ts index aabb58d7..046269b3 100644 --- a/packages/core-db-factories/src/series.factory.ts +++ b/packages/core-db-factories/src/series.factory.ts @@ -1,3 +1,4 @@ +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' @@ -5,7 +6,6 @@ import { nanoid } from 'nanoid' import { createArrayOfLength } from './lib/createArrayOfLength' import { db } from './lib/db' -import { generateRandomInt } from './utils/generateRandomInt' import { episodeFactory, seasonFactory } from '.' @@ -21,7 +21,7 @@ export const seriesFactory = Factory.define( return { id: sequence, - tmdbId: generateRandomInt(1, 9999999), + tmdbId: faker.number.int(9999999), imdbId: `tt${nanoid(12)}`, title: 'Testing Series', startYear: 2020, diff --git a/packages/core-db-factories/src/utils/generateRandomInt.ts b/packages/core-db-factories/src/utils/generateRandomInt.ts deleted file mode 100644 index e410cdc0..00000000 --- a/packages/core-db-factories/src/utils/generateRandomInt.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const generateRandomInt = (min: number, max: number) => { - return Math.floor(Math.random() * (max - min + 1) + min) -} diff --git a/packages/feature-tmdb/package.json b/packages/feature-tmdb/package.json index ff1f9b2f..10efcd57 100644 --- a/packages/feature-tmdb/package.json +++ b/packages/feature-tmdb/package.json @@ -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:*", diff --git a/packages/feature-tmdb/src/tmdbSeriesDetails.factory.ts b/packages/feature-tmdb/src/tmdbSeriesDetails.factory.ts index 74dcbe14..ff4d7cc0 100644 --- a/packages/feature-tmdb/src/tmdbSeriesDetails.factory.ts +++ b/packages/feature-tmdb/src/tmdbSeriesDetails.factory.ts @@ -1,3 +1,4 @@ +import { faker } from '@faker-js/faker' import { subDays } from 'date-fns' import { Factory } from 'fishery' import { nanoid } from 'nanoid' @@ -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(() => ({ - id: generateRandomInt(1, 9999999), + id: faker.number.int(9999999), name: 'Test Series', poster_path: 'foo.jpg', overview: 'Test plot.', @@ -30,7 +27,7 @@ export const tmdbSeriesDetailsFactory = Factory.define(() => ({ export const tmdbEpisodeFactory = Factory.define( ({ sequence }) => ({ - id: generateRandomInt(1, 9999999), + id: faker.number.int(9999999), episode_number: sequence, name: 'Test Episode', air_date: subDays(new Date(Date.now()), 2), @@ -39,7 +36,7 @@ export const tmdbEpisodeFactory = Factory.define( export const tmdbSeasonFactory = Factory.define( ({ 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()), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be793266..e77fbc3e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -532,6 +532,9 @@ importers: packages/core-db-factories: dependencies: + '@faker-js/faker': + specifier: ^8.4.0 + version: 8.4.0 '@serieslist/core-db': specifier: workspace:* version: link:../core-db @@ -858,6 +861,9 @@ importers: specifier: ^3.22.3 version: 3.22.3 devDependencies: + '@faker-js/faker': + specifier: ^8.4.0 + version: 8.4.0 '@serieslist/eslint-config-base': specifier: workspace:* version: link:../eslint-config-base @@ -3712,6 +3718,10 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: false + /@faker-js/faker@8.4.0: + resolution: {integrity: sha512-htW87352wzUCdX1jyUQocUcmAaFqcR/w082EC8iP/gtkF0K+aKcBp0hR5Arb7dzR8tQ1TrhE9DNa5EbJELm84w==} + engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0, npm: '>=6.14.13'} + /@fastify/accept-negotiator@1.1.0: resolution: {integrity: sha512-OIHZrb2ImZ7XG85HXOONLcJWGosv7sIvM2ifAPQVhg9Lv7qdmMBNVaai4QTdyuaqbKM5eO6sLSQOYI7wEQeCJQ==} engines: {node: '>=14'}