Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'in (false)' condition expression #159

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ function compare(comp, val, compVals) {
break
case 'IN':
case 'in':
if (!attrVal) return false
if (!compVals.some(function(compVal) {
compType = Object.keys(compVal)[0]
compVal = compVal[compType]
Expand Down
30 changes: 30 additions & 0 deletions test/updateItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,11 @@ describe('updateItem', function() {
UpdateExpression: 'SET #b = :b',
ExpressionAttributeNames: {'#a': 'active', '#b': 'active'},
ExpressionAttributeValues: {':a': {BOOL: false}, ':b': {BOOL: true}},
}, {
ConditionExpression: '#a IN (:a)',
UpdateExpression: 'SET #a = :b',
ExpressionAttributeNames: {'#a': 'active'},
ExpressionAttributeValues: {':a': {BOOL: false}, ':b': {BOOL: true}},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make test cases comprehensive, consider adding all kinds of JS falsy things: false, null, 0, 0.0, ""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added another test for those values. null and 0/0.0 don't fail because they're encoded as {NULL:true} and {N: "0"} respectively, so their "attrVal" becomes true and "0".

The one that confuses me as to how it didn't fail is "" ({S:""}) because I feel like that should have the same issue but doesn't for reasons I cannot understand.

}], function(updateOpts, cb) {
var item = {a: {S: helpers.randomString()}, active: {BOOL: false}}
request(helpers.opts('PutItem', {TableName: helpers.testHashTable, Item: item}), function(err, res) {
Expand All @@ -1469,6 +1474,31 @@ describe('updateItem', function() {
}, done)
})

it('should update when falsey in condition matches', function(done) {
async.forEach([{BOOL:false}, {S:""}, {N: "0.0"}, {"N": "0"}, {NULL: true}], function(value, cb) {
var item = {a: {S: helpers.randomString()}, updated: {BOOL: false}, value: value}
request(helpers.opts('PutItem', {TableName: helpers.testHashTable, Item: item}), function(err, res) {
if (err) return cb(err)
res.statusCode.should.equal(200)
res.body.should.eql({})
request(opts({
TableName: helpers.testHashTable,
Key: {a: item.a},
ReturnValues: 'UPDATED_NEW',
UpdateExpression: 'SET #u = :t',
ConditionExpression: '#v in (:v)',
ExpressionAttributeNames: {'#u': 'updated', '#v': 'value'},
ExpressionAttributeValues: {':t': {BOOL: true}, ':v': value},
}), function(err, res) {
if (err) return cb(err)
res.statusCode.should.equal(200, `Update failed when checking for {${Object.keys(value)[0]}:${Object.values(value)[0]}}`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use JSON.stringify(value) over custom keys/values

res.body.should.eql({Attributes: {updated: {BOOL: true}}})
cb()
})
})
}, done)
})

it('should update values from other attributes', function(done) {
var key = {a: {S: helpers.randomString()}}
request(opts({
Expand Down