Skip to content

Commit

Permalink
add cleanParams
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoXiCheng committed Jan 28, 2024
1 parent ffc3fe8 commit b91f346
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/__tests__/book-repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from "dayjs";
import { PlainObject } from "../index";
import { BookModel } from "./helper/book-model";
import { USE_API } from "./helper/helper";
import { USE_API, mock } from "./helper/helper";
import { Book } from "./helper/book-repository";
import { setupGithubMock } from "./mock/mock-github-api";

Expand Down Expand Up @@ -65,7 +65,7 @@ describe('Test Book Storage', () => {
if (USE_API) {
await Book.deleteAll();
} else {
await setupGithubMock();
setupGithubMock();
}
});

Expand Down Expand Up @@ -149,7 +149,7 @@ describe('Test Book Storage', () => {
}
});

(USE_API ? test: test.skip)('Test createAll Book', async () => {
(USE_API ? test : test.skip)('Test createAll Book', async () => {
await Book.deleteAll();
const result = await Book.createAll(bookList);
expect(result.length).toEqual(10);
Expand All @@ -176,6 +176,11 @@ describe('Test Book Storage', () => {
});
});

test('Test find Book with params since undefined', async () => {
const result = await Book.find({ since: undefined });
expect(result.length).toBeGreaterThan(0);
});

test('Test find Book with params page & per_page', async () => {
const firstPage = await Book.find({ page: 1, per_page: 3 });
expect(firstPage.length).toEqual(3);
Expand All @@ -184,9 +189,9 @@ describe('Test Book Storage', () => {
expect(lastPage.length).toEqual(1);
});

test('Test get Book Detail', async()=>{
test('Test get Book Detail', async () => {
const result = await Book.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
const { id, issue_number, comments, created_at, updated_at } = result;
expect(id).not.toBeNull();
expect(issue_number).not.toBeNull();
expect(comments).not.toBeNull();
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/chat-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ describe('Use Gitlab Test Chat Storage', () => {
expect(findAsc.map(item => item.id)).toEqual(findDesc.map(item => item.id).reverse());
});

test('Test find Chat with params sort & order_by undefined', async () => {
const result = await Chat.find({ sort: undefined, order_by: undefined });
expect(result.length).toBeGreaterThan(0);
});

test('Test get Chat Detail', async()=>{
const result = await Chat.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import timezone from 'dayjs/plugin/timezone';
import MockAdapter from 'axios-mock-adapter';
import jsonfile from 'jsonfile';

// export const mock = new MockAdapter(axios);

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Asia/Shanghai');
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/mock/mock-gitee-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function setupGiteeMock() {
function mockGiteeFind() {
mock?.onGet(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params) {
Object.values(config.params).forEach((item)=>{
expect(item).not.toBeNull();
});
}
if (config.params?.since) {
return [200, result.filter((item: any) => dayjs(item.created_at).isAfter(dayjs(config.params.since)))];
}
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/mock/mock-github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function setupGithubMock() {
function mockGithubFind() {
mock?.onGet(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params) {
Object.values(config.params).forEach((item)=>{
expect(item).not.toBeNull();
})
}
if (config.params?.since) {
return [200, result.filter((item: any) => dayjs(item.created_at).isAfter(dayjs(config.params.since)))];
}
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/mock/mock-gitlab-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function setupGitlabMock() {
function mockGitlabFind() {
mock?.onGet(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params) {
Object.values(config.params).forEach((item)=>{
expect(item).not.toBeNull();
});
}
if (config.params?.sort) {
const sort = config.params?.sort;
result.sort((a: any, b: any) => {
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/user-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ describe('Test User Repository', () => {
expect(lastPage.length).toEqual(1);
});

test('Test find user with params page & per_page undefined', async () => {
const result = await User.find({ page: undefined, per_page: undefined });
expect(result.length).toBeGreaterThan(0);
});

test('Test get User Detail', async()=>{
const result = await User.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
Expand Down
16 changes: 16 additions & 0 deletions src/repository-lib/base/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export abstract class BaseRepository<T extends BaseModel> {
}
}

/**
* Cleans the provided parameters by removing any properties with null or undefined values.
* @param params - The parameters to be cleaned.
* @returns The cleaned parameters or undefined if all properties are null or undefined.
*/
protected cleanParams(params?: BaseParams): BaseParams | undefined {
if (params == null) return;
const copyParams = JSON.parse(JSON.stringify(params));
for (const key in copyParams) {
if (copyParams[key] == null) {
delete copyParams[key];
}
}
return Object.keys(copyParams).length > 0 ? copyParams : undefined;
}

/**
* Finds items in the storage based on the provided parameters.
* @param params - The parameters used for filtering the items.
Expand Down
2 changes: 1 addition & 1 deletion src/repository-lib/gitee/gitee-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class GiteeRepository<T extends BaseModel> extends BaseRepository<T> {
* @returns A promise that resolves to an array of items.
*/
async find(params?: GiteeParams): Promise<T[]> {
return super.find(params);
return super.find(this.cleanParams(params));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/repository-lib/github/github-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class GithubRepository<T extends BaseModel> extends BaseRepository<T> {
* @returns A promise that resolves to an array of items.
*/
async find(params?: GithubParams): Promise<T[]> {
return super.find(params);
return super.find(this.cleanParams(params));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/repository-lib/gitlab/gitlab-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GitlabRepository<T extends BaseModel> extends BaseRepository<T> {
*/
async find(params?: GitlabParams): Promise<T[]> {
const url = this.getRoute(RouteType.find);
const response = await this.request.get<BaseComment[]>(url, params);
const response = await this.request.get<BaseComment[]>(url, this.cleanParams(params));
return response.filter(item => item['system'] === false).map((item) => this.deserialize<T>(item));
}

Expand Down

0 comments on commit b91f346

Please sign in to comment.