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

Commit 4d1d686c by Mick Hansen

test virtual getters aswell

1 parent ef41cb1a
Showing with 36 additions and 5 deletions
...@@ -104,11 +104,17 @@ module.exports = (function() { ...@@ -104,11 +104,17 @@ module.exports = (function() {
var values = {} var values = {}
, key , key
for (key in this.dataValues) { for (key in this._customGetters) {
if (this.dataValues.hasOwnProperty(key)) { if (this._customGetters.hasOwnProperty(key)) {
values[key] = this.get(key) values[key] = this.get(key)
} }
} }
for (key in this.dataValues) {
if (!values.hasOwnProperty(key) && this.dataValues.hasOwnProperty(key)) {
values[key] = this.dataValues[key]
}
}
return values return values
} }
return this.dataValues return this.dataValues
......
...@@ -137,10 +137,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -137,10 +137,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
describe('get', function () { describe('get', function () {
it('should use custom getters in get(key)', function () { it('should use custom attribute getters in get(key)', function () {
var Product = this.sequelize.define('Product', { var Product = this.sequelize.define('Product', {
price: { price: {
type: Sequelize.STRING, type: Sequelize.FLOAT,
get: function() { get: function() {
return this.dataValues['price'] * 100 return this.dataValues['price'] * 100
} }
...@@ -153,6 +153,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -153,6 +153,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
expect(product.get('price')).to.equal(1000) expect(product.get('price')).to.equal(1000)
}) })
it('should custom virtual getters in get(key)', function () {
var Product = this.sequelize.define('Product', {
priceInCents: {
type: Sequelize.FLOAT
}
}, {
getterMethods: {
price: function() {
return this.dataValues['priceInCents'] / 100
}
}
})
var product = Product.build({
priceInCents: 1000
})
expect(product.get('price')).to.equal(10)
})
it('should use custom getters in toJSON', function () { it('should use custom getters in toJSON', function () {
var Product = this.sequelize.define('Product', { var Product = this.sequelize.define('Product', {
price: { price: {
...@@ -161,12 +180,18 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -161,12 +180,18 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
return this.dataValues['price'] * 100 return this.dataValues['price'] * 100
} }
} }
}, {
getterMethods: {
withTaxes: function() {
return this.get('price') * 1.25
}
}
}) })
var product = Product.build({ var product = Product.build({
price: 10 price: 10
}) })
expect(product.toJSON()).to.deep.equal({price: 1000, id: null}) expect(product.toJSON()).to.deep.equal({withTaxes: 1250, price: 1000, id: null})
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!