Skip to content

Commit 9abe7f1

Browse files
committed
Test & fix search
1 parent 5bf30af commit 9abe7f1

File tree

7 files changed

+63
-28
lines changed

7 files changed

+63
-28
lines changed

Diff for: api/src/controllers/movies.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,13 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
317317
.where(
318318
and(
319319
filter,
320-
query ? sql`${query}::text %> ${showTranslations.name}` : undefined,
320+
query ? sql`${transQ.name} %> ${query}::text` : undefined,
321321
keysetPaginate({ table: shows, after, sort }),
322322
),
323323
)
324324
.orderBy(
325325
...(query
326-
? [sql`word_similarity(${query}::text, ${showTranslations.name})`]
326+
? [sql`word_similarity(${query}::text, ${transQ.name})`]
327327
: sortToSql(sort, shows)),
328328
shows.pk,
329329
)

Diff for: api/src/db/index.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
import { sql } from "drizzle-orm";
12
import { drizzle } from "drizzle-orm/node-postgres";
3+
import { migrate as migrateDb } from "drizzle-orm/node-postgres/migrator";
24
import * as schema from "./schema";
35

6+
const dbConfig = {
7+
user: process.env.POSTGRES_USER ?? "kyoo",
8+
password: process.env.POSTGRES_PASSWORD ?? "password",
9+
database: process.env.POSTGRES_DB ?? "kyooDB",
10+
host: process.env.POSTGRES_SERVER ?? "postgres",
11+
port: Number(process.env.POSTGRES_PORT) || 5432,
12+
ssl: false,
13+
};
414
export const db = drizzle({
515
schema,
6-
connection: {
7-
user: process.env.POSTGRES_USER ?? "kyoo",
8-
password: process.env.POSTGRES_PASSWORD ?? "password",
9-
database: process.env.POSTGRES_DB ?? "kyooDB",
10-
host: process.env.POSTGRES_SERVER ?? "postgres",
11-
port: Number(process.env.POSTGRES_PORT) || 5432,
12-
ssl: false,
13-
},
16+
connection: dbConfig,
1417
casing: "snake_case",
1518
});
19+
20+
export const migrate = async () => {
21+
await db.execute(
22+
sql.raw(
23+
`ALTER DATABASE "${dbConfig.database}" SET pg_trgm.word_similarity_threshold = 0.4;`,
24+
),
25+
);
26+
await migrateDb(db, {
27+
migrationsSchema: "kyoo",
28+
migrationsFolder: "./drizzle",
29+
});
30+
};

Diff for: api/src/db/schema/utils.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import { customType, jsonb, pgSchema, varchar } from "drizzle-orm/pg-core";
1+
import { jsonb, pgSchema, varchar } from "drizzle-orm/pg-core";
22

33
export const schema = pgSchema("kyoo");
44

55
export const language = () => varchar({ length: 255 });
66

77
export const image = () =>
88
jsonb().$type<{ id: string; source: string; blurhash: string }>();
9-
10-
// idk why they didn't implement this one
11-
export const tsvector = customType<{ data: string }>({
12-
dataType() {
13-
return "tsvector";
14-
},
15-
});

Diff for: api/src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import jwt from "@elysiajs/jwt";
22
import { swagger } from "@elysiajs/swagger";
3-
import { migrate } from "drizzle-orm/node-postgres/migrator";
43
import { Elysia } from "elysia";
54
import { base } from "./base";
65
import { entries } from "./controllers/entries";
@@ -9,11 +8,11 @@ import { seasons } from "./controllers/seasons";
98
import { seed } from "./controllers/seed";
109
import { series } from "./controllers/series";
1110
import { videos } from "./controllers/videos";
12-
import { db } from "./db";
11+
import { migrate } from "./db";
1312
import { Image } from "./models/utils";
1413
import { comment } from "./utils";
1514

16-
await migrate(db, { migrationsSchema: "kyoo", migrationsFolder: "./drizzle" });
15+
await migrate();
1716

1817
let secret = process.env.JWT_SECRET;
1918
if (!secret) {

Diff for: api/tests/movies/get-all-movies.test.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,38 @@ describe("Get all movies", () => {
280280
const [resp, body] = await getMovies({
281281
limit: 2,
282282
filter: "tags eq gravity",
283-
preferOriginal: true,
283+
});
284+
285+
expectStatus(resp, body).toBe(200);
286+
expect(body.items).toBeArrayOfSize(1);
287+
expect(body.items[0].slug).toBe(bubble.slug);
288+
});
289+
});
290+
291+
describe("search", () => {
292+
it("Partial match", async () => {
293+
const [resp, body] = await getMovies({
294+
limit: 2,
295+
query: "bub",
296+
});
297+
298+
expectStatus(resp, body).toBe(200);
299+
expect(body.items).toBeArrayOfSize(1);
300+
expect(body.items[0].slug).toBe(bubble.slug);
301+
});
302+
it("Invalid search don't match", async () => {
303+
const [resp, body] = await getMovies({
304+
limit: 2,
305+
query: "buboeuoeunhoeu",
306+
});
307+
308+
expectStatus(resp, body).toBe(200);
309+
expect(body.items).toBeArrayOfSize(0);
310+
});
311+
it("Typo match", async () => {
312+
const [resp, body] = await getMovies({
313+
limit: 2,
314+
query: "bobble",
284315
});
285316

286317
expectStatus(resp, body).toBe(200);

Diff for: api/tests/movies/movies-helper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const getMovies = async ({
3333
limit?: number;
3434
after?: string;
3535
sort?: string | string[];
36+
query?: string;
3637
langs?: string;
3738
preferOriginal?: boolean;
3839
}) => {

Diff for: api/tests/setup.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { beforeAll } from "bun:test";
2-
import { migrate } from "drizzle-orm/node-postgres/migrator";
3-
import { db } from "~/db";
2+
import { migrate } from "~/db";
43

54
beforeAll(async () => {
6-
await migrate(db, {
7-
migrationsSchema: "kyoo",
8-
migrationsFolder: "./drizzle",
9-
});
5+
await migrate();
106
});

0 commit comments

Comments
 (0)