Skip to content

Commit 726983b

Browse files
committed
getChapterNames
- remove identifier from segmentGen - add multiGenRandomValue - add videoInfo query
1 parent 7364499 commit 726983b

File tree

5 files changed

+60
-62
lines changed

5 files changed

+60
-62
lines changed

test/cases/getChapterNames.ts

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,43 @@ import { db } from "../../src/databases/databases";
33
import { Postgres } from "../../src/databases/Postgres";
44
import { client } from "../utils/httpClient";
55
import { partialDeepEquals } from "../utils/partialDeepEquals";
6-
7-
// Only works with Postgres
8-
if (db instanceof Postgres) {
9-
10-
describe("getChapterNames", function () {
11-
const endpoint = "/api/chapterNames";
12-
13-
const chapterNamesVid1 = "chapterNamesVid";
14-
const chapterChannelID = "chapterChannelID";
15-
16-
before(async () => {
17-
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
18-
await db.prepare("run", query, [chapterNamesVid1, 60, 80, 2, 0, "chapterNamesVid-1", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "Weird name"]);
19-
await db.prepare("run", query, [chapterNamesVid1, 70, 75, 2, 0, "chapterNamesVid-2", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "A different one"]);
20-
await db.prepare("run", query, [chapterNamesVid1, 71, 76, 2, 0, "chapterNamesVid-3", "testman", 0, 50, "chapter", "chapter", "YouTube", 0, 0, 0, "Something else"]);
21-
22-
await db.prepare("run", `INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published")
23-
SELECT ?, ?, ?, ?`, [
24-
chapterNamesVid1, chapterChannelID, "", 0
25-
]);
26-
});
27-
28-
it("Search for 'weird'", async () => {
29-
const result = await client.get(`${endpoint}?description=weird&channelID=${chapterChannelID}`);
30-
const expected = [{
31-
description: "Weird name",
32-
}];
33-
34-
assert.strictEqual(result.status, 200);
35-
assert.strictEqual(result.data.length, 3);
36-
assert.ok(partialDeepEquals(result.data, expected));
37-
});
38-
39-
it("Search for 'different'", async () => {
40-
const result = await client.get(`${endpoint}?description=different&channelID=${chapterChannelID}`);
41-
const expected = [{
42-
description: "A different one",
43-
}];
44-
45-
assert.strictEqual(result.status, 200);
46-
assert.strictEqual(result.data.length, 3);
47-
assert.ok(partialDeepEquals(result.data, expected));
48-
});
49-
50-
it("Search for 'something'", async () => {
51-
const result = await client.get(`${endpoint}?description=something&channelID=${chapterChannelID}`);
52-
const expected = [{
53-
description: "Something else",
54-
}];
55-
56-
assert.strictEqual(result.status, 200);
57-
assert.strictEqual(result.data.length, 3);
58-
assert.ok(partialDeepEquals(result.data, expected));
59-
});
6+
import { insertChapter } from "../utils/segmentQueryGen";
7+
import { genRandomValue } from "../utils/getRandom";
8+
import { insertVideoInfo } from "../utils/queryGen";
9+
10+
describe("getChapterNames", function () {
11+
const endpoint = "/api/chapterNames";
12+
13+
const chapterNamesVid1 = genRandomValue("video", "getChapterNames");
14+
const chapterChannelID = genRandomValue("channel", "getChapterNames");
15+
const chapterNames = [
16+
"Weird name",
17+
"A different one",
18+
"Something else",
19+
];
20+
21+
const nameSearch = (query: string, expected: string): Promise<void> => {
22+
const expectedData = [{
23+
description: expected
24+
}];
25+
return client.get(`${endpoint}?description=${query}&channelID=${chapterChannelID}`)
26+
.then(res => {
27+
assert.strictEqual(res.status, 200);
28+
assert.strictEqual(res.data.length, 1);
29+
assert.ok(partialDeepEquals(res.data, expectedData));
30+
});
31+
};
32+
33+
before(async function() {
34+
if (!(db instanceof Postgres)) this.skip(); // only works with Postgres
35+
await insertChapter(db, chapterNames[0], { videoID: chapterNamesVid1, startTime: 60, endTime: 80 });
36+
await insertChapter(db, chapterNames[1], { videoID: chapterNamesVid1, startTime: 70, endTime: 75 });
37+
await insertChapter(db, chapterNames[2], { videoID: chapterNamesVid1, startTime: 71, endTime: 76 });
38+
39+
await insertVideoInfo(db, chapterNamesVid1, chapterChannelID);
6040
});
61-
}
41+
42+
it("Search for 'weird'", () => nameSearch("weird", chapterNames[0]));
43+
it("Search for 'different'", () => nameSearch("different", chapterNames[1]));
44+
it("Search for 'something'", () => nameSearch("something", chapterNames[2]));
45+
});

test/cases/postClearCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const users = genUsers("postClearCache", cases);
1616
describe("postClearCache", () => {
1717
before(async () => {
1818
await insertVip(db, users["vip"].pubID);
19-
await insertSegment(db, "clearSegments", "clear-test", { videoID: "clear-test" });
19+
await insertSegment(db, { videoID: "clear-test" });
2020
});
2121

2222
it("Should be able to clear cache for existing video", () =>

test/utils/getRandom.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import crypto from "crypto";
22

3-
export const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
3+
export const genRandom = (bytes=8): string => crypto.pseudoRandomBytes(bytes).toString("hex");
44

5-
export const genRandomValue = (prefix: string, identifier: string, bytes=8) => `${prefix}-${identifier}-${genRandom(bytes)}`;
5+
export const genRandomValue = (prefix: string, identifier: string, bytes=8): string => `${prefix}-${identifier}-${genRandom(bytes)}`;
6+
export const multiGenRandomValue = (prefix: string, identifier: string, count: number, bytes=8): string[] => {
7+
const arr: string[] = [];
8+
for (let i = 0; i < count; i++) arr.push(genRandomValue(prefix, identifier, bytes));
9+
return arr;
10+
};

test/utils/queryGen.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray
3939
for (const user of Object.values(users))
4040
await insertUsername(db, user.pubID, user.username, false);
4141
};
42+
43+
// videoInfo
44+
export const insertVideoInfo = async (db: IDatabase, videoID: string, channelID: string, title = "", published = 0) => {
45+
const query = 'INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published") VALUES(?, ?, ?, ?)';
46+
await db.prepare("run", query, [videoID, channelID, title, published]);
47+
};

test/utils/segmentQueryGen.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IDatabase } from "../../src/databases/IDatabase";
22
import { Service, VideoIDHash, VideoID } from "../../src/types/segments.model";
33
import { HashedUserID, UserID } from "../../src/types/user.model";
4-
import { genRandomValue } from "./getRandom";
4+
import { genRandom, genRandomValue } from "./getRandom";
55
import { getHash } from "../../src/utils/getHash";
66

77
type insertSegmentParams = {
@@ -43,12 +43,11 @@ const defaultSegmentParams: insertSegmentParams = {
4343
description: ""
4444
};
4545

46-
// sponsorTimes
47-
export const insertSegment = async(db: IDatabase, fnname: string, testcase: string, params: insertSegmentParams = {}) => {
46+
export const insertSegment = async(db: IDatabase, params: insertSegmentParams = {}) => {
4847
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID", "description") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
4948
// corrections for parameters
50-
const identifier = `${fnname}-${testcase}`;
5149
const correctedParams = { ...defaultSegmentParams, ...params };
50+
const identifier = genRandom(7); // 7 to fill out videoID
5251
// generate defaults
5352
const videoID = (params.videoID || `vid-${identifier}`) as VideoID;
5453
const userID = (params.userID || `user-${identifier}`) as UserID;
@@ -61,4 +60,8 @@ export const insertSegment = async(db: IDatabase, fnname: string, testcase: stri
6160
correctedParams.hidden = Number(correctedParams.hidden);
6261
correctedParams.shadowHidden = Number(correctedParams.shadowHidden);
6362
await db.prepare("run", query, Object.values(correctedParams));
63+
};
64+
export const insertChapter = async(db: IDatabase, description: string, params: insertSegmentParams = {}) => {
65+
const overrides = { category: "chapter", actionType: "chapter", description, ...params };
66+
await insertSegment(db, overrides);
6467
};

0 commit comments

Comments
 (0)