Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aaron-schuster #244

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a155f60
initialize project
aaschuster Apr 2, 2023
09acfa1
build out migration and seed
aaschuster Apr 2, 2023
7083480
get basic server running
aaschuster Apr 2, 2023
5dda507
write out barebones endpoints
aaschuster Apr 2, 2023
b8a9f3c
add resetdb script and updated migration and seed to include abbrevia…
aaschuster Apr 2, 2023
dc3a60c
get tests for model up and running
aaschuster Apr 2, 2023
5074e1e
build out tests for get and begin fleshing out get db function
aaschuster Apr 2, 2023
c9c8a68
refactor get into get() and getBy()
aaschuster Apr 2, 2023
5abb492
write first test for add() and flesh it out
aaschuster Apr 2, 2023
cb659aa
write more tests for add()
aaschuster Apr 2, 2023
a66a28c
write first few tests for server.test and get /api/coasters working p…
aaschuster Apr 2, 2023
1bb54ed
flesh out initial tests
aaschuster Apr 2, 2023
90c7eb1
write tests for get by id and flesh out according endpoint
aaschuster Apr 2, 2023
287da5a
write tests testing for getByAbbrv and refactor endpoint to satisfy
aaschuster Apr 2, 2023
487cba9
refactor so abbreviations can be case insensitive
aaschuster Apr 2, 2023
43158a1
build first few tests for post endpoint
aaschuster Apr 2, 2023
629cd7d
flesh out post endpoint to satisfy tests
aaschuster Apr 2, 2023
a350e71
write add'l tests for post endpoint
aaschuster Apr 2, 2023
e89ef74
write test for delete functionality and implement
aaschuster Apr 2, 2023
42d6085
write add'l tests for delete functionality
aaschuster Apr 2, 2023
71c4d5b
begin writing tests for deletion endpoint and flesh it out
aaschuster Apr 2, 2023
6e2251a
write add'l tests for delete endpoint
aaschuster Apr 2, 2023
1530edb
add afterAll call to kill db and refactored exports in model
aaschuster Apr 3, 2023
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
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

node_modules
23 changes: 23 additions & 0 deletions api/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const db = require("../data/dbConfig")

let funcObj = {};

exports.get = function() {
return db("coasters");
}

exports.getBy = function(filter) {
return db("coasters").where(filter).first();
}

exports.add = async function(newCoaster) {
const [coaster_id] = await db("coasters").insert(newCoaster);
return exports.getBy({coaster_id});
}

exports.del = async function(id) {
const filter = { coaster_id: id };
const coasterToBeDeleted = await exports.getBy(filter);
await db("coasters").where(filter).del();
return coasterToBeDeleted;
}
167 changes: 167 additions & 0 deletions api/model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const Coasters = require("./model");
const db = require("../data/dbConfig");

beforeAll(async () => {
await db.migrate.rollback();
await db.migrate.latest();
})

beforeEach(async () => {
await db.seed.run();
})

test("environment is testing", () => {
expect(process.env.NODE_ENV).toBe("testing");
})

test("sanity", () => {
expect(1).toBe(1);
})

describe("get", () => {

test("resolves to all the coasters", async () => {
const coasters = await Coasters.get()

expect(coasters).toHaveLength(6)
expect(coasters[0].coaster_name).toBe("Steel Vengeance")
expect(coasters[3].coaster_name).toBe("Kingda Ka")
})

});

describe("getBy", () => {

test("resolves to the correct coaster when given id", async () => {

let coaster = await Coasters.getBy({ coaster_id: 1 })
expect(coaster.coaster_name).toBe("Steel Vengeance");

coaster = await Coasters.getBy({ coaster_id: 4 })
expect(coaster.coaster_name).toBe("Kingda Ka");

coaster = await Coasters.getBy({ coaster_id: 5 })
expect(coaster.abbrv).toBe("I305");
})

test("resolves to the correct coaster for a given abbreviation", async () => {

let coaster = await Coasters.getBy({ abbrv: "MF"})
expect(coaster.coaster_name).toBe("Millennium Force");

coaster = await Coasters.getBy({ abbrv: "KK"})
expect(coaster.coaster_name).toBe("Kingda Ka");

coaster = await Coasters.getBy({ abbrv: "SV"})
expect(coaster.coaster_name).toBe("Steel Vengeance");

})
})

