add more test on limit, page

This commit is contained in:
Haidang666 2021-12-19 10:13:06 +07:00
parent ec73ae309e
commit 89a83f78cc
2 changed files with 175 additions and 4 deletions

View file

@ -3,6 +3,7 @@ import { db } from "../databases/databases";
import { ActionType, Category, DBSegment, Service, VideoID, SortableFields } from "../types/segments.model";
import { getService } from "../utils/getService";
const maxSegmentsPerPage = 100;
const defaultSegmentsPerPage = 10;
type searchSegmentResponse = {
segmentCount: number,
@ -35,6 +36,26 @@ function getSortField<T extends string>(...value: T[]): SortableFields {
return SortableFields.timeSubmitted;
}
function getLimit<T extends string>(value: T): number {
const limit = Number(value);
if (Number.isInteger(limit)
&& limit >= 1
&& limit <= maxSegmentsPerPage) {
return limit;
}
return defaultSegmentsPerPage;
}
function getPage<T extends string>(value: T): number {
const page = Number(value);
if (Number.isInteger(page) && page >= 0) {
return page;
}
return 0;
}
/**
*
* Returns what would be sent to the client.
@ -78,10 +99,8 @@ async function handleGetSegments(req: Request, res: Response): Promise<searchSeg
const service = getService(req.query.service, req.body.service);
let page: number = req.query.page ?? req.body.page ?? 0;
page = Number(page);
let limit: number = req.query.limit ?? req.body.limit ?? 10;
limit = Number(limit) > maxSegmentsPerPage ? maxSegmentsPerPage : Number(limit);
const page: number = getPage(req.query.page ?? req.body.page);
const limit: number = getLimit(req.query.limit ?? req.body.limit);
const sortBy: SortableFields = getSortField(req.query.sortBy, req.body.sortBy);
const sortDir: string = req.query.sortDir ?? req.body.sortDir ?? "asc";

View file

@ -313,6 +313,140 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to get with over range page", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", limit: 2, page: 2000 } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 2000);
assert.strictEqual(segments.length, 0);
done();
})
.catch(err => done(err));
});
it("Should be able to get with invalid page (=-100)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", page: -100 } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments.length, 10);
done();
})
.catch(err => done(err));
});
it("Should be able to get with invalid page (=text)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", page: "hello" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments.length, 10);
done();
})
.catch(err => done(err));
});
it("Should be use default limit if invalid limit query (=0)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", limit: 0 } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments[0].UUID, "search-page1-1");
assert.strictEqual(segments[1].UUID, "search-page1-2");
assert.strictEqual(segments[2].UUID, "search-page1-3");
assert.strictEqual(segments[3].UUID, "search-page1-4");
assert.strictEqual(segments[4].UUID, "search-page1-5");
assert.strictEqual(segments[5].UUID, "search-page1-6");
assert.strictEqual(segments[6].UUID, "search-page1-7");
assert.strictEqual(segments[7].UUID, "search-page1-8");
assert.strictEqual(segments[8].UUID, "search-page1-9");
assert.strictEqual(segments[9].UUID, "search-page1-10");
done();
})
.catch(err => done(err));
});
it("Should be use default limit if invalid limit query (=-100)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", limit: -100 } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments[0].UUID, "search-page1-1");
assert.strictEqual(segments[1].UUID, "search-page1-2");
assert.strictEqual(segments[2].UUID, "search-page1-3");
assert.strictEqual(segments[3].UUID, "search-page1-4");
assert.strictEqual(segments[4].UUID, "search-page1-5");
assert.strictEqual(segments[5].UUID, "search-page1-6");
assert.strictEqual(segments[6].UUID, "search-page1-7");
assert.strictEqual(segments[7].UUID, "search-page1-8");
assert.strictEqual(segments[8].UUID, "search-page1-9");
assert.strictEqual(segments[9].UUID, "search-page1-10");
done();
})
.catch(err => done(err));
});
it("Should be use default limit if invalid limit query (=text)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", limit: "hello" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments[0].UUID, "search-page1-1");
assert.strictEqual(segments[1].UUID, "search-page1-2");
assert.strictEqual(segments[2].UUID, "search-page1-3");
assert.strictEqual(segments[3].UUID, "search-page1-4");
assert.strictEqual(segments[4].UUID, "search-page1-5");
assert.strictEqual(segments[5].UUID, "search-page1-6");
assert.strictEqual(segments[6].UUID, "search-page1-7");
assert.strictEqual(segments[7].UUID, "search-page1-8");
assert.strictEqual(segments[8].UUID, "search-page1-9");
assert.strictEqual(segments[9].UUID, "search-page1-10");
done();
})
.catch(err => done(err));
});
it("Should be use default limit if invalid limit query (=2000)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", limit: 2000 } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments[0].UUID, "search-page1-1");
assert.strictEqual(segments[1].UUID, "search-page1-2");
assert.strictEqual(segments[2].UUID, "search-page1-3");
assert.strictEqual(segments[3].UUID, "search-page1-4");
assert.strictEqual(segments[4].UUID, "search-page1-5");
assert.strictEqual(segments[5].UUID, "search-page1-6");
assert.strictEqual(segments[6].UUID, "search-page1-7");
assert.strictEqual(segments[7].UUID, "search-page1-8");
assert.strictEqual(segments[8].UUID, "search-page1-9");
assert.strictEqual(segments[9].UUID, "search-page1-10");
done();
})
.catch(err => done(err));
});
it("Should be able to get sorted result (desc)", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", sortBy: "endTime", sortDir: "desc" } })
.then(res => {
@ -348,4 +482,22 @@ describe("getSearchSegments", () => {
})
.catch(err => done(err));
});
it("Should be use default sorted if invalid sort field", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", sortBy: "not exist", sortDir: "desc" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
assert.strictEqual(segments[0].UUID, "search-page1-1");
assert.strictEqual(segments[1].UUID, "search-page1-2");
assert.strictEqual(segments[2].UUID, "search-page1-3");
assert.strictEqual(segments[3].UUID, "search-page1-4");
assert.strictEqual(segments[4].UUID, "search-page1-5");
done();
})
.catch(err => done(err));
});
});