-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mathiasberggren/basic-database
[Backend] Store scraped movie data
- Loading branch information
Showing
8 changed files
with
184 additions
and
41 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
apps/api/src/database/migrations/20240413170619_add_movie_entity/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" SERIAL NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"name" TEXT, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "movies" ( | ||
"id" SERIAL NOT NULL, | ||
"genre" TEXT NOT NULL, | ||
"director" TEXT NOT NULL, | ||
"duration" INTEGER NOT NULL, | ||
"subtitles" TEXT[], | ||
"release_date" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "movies_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "movie_titles" ( | ||
"movie_title_id" SERIAL NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"movie_id" INTEGER NOT NULL, | ||
"language" TEXT NOT NULL, | ||
|
||
CONSTRAINT "movie_titles_pkey" PRIMARY KEY ("movie_title_id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "movie_titles" ADD CONSTRAINT "movie_titles_movie_id_fkey" FOREIGN KEY ("movie_id") REFERENCES "movies"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
/* eslint-disable @typescript-eslint/no-extraneous-class */ | ||
export class CreateMovieDto {} | ||
export class CreateMovieDto { | ||
genre: string | ||
director: string | ||
duration: number | ||
subtitles: string[] | ||
releaseDate: Date | ||
movieTitles: CreateMovieTitleDto[] | ||
} | ||
|
||
export class CreateMovieTitleDto { | ||
language: string | ||
title: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,54 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
|
||
import { PrismaService } from '../database/prisma.service' | ||
|
||
import { MoviesService } from './movies.service' | ||
import { CreateMovieDto } from './dto/create-movie.dto' | ||
|
||
describe('MoviesService', () => { | ||
let service: MoviesService | ||
let prismaService: PrismaService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [MoviesService] | ||
providers: [ | ||
MoviesService, | ||
{ | ||
provide: PrismaService, | ||
useValue: { | ||
movie: { | ||
create: jest.fn().mockResolvedValue({}) | ||
} | ||
} | ||
} | ||
] | ||
}).compile() | ||
|
||
service = module.get<MoviesService>(MoviesService) | ||
prismaService = module.get<PrismaService>(PrismaService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
it('should create a movie', async () => { | ||
const createMovieDto: CreateMovieDto = { | ||
genre: 'Action', | ||
director: 'John Doe', | ||
duration: 120, | ||
subtitles: ['English', 'Spanish'], | ||
releaseDate: new Date(), | ||
movieTitles: [{ language: 'English', title: 'Example Title' }] | ||
} | ||
|
||
const expectedResult = { | ||
...createMovieDto, | ||
id: 1 | ||
} | ||
|
||
jest.spyOn(prismaService.movie, 'create').mockResolvedValue(expectedResult) | ||
|
||
await expect(service.create(createMovieDto)).resolves.toEqual(expectedResult) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { INestApplication } from '@nestjs/common' | ||
import request from 'supertest' | ||
import { App } from 'supertest/types' | ||
|
||
import { AppModule } from '../src/app/app.module' | ||
|
||
describe('MoviesController (e2e)', () => { | ||
let app: INestApplication | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule] | ||
}).compile() | ||
|
||
app = moduleFixture.createNestApplication() | ||
await app.init() | ||
}) | ||
|
||
it('/api/movies (POST)', () => { | ||
return request(app.getHttpServer() as App) | ||
.post('/movies') | ||
.send({ | ||
genre: 'Action', | ||
director: 'John Doe', | ||
duration: 120, | ||
subtitles: ['English', 'Spanish'], | ||
releaseDate: '2023-04-01T00:00:00.000Z', | ||
movieTitles: [ | ||
{ | ||
language: 'English', | ||
title: 'Example Title' | ||
} | ||
] | ||
}) | ||
.expect(201) | ||
.expect({ | ||
message: 'Movie created successfully' | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
}) |