Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=4000;
17 changes: 17 additions & 0 deletions Model/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const db = require('../data/dbConfig');

module.exports = {
getAll,
insert
}

async function getAll() {
return db('games');
}

async function insert(game) {
const [ id ] = await db('games').insert(game);
const addedGame = await db('games').where({ id });

return addedGame[0];
}
52 changes: 52 additions & 0 deletions Model/model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Games = require('./model');
const db = require('../data/dbConfig');

beforeEach(() => {
return db('games').truncate();
});

describe('GAMES', () => {
describe('getAll()', () => {
it('should return an empty array if no games in database', async () => {
const games = await Games.getAll();

expect(games).toEqual([]);
});

it('should return an array of games in database', async () => {
const gamesArr = [
{ title: 'Fire Emblem', genre: 'RPG', releaseYear: 1996 },
{ title: 'Spyro', genre: 'platformer', releaseYear: 1998 }
];

await Games.insert(gamesArr);

const games = await Games.getAll();

gamesArr[0].id = 1;
gamesArr[1].id = 2;

expect(games.length).toBe(2);
expect(games).toEqual(gamesArr);
});
});

describe('insert()', () => {
it('should assign an id to inserted game', async () => {
await Games.insert({ title: 'Spyro', genre: 'platformer', releaseYear: 1998 });

const games = await Games.getAll();

expect(games[0].id).toBe(1);
});

it('should insert a game into the database', async () => {
await Games.insert({ title: 'Spyro', genre: 'platformer', releaseYear: 1998 });

const games = await Games.getAll();

expect(games.length).toBe(1);
expect(games[0].title).toBe('Spyro');
});
});
});
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@ In this challenge use `Test Driven Development` to build a RESTful API using Nod
Demonstrate your understanding of this week's concepts by answering the following free-form questions. Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your project manager.

1. In Jest, what are the differences between `describe()` and `it()` globals, and what are good uses for them?

`describe()` is used to label a test suite, or collection of individual tests

`it()` is used to label a specific test within a suite

1. What is the point of `Test Driven Development`? What do you think about this approach?

It helps to prevent future bugs and if set up with error codes can help debugging future bugs

1. Mention three types of automated tests.

Integration Testing
Unit Testing
Regression Testing

## Project Set Up

- [ ] Fork and clone this repository.
Expand Down
30 changes: 30 additions & 0 deletions Server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const express = require('express');
const server = express();

const Games = require('../Model/model');

server.use(express.json());

server.get('/', async (req, res) => {
res.status(200).json({ api: "Server Running..." });
});

server.get('/games', async (req, res) => {
const games = await Games.getAll();

res.status(200).json(games);
});

server.post('/games', async (req, res) => {
const game = req.body;

if(!game.title || !game.genre || !game.releaseYear) {
res.status(422).json({ error: "Missing Field" });
} else {
const newGame = await Games.insert(game);

res.status(201).json(newGame);
}
});

module.exports = server;
91 changes: 91 additions & 0 deletions Server/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require('dotenv').config();

const request = require('supertest');
const server = require('./server');

const db = require('../data/dbConfig');

beforeEach(() => {
return db('games').truncate();
});

describe("SERVER", () => {
describe('Environment', () => {
it('should set environment to \'testing\'', () => {
const env = process.env.DB_ENV;

expect(env).toBe('testing');
});

it('should run on environmental port', () => {
const port = process.env.PORT;

expect(port).not.toBe(undefined);
});
});

describe('GET /', () => {
it('should return a json package', async () => {
const res = await request(server).get('/');

expect(res.type).toBe('application/json');
});

it('should return { api: \'Server Running...\' }', async () => {
const res = await request(server).get('/');

expect(res.body.api).toBe("Server Running...");
});
});

describe('GET /games', () => {
it('should return a json packet', async () => {
const res = await request(server).get('/games');

expect(res.type).toBe('application/json');
});

it('should return an array', async () => {
const res = await request(server).get('/games');

expect(typeof res.body).toBe('object');
});

it('should return status 200 OK', async () => {
const res = await request(server).get('/games');

expect(res.status).toBe(200);
});
});

describe('POST /games', () => {
it('should post game to database', async () => {
const game = { title: 'Insomnia', genre: 'horror', releaseYear: 2016 }

const res = await request(server).post('/games')
.send(game);

game.id = 1;

expect(res.body).toEqual(game);
});

it('should return status 422 if missing field', async () => {
const game = { genre: 'action', releaseYear: 2000 }

const res = await request(server).post('/games')
.send(game);

expect(res.status).toBe(422);
});

it('should return status 201 if successful', async () => {
const game = { title: 'Cuphead', genre: 'platformer', releaseYear: 2015 }

const res = await request(server).post('/games')
.send(game);

expect(res.status).toEqual(201);
});
});
});
Binary file added data/db.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('dotenv').config();

const knex = require('knex');
const config = require('../knexfile');

const dbEnv = process.env.DB_ENV || 'development';

module.exports = knex(config[dbEnv]);
15 changes: 15 additions & 0 deletions data/migrations/20190629120419_games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

exports.up = function(knex) {
return knex.schema.createTable('games', tbl => {
tbl.increments();

tbl.string('title').notNullable().unique();
tbl.string('genre').notNullable();

tbl.int('releaseYear');
});
};

exports.down = function(knex) {
return knex.schema.dropTableIfExists('games');
};
Binary file added data/test.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('dotenv').config();

const server = require('./Server/server');
const port = process.env.PORT || 5000;

server.listen(port, () => {
console.log(`Server Running on Port:${port}`);
});
65 changes: 65 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './data/db.sqlite3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
},

testing: {
client: 'sqlite3',
connection: {
filename: './data/test.sqlite3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}

};
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"description": "Testing Sprint Challenge",
"main": "index.js",
"scripts": {
"test": "jest --watch --verbose",
"start": "nodemon index.js"
"test": "cross-env DB_ENV=testing jest --watchAll --verbose",
"server": "nodemon index.js"
},
"jest": {
"testEnvironment": "node"
},
"repository": {
"type": "git",
Expand All @@ -19,10 +22,15 @@
},
"homepage": "https://github.com/LambdaSchool/Sprint-Challenge--Testing#readme",
"dependencies": {
"express": "^4.16.4"
"dotenv": "^8.0.0",
"express": "^4.16.4",
"knex": "^0.18.0",
"sqlite3": "^4.0.9"
},
"devDependencies": {
"cross-env": "^5.2.0",
"jest": "^23.6.0",
"nodemon": "^1.19.1",
"supertest": "^3.3.0"
}
}
}
Loading