Import json fixtures with new helper

This commit is contained in:
2022-08-25 00:23:19 +02:00
parent 2765154c45
commit 463b48a84f
6 changed files with 52 additions and 40 deletions

View File

@@ -0,0 +1,23 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function determineLocation(fixtureFilename) {
return path.join(__dirname, "..", "fixtures", fixtureFilename);
}
export default function readFixtureContents(fixtureFilename) {
const location = determineLocation(fixtureFilename);
let content = {};
try {
content = JSON.parse(fs.readFileSync(location, { encoding: "utf-8" }));
} catch (err) {
console.error(`Error loading fixture file at path: ${location}`);
}
return content;
}

View File

@@ -5,18 +5,17 @@ import chaiHttp from "chai-http";
import server from "../../src/webserver/server.js"; import server from "../../src/webserver/server.js";
import resetDatabase from "../helpers/resetDatabase.js"; import resetDatabase from "../helpers/resetDatabase.js";
import createCacheEntry from "../helpers/createCacheEntry.js"; import createCacheEntry from "../helpers/createCacheEntry.js";
const popularMoviesSuccess = await import( import readFixtureContents from "../helpers/importFixture.js";
"../fixtures/popular-movies-success-response.json",
{
assert: { type: "json" }
}
);
chai.use(chaiHttp); chai.use(chaiHttp);
const popularMoviesSuccess = readFixtureContents(
"popular-movies-success-response.json"
);
describe("As a user I want to get popular movies", () => { describe("As a user I want to get popular movies", () => {
beforeEach(() => resetDatabase()); beforeEach(() => resetDatabase());
beforeEach(() => createCacheEntry("tmdb/pm:1", popularMoviesSuccess.default)); beforeEach(() => createCacheEntry("tmdb/pm:1", popularMoviesSuccess));
it("should return 200 with the information", done => { it("should return 200 with the information", done => {
chai chai

View File

@@ -5,18 +5,17 @@ import chaiHttp from "chai-http";
import server from "../../src/webserver/server.js"; import server from "../../src/webserver/server.js";
import resetDatabase from "../helpers/resetDatabase.js"; import resetDatabase from "../helpers/resetDatabase.js";
import createCacheEntry from "../helpers/createCacheEntry.js"; import createCacheEntry from "../helpers/createCacheEntry.js";
const popularShowsSuccess = await import( import readFixtureContents from "../helpers/importFixture.js";
"../fixtures/popular-show-success-response.json",
{
assert: { type: "json" }
}
);
chai.use(chaiHttp); chai.use(chaiHttp);
const popularShowsSuccess = readFixtureContents(
"popular-show-success-response.json"
);
describe("As a user I want to get popular shows", () => { describe("As a user I want to get popular shows", () => {
beforeEach(() => resetDatabase()); beforeEach(() => resetDatabase());
beforeEach(() => createCacheEntry("tmdb/pt:1", popularShowsSuccess.default)); beforeEach(() => createCacheEntry("tmdb/pt:1", popularShowsSuccess));
it("should return 200 with the information", done => { it("should return 200 with the information", done => {
chai chai

View File

@@ -7,22 +7,18 @@ import createUser from "../helpers/createUser.js";
import createToken from "../helpers/createToken.js"; import createToken from "../helpers/createToken.js";
import resetDatabase from "../helpers/resetDatabase.js"; import resetDatabase from "../helpers/resetDatabase.js";
import createCacheEntry from "../helpers/createCacheEntry.js"; import createCacheEntry from "../helpers/createCacheEntry.js";
import readFixtureContents from "../helpers/importFixture.js";
const infoMovieSuccess = await import(
"../fixtures/blade_runner_2049-info-success-response.json",
{
assert: { type: "json" }
}
);
chai.use(chaiHttp); chai.use(chaiHttp);
const infoMovieSuccess = readFixtureContents(
"blade_runner_2049-info-success-response.json"
);
describe("As a user I want to request a movie", () => { describe("As a user I want to request a movie", () => {
beforeEach(() => resetDatabase()); beforeEach(() => resetDatabase());
beforeEach(() => createUser("test_user", "test@gmail.com", "password")); beforeEach(() => createUser("test_user", "test@gmail.com", "password"));
beforeEach(() => beforeEach(() => createCacheEntry("mi:335984:false", infoMovieSuccess));
createCacheEntry("mi:335984:false", infoMovieSuccess.default)
);
it("should return 200 when item is requested", () => { it("should return 200 when item is requested", () => {
chai chai

View File

@@ -5,22 +5,18 @@ import chaiHttp from "chai-http";
import server from "../../src/webserver/server.js"; import server from "../../src/webserver/server.js";
import resetDatabase from "../helpers/resetDatabase.js"; import resetDatabase from "../helpers/resetDatabase.js";
import createCacheEntry from "../helpers/createCacheEntry.js"; import createCacheEntry from "../helpers/createCacheEntry.js";
const interstellarQuerySuccess = await import( import readFixtureContents from "../helpers/importFixture.js";
"../fixtures/interstellar-query-movie-success-response.json",
{
assert: { type: "json" }
}
);
chai.use(chaiHttp); chai.use(chaiHttp);
const interstellarQuerySuccess = readFixtureContents(
"interstellar-query-movie-success-response.json"
);
describe("As an anonymous user I want to search for a movie", () => { describe("As an anonymous user I want to search for a movie", () => {
beforeEach(() => resetDatabase()); beforeEach(() => resetDatabase());
beforeEach(() => beforeEach(() =>
createCacheEntry( createCacheEntry("tmdb/mos:1:interstellar:false", interstellarQuerySuccess)
"tmdb/mos:1:interstellar:false",
interstellarQuerySuccess.default
)
); );
it("should return 200 with the search results even if user is not logged in", done => { it("should return 200 with the search results even if user is not logged in", done => {

View File

@@ -1,18 +1,17 @@
import assert from "assert"; import assert from "assert";
// import convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie'); // import convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
import { Movie } from "../../../src/tmdb/types.js"; import { Movie } from "../../../src/tmdb/types.js";
const bladeRunnerQuerySuccess = await import( import readFixtureContents from "../../helpers/importFixture.js";
"../../fixtures/blade_runner_2049-info-success-response.json",
{ const bladeRunnerQuerySuccess = readFixtureContents(
assert: { type: "json" } "blade_runner_2049-info-success-response.json"
}
); );
let bladeRunnerTmdbMovie; let bladeRunnerTmdbMovie;
describe("Convert tmdb movieInfo to movie", () => { describe("Convert tmdb movieInfo to movie", () => {
beforeEach(() => { beforeEach(() => {
bladeRunnerTmdbMovie = bladeRunnerQuerySuccess.default[0]; [bladeRunnerTmdbMovie] = bladeRunnerQuerySuccess;
}); });
it("should translate the tmdb release date to movie year", () => { it("should translate the tmdb release date to movie year", () => {