From 50664487b9ae903b6ddf7fdd5db82e5428f84d4a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 12 Feb 2021 20:42:58 -0500 Subject: [PATCH] test: additional tests for topic thumbs --- src/topics/thumbs.js | 12 ++++++++---- test/topicThumbs.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/topics/thumbs.js b/src/topics/thumbs.js index 2e40bf4ae9..6400f5af58 100644 --- a/src/topics/thumbs.js +++ b/src/topics/thumbs.js @@ -67,7 +67,7 @@ async function getThumbs(set) { return thumbs.slice(); } -Thumbs.associate = async function ({ id, path }) { +Thumbs.associate = async function ({ id, path, score }) { // Associates a newly uploaded file as a thumb to the passed-in draft or topic const isDraft = validator.isUUID(String(id)); const isLocal = !path.startsWith('http'); @@ -79,7 +79,7 @@ Thumbs.associate = async function ({ id, path }) { path = path.replace(nconf.get('upload_path'), ''); } const topics = require('.'); - await db.sortedSetAdd(set, numThumbs, path); + await db.sortedSetAdd(set, score || numThumbs, path); if (!isDraft) { const numThumbs = await db.sortedSetCard(set); await topics.setTopicField(id, 'numThumbs', numThumbs); @@ -96,8 +96,12 @@ Thumbs.associate = async function ({ id, path }) { Thumbs.migrate = async function (uuid, id) { // Converts the draft thumb zset to the topic zset (combines thumbs if applicable) const set = `draft:${uuid}:thumbs`; - const thumbs = await db.getSortedSetRange(set, 0, -1); - await Promise.all(thumbs.map(async path => await Thumbs.associate({ id, path }))); + const thumbs = await db.getSortedSetRangeWithScores(set, 0, -1); + await Promise.all(thumbs.map(async thumb => await Thumbs.associate({ + id, + path: thumb.value, + score: thumb.score, + }))); await db.delete(set); cache.del(set); }; diff --git a/test/topicThumbs.js b/test/topicThumbs.js index 7a7f7e3530..d23c225e04 100644 --- a/test/topicThumbs.js +++ b/test/topicThumbs.js @@ -157,6 +157,16 @@ describe('Topic thumbs', () => { assert(exists); }); + it('should have a score equal to the number of thumbs prior to addition', async () => { + const scores = await db.sortedSetScores('topic:2:thumbs', [relativeThumbPaths[0], relativeThumbPaths[2]]); + assert.deepStrictEqual(scores, [0, 1]); + }); + + it('should update the relevant topic hash with the number of thumbnails', async () => { + const numThumbs = await topics.getTopicField(2, 'numThumbs'); + assert.strictEqual(parseInt(numThumbs, 10), 2); + }); + it('should associate the thumbnail with that topic\'s main pid\'s uploads', async () => { const uploads = await posts.uploads.list(mainPid); assert(uploads.includes(path.basename(relativeThumbPaths[0])));