mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 20:17:02 +03:00
Add test for get bulk rating
This commit is contained in:
@@ -193,6 +193,7 @@ function setupRoutes(router: Router) {
|
|||||||
|
|
||||||
// ratings
|
// ratings
|
||||||
router.get("/api/ratings/rate/:prefix", getRating);
|
router.get("/api/ratings/rate/:prefix", getRating);
|
||||||
|
router.get("/api/ratings/rate", getRating);
|
||||||
router.post("/api/ratings/rate", postRateEndpoints);
|
router.post("/api/ratings/rate", postRateEndpoints);
|
||||||
router.post("/api/ratings/clearCache", ratingPostClearCache);
|
router.post("/api/ratings/clearCache", ratingPostClearCache);
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
|
|||||||
const data = await fetchFromDB();
|
const data = await fetchFromDB();
|
||||||
|
|
||||||
redis.setAsync(key, JSON.stringify(data));
|
redis.setAsync(key, JSON.stringify(data));
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,18 +5,25 @@ import { client } from "../../utils/httpClient";
|
|||||||
import { AxiosResponse } from "axios";
|
import { AxiosResponse } from "axios";
|
||||||
import { partialDeepEquals } from "../../utils/partialDeepEquals";
|
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 getBulkRating = (hashes: string[], params?: any): Promise<AxiosResponse> => client.get(endpoint, { params: { ...params, prefix: hashes } });
|
||||||
|
|
||||||
const videoOneID = "some-likes-and-dislikes";
|
const videoOneID = "some-likes-and-dislikes";
|
||||||
const videoOneIDHash = getHash(videoOneID, 1);
|
const videoOneIDHash = getHash(videoOneID, 1);
|
||||||
const videoOnePartialHash = videoOneIDHash.substr(0, 4);
|
const videoOnePartialHash = videoOneIDHash.substr(0, 4);
|
||||||
|
const videoTwoID = "some-likes-and-dislikes-2";
|
||||||
|
const videoTwoIDHash = getHash(videoTwoID, 1);
|
||||||
|
const videoTwoPartialHash = videoTwoIDHash.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, [videoOneID, "YouTube", 0, 5, videoOneIDHash]);
|
await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 0, 5, videoOneIDHash]);
|
||||||
await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 1, 10, videoOneIDHash]);
|
await db.prepare("run", insertUserNameQuery, [videoOneID, "YouTube", 1, 10, videoOneIDHash]);
|
||||||
|
|
||||||
|
await db.prepare("run", insertUserNameQuery, [videoTwoID, "YouTube", 0, 20, videoTwoIDHash]);
|
||||||
|
await db.prepare("run", insertUserNameQuery, [videoTwoID, "YouTube", 1, 30, videoTwoIDHash]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get dislikes and likes by default", (done) => {
|
it("Should be able to get dislikes and likes by default", (done) => {
|
||||||
@@ -51,6 +58,34 @@ describe("getRating", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to bulk fetch", (done) => {
|
||||||
|
getBulkRating([videoOnePartialHash, videoTwoPartialHash])
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const expected = [{
|
||||||
|
type: 0,
|
||||||
|
count: 20,
|
||||||
|
hash: videoTwoIDHash,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 1,
|
||||||
|
count: 30,
|
||||||
|
hash: videoTwoIDHash,
|
||||||
|
}, {
|
||||||
|
type: 0,
|
||||||
|
count: 5,
|
||||||
|
hash: videoOneIDHash,
|
||||||
|
}, {
|
||||||
|
type: 1,
|
||||||
|
count: 10,
|
||||||
|
hash: videoOneIDHash,
|
||||||
|
}];
|
||||||
|
assert.ok(partialDeepEquals(res.data, expected));
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
it("Should return 400 for invalid hash", (done) => {
|
it("Should return 400 for invalid hash", (done) => {
|
||||||
getRating("a")
|
getRating("a")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
|||||||
Reference in New Issue
Block a user