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

@@ -8,15 +8,19 @@ import { partialDeepEquals } from "../../utils/partialDeepEquals";
const endpoint = "/api/ratings/rate/";
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", () => {
before(async () => {
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, ["some-likes-and-dislikes", "YouTube", 1, 10, getHash("some-likes-and-dislikes", 1)]);
await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 0, 5, videoOneIDHash]);
await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 1, 10, videoOneIDHash]);
});
it("Should be able to get dislikes and likes by default", (done) => {
getRating("b3f0")
getRating(videoOnePartialHash)
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
@@ -33,7 +37,7 @@ describe("getRating", () => {
});
it("Should be able to filter for only dislikes", (done) => {
getRating("b3f0", { type: 0 })
getRating(videoOnePartialHash, { type: 0 })
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
@@ -46,4 +50,31 @@ describe("getRating", () => {
})
.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));
});
});