From 606808760edd7f0bf73715ae71a3d365a9c6ae95 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 7 Feb 2022 17:16:50 -0500 Subject: [PATCH] test: add test to verify that a sorted set is automatically deleted if its last element is removed (#10261) * test: add test to verify that a sorted set is automatically deleted if its last element is removed * fix: remote empty zsets when all elements have been removed #yolo * Revert "fix: remote empty zsets when all elements have been removed #yolo" This reverts commit 0ac73244bb6ffd802007a252a35844c589ce8721. * fix: altered behaviour in module.exists instead of zrem --- src/database/postgres/main.js | 7 +++++++ test/database/sorted.js | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/database/postgres/main.js b/src/database/postgres/main.js index a3f784960a..b7c0148aaa 100644 --- a/src/database/postgres/main.js +++ b/src/database/postgres/main.js @@ -17,6 +17,13 @@ module.exports = function (module) { return; } + // Redis/Mongo consider empty zsets as non-existent, match that behaviour + const type = await module.type(key); + if (type === 'zset') { + const members = await module.getSortedSetRange(key, 0, 0); + return members.length > 0; + } + if (Array.isArray(key)) { const res = await module.pool.query({ name: 'existsArray', diff --git a/test/database/sorted.js b/test/database/sorted.js index 9084723477..18cacb660f 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -1085,6 +1085,11 @@ describe('Sorted Set methods', () => { }); }); + it('should not think the sorted set exists if the last element is removed', async () => { + await db.sortedSetRemove('sorted3', 'value1'); + assert.strictEqual(await db.exists('sorted3'), false); + }); + it('should remove multiple values from multiple keys', (done) => { db.sortedSetAdd('multiTest1', [1, 2, 3, 4], ['one', 'two', 'three', 'four'], (err) => { assert.ifError(err);