From b8f3cac3fcc7edde8453e92a26456d64f38a4a3a Mon Sep 17 00:00:00 2001 From: Inga <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 9 Dec 2023 02:18:12 +0000 Subject: [PATCH] implemented basic local integration --- src/integration/movies/local.spec.ts | 53 +++++++++++++++++++ src/integration/movies/local.ts | 13 +++++ src/integration/movies/omdb.ts | 6 +-- .../resources/movies}/11043689.json | 0 .../resources/movies}/11528860.json | 0 {movies => src/resources/movies}/3532674.json | 0 {movies => src/resources/movies}/5979300.json | 0 tsconfig.json | 1 + 8 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/integration/movies/local.spec.ts create mode 100644 src/integration/movies/local.ts rename {movies => src/resources/movies}/11043689.json (100%) rename {movies => src/resources/movies}/11528860.json (100%) rename {movies => src/resources/movies}/3532674.json (100%) rename {movies => src/resources/movies}/5979300.json (100%) diff --git a/src/integration/movies/local.spec.ts b/src/integration/movies/local.spec.ts new file mode 100644 index 0000000..d3da06f --- /dev/null +++ b/src/integration/movies/local.spec.ts @@ -0,0 +1,53 @@ +import { describe, it, expect } from '@jest/globals'; + +import { createLocalMoviesClient } from './local'; + +describe('createLocalMoviesClient', () => { + const client = createLocalMoviesClient(); + + it('returns data for internal movie id', async () => { + const result = await client.getMovieMetadataByInternalId(11528860); + expect(result).toMatchObject({ + description: expect.any(String), + duration: 75, + id: 11528860, + imdbId: 'tt0061852', + languages: expect.any(Array), + originalLanguage: 'en', + productionYear: 1967, + studios: ['Disney'], + title: 'Das Dschungelbuch', + userrating: { + countStar1: expect.any(Number), + countStar2: expect.any(Number), + countStar3: expect.any(Number), + countStar4: expect.any(Number), + countStar5: expect.any(Number), + countTotal: expect.any(Number), + }, + }); + }); + + it('returns data for imdb id', async () => { + const result = await client.getMovieMetadataByImdbId('tt0061852'); + expect(result).toMatchObject({ + description: expect.any(String), + duration: 75, + id: 11528860, + imdbId: 'tt0061852', + languages: expect.any(Array), + originalLanguage: 'en', + productionYear: 1967, + studios: ['Disney'], + title: 'Das Dschungelbuch', + userrating: { + countStar1: expect.any(Number), + countStar2: expect.any(Number), + countStar3: expect.any(Number), + countStar4: expect.any(Number), + countStar5: expect.any(Number), + countTotal: expect.any(Number), + }, + }); + }); +}); diff --git a/src/integration/movies/local.ts b/src/integration/movies/local.ts new file mode 100644 index 0000000..29a3c72 --- /dev/null +++ b/src/integration/movies/local.ts @@ -0,0 +1,13 @@ +import movie1 from '../../resources/movies/3532674.json'; +import movie2 from '../../resources/movies/5979300.json'; +import movie3 from '../../resources/movies/11043689.json'; +import movie4 from '../../resources/movies/11528860.json'; + +const movies = [movie1, movie2, movie3, movie4]; + +export const createLocalMoviesClient = () => { + return { + getMovieMetadataByInternalId: async (internalId: number) => movies.find(({ id }) => id == internalId), + getMovieMetadataByImdbId: async (id: string) => movies.find(({ imdbId }) => imdbId == id), + }; +}; diff --git a/src/integration/movies/omdb.ts b/src/integration/movies/omdb.ts index 7cd609d..a045d3c 100644 --- a/src/integration/movies/omdb.ts +++ b/src/integration/movies/omdb.ts @@ -2,7 +2,7 @@ import fetch from 'node-fetch'; import PQueue from 'p-queue'; export const createOmdbClient = (apiKey: string) => { - // Rate limit (according to readme, it's 10k per day; + // Rate limit (according to readme, it's 1k per day; // here we set it 1 per second, should be enough for the demo app goal) const queue = new PQueue({ interval: 1000, @@ -12,10 +12,10 @@ export const createOmdbClient = (apiKey: string) => { }); return { - getMovieMetadata: async (movieId: string) => + getMovieMetadata: async (imdbId: string) => queue.add(async () => { const url = new URL('https://www.omdbapi.com/'); - url.searchParams.append('i', movieId); + url.searchParams.append('i', imdbId); url.searchParams.append('apikey', apiKey); url.searchParams.append('plot', 'full'); const response = await fetch(url); diff --git a/movies/11043689.json b/src/resources/movies/11043689.json similarity index 100% rename from movies/11043689.json rename to src/resources/movies/11043689.json diff --git a/movies/11528860.json b/src/resources/movies/11528860.json similarity index 100% rename from movies/11528860.json rename to src/resources/movies/11528860.json diff --git a/movies/3532674.json b/src/resources/movies/3532674.json similarity index 100% rename from movies/3532674.json rename to src/resources/movies/3532674.json diff --git a/movies/5979300.json b/src/resources/movies/5979300.json similarity index 100% rename from movies/5979300.json rename to src/resources/movies/5979300.json diff --git a/tsconfig.json b/tsconfig.json index 044f304..3301003 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, + "resolveJsonModule": true, "target": "ES2021", "sourceMap": true, "outDir": "./dist",