forked from howardmann/clean-node
-
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.
- Loading branch information
1 parent
5fefb3f
commit 3ad30da
Showing
14 changed files
with
1,273 additions
and
17 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 |
---|---|---|
@@ -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 |
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,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) | ||
|
||
|
||
}) | ||
|
||
}) |
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 |
---|---|---|
@@ -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 //? |
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,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) | ||
}) | ||
}) |
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,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') | ||
}) |
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,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 //? |
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,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) | ||
}) | ||
}) |
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,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') | ||
}) |
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,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 |
Oops, something went wrong.