Sort files chronologically + infinite scroll (#535)

This commit is contained in:
Montassar Ghanmy
2024-07-15 22:56:34 +01:00
committed by GitHub
parent 585da88b0d
commit e76da87916
25 changed files with 719 additions and 153 deletions
@@ -0,0 +1,156 @@
import { describe, beforeAll, afterAll, it, expect } from "@jest/globals";
import { init, TestPlatform } from "../setup";
import UserApi from "../common/user-api";
describe("The Documents Browser Window and API", () => {
let platform: TestPlatform;
let currentUser: UserApi;
beforeAll(async () => {
platform = await init({
services: [
"webserver",
"database",
"applications",
"search",
"storage",
"message-queue",
"user",
"files",
"auth",
"statistics",
"platform-services",
"documents",
],
});
currentUser = await UserApi.getInstance(platform);
});
afterAll(async () => {
await platform?.tearDown();
platform = null;
});
describe("Pagination and Sorting", () => {
it("Should paginate documents correctly", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
let page = 1;
const limit = 2;
let docs = await currentUser.browseDocuments(myDriveId, {
paginate: { page, limit },
});
expect(docs).toBeDefined();
expect(docs.children).toHaveLength(limit);
page = 2;
docs = await currentUser.browseDocuments(myDriveId, {
paginate: { page, limit },
});
expect(docs).toBeDefined();
expect(docs.children).toHaveLength(limit);
page = 3;
docs = await currentUser.browseDocuments(myDriveId, {
paginate: { page, limit },
});
expect(docs).toBeDefined();
expect(docs.children.length).toBeLessThanOrEqual(limit);
});
it("Should sort documents by name in ascending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "name";
const sortOrder = "asc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every((item, i, arr) => !i || arr[i - 1].name <= item.name);
expect(isSorted).toBe(true);
});
it("Should sort documents by name in descending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "name";
const sortOrder = "desc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every((item, i, arr) => !i || arr[i - 1].name >= item.name);
expect(isSorted).toBe(true);
});
it("Should sort documents by date in ascending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "date";
const sortOrder = "asc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every(
(item, i, arr) => !i || new Date(arr[i - 1].added) <= new Date(item.added),
);
expect(isSorted).toBe(true);
});
it("Should sort documents by date in descending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "date";
const sortOrder = "desc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every(
(item, i, arr) => !i || new Date(arr[i - 1].added) >= new Date(item.added),
);
expect(isSorted).toBe(true);
});
it("Should sort documents by size in ascending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "size";
const sortOrder = "asc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every((item, i, arr) => !i || arr[i - 1].size <= item.size);
expect(isSorted).toBe(true);
});
it("Should sort documents by size in descending order", async () => {
const myDriveId = "user_" + currentUser.user.id;
await currentUser.uploadAllFilesOneByOne(myDriveId);
const sortBy = "size";
const sortOrder = "desc";
const docs = await currentUser.browseDocuments(myDriveId, {
sort: { by: sortBy, order: sortOrder },
});
expect(docs).toBeDefined();
const isSorted = docs.children.every((item, i, arr) => !i || arr[i - 1].size >= item.size);
expect(isSorted).toBe(true);
});
});
});
@@ -8,6 +8,20 @@ import {
comparisonType
} from "../../../../../../../../../src/core/platform/services/database/services/orm/repository/repository";
type SortDirection = "asc" | "desc";
type SortOption = {
[field: string]: SortDirection;
};
interface FindOptions {
sort?: SortOption;
pagination?: {
limitStr: string;
page_token: string;
};
}
describe('The PostgresQueryBuilder', () => {
const subj: PostgresQueryBuilder = new PostgresQueryBuilder("");
@@ -103,6 +117,43 @@ describe('The PostgresQueryBuilder', () => {
expect(query[1]).toEqual([entity.company_id, entity.id]);
});
test('buildSelect query with sort option', async () => {
//given
const options = { sort: { added: "asc" as SortDirection, id: "desc" as SortDirection } };
//when
const query = subj.buildSelect(TestDbEntity, null, options);
//then
expect(normalizeWhitespace(query[0] as string)).toBe(`SELECT * FROM "test_table" ORDER BY added ASC, id DESC LIMIT 100 OFFSET 0`);
expect(query[1]).toEqual([]);
});
test('buildSelect query with pagination', async () => {
//given
const options = { pagination: { limitStr: "50", page_token: "2" } };
//when
const query = subj.buildSelect(TestDbEntity, null, options);
//then
expect(normalizeWhitespace(query[0] as string)).toBe(`SELECT * FROM "test_table" ORDER BY id DESC LIMIT 50 OFFSET 100`);
expect(query[1]).toEqual([]);
});
test('buildSelect query with filter, sort, and pagination', async () => {
//given
const filter = { id: randomUUID(), company_id: randomUUID() };
const options = { sort: { added: "asc" as SortDirection, id: "desc" as SortDirection }, pagination: { limitStr: "10", page_token: "3" } };
//when
const query = subj.buildSelect(TestDbEntity, filter as { [key: string]: any }, options);
//then
expect(normalizeWhitespace(query[0] as string)).toBe(`SELECT * FROM "test_table" WHERE id = $1 AND company_id = $2 ORDER BY added ASC, id DESC LIMIT 10 OFFSET 30`);
expect(query[1]).toEqual([filter.id, filter.company_id]);
});
test('buildInsert', async () => {
//given
const entity = newTestDbEntity();