Created
August 13, 2014 19:09
-
-
Save bcoe/a1b7bfed7f42e9cab862 to your computer and use it in GitHub Desktop.
user-test.js
This file contains hidden or 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
| var connection = require('../../lib/connection')(), | |
| Customer = require('../../lib/models/customer'), | |
| helper = require('../test-helper'), | |
| Lab = require('lab'), | |
| lab = exports.lab = Lab.script(); | |
| // See prepare-tests.js, for follower feed ordering. | |
| lab.experiment('Customer', function() { | |
| lab.before(function(done) { | |
| helper.reset(function() { done(); }); | |
| }); | |
| lab.experiment('save', function() { | |
| lab.test('saves a new customer to the database', function(done) { | |
| var c = new Customer({ | |
| email: '[email protected]', | |
| name: 'Ben Coe' | |
| }); | |
| // Save our user! | |
| c.save(function(err) { | |
| Lab.expect(c.id).to.not.be.undefined; | |
| // Perform a raw query and grab the user created. | |
| connection.raw('SELECT * FROM customers WHERE id = ?', [c.id]).then(function(results) { | |
| var obj = results.rows[0]; | |
| // Did we write the appropriate fields to the DB. | |
| Lab.expect(obj.email).to.eql('[email protected]'); | |
| Lab.expect(obj.name).to.eql('Ben Coe'); | |
| // did we create sane looking dates? | |
| Lab.expect(obj.created).to.be.gt(new Date(2006, 1, 1)); | |
| Lab.expect(obj.updated).to.be.gt(new Date(2006, 1, 1)); | |
| done(); | |
| }).done(); | |
| }); | |
| }); | |
| }); | |
| lab.experiment('find_by_id', function() { | |
| lab.it('fetch a customber by their id', function(done) { | |
| var c = new Customer({ | |
| email: '[email protected]', | |
| name: 'Ben Coe' | |
| }); | |
| // Save our user! | |
| c.save(function(err) { | |
| Customer.find_by_id(c.id, function(err, fetched) { | |
| Lab.expect(fetched.email).to.eql('[email protected]'); | |
| Lab.expect(fetched.name).to.eql('Ben Coe'); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); | |
| lab.experiment('update', function() { | |
| lab.it('updates an existing customer record', function(done) { | |
| var c = new Customer({ | |
| email: '[email protected]', | |
| name: 'Laurie Voss' | |
| }); | |
| // Save our user! | |
| c.save(function(err) { | |
| // now update a field. | |
| c.update({ | |
| phone: '555-555-5555' | |
| }, function(err) { | |
| // next, load the user and make sure we wrote the field. | |
| Customer.find_by_id(c.id, function(err, fetched) { | |
| Lab.expect(fetched.email).to.eql('[email protected]'); | |
| Lab.expect(fetched.phone).to.eql('555-555-5555'); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); | |
| lab.it("should set the updated at field", function(done) { | |
| var c = new Customer({ | |
| email: '[email protected]', | |
| name: 'Laurie Voss' | |
| }); | |
| // Save our user! | |
| c.save(function(err) { | |
| var updated = c.updated; // store original updated. | |
| // now update a field. | |
| c.update({ | |
| phone: '555-555-5555' | |
| }, function(err) { | |
| // next, load the user and make sure we wrote the field. | |
| Customer.find_by_id(c.id, function(err, fetched) { | |
| Lab.expect(fetched.updated).to.be.gt(updated); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); | |
| lab.it("updates the original object's values", function(done) { | |
| var c = new Customer({ | |
| email: '[email protected]', | |
| name: 'Laurie Voss' | |
| }); | |
| // Save our user! | |
| c.save(function(err) { | |
| var updated = c.updated; // store original updated. | |
| // now update a field. | |
| c.update({ | |
| phone: '555-555-5555' | |
| }, function(err) { | |
| Lab.expect(c.phone).to.eql('555-555-5555'); | |
| // we should modify updated! | |
| Lab.expect(c.updated).to.be.gt(updated); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment