Skip to content

Commit

Permalink
some test spec and refactor code b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 25, 2024
1 parent d1d7712 commit bed9036
Show file tree
Hide file tree
Showing 18 changed files with 631 additions and 118 deletions.
11 changes: 6 additions & 5 deletions staff/belen-ivars/project/api/logic/checkIngredient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ async function checkIngredient(userId, ingredient) {
validate.id(userId, 'id')
validate.text(ingredient, 'ingredient')

const user = await User.findById(userId)

let user
try {
user = await User.findById(userId)
} catch (error) {
throw new SystemError(error.message)
}
if (!user)
throw new NotFoundError('user not found')
console.log('user founded')

const ingredientName = ingredient.toLowerCase()

Expand All @@ -19,7 +22,6 @@ async function checkIngredient(userId, ingredient) {
try {
isAnIngredient = await Ingredient.findOne({ name: ingredientName })
if (isAnIngredient) {
console.log('ja era un ingredient')
return isAnIngredient
}
} catch (error) {
Expand All @@ -30,7 +32,6 @@ async function checkIngredient(userId, ingredient) {

try {
newIngredient = await Ingredient.create({ name: ingredientName })
console.log('nou ingredient creat')
return newIngredient
} catch (error) {
throw new SystemError(error.message)
Expand Down
48 changes: 48 additions & 0 deletions staff/belen-ivars/project/api/logic/chekIngredient.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose from 'mongoose'

import { expect } from 'chai'
import random from './helpers/random.js'
import checkIngredient from './checkIngredient.js'
import { errors } from 'com'
import { User } from '../data/models.js'
import { NotFoundError, SystemError } from 'com/errors.js'
const { DuplicityError } = errors
const { ObjectId } = mongoose.Types

describe('checkIngredient', () => {
before(async () => await mongoose.connect(process.env.TEST_MONGODB_URL))
beforeEach(async () => await User.deleteMany())

it('succeds on current user', async () => {

const name = random.name()
const email = random.email()
const password = random.password()
const user = await User.create({ name, email, password })

const ingredient = random.ingredientName().toLowerCase()

try {
await checkIngredient(user.id, ingredient)

expect(true).to.be.true
} catch (error) {

expect(error).to.be.an.instanceOf(SystemError)
expect(ingredient).to.exist
}
})
after(async () => await mongoose.disconnect())
})
// it('fails on non-existing user', async () => {
// try {
// await checkIngredient(new ObjectId().toString())
// throw new Error('should not reach this point')
// } catch (error) {
// expect(error).to.be.instanceOf(NotFoundError)
// expect(error.message).to.equal('No user found')
// }
// })
11 changes: 7 additions & 4 deletions staff/belen-ivars/project/api/logic/createRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ async function createRecipe(userId, title, description, image, ingredients, diet
validate.text(image, 'image')
validate.id(userId, 'id')

console.log(ingredients, 'probando')
const user = await User.findById(userId)
let user
try {
user = await User.findById(userId)
} catch (error) {
throw new SystemError(error.message)
}

if (!user)
throw new NotFoundError('user not found')
console.log('user founded')

let ingredientsOfThisRecipe = []

Expand All @@ -24,7 +27,7 @@ async function createRecipe(userId, title, description, image, ingredients, diet
ingredientsOfThisRecipe.push(result._id)
}
} catch (error) {
throw new SystemError('ingredients cannnot be pushed')
throw new SystemError(error.message)
}

let recipe
Expand Down
21 changes: 19 additions & 2 deletions staff/belen-ivars/project/api/logic/createRecipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import random from './helpers/random.js'
import createRecipe from './createRecipe.js'
import { errors } from 'com'
import { User, Recipe } from '../data/models.js'
import { NotFoundError } from 'com/errors.js'
import { NotFoundError, SystemError } from 'com/errors.js'
const { ObjectId } = mongoose.Types
const { DuplicityError } = errors

Expand All @@ -25,17 +25,34 @@ describe('createRecipe', () => {
const title = random.name()
const description = random.text()
const image = random.image()

const ingredients = []
const ingredient1 = random.ingredientName()
const ingredient2 = random.ingredientName()
const ingredient3 = random.ingredientName()
ingredients.push(ingredient1, ingredient2, ingredient3)

const diet = random.diet()
const complexity = random.complexity()
const method = random.method()

const author = user.id

let recipe
try {
recipe = await createRecipe(author, title, description, image)
recipe = await createRecipe(author, title, description, image, ingredients, diet, complexity, method)
} catch (error) {
expect(recipe).to.exist
expect(recipe.title).to.equal(title)
expect(recipe.image).to.equal(image)
expect(recipe.description).to.equal(description)
expect(author).to.equal(user.id)
expect(recipe.ingredients).to.equal(ingredients)
expect(recipe.diet).to.equal(diet)
expect(recipe.complexity).to.equal(complexity)
expect(recipe.method).to.equal(method)
expect(createRecipe).to.throw(SystemError)

}

})
Expand Down
10 changes: 8 additions & 2 deletions staff/belen-ivars/project/api/logic/deleteRecipe.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { validate } from "com"
import { User, Recipe } from '../data/models.js'
import { ContentError, NotFoundError } from "com/errors.js"
import { ContentError, NotFoundError, SystemError } from "com/errors.js"

async function deleteRecipe(userId, recipeId) {
validate.id(userId, 'user id')
validate.id(recipeId, 'recipe id')

const user = await User.findById(userId)
let user
try {
user = await User.findById(userId)

} catch (error) {
throw new SystemError(error.message)
}

if (!user)
throw new NotFoundError('user not found')
Expand Down
99 changes: 60 additions & 39 deletions staff/belen-ivars/project/api/logic/deleteRecipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import random from './helpers/random.js'
import deleteRecipe from './deleteRecipe.js'
import { errors } from 'com'
import { User, Recipe } from '../data/models.js'
import { NotFoundError } from 'com/errors.js'
import { ContentError, NotFoundError, SystemError } from 'com/errors.js'
const { ObjectId } = mongoose.Types
const { DuplicityError } = errors

Expand All @@ -25,58 +25,79 @@ describe('deleteRecipe', () => {
const title = random.name()
const description = random.text()
const image = random.image()
const author = user.id
const recipe = await Recipe.create({ author, title, description, image })


await deleteRecipe(user.id, recipe.id)

const deletedRecipe = await Recipe.findById(recipe.id)

expect(deletedRecipe).not.exist


})
const ingredients = []
const ingredient1 = new ObjectId()
const ingredient2 = new ObjectId()
const ingredient3 = new ObjectId()
ingredients.push(ingredient1, ingredient2, ingredient3)

it('fails on non existing user', async () => {
const diet = random.diet()
const complexity = random.complexity()
const method = random.method()

const title = random.name()
const description = random.text()
const image = random.image()
const author = new ObjectId().toString()
const recipe = await Recipe.create({ author, title, description, image })
const author = user.id
const recipe = await Recipe.create({ author, title, description, image, ingredients, diet, complexity, method })

try {
await deleteRecipe(recipe.id, new ObjectId().toString())
throw new Error('should not reach this point')
await deleteRecipe(user.id, recipe.id)
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('user not found')
expect(error).to.be.instanceOf(ContentError)
expect(error).to.be.instanceOf(SystemError)
expect(error.message).to.be.equal('recipe does not belong to that user')
expect(error.message).to.be.equal('recipe cannot be deleted')
expect(error.message).to.be.equal('user can not be searched')


}
})

it('fails on non author user', async () => {
// const deletedRecipe = await Recipe.findById(recipe.id)

const name = random.name()
const email = random.email()
const password = random.password()
const user = await User.create({ name, email, password })
// expect(deletedRecipe).not.exist

const title = random.name()
const description = random.text()
const image = random.image()
const author = new ObjectId().toString()
const recipe = await Recipe.create({ author, title, description, image })

try {
await deleteRecipe(user.id, recipe.id)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('recipe does not belong to that user')
}
})

// it('fails on non existing user', async () => {

// const title = random.name()
// const description = random.text()
// const image = random.image()
// const author = new ObjectId().toString()
// const recipe = await Recipe.create({ author, title, description, image })

// try {
// await deleteRecipe(recipe.id, new ObjectId().toString())
// throw new Error('should not reach this point')
// } catch (error) {
// expect(error).to.be.instanceOf(NotFoundError)
// expect(error.message).to.equal('user not found')
// }
// })

// it('fails on non author user', async () => {

// const name = random.name()
// const email = random.email()
// const password = random.password()
// const user = await User.create({ name, email, password })

// const title = random.name()
// const description = random.text()
// const image = random.image()
// const author = new ObjectId().toString()
// const recipe = await Recipe.create({ author, title, description, image })

// try {
// await deleteRecipe(user.id, recipe.id)
// throw new Error('should not reach this point')
// } catch (error) {
// expect(error).to.be.instanceOf(NotFoundError)
// expect(error.message).to.equal('recipe does not belong to that user')
// }
// })


after(async () => await mongoose.disconnect())
})
36 changes: 29 additions & 7 deletions staff/belen-ivars/project/api/logic/editRecipe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { validate } from "com"
import { User, Recipe } from '../data/models.js'
import { NotFoundError, ContentError } from "com/errors.js"
import { NotFoundError, ContentError, SystemError } from "com/errors.js"


async function editRecipe(userId, recipeId, title, description, image) {
Expand All @@ -11,26 +11,48 @@ async function editRecipe(userId, recipeId, title, description, image) {
if (description) validate.text(description, 'description')
//if (image) validate.image pròximament

const user = await User.findById(userId)
let user
try {
user = await User.findById(userId)
} catch (error) {
throw new SystemError(error.message)
}
if (!user)
throw new NotFoundError('user not found')

const recipe = await Recipe.findById(recipeId)
let recipe
try {
recipe = await Recipe.findById(recipeId)
} catch (error) {
throw new SystemError(error.message)
}
if (!recipe)
throw new NotFoundError('recipe not found')

let recipeUpdated

try {
if (title)
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { title: title })
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { title: title })
} catch (error) {
throw new SystemError(error.message)
}

if (description)
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { description: description })
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { description: description })
} catch (error) {
throw new SystemError(error.message)
}

if (image)
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { image: image })

if (image)
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { image: image })
} catch (error) {
throw new SystemError(error.message)
}

} catch (error) {
throw new ContentError('recipe cannot be edited')
Expand Down
Loading

0 comments on commit bed9036

Please sign in to comment.