mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	feat: add null tests for sorted sets
This commit is contained in:
		| @@ -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(); | ||||
|   | ||||
| @@ -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]); | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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 () { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user