When testing we dont have api access so we create cache entries before each request. The cache key changed so this is now updated in the tests and tmdb.

This commit is contained in:
2018-03-20 20:12:49 +01:00
parent 7051edb212
commit 9e145f7068
8 changed files with 44 additions and 27 deletions

View File

@@ -28,7 +28,7 @@ class Config {
const field = new Field(this.fields[section][option]);
if (field.value === '') {
const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')];
const envField = process.env[['SEASONED', section.toUpperCase(), option.toUpperCase()].join('_')];
if (envField !== undefined && envField.length !== 0) { return envField; }
}

View File

@@ -1,8 +1,7 @@
const configuration = require('src/config/configuration').getInstance();
const SqliteDatabase = require('src/database/sqliteDatabase');
const host = process.env.TESTING ? ':memory:' : configuration.get('database', 'host');
const database = new SqliteDatabase(host);
const database = new SqliteDatabase(configuration.get('database', 'host'));
/**
* This module establishes a connection to the database
* specified in the confgiuration file. It tries to setup

View File

@@ -29,6 +29,7 @@ class TMDB {
/**
* Retrieve a specific movie by id from TMDB.
* @param {Number} identifier of the movie you want to retrieve
* @param {String} type filter results by type (default movie).
* @returns {Promise} succeeds if movie was found
*/
lookup(identifier, type = 'movie') {
@@ -36,7 +37,7 @@ class TMDB {
const cacheKey = `${this.cacheTags.info}:${type}:${identifier}`;
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb(this.tmdbMethod('info', type), query))
.catch(() => this.tmdb(TMDB_METHODS['info'][type]), query))
.catch(() => { throw new Error('Could not find a movie with that id.'); })
.then(response => this.cache.set(cacheKey, response))
.then((response) => {
@@ -50,16 +51,18 @@ class TMDB {
}
/**
* Retrive list of of items from TMDB matching the query and/or type given.
* @param {queryText, page, type} the page number to specify in the request for discover,
* Retrive search results from TMDB.
* @param {String} text query you want to search for
* @param {Number} page representing pagination of results
* @param {String} type filter results by type (default multi)
* @returns {Promise} dict with query results, current page and total_pages
*/
search(text, page = 1, type = 'multi') {
const query = { query: text, page };
const query = { query: text, page: page };
const cacheKey = `${this.cacheTags.search}:${page}:${type}:${text}`;
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb(this.tmdbMethod('search', type), query))
.catch(() => this.tmdb(TMDB_METHODS['search'][type], query))
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
.then(response => this.cache.set(cacheKey, response))
.then(response => this.mapResults(response))
@@ -71,18 +74,17 @@ class TMDB {
/**
* Fetches a given list from tmdb.
* @param {listName} List we want to fetch.
* @param {type} The to specify in the request for discover (default 'movie').
* @param {id} When finding similar a id can be added to query
* @param {page} Page number we want to fetch.
* @param {String} listName Name of list
* @param {String} type filter results by type (default movie)
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
listSearch(listName, type = 'movie', id, page = '1') {
const params = { id, page };
const cacheKey = `${this.cacheTags[listName]}:${type}:${id}:${page}`;
listSearch(listName, type = 'movie', page = '1') {
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb(this.tmdbMethod(listName, type), params))
.catch(() => this.tmdb(TMDB_METHODS[listName][type], page))
.catch(() => { throw new Error('Error fetching list from tmdb.')})
.then(response => this.cache.set(cacheKey, response))
.then(response => this.mapResults(response, type))
.catch((error) => { throw new Error(error); })
@@ -91,16 +93,10 @@ class TMDB {
}));
}
tmdbMethod(apiMethod, type) {
const method = TMDB_METHODS[apiMethod][type];
if (method !== undefined) return method;
throw new Error('Could not find tmdb api method.');
}
/**
* Maps our response from tmdb api to a movie/show object.
* @param {response} JSON response from tmdb.
* @param {type} The type declared in listSearch.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
mapResults(response, type) {
@@ -114,6 +110,12 @@ class TMDB {
.catch((error) => { throw new Error(error); });
}
/**
* Wraps moviedb library to support Promises.
* @param {String} method function name in the library
* @param {Object} argument argument to function being called
* @returns {Promise} succeeds if callback succeeds
*/
tmdb(method, argument) {
return new Promise((resolve, reject) => {
const callback = (error, reponse) => {

View File

@@ -0,0 +1,13 @@
{
"background_path": "/yIZ1xendyqKvY3FGeeUYUd5X9Mm.jpg",
"id": 329865,
"popularity": 26.978601,
"poster_path": "/hLudzvGfpi6JlwUnsNhXwKKg4j.jpg",
"release_status": "Released",
"score": 7.3,
"summary": "Taking place after alien crafts land around the world, an expert linguist is recruited by the military to determine whether they come in peace or are a threat.",
"tagline": "Why are they here?",
"title": "Arrival",
"type": "movie",
"year": 2016
}

View File

@@ -7,7 +7,7 @@ const popularMoviesSuccess = require('test/fixtures/popular-movies-success-respo
describe('As a user I want to get popular movies', () => {
before(() => resetDatabase());
before(() => createCacheEntry('p:movie::1', popularMoviesSuccess));
before(() => createCacheEntry('p:movie:1', popularMoviesSuccess));
it('should return 200 with the information', () =>
request(app)

View File

@@ -7,7 +7,7 @@ const popularShowsSuccess = require('test/fixtures/popular-show-success-response
describe('As a user I want to get popular shows', () => {
before(() => resetDatabase());
before(() => createCacheEntry('p:show::1', popularShowsSuccess));
before(() => createCacheEntry('p:show:1', popularShowsSuccess));
it('should return 200 with the information', () =>
request(app)

View File

@@ -1,14 +1,17 @@
const resetDatabase = require('test/helpers/resetDatabase');
const createCacheEntry = require('test/helpers/createCacheEntry');
const app = require('src/webserver/app');
const request = require('supertest-as-promised');
const createUser = require('test/helpers/createUser');
const createToken = require('test/helpers/createToken');
const infoMovieSuccess = require('test/fixtures/arrival-info-success-response.json');
describe('As a user I want to request a movie', () => {
before(() => {
return resetDatabase()
.then(() => createUser('test_user', 'test@gmail.com', 'password'));
})
before(() => createCacheEntry('i:movie:329865', infoMovieSuccess));
it('should return 200 when item is requested', () =>
request(app)

View File

@@ -6,7 +6,7 @@ const interstellarQuerySuccess = require('test/fixtures/interstellar-query-succe
describe('As an anonymous user I want to search for a movie', () => {
before(() => resetDatabase());
before(() => createCacheEntry('s:1:movie:interstellar', interstellarQuerySuccess));
before(() => createCacheEntry('se:1:multi:interstellar', interstellarQuerySuccess));
it('should return 200 with the search results even if user is not logged in', () =>
request(app)