getChapterNames

- remove identifier from segmentGen
- add multiGenRandomValue
- add videoInfo query
This commit is contained in:
Michael C
2023-09-27 23:53:18 -04:00
parent 7364499f11
commit 726983bb9b
5 changed files with 55 additions and 57 deletions

View File

@@ -3,59 +3,43 @@ import { db } from "../../src/databases/databases";
import { Postgres } from "../../src/databases/Postgres"; import { Postgres } from "../../src/databases/Postgres";
import { client } from "../utils/httpClient"; import { client } from "../utils/httpClient";
import { partialDeepEquals } from "../utils/partialDeepEquals"; import { partialDeepEquals } from "../utils/partialDeepEquals";
import { insertChapter } from "../utils/segmentQueryGen";
import { genRandomValue } from "../utils/getRandom";
import { insertVideoInfo } from "../utils/queryGen";
// Only works with Postgres describe("getChapterNames", function () {
if (db instanceof Postgres) {
describe("getChapterNames", function () {
const endpoint = "/api/chapterNames"; const endpoint = "/api/chapterNames";
const chapterNamesVid1 = "chapterNamesVid"; const chapterNamesVid1 = genRandomValue("video", "getChapterNames");
const chapterChannelID = "chapterChannelID"; const chapterChannelID = genRandomValue("channel", "getChapterNames");
const chapterNames = [
"Weird name",
"A different one",
"Something else",
];
before(async () => { const nameSearch = (query: string, expected: string): Promise<void> => {
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; const expectedData = [{
await db.prepare("run", query, [chapterNamesVid1, 60, 80, 2, 0, "chapterNamesVid-1", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "Weird name"]); description: expected
await db.prepare("run", query, [chapterNamesVid1, 70, 75, 2, 0, "chapterNamesVid-2", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "A different one"]);
await db.prepare("run", query, [chapterNamesVid1, 71, 76, 2, 0, "chapterNamesVid-3", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "Something else"]);
await db.prepare("run", `INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published")
SELECT ?, ?, ?, ?`, [
chapterNamesVid1, chapterChannelID, "", 0
]);
});
it("Search for 'weird'", async () => {
const result = await client.get(`${endpoint}?description=weird&channelID=${chapterChannelID}`);
const expected = [{
description: "Weird name",
}]; }];
return client.get(`${endpoint}?description=${query}&channelID=${chapterChannelID}`)
.then(res => {
assert.strictEqual(res.status, 200);
assert.strictEqual(res.data.length, 1);
assert.ok(partialDeepEquals(res.data, expectedData));
});
};
assert.strictEqual(result.status, 200); before(async function() {
assert.strictEqual(result.data.length, 3); if (!(db instanceof Postgres)) this.skip(); // only works with Postgres
assert.ok(partialDeepEquals(result.data, expected)); await insertChapter(db, chapterNames[0], { videoID: chapterNamesVid1, startTime: 60, endTime: 80 });
await insertChapter(db, chapterNames[1], { videoID: chapterNamesVid1, startTime: 70, endTime: 75 });
await insertChapter(db, chapterNames[2], { videoID: chapterNamesVid1, startTime: 71, endTime: 76 });
await insertVideoInfo(db, chapterNamesVid1, chapterChannelID);
}); });
it("Search for 'different'", async () => { it("Search for 'weird'", () => nameSearch("weird", chapterNames[0]));
const result = await client.get(`${endpoint}?description=different&channelID=${chapterChannelID}`); it("Search for 'different'", () => nameSearch("different", chapterNames[1]));
const expected = [{ it("Search for 'something'", () => nameSearch("something", chapterNames[2]));
description: "A different one", });
}];
assert.strictEqual(result.status, 200);
assert.strictEqual(result.data.length, 3);
assert.ok(partialDeepEquals(result.data, expected));
});
it("Search for 'something'", async () => {
const result = await client.get(`${endpoint}?description=something&channelID=${chapterChannelID}`);
const expected = [{
description: "Something else",
}];
assert.strictEqual(result.status, 200);
assert.strictEqual(result.data.length, 3);
assert.ok(partialDeepEquals(result.data, expected));
});
});
}

