fixed tests, typos and optimized code for ratings

This commit is contained in:
Michael C
2021-11-15 01:17:36 -05:00
parent bc6db0d109
commit b9ebd00365
3 changed files with 80 additions and 34 deletions

View File

@@ -33,12 +33,12 @@ export async function getRating(req: Request, res: Response): Promise<Response>
: [req.query.type] : [req.query.type]
: [RatingType.Upvote, RatingType.Downvote]; : [RatingType.Upvote, RatingType.Downvote];
if (!Array.isArray(types)) { if (!Array.isArray(types)) {
return res.status(400).send("Categories parameter does not match format requirements."); return res.status(400).send("Types parameter does not match format requirements.");
} }
types = types.map((type) => parseInt(type as unknown as string, 10)); types = types.map((type) => parseInt(type as unknown as string, 10));
} catch(error) { } catch(error) {
return res.status(400).send("Bad parameter: categories (invalid JSON)"); return res.status(400).send("Bad parameter: types (invalid JSON)");
} }
const service: Service = getService(req.query.service, req.body.service); const service: Service = getService(req.query.service, req.body.service);
@@ -53,20 +53,15 @@ export async function getRating(req: Request, res: Response): Promise<Response>
type: rating.type, type: rating.type,
count: rating.count count: rating.count
})); }));
return res.status((ratings.length) ? 200 : 404)
if (ratings) { .send(ratings ?? []);
res.status(200);
} else {
res.status(404);
}
return res.send(ratings ?? []);
} catch (err) { } catch (err) {
Logger.error(err as string); Logger.error(err as string);
return res.sendStatus(500); return res.sendStatus(500);
} }
} }
async function getRatings(hashPrefix: VideoIDHash, service: Service): Promise<DBRating[]> { function getRatings(hashPrefix: VideoIDHash, service: Service): Promise<DBRating[]> {
const fetchFromDB = () => db const fetchFromDB = () => db
.prepare( .prepare(
"all", "all",
@@ -74,9 +69,7 @@ async function getRatings(hashPrefix: VideoIDHash, service: Service): Promise<DB
[`${hashPrefix}%`, service] [`${hashPrefix}%`, service]
) as Promise<DBRating[]>; ) as Promise<DBRating[]>;
if (hashPrefix.length === 4) { return (hashPrefix.length === 4)
return await QueryCacher.get(fetchFromDB, ratingHashKey(hashPrefix, service)); ? QueryCacher.get(fetchFromDB, ratingHashKey(hashPrefix, service))
} : fetchFromDB();
return fetchFromDB();
} }

View File

