From 250555744c2bf7f7f988ced21b41baa05cbecd8e Mon Sep 17 00:00:00 2001 From: Samuel Schmitz Date: Fri, 10 Mar 2023 15:44:03 -0600 Subject: [PATCH] 'Database Started' --- server.js | 32 ++++++----------- spec/support/CVSpec.js | 80 ++++++++++++++++++++---------------------- 2 files changed, 50 insertions(+), 62 deletions(-) diff --git a/server.js b/server.js index 68c7e28..a06e552 100644 --- a/server.js +++ b/server.js @@ -13,7 +13,6 @@ const pool = new Pool({ }) const query = async function (sql, params) { - let client let results = [] try { @@ -29,20 +28,20 @@ const query = async function (sql, params) { return results } -const queryAllCharacters = async function() { +const queryAllCharacters = async function () { const sql = 'SELECT * FROM character;' const results = await query(sql) - return {characters: results} + return { characters: results } } -const queryAllComics = async function() { +const queryAllComics = async function () { const sql = 'SELECT * FROM comic;' const results = await query(sql) - return {comics: results} + return { comics: results } } express() @@ -56,16 +55,9 @@ express() }) .get('/health', async function (req, res) { const characters = await queryAllCharacters() - - if (characters != null) { - res.status(200).send('healthy') - } else { - res.status(500).send('query failed') - } - const comics = await queryAllComics() - if (comics != null) { + if (characters != null && comics != null) { res.status(200).send('healthy') } else { res.status(500).send('query failed') @@ -74,20 +66,18 @@ express() .get('/about', function (req, res) { res.render('pages/about') }) - .get('/getSQL', async function(req, res) { + .get('/getSQL', async function (req, res) { const characterData = await queryAllCharacters() res.render('pages/character', characterData) const comicData = await queryAllComics() res.render('pages/comic', comicData) }) - - // end of implementation /////////////////////////////////// .listen(PORT, () => console.log(`Listening on ${PORT}`)) - module.exports = { - query, - queryAllCharacters, - queryAllComics - } \ No newline at end of file +module.exports = { + query, + queryAllCharacters, + queryAllComics +} diff --git a/spec/support/CVSpec.js b/spec/support/CVSpec.js index 2f5eb08..7771156 100644 --- a/spec/support/CVSpec.js +++ b/spec/support/CVSpec.js @@ -1,57 +1,55 @@ -const {queryAllCharacters, queryAllComics} = require('../../server.js') +const { queryAllCharacters, queryAllComics } = require('../../server.js') describe('server', function () { - const baseURL = 'http://localhost:5163' + const baseURL = 'http://localhost:5163' - const shouldBeAbove200 = async function (route) { - it('should be above 200', async function () { - const url = new URL(route, baseURL) + const shouldBeAbove200 = async function (route) { + it('should be above 200', async function () { + const url = new URL(route, baseURL) - const res = await fetch(url) + const res = await fetch(url) - expect(res.status).toBeGreaterThanOrEqual(200) + expect(res.status).toBeGreaterThanOrEqual(200) + }, 10000) + } + const shouldBeBelow399 = async function (route) { + it('should be below 399', async function () { + const url = new URL(route, baseURL) - }, 10000) - } + const res = await fetch(url) - const shouldBeBelow399 = async function (route) { - it('should be below 399', async function () { - const url = new URL(route, baseURL) + expect(res.status).toBeLessThanOrEqual(399) + }, 10000) + } - const res = await fetch(url) + describe("GET '/health'", function () { + shouldBeAbove200('/health') + }) - expect(res.status).toBeLessThanOrEqual(399) + describe("GET '/health'", function () { + shouldBeBelow399('/health') + }) + describe("GET '/'", function () { + shouldBeAbove200('/health') + }) - }, 10000) - } + describe("GET '/'", function () { + shouldBeBelow399('/health') + }) - const queryCheck = async function (route) { - it('check database queries', async function () { - expect(characterData).notEqual(null) - - expect(comicData).notEqual(null) - }) - } - - describe("GET '/health'", function () { - shouldBeAbove200('/health') - }) - - describe("GET '/health'", function () { - shouldBeBelow399('/health') - }) - - describe("GET '/'", function () { - shouldBeAbove200('/health') - }) - - describe("GET '/'", function () { - shouldBeBelow399('/health') + describe('queryAllCharacters', function () { + it('should be defined', async function () { + const characterData = await queryAllCharacters() + expect(characterData).toBeDefined() }) + }) - describe ("GET '/getSQL'", function () { - queryCheck('/getSQL') + describe('queryAllComics', function () { + it('should be defined', async function () { + const comicData = await queryAllComics() + expect(comicData).toBeDefined() }) -}) \ No newline at end of file + }) +})