Skip to content

Commit

Permalink
add getPosts, getResults ans getRandomWorkout spec test in logic; fix…
Browse files Browse the repository at this point in the history
… validate errors b00tc4mp#178
  • Loading branch information
Debi312 committed Aug 20, 2024
1 parent 149ca94 commit 9d40f7c
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 16 deletions.
3 changes: 2 additions & 1 deletion staff/debora-garcia/project/api/logic/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { NotFoundError, SystemError } from "com/errors.js"
const createPost = (userId, workoutId, image, description, time, repetitions, weight) => {
validate.id(userId, "userId")
validate.id(workoutId, "workoutId")
validate.url(image, "image")

validate.url(image, "image")
validate.text(description, "description", 150)
validate.number(time, "time")
validate.number(repetitions, "repetitions")
Expand Down
4 changes: 1 addition & 3 deletions staff/debora-garcia/project/api/logic/createPost.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const { MONGODB_URL_TEST } = process.env
const { ObjectId } = Types

describe("createPost", () => {
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => {
return Promise.all([User.deleteMany(), Post.deleteMany(), Result.deleteMany()])
}))
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => Promise.all([User.deleteMany(), Post.deleteMany(), Result.deleteMany()])))

beforeEach(() => Promise.all([User.deleteMany(), Post.deleteMany(), Result.deleteMany()]))

Expand Down
8 changes: 3 additions & 5 deletions staff/debora-garcia/project/api/logic/getPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { SystemError, NotFoundError } from "com/errors.js"
import validate from "com/validate.js"
const getPosts = (userId) => {
validate.id(userId, "userId")
// validate.number(time, "time")
// validate.number(repetitions, "repetitions")
// validate.number(weight, "weight")

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
Expand All @@ -24,6 +21,7 @@ const getPosts = (userId) => {
.lean()
.catch(error => { throw new SystemError(error.message) })
.then(posts => {
if (posts.length === 0) throw new NotFoundError("there are no posts yet")
posts.forEach(post => {
post.id = post._id.toString()

Expand All @@ -48,8 +46,8 @@ const getPosts = (userId) => {
}

post.likes.map(userObjectId => userObjectId.toString())
post.coments.map(userObjectId => userObjectId.toString())

//post.comments.map(userObjectId => userObjectId.toString())
//TODO COMMENTS

})
return (posts)
Expand Down
115 changes: 115 additions & 0 deletions staff/debora-garcia/project/api/logic/getPosts.spec.js
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()))
})
2 changes: 1 addition & 1 deletion staff/debora-garcia/project/api/logic/getRandomWorkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getRandomWorkout = (userId, workoutType) => {
return Workout.find({ workoutType }).select("-__v").populate("movements").lean()
.catch(error => { throw new SystemError(error.message) })
.then(workouts => {
if (!workouts) throw new NotFoundError("workouts not found")
if (!workouts || workouts.length === 0) throw new NotFoundError("workouts not found")

const randomNumber = Math.floor(Math.random() * workouts.length)

Expand Down
152 changes: 152 additions & 0 deletions staff/debora-garcia/project/api/logic/getRandomWorkout.spec.js
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()))

})
2 changes: 1 addition & 1 deletion staff/debora-garcia/project/api/logic/getResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getResults = (userId) => {
.lean()
.catch(error => { throw new SystemError(error.message) })
.then(results => {
if (!results) throw new NotFoundError("you did't submit any result")
if (results.length === 0) throw new NotFoundError("you did't submit any result")
results.forEach(result => {
result.id = result._id.toString()
delete result._id
Expand Down
Loading

0 comments on commit 9d40f7c

Please sign in to comment.