不要怂,就是干,撸起袖子干!

Commit 1007ac22 by Seth Samuel

Promisify tests

1 parent 41676e2b
Showing with 25 additions and 50 deletions
...@@ -20,8 +20,7 @@ if (dialect.match(/^postgres/)) { ...@@ -20,8 +20,7 @@ if (dialect.match(/^postgres/)) {
phones: DataTypes.ARRAY(DataTypes.HSTORE), phones: DataTypes.ARRAY(DataTypes.HSTORE),
emergency_contact: DataTypes.JSON emergency_contact: DataTypes.JSON
}) })
this.User.sync({ force: true }) this.User.sync({ force: true }).success(function() {
.success(function() {
done() done()
}) })
}) })
...@@ -180,23 +179,21 @@ if (dialect.match(/^postgres/)) { ...@@ -180,23 +179,21 @@ if (dialect.match(/^postgres/)) {
}); });
describe('hstore', function() { describe('hstore', function() {
it('should tell me that a column is hstore and not USER-DEFINED', function(done) { it('should tell me that a column is hstore and not USER-DEFINED', function() {
this.sequelize.queryInterface.describeTable('Users').success(function(table) { return this.sequelize.queryInterface.describeTable('Users').then(function(table) {
expect(table.settings.type).to.equal('HSTORE') expect(table.settings.type).to.equal('HSTORE')
expect(table.document.type).to.equal('HSTORE') expect(table.document.type).to.equal('HSTORE')
done()
}) })
}) })
it('should stringify hstore with insert', function(done) { it('should stringify hstore with insert', function() {
this.User.create({ return this.User.create({
username: 'bob', username: 'bob',
email: ['myemail@email.com'], email: ['myemail@email.com'],
settings: {mailing: false, push: 'facebook', frequency: 3} settings: {mailing: false, push: 'facebook', frequency: 3}
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expected = 'INSERT INTO "Users" ("id","username","email","settings","document","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',ARRAY[\'myemail@email.com\']::TEXT[],\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\'' var expected = 'INSERT INTO "Users" ("id","username","email","settings","document","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',ARRAY[\'myemail@email.com\']::TEXT[],\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\''
expect(sql.indexOf(expected)).to.equal(0) expect(sql.indexOf(expected)).to.equal(0)
done()
}) })
}) })
...@@ -385,101 +382,79 @@ if (dialect.match(/^postgres/)) { ...@@ -385,101 +382,79 @@ if (dialect.match(/^postgres/)) {
}) })
}) })
it("should save hstore correctly", function(done) { it("should save hstore correctly", function() {
this.User return this.User.create({ username: 'user', email: ['foo@bar.com'], settings: { created: '"value"' }}).then(function(newUser) {
.create({ username: 'user', email: ['foo@bar.com'], settings: { created: '"value"' }})
.success(function(newUser) {
// Check to see if the default value for an hstore field works // Check to see if the default value for an hstore field works
expect(newUser.document).to.deep.equal({ default: "'value'" }) expect(newUser.document).to.deep.equal({ default: "'value'" })
expect(newUser.settings).to.deep.equal({ created: '"value"' }) expect(newUser.settings).to.deep.equal({ created: '"value"' })
// Check to see if updating an hstore field works // Check to see if updating an hstore field works
newUser.updateAttributes({settings: {should: 'update', to: 'this', first: 'place'}}).success(function(oldUser){ return newUser.updateAttributes({settings: {should: 'update', to: 'this', first: 'place'}}).then(function(oldUser){
// Postgres always returns keys in alphabetical order (ascending) // Postgres always returns keys in alphabetical order (ascending)
expect(oldUser.settings).to.deep.equal({first: 'place', should: 'update', to: 'this'}) expect(oldUser.settings).to.deep.equal({first: 'place', should: 'update', to: 'this'})
done()
}) })
}) })
.error(console.log)
}) })
it('should save hstore array correctly', function(done) { it('should save hstore array correctly', function() {
var User = this.User; var User = this.User;
this.User.create({ return this.User.create({
username: 'bob', username: 'bob',
email: ['myemail@email.com'], email: ['myemail@email.com'],
phones: [{ number: '123456789', type: 'mobile' }, { number: '987654321', type: 'landline' }, { number : '8675309', type : "Jenny's"}, {number : '5555554321', type : '"home"' }] phones: [{ number: '123456789', type: 'mobile' }, { number: '987654321', type: 'landline' }, { number : '8675309', type : "Jenny's"}, {number : '5555554321', type : '"home"' }]
}) }).then(function(){
.success(function(){ return User.find(1).then(function(user){
User.find(1)
.success(function(user){
expect(user.phones.length).to.equal(4); expect(user.phones.length).to.equal(4);
expect(user.phones[1].number).to.equal('987654321'); expect(user.phones[1].number).to.equal('987654321');
expect(user.phones[2].type).to.equal("Jenny's"); expect(user.phones[2].type).to.equal("Jenny's");
expect(user.phones[3].type).to.equal('"home"'); expect(user.phones[3].type).to.equal('"home"');
done();
}) })
.error(done)
}) })
.error(done)
}) })
it('should bulkCreate with hstore property', function(done) { it('should bulkCreate with hstore property', function() {
var User = this.User; var User = this.User;
this.User.bulkCreate([{ return this.User.bulkCreate([{
username: 'bob', username: 'bob',
email: ['myemail@email.com'], email: ['myemail@email.com'],
settings: {mailing: true, push: 'facebook', frequency: 3} settings: {mailing: true, push: 'facebook', frequency: 3}
}]) }]).then(function(){
.success(function(){ return User.find(1).then(function(user){
User.find(1)
.success(function(user){
expect(user.settings.mailing).to.equal("true") expect(user.settings.mailing).to.equal("true")
done()
}) })
.error(done)
}) })
.error(done)
}) })
it("should update hstore correctly", function(done) { it("should update hstore correctly", function() {
var self = this var self = this;
this.User return this.User.create({ username: 'user', email: ['foo@bar.com'], settings: { test: '"value"' }}).then(function(newUser) {
.create({ username: 'user', email: ['foo@bar.com'], settings: { test: '"value"' }})
.success(function(newUser) {
// Check to see if the default value for an hstore field works // Check to see if the default value for an hstore field works
expect(newUser.document).to.deep.equal({default: "'value'"}) expect(newUser.document).to.deep.equal({default: "'value'"})
expect(newUser.settings).to.deep.equal({ test: '"value"' }) expect(newUser.settings).to.deep.equal({ test: '"value"' })
// Check to see if updating an hstore field works // Check to see if updating an hstore field works
self.User.update({settings: {should: 'update', to: 'this', first: 'place'}}, {where: newUser.identifiers}).success(function() { return self.User.update({settings: {should: 'update', to: 'this', first: 'place'}}, {where: newUser.identifiers}).then(function() {
newUser.reload().success(function() { return newUser.reload().success(function() {
// Postgres always returns keys in alphabetical order (ascending) // Postgres always returns keys in alphabetical order (ascending)
expect(newUser.settings).to.deep.equal({first: 'place', should: 'update', to: 'this'}) expect(newUser.settings).to.deep.equal({first: 'place', should: 'update', to: 'this'})
done() })
});
}) })
}) })
.error(console.log)
}) })
it("should update hstore correctly and return the affected rows", function(done) { it("should update hstore correctly and return the affected rows", function() {
var self = this var self = this
this.User return this.User.create({ username: 'user', email: ['foo@bar.com'], settings: { test: '"value"' }}).then(function(oldUser) {
.create({ username: 'user', email: ['foo@bar.com'], settings: { test: '"value"' }})
.success(function(oldUser) {
// Update the user and check that the returned object's fields have been parsed by the hstore library // Update the user and check that the returned object's fields have been parsed by the hstore library
self.User.update({settings: {should: 'update', to: 'this', first: 'place'}}, {where: oldUser.identifiers, returning: true }).spread(function(count, users) { return self.User.update({settings: {should: 'update', to: 'this', first: 'place'}}, {where: oldUser.identifiers, returning: true }).spread(function(count, users) {
expect(count).to.equal(1); expect(count).to.equal(1);
expect(users[0].settings).to.deep.equal({should: 'update', to: 'this', first: 'place'}) expect(users[0].settings).to.deep.equal({should: 'update', to: 'this', first: 'place'})
done()
}) })
}) })
.error(console.log)
}) })
it("should read hstore correctly", function(done) { it("should read hstore correctly", function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!