@@ -8,15 +8,19 @@ import { partialDeepEquals } from "../../utils/partialDeepEquals";
const endpoint = "/api/ratings/rate/"; const endpoint = "/api/ratings/rate/";
const getRating = (hash: string, params?: unknown): Promise<AxiosResponse> => client.get(endpoint + hash, { params }); const getRating = (hash: string, params?: unknown): Promise<AxiosResponse> => client.get(endpoint + hash, { params });
const videoOneID = "some-likes-and-dislikes";
const videoOneIDHash = getHash(videoOneID, 1);
const videoOnePartialHash = videoOneIDHash.substr(0, 4);
describe("getRating", () => { describe("getRating", () => {
before(async () => { before(async () => {
const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)'; const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
await db.prepare("run", insertUserNameQuery, ["some-likes-and-dislikes", "YouTube", 0, 5, getHash("some-likes-and-dislikes", 1)]); //b3f0 await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 0, 5, videoOneIDHash]);
await db.prepare("run", insertUserNameQuery, ["some-likes-and-dislikes", "YouTube", 1, 10, getHash("some-likes-and-dislikes", 1)]); await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 1, 10, videoOneIDHash]);
}); });
it("Should be able to get dislikes and likes by default", (done) => { it("Should be able to get dislikes and likes by default", (done) => {
getRating("b3f0") getRating(videoOnePartialHash)
.then(res => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const expected = [{ const expected = [{
@@ -33,7 +37,7 @@ describe("getRating", () => {
}); });
it("Should be able to filter for only dislikes", (done) => { it("Should be able to filter for only dislikes", (done) => {
getRating("b3f0", { type: 0 }) getRating(videoOnePartialHash, { type: 0 })
.then(res => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const expected = [{ const expected = [{
@@ -46,4 +50,31 @@ describe("getRating", () => {
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should return 400 for invalid hash", (done) => {
getRating("a")
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should return 404 for nonexitent type", (done) => {
getRating(videoOnePartialHash, { type: 100 })
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
});
it("Should return 404 for nonexistent videoID", (done) => {
getRating("aaaa")
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
});
}); });

View File

@@ -9,37 +9,43 @@ const endpoint = "/api/ratings/rate/";
const postRating = (body: unknown): Promise<AxiosResponse> => client.post(endpoint, body); const postRating = (body: unknown): Promise<AxiosResponse> => client.post(endpoint, body);
const queryDatabase = (videoID: string) => db.prepare("all", `SELECT * FROM "ratings" WHERE "videoID" = ?`, [videoID]); const queryDatabase = (videoID: string) => db.prepare("all", `SELECT * FROM "ratings" WHERE "videoID" = ?`, [videoID]);
const videoIDOne = "normal-video";
const videoIDTwo = "multiple-rates";
const ratingUserID = "rating-testman";
describe("postRating", () => { describe("postRating", () => {
before(async () => { before(async () => {
const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)'; const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
await db.prepare("run", insertUserNameQuery, ["multiple-rates", "YouTube", 0, 3, getHash("multiple-rates", 1)]); await db.prepare("run", insertUserNameQuery, [videoIDTwo, "YouTube", 0, 3, getHash(videoIDTwo, 1)]);
}); });
it("Should be able to vote on a video", (done) => { it("Should be able to vote on a video", (done) => {
const videoID = videoIDOne;
postRating({ postRating({
userID: "rating-testman", userID: ratingUserID,
videoID: "normal-video", videoID,
type: 0 type: 0
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const expected = [{ const expected = [{
hashedVideoID: getHash("normal-video", 1), hashedVideoID: getHash(videoID, 1),
videoID: "normal-video", videoID,
type: 0, type: 0,
count: 1, count: 1,
service: "YouTube" service: "YouTube"
}]; }];
assert.ok(partialDeepEquals(await queryDatabase("normal-video"), expected)); assert.ok(partialDeepEquals(await queryDatabase(videoID), expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to undo a vote on a video", (done) => { it("Should be able to undo a vote on a video", (done) => {
const videoID = videoIDOne;
postRating({ postRating({
userID: "rating-testman", userID: ratingUserID,
videoID: "normal-video", videoID,
type: 0, type: 0,
enabled: false enabled: false
}) })
@@ -49,16 +55,17 @@ describe("postRating", () => {
type: 0, type: 0,
count: 0 count: 0
}]; }];
assert.ok(partialDeepEquals(await queryDatabase("normal-video"), expected)); assert.ok(partialDeepEquals(await queryDatabase(videoID), expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to vote after someone else on a video", (done) => { it("Should be able to vote after someone else on a video", (done) => {
const videoID = videoIDTwo;
postRating({ postRating({
userID: "rating-testman", userID: ratingUserID,
videoID: "multiple-rates", videoID,
type: 0 type: 0
}) })
.then(async res => { .then(async res => {
@@ -67,16 +74,17 @@ describe("postRating", () => {
type: 0, type: 0,
count: 4 count: 4
}]; }];
assert.ok(partialDeepEquals(await queryDatabase("multiple-rates"), expected)); assert.ok(partialDeepEquals(await queryDatabase(videoID), expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to vote a different type than existing votes on a video", (done) => { it("Should be able to vote a different type than existing votes on a video", (done) => {
const videoID = videoIDTwo;
postRating({ postRating({
userID: "rating-testman", userID: ratingUserID,
videoID: "multiple-rates", videoID,
type: 1 type: 1
}) })
.then(async res => { .then(async res => {
@@ -88,7 +96,21 @@ describe("postRating", () => {
type: 1, type: 1,
count: 1 count: 1
}]; }];
assert.ok(partialDeepEquals(await queryDatabase("multiple-rates"), expected)); assert.ok(partialDeepEquals(await queryDatabase(videoID), expected));
done();
})
.catch(err => done(err));
});
it("Should not be able to vote with nonexistent type", (done) => {
const videoID = videoIDOne;
postRating({
userID: ratingUserID,
videoID,
type: 100
})
.then(res => {
assert.strictEqual(res.status, 400);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));