View File

@@ -16,7 +16,7 @@ const users = genUsers("postClearCache", cases);
describe("postClearCache", () => { describe("postClearCache", () => {
before(async () => { before(async () => {
await insertVip(db, users["vip"].pubID); await insertVip(db, users["vip"].pubID);
await insertSegment(db, "clearSegments", "clear-test", { videoID: "clear-test" }); await insertSegment(db, { videoID: "clear-test" });
}); });
it("Should be able to clear cache for existing video", () => it("Should be able to clear cache for existing video", () =>

View File

@@ -1,5 +1,10 @@
import crypto from "crypto"; import crypto from "crypto";
export const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex"); export const genRandom = (bytes=8): string => crypto.pseudoRandomBytes(bytes).toString("hex");
export const genRandomValue = (prefix: string, identifier: string, bytes=8) => `${prefix}-${identifier}-${genRandom(bytes)}`; export const genRandomValue = (prefix: string, identifier: string, bytes=8): string => `${prefix}-${identifier}-${genRandom(bytes)}`;
export const multiGenRandomValue = (prefix: string, identifier: string, count: number, bytes=8): string[] => {
const arr: string[] = [];
for (let i = 0; i < count; i++) arr.push(genRandomValue(prefix, identifier, bytes));
return arr;
};

View File

@@ -39,3 +39,9 @@ export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray
for (const user of Object.values(users)) for (const user of Object.values(users))
await insertUsername(db, user.pubID, user.username, false); await insertUsername(db, user.pubID, user.username, false);
}; };
// videoInfo
export const insertVideoInfo = async (db: IDatabase, videoID: string, channelID: string, title = "", published = 0) => {
const query = 'INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published") VALUES(?, ?, ?, ?)';
await db.prepare("run", query, [videoID, channelID, title, published]);
};

View File

@@ -1,7 +1,7 @@
import { IDatabase } from "../../src/databases/IDatabase"; import { IDatabase } from "../../src/databases/IDatabase";
import { Service, VideoIDHash, VideoID } from "../../src/types/segments.model"; import { Service, VideoIDHash, VideoID } from "../../src/types/segments.model";
import { HashedUserID, UserID } from "../../src/types/user.model"; import { HashedUserID, UserID } from "../../src/types/user.model";
import { genRandomValue } from "./getRandom"; import { genRandom, genRandomValue } from "./getRandom";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
type insertSegmentParams = { type insertSegmentParams = {
@@ -43,12 +43,11 @@ const defaultSegmentParams: insertSegmentParams = {
description: "" description: ""
}; };
// sponsorTimes export const insertSegment = async(db: IDatabase, params: insertSegmentParams = {}) => {
export const insertSegment = async(db: IDatabase, fnname: string, testcase: string, params: insertSegmentParams = {}) => {
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID", "description") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID", "description") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
// corrections for parameters // corrections for parameters
const identifier = `${fnname}-${testcase}`;
const correctedParams = { ...defaultSegmentParams, ...params }; const correctedParams = { ...defaultSegmentParams, ...params };
const identifier = genRandom(7); // 7 to fill out videoID
// generate defaults // generate defaults
const videoID = (params.videoID || `vid-${identifier}`) as VideoID; const videoID = (params.videoID || `vid-${identifier}`) as VideoID;
const userID = (params.userID || `user-${identifier}`) as UserID; const userID = (params.userID || `user-${identifier}`) as UserID;
@@ -62,3 +61,7 @@ export const insertSegment = async(db: IDatabase, fnname: string, testcase: stri
correctedParams.shadowHidden = Number(correctedParams.shadowHidden); correctedParams.shadowHidden = Number(correctedParams.shadowHidden);
await db.prepare("run", query, Object.values(correctedParams)); await db.prepare("run", query, Object.values(correctedParams));
}; };
export const insertChapter = async(db: IDatabase, description: string, params: insertSegmentParams = {}) => {
const overrides = { category: "chapter", actionType: "chapter", description, ...params };
await insertSegment(db, overrides);
};