-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE#212] mates 서비스에 대한 테스트 코드 작성 (#306)
* test: mate service 테스트 코드 작성 * fix: 친구 통계 조회 시 배열 길이 6개인 오류 수정 * test: getMateAndMyStats() 테스트 케이스 추가
- Loading branch information
Showing
12 changed files
with
386 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import categoriesData from '../mock-table/categories.json'; | ||
|
||
export class MockCategoriesRepository { | ||
private data = categoriesData; | ||
create(entity: object): object { | ||
return { | ||
id: this.data.length + 1, | ||
...entity, | ||
}; | ||
} | ||
|
||
save(entity: object): Promise<object> { | ||
return Promise.resolve(entity); | ||
} | ||
|
||
find({ where: { user_id } }): Promise<object> { | ||
const categories = this.data.filter( | ||
(category) => category.user_id === user_id.id, | ||
); | ||
return Promise.resolve(categories); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import matesData from '../mock-table/mates.json'; | ||
|
||
export class MockMatesRepository { | ||
private data = matesData; | ||
create(entity: object): object { | ||
return { | ||
id: this.data.length + 1, | ||
...entity, | ||
}; | ||
} | ||
|
||
save(entity: object): Promise<object> { | ||
return Promise.resolve(entity); | ||
} | ||
|
||
find({ where }: { where: { follower_id } }): Promise<object> { | ||
const mates = this.data.filter( | ||
(mate) => mate.follower_id === where.follower_id.id, | ||
); | ||
return Promise.resolve(mates); | ||
} | ||
|
||
findOne({ | ||
where, | ||
}: { | ||
where: { follower_id; following_id }; | ||
}): Promise<object> { | ||
const mate = this.data.find( | ||
(mate) => | ||
mate.follower_id === where.follower_id.id && | ||
mate.following_id === where.following_id.id, | ||
); | ||
return Promise.resolve(mate); | ||
} | ||
|
||
delete({ follower_id, following_id }): Promise<object> { | ||
const mate = this.data.find( | ||
(mate) => | ||
mate.follower_id === follower_id.id && | ||
mate.following_id === following_id.id, | ||
); | ||
|
||
return Promise.resolve(mate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import usersData from '../mock-table/users.json'; | ||
import mateData from '../mock-table/mates.json'; | ||
import studyLogsData from '../mock-table/study-logs.json'; | ||
|
||
export class MockUsersRepository { | ||
private data = usersData; | ||
private mate = mateData; | ||
private studyLogs = studyLogsData; | ||
|
||
create(entity: object): object { | ||
return { | ||
id: this.data.length + 1, | ||
...entity, | ||
}; | ||
} | ||
|
||
save(entity: object): Promise<object> { | ||
return Promise.resolve(entity); | ||
} | ||
|
||
find({ where: { id } }): Promise<object> { | ||
const user = this.data.find((user) => user.id === id); | ||
return Promise.resolve(user); | ||
} | ||
|
||
findOne({ | ||
where, | ||
}: { | ||
where: { id?: number; nickname?: string }; | ||
}): Promise<object> { | ||
if (where.id) { | ||
const user = this.data.find((user) => user.id === where.id); | ||
return Promise.resolve(user); | ||
} | ||
const user = this.data.find((user) => user.nickname === where.nickname); | ||
return Promise.resolve(user); | ||
} | ||
|
||
delete({ where: { id } }): Promise<object> { | ||
const user = this.data.find((user) => user.id === id); | ||
const index = this.data.indexOf(user); | ||
this.data.splice(index, 1); | ||
return Promise.resolve(user); | ||
} | ||
|
||
query( | ||
query: string, | ||
param: [date: string, user_id: number], | ||
): Promise<object> { | ||
switch (query) { | ||
case ` | ||
SELECT u.id, u.nickname, u.image_url, COALESCE(SUM(s.learning_time), 0) AS total_time | ||
FROM users_model u | ||
LEFT JOIN mates m ON m.following_id = u.id | ||
LEFT JOIN study_logs s ON s.user_id = u.id AND s.date = ? | ||
WHERE m.follower_id = ? | ||
GROUP BY u.id | ||
ORDER BY total_time DESC | ||
`: | ||
const result = this.data | ||
.filter((user) => | ||
this.mate.find( | ||
(mate) => | ||
mate.follower_id === param[1] && mate.following_id === user.id, | ||
), | ||
) | ||
.map((user) => { | ||
const total_time = this.studyLogs | ||
.filter( | ||
(studyLog) => | ||
studyLog.user_id === user.id && studyLog.date === param[0], | ||
) | ||
.reduce((acc, cur) => acc + cur.learning_time, 0); | ||
return { | ||
id: user.id, | ||
nickname: user.nickname, | ||
image_url: user.image_url, | ||
total_time, | ||
}; | ||
}) | ||
.sort((a, b) => b.total_time - a.total_time); | ||
return Promise.resolve(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export class MockRedisService { | ||
private redis: Map<string, string> = new Map(); | ||
constructor() { | ||
this.redis.set('2', '2023-11-29 16:00:00'); | ||
} | ||
set(key: string, value: string) { | ||
this.redis.set(key, value); | ||
} | ||
|
||
get(key: string): Promise<string | null> { | ||
return Promise.resolve(this.redis.get(key)); | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
await this.redis.delete(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import moment from 'moment'; | ||
import studyLogsData from '../mock-table/study-logs.json'; | ||
|
||
export class MockStudyLogsService { | ||
private data = studyLogsData; | ||
|
||
calculateTotalTimes(id, start_date, end_date) { | ||
const startMoment = moment(start_date); | ||
const diffDays = moment(end_date).diff(startMoment, 'days') + 1; | ||
const result = Array.from({ length: diffDays }, () => 0); | ||
const daily_sums = this.data | ||
.filter( | ||
(studyLog) => | ||
studyLog.user_id === id && | ||
studyLog.date >= start_date && | ||
studyLog.date <= end_date, | ||
) | ||
.reduce((acc, cur) => { | ||
const index = moment(cur.date).diff(startMoment, 'days'); | ||
acc[index] += cur.learning_time; | ||
return acc; | ||
}, result); | ||
return daily_sums; | ||
} | ||
} |
Oops, something went wrong.