describe("add", () => {

const raptor = {
coaster_name: "Raptor",
height: 137,
speed: 57
}

const outlawRun = {
coaster_name: "Outlaw Run",
height: 107,
speed: 68,
abbrv: "OR"
}

const smiler = {
coaster_name: "Smiler",
height: 98.4,
speed: 52.8
}


test("resolves to added coaster", async () => {

let res = await Coasters.add(raptor);

expect(res).toMatchObject(raptor);

res = await Coasters.add(outlawRun);

expect(res).toMatchObject(outlawRun);

res = await Coasters.add(smiler);

expect(res).toMatchObject(smiler);
})

test("coasters db size increases with each add", async () => {

await Coasters.add(raptor);
expect(await db("coasters")).toHaveLength(7);

await Coasters.add(outlawRun);
expect(await db("coasters")).toHaveLength(8);

await Coasters.add(smiler);
expect(await db("coasters")).toHaveLength(9);
})

test("added coasters are in db", async () => {
let newCoaster = await Coasters.add(raptor);
let res = await db("coasters").where({coaster_id: newCoaster.coaster_id}).first();
expect(res).toMatchObject(raptor);

newCoaster = await Coasters.add(outlawRun);
res = await db("coasters").where({coaster_id: newCoaster.coaster_id}).first();
expect(res).toMatchObject(outlawRun);

newCoaster = await Coasters.add(smiler);
res = await db("coasters").where({coaster_id: newCoaster.coaster_id}).first();
expect(res).toMatchObject(smiler);

})
})

describe("del", () => {
test("resolves to deleted coaster", async () => {
let coasterToBeDeleted = await Coasters.del(1);
expect(coasterToBeDeleted.coaster_name).toBe("Steel Vengeance");

coasterToBeDeleted = await Coasters.del(2);
expect(coasterToBeDeleted.coaster_name).toBe("Millennium Force");

coasterToBeDeleted = await Coasters.del(6);
expect(coasterToBeDeleted.abbrv).toBe("SEFK");
})

test("coasters db decreases with each deletion", async () => {
await Coasters.del(1);
expect(await db("coasters")).toHaveLength(5);

await Coasters.del(2);
expect(await db("coasters")).toHaveLength(4);

await Coasters.del(6);
expect(await db("coasters")).toHaveLength(3);
})

test("deleted coasters are removed from db", async () => {
await Coasters.del(1);
expect(
await db("coasters").where({abbrv: "SV"})
).toHaveLength(0); //returns empty array

await Coasters.del(6);
expect(
await db("coasters").where({abbrv: "SEFK"})
).toHaveLength(0); //returns empty array

await Coasters.del(2);
expect(
await db("coasters").where({abbrv: "MF"})
).toHaveLength(0); //returns empty array


})
})
38 changes: 38 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require("express");

const server = express();

server.use(express.json());

const Coasters = require("./model");


server.get("/api/coasters", (req, res) => {
Coasters.get()
.then( coasters => res.json(coasters));
})

server.get("/api/coasters/:id", async (req, res) => {
let filter = Number(req.params.id) ?
{coaster_id: req.params.id}:
{abbrv: req.params.id.toUpperCase()};

const coaster = await Coasters.getBy(filter);
res.json(coaster);
})

server.post("/api/coasters/", (req, res) => {
Coasters.add(req.body)
.then( newCoaster => res.status(201).json(newCoaster));
})

server.delete("/api/coasters/:coaster_id", (req, res) => {
Coasters.del(req.params.coaster_id)
.then( deletedCoaster => res.json(deletedCoaster));
})

server.use("*", (req, res) => {
res.status(404).json({ message: "There's nothing here!"});
})

module.exports = server;
Loading