Skip to content

Commit 1f57566

Browse files
committed
[TEST] template test
1 parent 98354dd commit 1f57566

File tree

7 files changed

+197
-80
lines changed

7 files changed

+197
-80
lines changed

src/app.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class App {
1515
const swaggerSpec = YAML.load(path.join(__dirname, '../swagger.yaml'))
1616
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
1717

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

2626
app.use(globalExceptionHandler);
2727

28-
// scheduler.deadline_alarm_1.start();
29-
// scheduler.deadline_alarm_2.start();
30-
// scheduler.deadline_alarm_3.start();
28+
scheduler.deadline_alarm_1.start();
29+
scheduler.deadline_alarm_2.start();
30+
scheduler.deadline_alarm_3.start();
3131

3232
// scheduler.service_end_alarm.start();
3333

src/common/error/exceptions/customExceptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class ClientException extends Error {
55
this.statusCode = statusCode;
66
}
77
}
8-
class CustomException extends Error {
8+
class ServerException extends Error {
99
statusCode: number;
1010
constructor(msg: string | undefined = undefined, statusCode: number = 500) {
1111
super(msg);
1212
this.statusCode = statusCode;
1313
}
1414
}
1515

16-
export { ClientException, CustomException };
16+
export { ClientException, ServerException };

src/router/templateRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import templateController from "../controller/templateController";
88
const router = Router();
99

1010
router.get("/",
11-
auth,
11+
// auth,
1212
templateController.getAllTemplate
1313
);
1414

src/service/templateService.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,58 @@
1-
import { ClientException } from "../common/error/exceptions/customExceptions";
2-
import { rm } from "../constants";
1+
import { ClientException, ServerException } from "../common/error/exceptions/customExceptions";
2+
import { rm , sc } from "../constants";
33
import templateRepository from "../repository/templateRepository";
44
import userService from "./userService";
55

66
const getTemplateById =async (templateId: number) => {
7-
const template = await templateRepository.findTemplateById(templateId);
8-
9-
if (!template) {
10-
throw new ClientException(rm.READ_TEMPLATE_FAIL);
11-
}
7+
try{
8+
const template = await templateRepository.findTemplateById(templateId);
9+
if(!template){
10+
console.log("template is null")
11+
throw new ClientException(rm.READ_TEMPLATE_FAIL)
12+
}
13+
const data = {
14+
title: template.title,
15+
guideline: template.guideline,
16+
questions: template.questions,
17+
hints: template.hints
18+
}
19+
return data;
20+
}catch(error:any){
21+
console.log(error)
22+
23+
if(error.code == 'P2021'){
24+
throw new ServerException(rm.INTERNAL_SERVER_ERROR)
25+
}
26+
throw new ClientException(rm.READ_TEMPLATE_FAIL)
1227

13-
const data = {
14-
title: template.title,
15-
guideline: template.guideline,
16-
questions: template.questions,
17-
hints: template.hints
1828
}
19-
return data;
2029

2130
}
2231

2332
const getAllTemplate =async (userId: number) => {
24-
25-
const templates = await templateRepository.findAllTemplate();
26-
if (!templates) {
33+
try{
34+
const templates = await templateRepository.findAllTemplate();
35+
36+
const user = await userService.getUserById(userId);
37+
const usedTemplate = user.used_template;
38+
39+
const data :Array<object> = [];
40+
for(var i=0;i<templates.length;i++){
41+
data.push({
42+
templateId: templates[i].id,
43+
title: templates[i].title,
44+
shortInfo: templates[i].shortInfo,
45+
info: templates[i].info,
46+
hasUsed: usedTemplate.includes(templates[i].id),
47+
})
48+
}
49+
50+
return data;
51+
52+
}catch(error){
53+
console.log(error)
2754
throw new ClientException(rm.READ_ALL_TEMPLATES_FAIL);
2855
}
29-
30-
const user = await userService.getUserById(userId);
31-
const usedTemplate = user.used_template;
32-
33-
34-
const data :Array<object> = [];
35-
for(var i=0;i<templates.length;i++){
36-
data.push({
37-
templateId: templates[i].id,
38-
title: templates[i].title,
39-
shortInfo: templates[i].shortInfo,
40-
info: templates[i].info,
41-
hasUsed: usedTemplate.includes(templates[i].id),
42-
})
43-
}
44-
45-
return data;
4656
}
4757

4858
export default{

test/functions.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/template.test.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { prismaMock } from './singleton';
2+
import templateService from "../src/service/templateService";
3+
import templateRepository from '../src/repository/templateRepository';
4+
import { userService } from '../src/service';
5+
6+
7+
// test('should read all templates ', async () => {
8+
9+
// const newUser = {
10+
// name: 'Rich',
11+
// email: 'hello@prisma.io',
12+
// kakaoId: 2020200,
13+
// appleId: "apple_id",
14+
// ageRange: "20~29",
15+
// gender: "male",
16+
// refreshToken: "refresh_token",
17+
// deviceToken: "device_token",
18+
// }
19+
20+
// const createdNewUser = {
21+
// id:1,
22+
// ...newUser,
23+
// used_template:[1,2],
24+
// created_at: new Date(),
25+
// updated_at: new Date(),
26+
// kakao_id: BigInt(2020200),
27+
// apple_id: "apple_id",
28+
// age_range: "20~29",
29+
// gender: "male"
30+
// }
31+
32+
// // mock으로 가짜 유저 생성
33+
// prismaMock.user.create.mockResolvedValue(createdNewUser);
34+
35+
36+
// const templates = [
37+
// {
38+
// id: 1,
39+
// info: "template_1_info",
40+
// title: "template_1_title",
41+
// shortInfo: "emplate_1_shortInfo",
42+
// questions: ["질문1", "질문2","질문3"],
43+
// guideline: "template_1_guideline",
44+
// hints: ["힌트1", "힌트2","힌트3"],
45+
// subtitles: ["소제목1", "소제목2","소제목3"]
46+
// },
47+
// {
48+
// id: 2,
49+
// info: "template_2_info",
50+
// title: "template_2_title",
51+
// shortInfo: "emplate_2_shortInfo",
52+
// questions: ["질문1", "질문2","질문3"],
53+
// guideline: "template_2_guideline",
54+
// hints: ["힌트1", "힌트2","힌트3"],
55+
// subtitles: ["소제목1", "소제목2","소제목3"]
56+
// }
57+
// ]
58+
// (templateRepository.findAllTemplate as jest.Mock).mockResolvedValue(templates);
59+
60+
// const result = [
61+
// {
62+
// templateId: templates[0].id,
63+
// title: templates[0].title,
64+
// shortInfo: templates[0].shortInfo,
65+
// info: templates[0].info,
66+
// hasUsed: createdNewUser.used_template.includes(templates[0].id),
67+
// },
68+
// {
69+
// templateId: templates[1].id,
70+
// title: templates[1].title,
71+
// shortInfo: templates[1].shortInfo,
72+
// info: templates[1].info,
73+
// hasUsed: createdNewUser.used_template.includes(templates[1].id),
74+
// }
75+
// ]
76+
77+
// await expect(templateRepository.findAllTemplate()).resolves.toEqual(templates);
78+
// await expect(templateService.getAllTemplate(createdNewUser.id)).resolves.toEqual(result);
79+
80+
// })
81+
82+
describe('getAllTemplate', () => {
83+
it('should return all templates with hasUsed flag', async () => {
84+
const mockTemplates = [
85+
{
86+
id: 1,
87+
info: "template_1_info",
88+
title: "template_1_title",
89+
shortInfo: "emplate_1_shortInfo",
90+
questions: ["질문1", "질문2","질문3"],
91+
guideline: "template_1_guideline",
92+
hints: ["힌트1", "힌트2","힌트3"],
93+
subtitles: ["소제목1", "소제목2","소제목3"]
94+
},
95+
{
96+
id: 2,
97+
info: "template_2_info",
98+
title: "template_2_title",
99+
shortInfo: "emplate_2_shortInfo",
100+
questions: ["질문1", "질문2","질문3"],
101+
guideline: "template_2_guideline",
102+
hints: ["힌트1", "힌트2","힌트3"],
103+
subtitles: ["소제목1", "소제목2","소제목3"]
104+
}
105+
];
106+
const mockUser = {
107+
id:1,
108+
name: "user",
109+
email: "email@prisma.io",
110+
used_template:[1,2],
111+
created_at: new Date(),
112+
updated_at: new Date(),
113+
kakao_id: BigInt(2020200),
114+
apple_id: "apple_id",
115+
age_range: "20~29",
116+
gender: "male"
117+
};
118+
119+
// Mock repository behavior
120+
jest.spyOn(templateRepository, 'findAllTemplate').mockResolvedValue(mockTemplates);
121+
122+
// Mock service behavior
123+
jest.spyOn(userService, 'getUserById').mockResolvedValue(mockUser);
124+
125+
const result = await templateService.getAllTemplate(1);
126+
127+
expect(result).toEqual([
128+
{
129+
templateId: mockTemplates[0].id,
130+
title: mockTemplates[0].title,
131+
shortInfo: mockTemplates[0].shortInfo,
132+
info: mockTemplates[0].info,
133+
hasUsed: true
134+
},
135+
{
136+
templateId: mockTemplates[1].id,
137+
title: mockTemplates[1].title,
138+
shortInfo: mockTemplates[1].shortInfo,
139+
info: mockTemplates[1].info,
140+
hasUsed: true
141+
}
142+
]);
143+
});
144+
145+
146+
147+
});

test/testApp.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)