Skip to content

Commit

Permalink
[TEST] template test
Browse files Browse the repository at this point in the history
  • Loading branch information
leGit-y committed Jan 27, 2025
1 parent 98354dd commit 1f57566
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 80 deletions.
8 changes: 4 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class App {
const swaggerSpec = YAML.load(path.join(__dirname, '../swagger.yaml'))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// app.use("/v1", router);
app.use("/v1", router);
app.get("/health", (req: Request, res: Response, next: NextFunction) => {
res.status(200).json({
status: 200,
Expand All @@ -25,9 +25,9 @@ class App {

app.use(globalExceptionHandler);

// scheduler.deadline_alarm_1.start();
// scheduler.deadline_alarm_2.start();
// scheduler.deadline_alarm_3.start();
scheduler.deadline_alarm_1.start();
scheduler.deadline_alarm_2.start();
scheduler.deadline_alarm_3.start();

// scheduler.service_end_alarm.start();

Expand Down
4 changes: 2 additions & 2 deletions src/common/error/exceptions/customExceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class ClientException extends Error {
this.statusCode = statusCode;
}
}
class CustomException extends Error {
class ServerException extends Error {
statusCode: number;
constructor(msg: string | undefined = undefined, statusCode: number = 500) {
super(msg);
this.statusCode = statusCode;
}
}

export { ClientException, CustomException };
export { ClientException, ServerException };
2 changes: 1 addition & 1 deletion src/router/templateRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import templateController from "../controller/templateController";
const router = Router();

router.get("/",
auth,
// auth,
templateController.getAllTemplate
);

Expand Down
76 changes: 43 additions & 33 deletions src/service/templateService.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
import { ClientException } from "../common/error/exceptions/customExceptions";
import { rm } from "../constants";
import { ClientException, ServerException } from "../common/error/exceptions/customExceptions";
import { rm , sc } from "../constants";
import templateRepository from "../repository/templateRepository";
import userService from "./userService";

const getTemplateById =async (templateId: number) => {
const template = await templateRepository.findTemplateById(templateId);

if (!template) {
throw new ClientException(rm.READ_TEMPLATE_FAIL);
}
try{
const template = await templateRepository.findTemplateById(templateId);
if(!template){
console.log("template is null")
throw new ClientException(rm.READ_TEMPLATE_FAIL)
}
const data = {
title: template.title,
guideline: template.guideline,
questions: template.questions,
hints: template.hints
}
return data;
}catch(error:any){
console.log(error)

if(error.code == 'P2021'){
throw new ServerException(rm.INTERNAL_SERVER_ERROR)
}
throw new ClientException(rm.READ_TEMPLATE_FAIL)

const data = {
title: template.title,
guideline: template.guideline,
questions: template.questions,
hints: template.hints
}
return data;

}

const getAllTemplate =async (userId: number) => {

const templates = await templateRepository.findAllTemplate();
if (!templates) {
try{
const templates = await templateRepository.findAllTemplate();

const user = await userService.getUserById(userId);
const usedTemplate = user.used_template;

const data :Array<object> = [];
for(var i=0;i<templates.length;i++){
data.push({
templateId: templates[i].id,
title: templates[i].title,
shortInfo: templates[i].shortInfo,
info: templates[i].info,
hasUsed: usedTemplate.includes(templates[i].id),
})
}

return data;

}catch(error){
console.log(error)
throw new ClientException(rm.READ_ALL_TEMPLATES_FAIL);
}

const user = await userService.getUserById(userId);
const usedTemplate = user.used_template;


const data :Array<object> = [];
for(var i=0;i<templates.length;i++){
data.push({
templateId: templates[i].id,
title: templates[i].title,
shortInfo: templates[i].shortInfo,
info: templates[i].info,
hasUsed: usedTemplate.includes(templates[i].id),
})
}

return data;
}

export default{
Expand Down
30 changes: 0 additions & 30 deletions test/functions.ts

This file was deleted.

147 changes: 147 additions & 0 deletions test/template.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { prismaMock } from './singleton';
import templateService from "../src/service/templateService";
import templateRepository from '../src/repository/templateRepository';
import { userService } from '../src/service';


// test('should read all templates ', async () => {

// const newUser = {
// name: 'Rich',
// email: '[email protected]',
// kakaoId: 2020200,
// appleId: "apple_id",
// ageRange: "20~29",
// gender: "male",
// refreshToken: "refresh_token",
// deviceToken: "device_token",
// }

// const createdNewUser = {
// id:1,
// ...newUser,
// used_template:[1,2],
// created_at: new Date(),
// updated_at: new Date(),
// kakao_id: BigInt(2020200),
// apple_id: "apple_id",
// age_range: "20~29",
// gender: "male"
// }

// // mock으로 가짜 유저 생성
// prismaMock.user.create.mockResolvedValue(createdNewUser);


// const templates = [
// {
// id: 1,
// info: "template_1_info",
// title: "template_1_title",
// shortInfo: "emplate_1_shortInfo",
// questions: ["질문1", "질문2","질문3"],
// guideline: "template_1_guideline",
// hints: ["힌트1", "힌트2","힌트3"],
// subtitles: ["소제목1", "소제목2","소제목3"]
// },
// {
// id: 2,
// info: "template_2_info",
// title: "template_2_title",
// shortInfo: "emplate_2_shortInfo",
// questions: ["질문1", "질문2","질문3"],
// guideline: "template_2_guideline",
// hints: ["힌트1", "힌트2","힌트3"],
// subtitles: ["소제목1", "소제목2","소제목3"]
// }
// ]
// (templateRepository.findAllTemplate as jest.Mock).mockResolvedValue(templates);

// const result = [
// {
// templateId: templates[0].id,
// title: templates[0].title,
// shortInfo: templates[0].shortInfo,
// info: templates[0].info,
// hasUsed: createdNewUser.used_template.includes(templates[0].id),
// },
// {
// templateId: templates[1].id,
// title: templates[1].title,
// shortInfo: templates[1].shortInfo,
// info: templates[1].info,
// hasUsed: createdNewUser.used_template.includes(templates[1].id),
// }
// ]

// await expect(templateRepository.findAllTemplate()).resolves.toEqual(templates);
// await expect(templateService.getAllTemplate(createdNewUser.id)).resolves.toEqual(result);

// })

describe('getAllTemplate', () => {
it('should return all templates with hasUsed flag', async () => {
const mockTemplates = [
{
id: 1,
info: "template_1_info",
title: "template_1_title",
shortInfo: "emplate_1_shortInfo",
questions: ["질문1", "질문2","질문3"],
guideline: "template_1_guideline",
hints: ["힌트1", "힌트2","힌트3"],
subtitles: ["소제목1", "소제목2","소제목3"]
},
{
id: 2,
info: "template_2_info",
title: "template_2_title",
shortInfo: "emplate_2_shortInfo",
questions: ["질문1", "질문2","질문3"],
guideline: "template_2_guideline",
hints: ["힌트1", "힌트2","힌트3"],
subtitles: ["소제목1", "소제목2","소제목3"]
}
];
const mockUser = {
id:1,
name: "user",
email: "[email protected]",
used_template:[1,2],
created_at: new Date(),
updated_at: new Date(),
kakao_id: BigInt(2020200),
apple_id: "apple_id",
age_range: "20~29",
gender: "male"
};

// Mock repository behavior
jest.spyOn(templateRepository, 'findAllTemplate').mockResolvedValue(mockTemplates);

// Mock service behavior
jest.spyOn(userService, 'getUserById').mockResolvedValue(mockUser);

const result = await templateService.getAllTemplate(1);

expect(result).toEqual([
{
templateId: mockTemplates[0].id,
title: mockTemplates[0].title,
shortInfo: mockTemplates[0].shortInfo,
info: mockTemplates[0].info,
hasUsed: true
},
{
templateId: mockTemplates[1].id,
title: mockTemplates[1].title,
shortInfo: mockTemplates[1].shortInfo,
info: mockTemplates[1].info,
hasUsed: true
}
]);
});



});
10 changes: 0 additions & 10 deletions test/testApp.ts

This file was deleted.

0 comments on commit 1f57566

Please sign in to comment.