forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getPosts, getResults ans getRandomWorkout spec test in logic; fix…
… validate errors b00tc4mp#178
- Loading branch information
Showing
11 changed files
with
393 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import "dotenv/config" | ||
import mongoose, { Types } from "mongoose" | ||
import bcrypt from "bcryptjs" | ||
|
||
import getPosts from "./getPosts.js" | ||
import { Post, Result, User, Workout } from "../data/index.js" | ||
import { expect } from "chai" | ||
import { ContentError, NotFoundError } from "com/errors.js" | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
const { ObjectId } = Types | ||
|
||
|
||
describe("getPosts", () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => Promise.all([User.deleteMany(), Post.deleteMany(), Result.deleteMany()]))) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Post.deleteMany(), Result.deleteMany()])) | ||
|
||
it("suceeds on getting all posts", () => | ||
bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ | ||
name: "nameTest", | ||
surname: "surnameTest", | ||
email: "[email protected]", | ||
username: "usernameTest", | ||
password: hash | ||
})) | ||
.then((user) => Workout.create({ | ||
workoutType: "benchmark", | ||
title: "Fran", | ||
rounds: 10, | ||
movements: [], | ||
duration: 10, | ||
description: "descriptionTest" | ||
}) | ||
.then((workout) => Result.create({ | ||
workout: workout.id, | ||
athlete: user.id, | ||
time: 10, | ||
repetitions: 10, | ||
weight: 10 | ||
}) | ||
.then((result) => Post.create({ | ||
author: user.id, | ||
workout: workout.id, | ||
result: result.id, | ||
image: "http://test.com", | ||
description: "descriptionTest", | ||
time: 10, | ||
repetitions: 10, | ||
weight: 10, | ||
likes: [], | ||
comments: [] | ||
}))) | ||
.then(() => user) | ||
) | ||
.then((user) => getPosts(user.id)) | ||
.then(posts => { | ||
expect(posts).to.be.an.instanceOf(Array) | ||
expect(posts).to.have.lengthOf(1) | ||
|
||
expect(posts[0].author).to.be.an.instanceOf(Object) | ||
expect(posts[0].image).to.equal("http://test.com") | ||
expect(posts[0].workout).to.be.an.instanceOf(Object) | ||
expect(posts[0].result).to.be.an.instanceOf(Object) | ||
expect(posts[0].description).to.equal("descriptionTest") | ||
|
||
expect(posts[0].likes).to.be.an.instanceOf(Array) | ||
expect(posts[0].comments).to.be.an.instanceOf(Array) | ||
}) | ||
) | ||
|
||
it("fails on non-existing user", () => { | ||
let errorThrown | ||
|
||
return getPosts(new ObjectId().toString()) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.be.equal("user not found") | ||
}) | ||
}) | ||
|
||
it("fails on non-existing post", () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ | ||
name: "nameTest", | ||
surname: "surnameTest", | ||
email: "[email protected]", | ||
username: "usernameTest", | ||
password: hash | ||
})) | ||
.then(user => getPosts(user.id)) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.be.equal("there are no posts yet") | ||
}) | ||
}) | ||
|
||
it("fails on invalid user id", () => { | ||
let errorThrown | ||
try { | ||
getPosts(1234) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.an.instanceof(ContentError) | ||
expect(errorThrown.message).to.equal("userId is not valid") | ||
} | ||
}) | ||
after(() => Promise.all([User.deleteMany(), Post.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
staff/debora-garcia/project/api/logic/getRandomWorkout.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import "dotenv/config" | ||
import mongoose, { Types } from "mongoose" | ||
import bcrypt from "bcryptjs" | ||
|
||
import getRandomWorkout from "./getRandomWorkout.js" | ||
import { Post, Result, User, Workout } from "../data/index.js" | ||
import { expect } from "chai" | ||
import { ContentError, NotFoundError } from "com/errors.js" | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
const { ObjectId } = Types | ||
|
||
describe("getRandomWorkout", () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => Promise.all([User.deleteMany(), Workout.deleteMany(), Result.deleteMany()]))) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Workout.deleteMany(), Result.deleteMany()])) | ||
|
||
it("succeeds on getting a random workout", () => { | ||
let workoutType = "benchmark" | ||
|
||
return bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ | ||
name: "nameTest", | ||
surname: "surnameTest", | ||
email: "[email protected]", | ||
username: "usernameTest", | ||
password: hash | ||
})) | ||
.then((user) => { | ||
return Promise.all([Workout.create({ | ||
workoutType: workoutType, | ||
title: "Fran", | ||
rounds: 10, | ||
movements: [], | ||
duration: 10, | ||
description: "descriptionTest1" | ||
}), | ||
Workout.create({ | ||
workoutType: workoutType, | ||
title: "Murph", | ||
rounds: 1, | ||
movements: [], | ||
duration: 60, | ||
description: "descriptionTest2" | ||
})]) | ||
|
||
.then(() => user) | ||
}) | ||
.then((user) => getRandomWorkout(user.id, workoutType)) | ||
.then(workout => { | ||
expect(workout.workoutType).to.be.equal(workoutType) | ||
expect(workout.movements).to.be.an.instanceOf(Array) | ||
expect(workout.description).to.be.an.string | ||
if (workout.title === "Fran") { | ||
expect(workout.rounds).to.be.equal(10) | ||
expect(workout.duration).to.be.equal(10) | ||
expect(workout.description).to.be.equal("descriptionTest1") | ||
} else if (workout.title === "Murph") { | ||
expect(workout.rounds).to.be.equal(1) | ||
expect(workout.duration).to.be.equal(60) | ||
expect(workout.description).to.be.equal("descriptionTest2") | ||
} | ||
}) | ||
}) | ||
|
||
it("fails on non-existing user", () => { | ||
let errorThrown | ||
|
||
return getRandomWorkout(new ObjectId().toString(), "benchmark") | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.be.equal("user not found") | ||
}) | ||
}) | ||
|
||
it("fails on no workouts found for given workoutType", () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ | ||
name: "nameTest", | ||
surname: "surnameTest", | ||
email: "[email protected]", | ||
username: "usernameTest", | ||
password: hash | ||
})) | ||
.then((user) => getRandomWorkout(user.id, "for-time")) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.be.equal("workouts not found") | ||
}) | ||
|
||
}) | ||
|
||
it("fails on generating random workout", () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ | ||
name: "nameTest", | ||
surname: "surnameTest", | ||
email: "[email protected]", | ||
username: "usernameTest", | ||
password: hash | ||
})) | ||
.then(user => Workout.create({ | ||
workoutType: "benchmark", | ||
title: "Fran", | ||
rounds: 10, | ||
movements: [], | ||
duration: 10, | ||
description: "descriptionTest" | ||
}) | ||
.then(() => Workout.deleteMany({ workoutType: "benchmark" })) | ||
.then(() => getRandomWorkout(user.id, "benchmark")) | ||
) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.be.equal("workout not found") | ||
}) | ||
|
||
}) | ||
it("fails on invalid user id", () => { | ||
let errorThrown | ||
try { | ||
getRandomWorkout(1234, "benchmark") | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.an.instanceof(ContentError) | ||
expect(errorThrown.message).to.equal("userId is not valid") | ||
} | ||
}) | ||
|
||
it("fails on invalid workoutType", () => { | ||
let errorThrown | ||
try { | ||
getRandomWorkout(new ObjectId().toString(), 1234) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.an.instanceof(ContentError) | ||
expect(errorThrown.message).to.equal("workoutType is not valid") | ||
} | ||
}) | ||
|
||
after(() => Promise.all([User.deleteMany(), Workout.deleteMany()]).then(() => mongoose.disconnect())) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.