From edf9fe3b7fdaf4cca972a20bd3f876a358e9cdb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 9 May 2020 10:38:56 -0400 Subject: [PATCH] feat: add null tests for sorted sets --- src/database/mongo/sorted/add.js | 6 +++++- src/database/postgres/sorted/add.js | 6 +++++- src/database/redis/sorted/add.js | 6 +++++- test/database/sorted.js | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/database/mongo/sorted/add.js b/src/database/mongo/sorted/add.js index 6dc206b953..f576a27100 100644 --- a/src/database/mongo/sorted/add.js +++ b/src/database/mongo/sorted/add.js @@ -52,7 +52,8 @@ module.exports = function (module) { return; } const isArrayOfScores = Array.isArray(scores); - if (!isArrayOfScores && !utils.isNumber(scores)) { + if ((!isArrayOfScores && !utils.isNumber(scores)) || + (isArrayOfScores && scores.map(s => utils.isNumber(s)).includes(false))) { throw new Error('[[error:invalid-score, ' + scores + ']]'); } @@ -75,6 +76,9 @@ module.exports = function (module) { } var bulk = module.client.collection('objects').initializeUnorderedBulkOp(); data.forEach(function (item) { + if (!utils.isNumber(item[1])) { + throw new Error('[[error:invalid-score, ' + item[1] + ']]'); + } bulk.find({ _key: item[0], value: String(item[2]) }).upsert().updateOne({ $set: { score: parseFloat(item[1]) } }); }); await bulk.execute(); diff --git a/src/database/postgres/sorted/add.js b/src/database/postgres/sorted/add.js index 392482dbdd..32dc2f6325 100644 --- a/src/database/postgres/sorted/add.js +++ b/src/database/postgres/sorted/add.js @@ -69,7 +69,8 @@ DO UPDATE SET "score" = EXCLUDED."score"`, return; } const isArrayOfScores = Array.isArray(scores); - if (!isArrayOfScores && !utils.isNumber(scores)) { + if ((!isArrayOfScores && !utils.isNumber(scores)) || + (isArrayOfScores && scores.map(s => utils.isNumber(s)).includes(false))) { throw new Error('[[error:invalid-score, ' + scores + ']]'); } @@ -108,6 +109,9 @@ INSERT INTO "legacy_zset" ("_key", "value", "score") const values = []; const scores = []; data.forEach(function (item) { + if (!utils.isNumber(item[1])) { + throw new Error('[[error:invalid-score, ' + item[1] + ']]'); + } keys.push(item[0]); scores.push(item[1]); values.push(item[2]); diff --git a/src/database/redis/sorted/add.js b/src/database/redis/sorted/add.js index b4f28de949..f8d5c4bd8c 100644 --- a/src/database/redis/sorted/add.js +++ b/src/database/redis/sorted/add.js @@ -42,7 +42,8 @@ module.exports = function (module) { return; } const isArrayOfScores = Array.isArray(scores); - if (!isArrayOfScores && !utils.isNumber(scores)) { + if ((!isArrayOfScores && !utils.isNumber(scores)) || + (isArrayOfScores && scores.map(s => utils.isNumber(s)).includes(false))) { throw new Error('[[error:invalid-score, ' + scores + ']]'); } @@ -65,6 +66,9 @@ module.exports = function (module) { } var batch = module.client.batch(); data.forEach(function (item) { + if (!utils.isNumber(item[1])) { + throw new Error('[[error:invalid-score, ' + item[1] + ']]'); + } batch.zadd(item[0], item[1], item[2]); }); await helpers.execBatch(batch); diff --git a/test/database/sorted.js b/test/database/sorted.js index 73cc7714e2..922c70a9cb 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -118,6 +118,18 @@ describe('Sorted Set methods', function () { done(); }); }); + + it('should error if scores has null', async function () { + let err; + try { + await db.sortedSetsAdd(['sorted1', 'sorted2'], [1, null], 'dontadd'); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:invalid-score, 1,]]'); + assert.strictEqual(await db.isSortedSetMember('sorted1', 'dontadd'), false); + assert.strictEqual(await db.isSortedSetMember('sorted2', 'dontadd'), false); + }); }); describe('sortedSetAddMulti()', function () { @@ -146,6 +158,21 @@ describe('Sorted Set methods', function () { done(); }); }); + + it('should error if score is null', async function () { + let err; + try { + await db.sortedSetAddBulk([ + ['bulk4', 0, 'dontadd'], + ['bulk5', null, 'dontadd'], + ]); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:invalid-score, null]]'); + assert.strictEqual(await db.isSortedSetMember('bulk4', 'dontadd'), false); + assert.strictEqual(await db.isSortedSetMember('bulk5', 'dontadd'), false); + }); }); describe('getSortedSetRange()', function () {