Skip to content

Commit 038c1e3

Browse files
committed
Be MEAN: Node.JS (AULA 9 pt.2/2) TDD
1 parent 3d07b4a commit 038c1e3

File tree

13 files changed

+326
-1
lines changed

13 files changed

+326
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const Model = require('./pokemon-schema');
4+
5+
const CRUD = {
6+
create: function (data, callback) {
7+
const Suissamon = new Model(data);
8+
Suissamon.save(callback);
9+
},
10+
retrieve: function (query, callback) {
11+
Model.find(query, callback);
12+
},
13+
update: function (query, mod, options) {
14+
options = options || {};
15+
Model.update(query, mod, options, function (err, data) {
16+
if (err) {
17+
return console.log('ERRO: ', err);
18+
}
19+
return console.log('Alterou:', data);
20+
});
21+
},
22+
delete: function (query) {
23+
Model.remove(query, function (err, data) {
24+
if (err) {
25+
return console.log('ERRO: ', err);
26+
}
27+
return console.log('Deletou:', data);
28+
});
29+
},
30+
};
31+
32+
module.exports = CRUD;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const controller = require('./pokemon-controller');
5+
6+
describe('Pokemons` Controller', () => {
7+
const pokemon = {
8+
name: 'Jean',
9+
description: 'Pokemonzudo S2',
10+
type: 'Fire',
11+
attack: 85,
12+
defense: 85,
13+
height: 1.80
14+
};
15+
16+
after(done => {
17+
let Model = require('./pokemon-schema');
18+
Model.remove({});
19+
done();
20+
});
21+
22+
describe('create a new pokemon', () => {
23+
it('expect a new pokemon had been created', done => {
24+
controller.create(pokemon, (err, data)=> {
25+
expect(err).to.not.exist;
26+
expect(data._id).to.exist;
27+
expect(data.name).to.be.eq('Jean');
28+
expect(data.description).to.exist;
29+
expect(data.type).to.be.eq('Fire');
30+
expect(data.attack).to.exist;
31+
expect(data.defense).to.exist;
32+
expect(data.height).to.exist;
33+
done();
34+
});
35+
});
36+
});
37+
38+
describe('find all pokemon', () => {
39+
it('expect to find all pokemon that had been created', done => {
40+
controller.retrieve({}, (err, data)=> {
41+
expect(err).to.not.exist;
42+
expect(data).to.be.an('array');
43+
expect(data).to.have.length.above(1);
44+
done();
45+
});
46+
});
47+
});
48+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const Schema = mongoose.Schema;
5+
mongoose.connect('mongodb://localhost/test');
6+
7+
const pokemonSchema = new Schema({
8+
name: String,
9+
description: String,
10+
type: String,
11+
attack: Number,
12+
defense: Number,
13+
height: Number
14+
});
15+
16+
module.exports = mongoose.model('Pokemon', pokemonSchema);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const Schema = mongoose.Schema;
5+
6+
mongoose.connect('mongodb://localhost/test');
7+
8+
let onlyUpperCase = (v) => v.toUpperCase();
9+
10+
const CommentsSchema = new Schema({
11+
title: String,
12+
body: String,
13+
date: Date
14+
});
15+
16+
const BlogPostSchema = new Schema({
17+
title: {type: String, get: onlyUpperCase},
18+
body: String,
19+
comments: [CommentsSchema]
20+
});
21+
22+
module.exports = mongoose.model('BlogPost', BlogPostSchema);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const Blog = require('./schema-getter');
4+
const util = require('util');
5+
const expect = require('chai').expect;
6+
7+
describe('BlogPostSchema', () => {
8+
const title = 'nodejs power of c++ and v8';
9+
10+
before(done=> {
11+
Blog.create({
12+
title: title
13+
}).then(blog=> {
14+
util.log(blog);
15+
done(); // async
16+
});
17+
});
18+
19+
after(done=> {
20+
Blog.remove({title: title})
21+
.then(deleted=> {
22+
util.log(deleted.result);
23+
done();
24+
});
25+
});
26+
27+
describe('testing getters', ()=> {
28+
it('title shown as upperCased', ()=> {
29+
Blog.findOne({title: title})
30+
.then(blog => {
31+
util.log(blog.title);
32+
expect(blog.title).to.be.equal(title.toUpperCase());
33+
});
34+
});
35+
});
36+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "validate",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "schema-validate-age.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "nncl",
10+
"license": "ISC",
11+
"dependencies": {
12+
"mongoose": "^4.6.0"
13+
},
14+
"devDependencies": {
15+
"chai": "^3.5.0"
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const User = require('./schema-setter');
4+
const expect = require('chai').expect;
5+
const _email = 'NNCL@LIVE.com';
6+
7+
describe('User`s Schema test', () => {
8+
9+
describe('Setter to Uppercase Test', ()=>{
10+
it('email saved as lowercase on database', ()=>{
11+
const user = new User({email : _email});
12+
expect(user.email).to.be.equal(_email.toLowerCase());
13+
});
14+
});
15+
16+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const Schema = mongoose.Schema;
5+
6+
let toLower = (v) => v.toLowerCase();
7+
8+
const UserSchema = new Schema({
9+
email: {
10+
type: String
11+
, set: toLower
12+
}
13+
});
14+
15+
module.exports = mongoose.model('Users', UserSchema);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const Schema = mongoose.Schema;
5+
6+
mongoose.connect('mongodb://localhost/test');
7+
8+
const greaterThanEighteen = age => age >= 18;
9+
10+
const UserSchema = new Schema({
11+
age: {
12+
type: Number
13+
, validate: {
14+
validator: greaterThanEighteen
15+
, message: 'Your age ({VALUE}) is not permitted'
16+
}
17+
}
18+
});
19+
20+
module.exports = mongoose.model('user', UserSchema);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const User = require('./schema-validate-age');
4+
const expect = require('chai').expect;
5+
6+
describe('User Validate', () => {
7+
8+
describe('Age validator to 18 or older', ()=>{
9+
it('age only accept 18 or greater', ()=>{
10+
let u = new User();
11+
u.age = 24;
12+
13+
expect(u.validateSync()).to.be.undefined; // do not exist any error
14+
});
15+
});
16+
17+
describe('Age validator 17 or younger', ()=>{
18+
it('age only accept 17 or younger', ()=>{
19+
let u = new User();
20+
u.age = 2;
21+
const validate = u.validateSync();
22+
expect(validate.message).to.be.eq('user validation failed');
23+
expect(validate.errors).to.be.exist; // errors exist
24+
});
25+
});
26+
27+
});

0 commit comments

Comments
 (0)