Skip to content

Commit

Permalink
Merge pull request #92 from GuoXiCheng/dev-h
Browse files Browse the repository at this point in the history
Dev h
  • Loading branch information
GuoXiCheng authored Jan 28, 2024
2 parents ffc3fe8 + d7b6671 commit 8218fb6
Show file tree
Hide file tree
Showing 22 changed files with 425 additions and 372 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-crud",
"version": "1.1.0",
"version": "1.1.1",
"description": "Lightweight Data Repository Based on Git Issue API",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-gitee.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { USE_API, giteeRequest } from './helper/helper';
import { mockGiteeUser } from './mock/mock-git-user';
import { GiteeMock } from './mock/gitee-mock';

describe('Test Authenticate Gitee', () => {
beforeAll(()=>{
if (USE_API) return;
mockGiteeUser();
new GiteeMock().setUpMock();
});

test('Test Authenticate Gitee', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-github.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GithubUser } from "../index";
import { USE_API, githubRequest } from "./helper/helper";
import { mockGithubUser } from "./mock/mock-git-user";
import { GithubMock } from "./mock/github-mock";

describe('Test Authenticate Github', () => {
beforeAll(() => {
if (USE_API) return;
mockGithubUser();
new GithubMock().setUpMock();
});

test('Test Authenticate Github', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { USE_API, gitlabRequest } from "./helper/helper";
import { mockGitlabUser } from "./mock/mock-git-user";
import { GitlabMock } from "./mock/gitlab-mock";

describe('Test Authenticate Gitlab', () => {
beforeAll(() => {
if (USE_API) return;
mockGitlabUser();
new GitlabMock().setUpMock();
});

test('Test Authenticate Gitlab', async () => {
Expand Down
15 changes: 10 additions & 5 deletions src/__tests__/book-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PlainObject } from "../index";
import { BookModel } from "./helper/book-model";
import { USE_API } from "./helper/helper";
import { Book } from "./helper/book-repository";
import { setupGithubMock } from "./mock/mock-github-api";
import { GithubMock } from "./mock/github-mock";


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

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
9 changes: 7 additions & 2 deletions src/__tests__/chat-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PlainObject } from "../index";
import { ChatModel } from "./helper/chat-model";
import { Chat } from "./helper/chat-repository";
import { USE_API } from "./helper/helper";
import { setupGitlabMock} from "./mock/mock-gitlab-api";
import { GitlabMock } from "./mock/gitlab-mock";


describe('Use Gitlab Test Chat Storage', () => {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('Use Gitlab Test Chat Storage', () => {
if (USE_API) {
await Chat.deleteAll();
} else {
setupGitlabMock();
new GitlabMock().setUpMock();
}
});

Expand Down 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
25 changes: 25 additions & 0 deletions src/__tests__/mock/base-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { writeJSONSync } from "../helper/helper";

export abstract class BaseMock {
constructor(filename: string) {
writeJSONSync(filename, []);
}

setUpMock() {
this.mockUser();
this.mockCreate();
this.mockFind();
this.mockFindById();
this.mockUpdateById();
this.mockDeleteById();
this.mockDetail();
}

abstract mockUser(): void;
abstract mockCreate(): void;
abstract mockFind(): void;
abstract mockFindById(): void;
abstract mockUpdateById(): void;
abstract mockDeleteById(): void;
abstract mockDetail(): void;
}
111 changes: 111 additions & 0 deletions src/__tests__/mock/gitee-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import dayjs from "dayjs";
import { OfficialUrl } from "../../enums";
import { GITEE_NUMBER, GITEE_OWNER, GITEE_REPO, mock, readJSONSync, writeJSONSync } from "../helper/helper";
import { BaseMock } from "./base-mock";

const filename = "temp-gitee.json";

export class GiteeMock extends BaseMock {
private baseURL = OfficialUrl.gitee;

constructor() {
super(filename)
}

mockUser(): void {
mock?.onGet(`${this.baseURL}/api/v5/user`).reply(async () => {
return [200, readJSONSync('mock-gitee-user.json')];
});
}
mockCreate(): void {
mock?.onPost(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
id: Math.round(Math.random() * 1000000),
body: JSON.parse(config.data).body,
user: {
id: 100001,
login: "***",
name: "***",
avatar_url: "https://foruda.gitee.com/avatar/***/***.png",
},
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
writeJSONSync(filename, result);
return [200, data];
});
}
mockFind(): void {
mock?.onGet(`${this.baseURL}/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)))];
}
if (config.params?.order) {
return [200, config.params.order == 'desc' ? result.reverse() : result];
}
if (config.params?.page && config.params?.per_page) {
const start = (config.params.page - 1) * config.params.per_page;
const end = config.params.page * config.params.per_page;
return [200, result.slice(start, end)];
}
return [200, result];
});
}
mockFindById(): void {
mock?.onGet(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const result = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = result.find((item: any) => item.id == id);
if (!target) {
return [404, { message: '404 Not Found' }];
}
return [200, target];
});
}
mockUpdateById(): void {
mock?.onPatch(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, { message: '404 Not Found' }];
}
raw.forEach((item: any) => {
if (item.id == id) {
item.body = JSON.parse(config.data).body;
item.updated_at = dayjs().format();
}
});
writeJSONSync(filename, raw);
const resAfter = readJSONSync(filename);
return [200, resAfter.find((item: any) => item.id == id)];
});
}
mockDeleteById(): void {
mock?.onDelete(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, { message: '404 Not Found' }];
}
const remain = raw.find((item: any) => item.id != id);
writeJSONSync(filename, remain ? remain : []);
return [204];
});
}
mockDetail(): void {
mock?.onGet(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}`)).reply(async (config) => {
return [200, readJSONSync('mock-gitee-detail.json')];
});
}

}
113 changes: 113 additions & 0 deletions src/__tests__/mock/github-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import dayjs from "dayjs";
import { OfficialUrl } from "../../enums";
import { GITHUB_NUMBER, GITHUB_OWNER, GITHUB_REPO, mock, readJSONSync, writeJSONSync } from "../helper/helper";
import { BaseMock } from "./base-mock";

const filename = "temp-github.json";
export class GithubMock extends BaseMock {
private baseURL = OfficialUrl.github;
constructor() {
super(filename)
}
mockUser(): void {
mock?.onGet(`${this.baseURL}/user`).reply(async () => {
return [200, readJSONSync('mock-github-user.json')];
});
}
mockCreate(): void {
mock?.onPost(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
id: Math.round(Math.random() * 1000000),
body: JSON.parse(config.data).body,
user: {
id: 100001,
login: "***",
name: "***",
avatar_url: "https://foruda.github.com/avatar/***/***.png",
},
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
writeJSONSync(filename, result);
return [200, data];
});
}
mockFind(): void {
mock?.onGet(`${this.baseURL}/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)))];
}
if (config.params?.page && config.params?.per_page) {
const start = (config.params.page - 1) * config.params.per_page;
const end = config.params.page * config.params.per_page;
return [200, result.slice(start, end)];
}
return [200, result];
});
}
mockFindById(): void {
mock?.onGet(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const result = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = result.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#get-an-issue-comment"
}];
}
return [200, target];
});
}
mockUpdateById(): void {
mock?.onPatch(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#update-an-issue-comment"
}];
}
raw.forEach((item: any) => {
if (item.id == id) {
item.body = JSON.parse(config.data).body;
item.updated_at = dayjs().format();
}
});
writeJSONSync(filename, raw);
const resAfter = readJSONSync(filename);
return [200, resAfter.find((item: any) => item.id == id)];
});
}
mockDeleteById(): void {
mock?.onDelete(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#delete-an-issue-comment"
}];
}
const remain = raw.find((item: any) => item.id != id);
writeJSONSync(filename, remain ? remain : []);
return [204];
});
}
mockDetail(): void {
mock?.onGet(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}`)).reply(async (config) => {
return [200, readJSONSync('mock-github-detail.json')];
});
}
}
Loading

0 comments on commit 8218fb6

Please sign in to comment.