Skip to content

Commit

Permalink
test: additional tests for topic thumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Feb 13, 2021
1 parent 807b0d4 commit 5066448
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/topics/thumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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);
};
Expand Down
10 changes: 10 additions & 0 deletions test/topicThumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])));
Expand Down

0 comments on commit 5066448

Please sign in to comment.