add test case
This commit is contained in:
parent
a6dcfce339
commit
f5cf346dd1
@ -57,7 +57,10 @@ export class CommentService {
|
|||||||
parent,
|
parent,
|
||||||
post: await this.getPost(data.post),
|
post: await this.getPost(data.post),
|
||||||
});
|
});
|
||||||
return this.repository.findOneOrFail({ where: { id: item.id } });
|
return this.repository.findOneOrFail({
|
||||||
|
where: { id: item.id },
|
||||||
|
relations: ['parent', 'children', 'post'],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string) {
|
async delete(id: string) {
|
||||||
|
@ -9,14 +9,29 @@ import { DataSource } from 'typeorm';
|
|||||||
import { database } from '@/config';
|
import { database } from '@/config';
|
||||||
import { ContentModule } from '@/modules/content/content.module';
|
import { ContentModule } from '@/modules/content/content.module';
|
||||||
import { CategoryEntity, CommentEntity, PostEntity, TagEntity } from '@/modules/content/entities';
|
import { CategoryEntity, CommentEntity, PostEntity, TagEntity } from '@/modules/content/entities';
|
||||||
|
import {
|
||||||
|
CategoryRepository,
|
||||||
|
CommentRepository,
|
||||||
|
PostRepository,
|
||||||
|
TagRepository,
|
||||||
|
} from '@/modules/content/repositories';
|
||||||
import { DatabaseModule } from '@/modules/database/database.module';
|
import { DatabaseModule } from '@/modules/database/database.module';
|
||||||
|
|
||||||
import { generateRandomNumber, generateUniqueRandomNumbers } from './generate-mock-data';
|
import { generateRandomNumber, generateUniqueRandomNumbers } from './generate-mock-data';
|
||||||
import { commentData, INIT_DATA, initialCategories, postData, tagData } from './test-data';
|
import { categoriesData, commentData, INIT_DATA, postData, tagData } from './test-data';
|
||||||
|
|
||||||
describe('category test', () => {
|
describe('category test', () => {
|
||||||
let datasource: DataSource;
|
let datasource: DataSource;
|
||||||
let app: NestFastifyApplication;
|
let app: NestFastifyApplication;
|
||||||
|
let categoryRepository: CategoryRepository;
|
||||||
|
let tagRepository: TagRepository;
|
||||||
|
let postRepository: PostRepository;
|
||||||
|
let commentRepository: CommentRepository;
|
||||||
|
|
||||||
|
let posts: PostEntity[];
|
||||||
|
let categories: CategoryEntity[];
|
||||||
|
let tags: TagEntity[];
|
||||||
|
let comments: CommentEntity[];
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
@ -26,13 +41,14 @@ describe('category test', () => {
|
|||||||
await app.init();
|
await app.init();
|
||||||
await app.getHttpAdapter().getInstance().ready();
|
await app.getHttpAdapter().getInstance().ready();
|
||||||
|
|
||||||
|
categoryRepository = module.get<CategoryRepository>(CategoryRepository);
|
||||||
|
tagRepository = module.get<TagRepository>(TagRepository);
|
||||||
|
postRepository = module.get<PostRepository>(PostRepository);
|
||||||
|
commentRepository = module.get<CommentRepository>(CommentRepository);
|
||||||
datasource = module.get<DataSource>(DataSource);
|
datasource = module.get<DataSource>(DataSource);
|
||||||
if (!datasource.isInitialized) {
|
if (!datasource.isInitialized) {
|
||||||
await datasource.initialize();
|
await datasource.initialize();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
if (INIT_DATA) {
|
if (INIT_DATA) {
|
||||||
const queryRunner = datasource.createQueryRunner();
|
const queryRunner = datasource.createQueryRunner();
|
||||||
try {
|
try {
|
||||||
@ -46,34 +62,29 @@ describe('category test', () => {
|
|||||||
await queryRunner.query(`TRUNCATE TABLE ${table}`);
|
await queryRunner.query(`TRUNCATE TABLE ${table}`);
|
||||||
return table;
|
return table;
|
||||||
});
|
});
|
||||||
// await queryRunner.query(`TRUNCATE TABLE ${tables}`);
|
|
||||||
} finally {
|
} finally {
|
||||||
await queryRunner.query('SET FOREIGN_KEY_CHECKS = 1');
|
await queryRunner.query('SET FOREIGN_KEY_CHECKS = 1');
|
||||||
await queryRunner.release();
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
// init category data
|
// init category data
|
||||||
const categories = await addCategory(app, initialCategories);
|
categories = await addCategory(app, categoriesData);
|
||||||
console.log(categories);
|
|
||||||
// init tag data
|
// init tag data
|
||||||
const tags = await addTag(app, tagData);
|
tags = await addTag(app, tagData);
|
||||||
console.log(tags);
|
|
||||||
// init post data
|
// init post data
|
||||||
const posts = await addPost(
|
posts = await addPost(
|
||||||
app,
|
app,
|
||||||
postData,
|
postData,
|
||||||
tags.map((tag) => tag.id),
|
tags.map((tag) => tag.id),
|
||||||
categories.map((category) => category.id),
|
categories.map((category) => category.id),
|
||||||
);
|
);
|
||||||
console.log(posts);
|
|
||||||
console.log('='.repeat(100));
|
|
||||||
// init comment data
|
// init comment data
|
||||||
const comments = await addComment(
|
comments = await addComment(
|
||||||
app,
|
app,
|
||||||
commentData,
|
commentData,
|
||||||
posts.map((post) => post.id),
|
posts.map((post) => post.id),
|
||||||
);
|
);
|
||||||
console.log(comments);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -81,6 +92,39 @@ describe('category test', () => {
|
|||||||
expect(app).toBeDefined();
|
expect(app).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('category test', () => {
|
||||||
|
it('repository init', () => {
|
||||||
|
expect(categoryRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('tag test', () => {
|
||||||
|
it('tag init', () => {
|
||||||
|
expect(tagRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
it('tag test data check', () => {
|
||||||
|
expect(tags.length).toEqual(tagData.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('posts test', () => {
|
||||||
|
it('posts init', () => {
|
||||||
|
expect(postRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
it('posts test data check', () => {
|
||||||
|
expect(posts.length).toEqual(postData.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('comment test', () => {
|
||||||
|
it('comment init', () => {
|
||||||
|
expect(commentRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
it('comment test data check', () => {
|
||||||
|
expect(comments.length).toEqual(commentData.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await datasource.destroy(); // 关闭数据库连接
|
await datasource.destroy(); // 关闭数据库连接
|
||||||
await app.close();
|
await app.close();
|
||||||
@ -92,7 +136,7 @@ async function addCategory(
|
|||||||
data: RecordAny[],
|
data: RecordAny[],
|
||||||
parentId?: string,
|
parentId?: string,
|
||||||
): Promise<CategoryEntity[]> {
|
): Promise<CategoryEntity[]> {
|
||||||
const categories: CategoryEntity[] = [];
|
const results: CategoryEntity[] = [];
|
||||||
if (app && data && data.length > 0) {
|
if (app && data && data.length > 0) {
|
||||||
for (let index = 0; index < data.length; index++) {
|
for (let index = 0; index < data.length; index++) {
|
||||||
const item = data[index];
|
const item = data[index];
|
||||||
@ -102,15 +146,15 @@ async function addCategory(
|
|||||||
body: { ...pick(item, ['name', 'customOrder']), parent: parentId },
|
body: { ...pick(item, ['name', 'customOrder']), parent: parentId },
|
||||||
});
|
});
|
||||||
const addedItem: CategoryEntity = result.json();
|
const addedItem: CategoryEntity = result.json();
|
||||||
categories.push(addedItem);
|
results.push(addedItem);
|
||||||
categories.push(...(await addCategory(app, item.children, addedItem.id)));
|
results.push(...(await addCategory(app, item.children, addedItem.id)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return categories;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addTag(app: NestFastifyApplication, data: RecordAny[]): Promise<TagEntity[]> {
|
async function addTag(app: NestFastifyApplication, data: RecordAny[]): Promise<TagEntity[]> {
|
||||||
const tags: TagEntity[] = [];
|
const results: TagEntity[] = [];
|
||||||
if (app && data && data.length > 0) {
|
if (app && data && data.length > 0) {
|
||||||
for (let index = 0; index < data.length; index++) {
|
for (let index = 0; index < data.length; index++) {
|
||||||
const item = data[index];
|
const item = data[index];
|
||||||
@ -120,10 +164,10 @@ async function addTag(app: NestFastifyApplication, data: RecordAny[]): Promise<T
|
|||||||
body: item,
|
body: item,
|
||||||
});
|
});
|
||||||
const addedItem: TagEntity = result.json();
|
const addedItem: TagEntity = result.json();
|
||||||
tags.push(addedItem);
|
results.push(addedItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tags;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addPost(
|
async function addPost(
|
||||||
@ -132,23 +176,22 @@ async function addPost(
|
|||||||
tags: string[] = [],
|
tags: string[] = [],
|
||||||
categories: string[] = [],
|
categories: string[] = [],
|
||||||
) {
|
) {
|
||||||
const posts: PostEntity[] = [];
|
const results: PostEntity[] = [];
|
||||||
if (app && data && data.length > 0) {
|
if (app && data && data.length > 0) {
|
||||||
for (let index = 0; index < data.length; index++) {
|
for (let index = 0; index < data.length; index++) {
|
||||||
const item = data[index];
|
const item = data[index];
|
||||||
item.category = categories[generateRandomNumber(1, categories.length - 1)[0]];
|
item.category = categories[generateRandomNumber(1, categories.length - 1)[0]];
|
||||||
item.tags = generateUniqueRandomNumbers(0, tags.length - 1, 3).map((idx) => tags[idx]);
|
item.tags = generateUniqueRandomNumbers(0, tags.length - 1, 3).map((idx) => tags[idx]);
|
||||||
// console.log(JSON.stringify(item));
|
|
||||||
const result = await app.inject({
|
const result = await app.inject({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/posts',
|
url: '/posts',
|
||||||
body: item,
|
body: item,
|
||||||
});
|
});
|
||||||
const addedItem: PostEntity = result.json();
|
const addedItem: PostEntity = result.json();
|
||||||
posts.push(addedItem);
|
results.push(addedItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return posts;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addComment(
|
async function addComment(
|
||||||
@ -156,32 +199,27 @@ async function addComment(
|
|||||||
data: RecordAny[],
|
data: RecordAny[],
|
||||||
posts: string[],
|
posts: string[],
|
||||||
): Promise<CommentEntity[]> {
|
): Promise<CommentEntity[]> {
|
||||||
const comments: CommentEntity[] = [];
|
const results: CommentEntity[] = [];
|
||||||
if (app && data && data.length > 0) {
|
if (app && data && data.length > 0) {
|
||||||
for (let index = 0; index < data.length; index++) {
|
for (let index = 0; index < data.length; index++) {
|
||||||
const item = data[index];
|
const item = data[index];
|
||||||
item.post = posts[generateRandomNumber(0, posts.length - 1)[0]];
|
item.post = posts[generateRandomNumber(0, posts.length - 1)[0]];
|
||||||
|
|
||||||
const commentsFilter = comments
|
const commentsFilter = results
|
||||||
.filter((comment) => comment.post === item.post)
|
.filter((comment) => comment.post === item.post)
|
||||||
.map((comment) => comment.id);
|
.map((comment) => comment.id);
|
||||||
console.log('A'.repeat(100));
|
|
||||||
console.log(commentsFilter);
|
|
||||||
item.parent =
|
item.parent =
|
||||||
commentsFilter.length > 0
|
commentsFilter.length > 0
|
||||||
? commentsFilter[generateRandomNumber(0, commentsFilter.length - 1)[0]]
|
? commentsFilter[generateRandomNumber(0, commentsFilter.length - 1)[0]]
|
||||||
: undefined;
|
: undefined;
|
||||||
console.log(JSON.stringify(item));
|
|
||||||
const result = await app.inject({
|
const result = await app.inject({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/comment',
|
url: '/comment',
|
||||||
body: item,
|
body: item,
|
||||||
});
|
});
|
||||||
const addedItem = result.json();
|
const addedItem = result.json();
|
||||||
console.log(addedItem);
|
results.push(addedItem);
|
||||||
addedItem.post = item.post;
|
|
||||||
comments.push(addedItem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return comments;
|
return results;
|
||||||
}
|
}
|
@ -1,54 +0,0 @@
|
|||||||
import { describe } from 'node:test';
|
|
||||||
|
|
||||||
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
|
|
||||||
import { DataSource } from 'typeorm';
|
|
||||||
|
|
||||||
import { database } from '@/config';
|
|
||||||
import { ContentModule } from '@/modules/content/content.module';
|
|
||||||
import { CategoryController } from '@/modules/content/controllers';
|
|
||||||
import { DatabaseModule } from '@/modules/database/database.module';
|
|
||||||
|
|
||||||
describe('category test', () => {
|
|
||||||
let controller: CategoryController;
|
|
||||||
let datasource: DataSource;
|
|
||||||
let app: NestFastifyApplication;
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
imports: [ContentModule, DatabaseModule.forRoot(database)],
|
|
||||||
}).compile();
|
|
||||||
app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
|
||||||
await app.init();
|
|
||||||
await app.getHttpAdapter().getInstance().ready();
|
|
||||||
controller = module.get<CategoryController>(CategoryController);
|
|
||||||
datasource = module.get<DataSource>(DataSource);
|
|
||||||
});
|
|
||||||
it('check datasource', () => {
|
|
||||||
expect(datasource).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('check new category', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('create new category', () => {
|
|
||||||
expect(app).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('/category', async () => {
|
|
||||||
const result = await app.inject({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/category',
|
|
||||||
});
|
|
||||||
console.log(result.json());
|
|
||||||
expect(result.statusCode).toEqual(200);
|
|
||||||
expect(result.json().items).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await datasource.destroy(); // 关闭数据库连接
|
|
||||||
await app.close();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,6 +1,6 @@
|
|||||||
export const INIT_DATA = true;
|
export const INIT_DATA = true;
|
||||||
|
|
||||||
export const initialCategories = [
|
export const categoriesData = [
|
||||||
{
|
{
|
||||||
name: '全栈',
|
name: '全栈',
|
||||||
customOrder: 100,
|
customOrder: 100,
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
import path from 'path';
|
|
||||||
|
|
||||||
import { Test } from '@jest/test-result';
|
|
||||||
import TestSequencer from '@jest/test-sequencer';
|
|
||||||
|
|
||||||
export default class CustomSequencer extends TestSequencer {
|
|
||||||
private getOrder(filePath: string): number {
|
|
||||||
const filename = path.basename(filePath);
|
|
||||||
const match = filename.match(/^(\d+)-/);
|
|
||||||
return match ? parseInt(match[1], 10) : Infinity;
|
|
||||||
}
|
|
||||||
|
|
||||||
sort(tests: Array<Test>) {
|
|
||||||
return [...tests].sort((a, b) => {
|
|
||||||
return this.getOrder(a.path) - this.getOrder(b.path);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user