Skip to content

Commit

Permalink
feat(api): ✅ Add integration tests for searching and fuzzy matching i…
Browse files Browse the repository at this point in the history
…n DB
  • Loading branch information
mathiasberggren committed Apr 29, 2024
1 parent 6ecfc9c commit 2aeb415
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion apps/api/test/movies.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import request from 'supertest'
import { App } from 'supertest/types'

import { AppModule } from '../src/app/app.module'
import { movieFactory } from '../src/database/factories/movie'
import { IMovieFactory, movieFactory } from '../src/database/factories/movie'
import { movieTitleFactory } from '../src/database/factories/movieTitle'
import prisma from '../src/database/client'

Expand Down Expand Up @@ -77,6 +77,7 @@ describe('MoviesController (e2e)', () => {
})
})
})

it('should return 400 if the body is invalid', async () => {
const response = await request(app.getHttpServer() as App)
.post('/movies')
Expand All @@ -86,6 +87,77 @@ describe('MoviesController (e2e)', () => {
})
})

describe('/movies/search (GET)', () => {
const movies: IMovieFactory[] = []

// For some reason if I use this beforeAll, the movies are not
// inserted into the DB during the test even though I verified in logs
// that they do get inserted... Any idea why?
// beforeAll(async () => {
// movies.push(...await movieFactory.createList(10))
// })

afterEach(async () => {
await truncateTables()
})

it('should return the movie searched as the top selection', async () => {
movies.push(...await movieFactory.createList(10))

const title = 'The ultimate movie to search for'
const movie = await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title, language: 'en' })] })
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const movieTitle = movie.movieTitles![0]
const uriTitle = encodeURIComponent(movieTitle.title)

const response = await request(app.getHttpServer() as App).get(`/movies/search?title=${uriTitle}`)

expect(response.status).toBe(200)
expect(response.body[0]).toEqual({
...movie,
movieTitles: [movieTitle],
releaseDate: movie.releaseDate.toISOString()
})
})

it('should fuzzy match the movie title and only return the related ones', async () => {
const theMatrixMovie = await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title: 'The Matrix', language: 'en' })] })
await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title: 'Not at all related', language: 'en' })] })

const response = await request(app.getHttpServer() as App).get('/movies/search?title=matris')

expect(response.status).toBe(200)
expect(response.body).toEqual([
{
...theMatrixMovie,
releaseDate: theMatrixMovie.releaseDate.toISOString()
}
])
})

it.todo('should fuzzy match better'
// , async () => {
// const theMatrixMovie = await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title: 'The Matrix', language: 'en' })] })
// const theMatrixReloadedMovie = await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title: 'The Matrix Reloaded', language: 'en' })] })
// await movieFactory.create({ movieTitles: [movieTitleFactory.build({ title: 'Not at all related', language: 'en' })] })

// const response = await request(app.getHttpServer()).get('/movies/search?title=matris')

// expect(response.status).toBe(200)
// expect(response.body).toEqual([
// theMatrixMovie,
// theMatrixReloadedMovie
// ])}
)

it('should return 200 and an empty parameter list if the movie is not found', async () => {
const response = await request(app.getHttpServer() as App).get('/movies/search?title=not-a-movie')

expect(response.status).toBe(200)
expect(response.body).toEqual([])
})
})

afterAll(async () => {
await app.close()
})
Expand Down

0 comments on commit 2aeb415

Please sign in to comment.