Skip to content

Commit

Permalink
Merge pull request #48 from emccarthy510/testCharacterInstanceMethod-#41
Browse files Browse the repository at this point in the history
Character model and instance method tests
  • Loading branch information
EmilyCMcCarthy authored Jul 28, 2017
2 parents 6d3e393 + 53ff45d commit 584980e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
30 changes: 18 additions & 12 deletions server/db/models/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
var Sequelize = require('sequelize');
var db = require('../db');

module.exports = db.define('character', {


const Character = db.define('character', {
name: {
type: Sequelize.STRING,
allowNull: false,
Expand Down Expand Up @@ -38,18 +40,22 @@ module.exports = db.define('character', {
notEmpty: true
}
}
}, {
instanceMethods: {
decreaseInventory: function(num){
if (num > this.inventory){
throw new Error('Sorry, we have only' + this.inventory + 'at this moment' );
}, {});

module.exports = Character;

Character.prototype.decreaseInventory = function(num) {
if (num > this.inventory){
return new Error('Sorry, we have only' + this.inventory + 'at this moment' );
}
else {
else {
this.inventory = this.inventory - num;
return this.inventory;
}
return this.inventory;
}
}

});

};

Character.prototype.increaseInventory = function(num) {
this.inventory = this.inventory + num;
return this.inventory;
};
66 changes: 45 additions & 21 deletions tests/db/character.spec.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
import db from '../../server/db';

import db from '../../server/db/db';
const Character = db.model('character');
var Sequelize = require('sequelize');

import chai from 'chai';
import chaiProperties from 'chai-properties';
import chaiThings from 'chai-things';
chai.use(chaiProperties);
chai.use(chaiThings);
const expect = chai.expect;
const Promise = require('bluebird');

describe('Character Model Tests', () => {

beforeEach('Synchronize and clear database', () => db.sync({force: true}));

after('Synchronize and clear database', () => db.sync({force: true}));

describe('Sequelize models', function () {

describe('Character Model', () => {
var CharacterData = {name: "Albus Dumbledore",
price: 100,
imageUrl: "./characters/albus.jpg",
description: "Be Dumbledore. Live in the magical world of Harry Potter... Be the headmaster at Hogwarts",
inventory: 15
};

// *Assertion translation*:
// This assertion expects that the Character model will
// put an `email` column in the users table.

it('has the expected schema definition', () => {
expect(Character.attributes.name).to.be.an('object');

});

describe('validations', () => {

// *Assertion translation*:
// The `name` column should be a required field.
it('requires name', () => {
const character = Character.build();
return character.validate()
.then(err => {
expect(err).to.be.an('object');
expect(err.errors).to.contain.a.thing.with.properties({
path: 'name',
type: 'notNull Violation'
});
});
});
it('can create a character', () => {
return Character.create(CharacterData)
.then(function (savedCharacter){
expect(savedCharacter.price).to.equal(100);
expect(savedCharacter.imageUrl).to.equal("./characters/albus.jpg");
expect(savedCharacter.name).to.equal('Albus Dumbledore');
expect(savedCharacter.description).to.equal("Be Dumbledore. Live in the magical world of Harry Potter... Be the headmaster at Hogwarts");
expect(savedCharacter.inventory).to.equal(15);
})
})
it('Instance method can reduce inventory', () => {
return Character.create(CharacterData)
.then(function (savedCharacter){
expect(savedCharacter.inventory).to.equal(15);
savedCharacter.decreaseInventory(1);
expect(savedCharacter.inventory).to.equal(14);
})
})

});
it('Instance method will not reduce inventory if you ask to remove too many', () => {
return Character.create(CharacterData)
.then(function (savedCharacter){
expect(savedCharacter.inventory).to.equal(15);
expect(savedCharacter.decreaseInventory(16)).to.be.an.instanceOf(Error);
})
})

it('Instance method increaseInventroy increase inventory', () => {
return Character.create(CharacterData)
.then(function (savedCharacter){
expect(savedCharacter.inventory).to.equal(15);
savedCharacter.increaseInventory(5);
expect(savedCharacter.inventory).to.equal(20);
})
})
});
});
});
Expand Down
1 change: 1 addition & 0 deletions tests/db/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 584980e

Please sign in to comment.