Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
howardmann committed Jul 27, 2019
1 parent 5fefb3f commit 3ad30da
Show file tree
Hide file tree
Showing 14 changed files with 1,273 additions and 17 deletions.
10 changes: 8 additions & 2 deletions data-access/students-db/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
let {
listStudents,
findStudent,
addStudent
findStudentsBy,
addStudent,
deleteStudent,
dropAll
} = require('./memory/index') // switch out db as required

let studentsDb = {
listStudents,
findStudent,
addStudent
findStudentsBy,
addStudent,
deleteStudent,
dropAll
}

module.exports = studentsDb
93 changes: 93 additions & 0 deletions data-access/students-db/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
let chai = require('chai');
let expect = chai.expect;
let studentsDb = require('./index')

describe('studentsDb', () => {
beforeEach(async () => {
await studentsDb.dropAll();
let howie = {
name: 'howie',
age: 12,
grade: 3,
prefect: true
}
let bill = {
name: 'bill',
age: 13,
grade: 3,
prefect: false
}
await studentsDb.addStudent(howie)
await studentsDb.addStudent(bill)
})

it('drops database', async () => {
await studentsDb.dropAll()
let students = await studentsDb.listStudents()
let input = students.length
let actual = 0
expect(input).to.equal(actual)
})

it('lists students', async () => {
let input = await studentsDb.listStudents()
let actual = 2
expect(input.length).to.equal(actual)
})

it('find single student by id', async () => {
let id = 1
let student = await studentsDb.findStudent({id})
let input = student.id
let actual = id
expect(input).to.equal(actual)
})

it('finds all students by property', async () => {
let students = await studentsDb.findStudentsBy({grade: 3})
let input = students.map(el => el.name)
let actual = ['howie', 'bill']
expect(input).to.eql(actual)
})

it('inserts a student', async () => {
let felix = {
name: 'felix',
grade: 2,
age: 6
}
let newStudent = await studentsDb.addStudent(felix)
let {id, ...input} = newStudent
let actual = {
name: 'felix',
grade: 2,
age: 6,
prefect: false
}
expect(input).to.eql(actual)
})

it('deletes a student', async () => {
let validInput = await studentsDb.deleteStudent(1)
let validActual = {
status: 'success',
id: 1
}

expect(validInput).to.eql(validActual)

let students = await studentsDb.listStudents()
let inputLength = students.length
let actualLength = 1
expect(inputLength).to.equal(actualLength)

let invalidInput = await studentsDb.deleteStudent(4)
let invalidActual = {
status: 'fail'
}
expect(invalidInput).to.eql(invalidActual)


})

})
32 changes: 31 additions & 1 deletion data-access/students-db/memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ let findStudent = (prop, val) => {
return Promise.resolve(serialize(student))
}

let findStudentsBy = (prop, val) => {
let student = STUDENTS.filter(student => student[prop] == val)
return Promise.resolve(serialize(student))
}


