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 new specs(getAnswers, addStudent, getStudents) and fixed anothers…
… specs b00tc4mp#182
- Loading branch information
1 parent
0e993a2
commit 13b635d
Showing
41 changed files
with
742 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ const answer = new Schema({ | |
|
||
const Answer = model('Answer', answer) | ||
|
||
export default Answer | ||
export default Answer | ||
|
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
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
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
103 changes: 102 additions & 1 deletion
103
staff/agustin-birman/api/logic/answer/deleteAnswers.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 |
---|---|---|
@@ -1,3 +1,104 @@ | ||
import 'dotenv/config' | ||
import bcrypt from 'bcryptjs' | ||
import mongoose, { Types } from 'mongoose' | ||
import deleteAnswers from './deleteAnswers.js' | ||
import { expect } from 'chai' | ||
import { User, Activity, Exercise, Answer } from '../../data/index.js' | ||
import { ContentError, NotFoundError } from 'com/errors.js' | ||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
|
||
describe('deleteAnswers', () => { | ||
before | ||
before(() => mongoose.connect(MONGODB_URL_TEST)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany(), Answer.deleteMany()])) | ||
|
||
// it('suceeds on deleting answers', () => { | ||
// return bcrypt.hash('12345678', 8) | ||
// .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
// .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
// .then(activity => ({ user, activity }))) | ||
// .then(({ user, activity }) => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
// .then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
// .then(exercise2 => ({ user, activity, exercise1, exercise2 })))) | ||
// .then(({ user, activity, exercise1, exercise2 }) => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' }) | ||
// .then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' }) | ||
// .then(answer2 => ({ user, activity, exercise1, exercise2, answer1, answer2 })))) | ||
// .then(({ user, activity, exercise1, exercise2, answer1, answer2 }) => console.log(answer1, answer2)) | ||
// }) | ||
|
||
it('suceeds on deleting answers', () => { | ||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' }) | ||
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' }) | ||
.then(answer2 => deleteAnswers(user.id, activity.id) | ||
.then(() => Answer.find({ activity: activity.id }) | ||
.then(answers => expect(answers).to.be.an('array').that.is.empty)))))))) | ||
|
||
}) | ||
|
||
it('fails non-existing user', () => { | ||
let errorThrown | ||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' }) | ||
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' }) | ||
.then(answer2 => deleteAnswers(new ObjectId().toString(), activity.id)) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
})))))) | ||
}) | ||
|
||
it('fails non-existing activity', () => { | ||
let errorThrown | ||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' }) | ||
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' }) | ||
.then(answer2 => deleteAnswers(user.id, new ObjectId().toString())) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('activity not found') | ||
})))))) | ||
}) | ||
|
||
it('fails on invalid userId', () => { | ||
let errorThrown | ||
try { | ||
deleteAnswers(123, new ObjectId().toString()) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('userId is not valid') | ||
} | ||
}) | ||
|
||
it('fails on invalid userId', () => { | ||
let errorThrown | ||
try { | ||
deleteAnswers(new ObjectId().toString(), 123) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('activityId is not valid') | ||
} | ||
}) | ||
after(() => () => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany(), Answer.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.