let addStudent = (studentInfo) => {
let student = makeStudent(studentInfo)
let newStudent = {
Expand All @@ -25,10 +31,34 @@ let addStudent = (studentInfo) => {
return findStudent('serial', newStudent.serial)
}

let deleteStudent = (id) => {
return findStudent({id})
.then(student => {
if (student.id === id) {
STUDENTS = STUDENTS.filter(student => student.serial !== id)
return {
id,
status: 'success'
}
}
return {
status: 'fail'
}
})
}

let dropAll = () => {
STUDENTS = [];
return STUDENTS;
}

module.exports = {
listStudents,
findStudent,
addStudent
findStudentsBy,
addStudent,
deleteStudent,
dropAll
}

// let students = listStudents() //?
Expand Down
6 changes: 3 additions & 3 deletions models/student/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let buildMakeStudent = require('./student')
let {studentValidator} = require('../../validator')
let studentSchema = require('./student-schema')
// let {studentValidator} = require('../../validator')
let studentValidator = require('../validator/')(studentSchema)

let makeStudent = buildMakeStudent(studentValidator)

module.exports = makeStudent

// let howie = makeStudent({name: 'howie'})
// howie //?
48 changes: 48 additions & 0 deletions models/student/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let chai = require('chai');
let expect = chai.expect;
let makeStudent = require('./index')

describe('makeStudent', () => {
it('validates name (string:required), grade (number), age (number) and prefect (boolean)', () => {
let errorMessage = [
'must have name as string',
'age must be a number',
'grade must be a number',
'prefect must be a boolean'
].join('\n')

expect(() => {
makeStudent({
grade: 'twelve',
age: 'twleve',
prefect: 12
})
}).to.throw(errorMessage)
})
it('must have name', () => {
let student = makeStudent({
name: 'howie',
})
let input = student.getName()
let actual = 'howie'
expect(input).to.equal(actual)
})
it('can have grade', () => {
let student = makeStudent({name: 'howie', grade: 2})
let input = student.getGrade()
let actual = 2
expect(input).to.equal(actual)
})
it('can have age', () => {
let student = makeStudent({name: 'howie', age: 12})
let input = student.getAge()
let actual = 12
expect(input).to.equal(actual)
})
it('sets prefect to false by default', () => {
let student = makeStudent({name: 'howie'})
let input = student.isPrefect()
let actual = false
expect(input).to.equal(actual)
})
})
8 changes: 8 additions & 0 deletions models/student/student-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let Joi = require('joi')

module.exports = Joi.object().keys({
name: Joi.string().required().error(() => 'must have name as string'),
age: Joi.number().error(() => 'age must be a number'),
grade: Joi.number().error(() => 'grade must be a number'),
prefect: Joi.boolean().error(() => 'prefect must be a boolean')
})
10 changes: 5 additions & 5 deletions models/teacher/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
let buildMakeTeacher = require('./teacher')
let {
teacherValidator
} = require('../../validator')
let teacherSchema = require('./teacher-schema')
let teacherValidator = require('../validator')(teacherSchema)
// let {
// teacherValidator
// } = require('../../validator')

let makeTeacher = buildMakeTeacher(teacherValidator)

module.exports = makeTeacher

// let robert = makeTeacher({name: 'robert frost', subject: 12})
// robert //?
45 changes: 45 additions & 0 deletions models/teacher/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
let chai = require('chai');
let expect = chai.expect;
let makeTeacher = require('./index')

describe('makeTeacher', () => {
it('validates name (string:required), grade (number), age (number) and prefect (boolean)', () => {
let errorMessage = [
'must have name as string',
'subject must be a string',
'tenure must be a boolean'
].join('\n')

expect(() => {
makeTeacher({
subject: 31,
tenure: 12
})
}).to.throw(errorMessage)
})
it('must have name', () => {
let teacher = makeTeacher({
name: 'howie',
})
let input = teacher.getName()
let actual = 'howie'
expect(input).to.equal(actual)
})
it('can have subject', () => {
let teacher = makeTeacher({
name: 'howie',
subject: 'maths'
})
let input = teacher.getSubject()
let actual = 'maths'
expect(input).to.equal(actual)
})
it('sets tenure to false by default', () => {
let teacher = makeTeacher({
name: 'howie'
})
let input = teacher.isTenure()
let actual = false
expect(input).to.equal(actual)
})
})
7 changes: 7 additions & 0 deletions models/teacher/teacher-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let Joi = require('joi')

module.exports = Joi.object().keys({
name: Joi.string().required().error(() => 'must have name as string'),
subject: Joi.string().error(() => 'subject must be a string'),
tenure: Joi.boolean().error(() => 'tenure must be a boolean')
})
15 changes: 15 additions & 0 deletions models/validator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let Joi = require('joi')

let validator = (schema) =>
(payload) => {
let {error} = Joi.validate(payload, schema, {abortEarly: false})
if (error) {
let message = error.details.map(el => el.message).join('\n')
return {
error: message
}
}
return true
}

module.exports = validator
Loading

0 comments on commit 3ad30da

Please sign in to comment.