-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from boostcampwm-2024/test/all-statistic-api
✅ test: 전체 통계 API 테스트 코드 작성
- Loading branch information
Showing
2 changed files
with
36 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,46 @@ | ||
import * as request from 'supertest'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { RedisService } from '../../src/common/redis/redis.service'; | ||
import { RssAcceptRepository } from '../../src/rss/rss.repository'; | ||
import { FeedRepository } from '../../src/feed/feed.repository'; | ||
import { RssAcceptFixture } from '../fixture/rssAccept.fixture'; | ||
import { FeedFixture } from '../fixture/feed.fixture'; | ||
import { Feed } from '../../src/feed/feed.entity'; | ||
|
||
describe('All view count statistic E2E Test : GET /api/statistic/all', () => { | ||
describe('GET /api/statistic/all E2E Test', () => { | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
app = global.testApp; | ||
const rssAcceptRepository = app.get(RssAcceptRepository); | ||
const feedRepository = app.get(FeedRepository); | ||
const redisService = app.get(RedisService); | ||
const [blog] = await Promise.all([ | ||
rssAcceptRepository.save({ | ||
id: 1, | ||
name: 'test', | ||
userName: 'test', | ||
email: '[email protected]', | ||
rssUrl: 'https://test.com/rss', | ||
}), | ||
redisService.redisClient.set('auth:test1234', 'test'), | ||
]); | ||
await feedRepository.save([ | ||
{ | ||
id: 1, | ||
createdAt: '2024-11-26 09:00:00', | ||
title: 'test1', | ||
path: 'test1', | ||
viewCount: 5, | ||
thumbnail: 'https://test.com/test.png', | ||
blog: blog, | ||
}, | ||
{ | ||
id: 2, | ||
createdAt: '2024-11-26 09:00:00', | ||
title: 'test2', | ||
path: 'test2', | ||
viewCount: 4, | ||
thumbnail: 'https://test.com/test.png', | ||
blog: blog, | ||
}, | ||
{ | ||
id: 3, | ||
createdAt: '2024-11-26 09:00:00', | ||
title: 'test3', | ||
path: 'test3', | ||
viewCount: 3, | ||
thumbnail: 'https://test.com/test.png', | ||
blog: blog, | ||
}, | ||
{ | ||
id: 4, | ||
createdAt: '2024-11-26 09:00:00', | ||
title: 'test4', | ||
path: 'test4', | ||
viewCount: 2, | ||
thumbnail: 'https://test.com/test.png', | ||
blog: blog, | ||
}, | ||
{ | ||
id: 5, | ||
createdAt: '2024-11-26 09:00:00', | ||
title: 'test5', | ||
path: 'test5', | ||
viewCount: 1, | ||
thumbnail: 'https://test.com/test.png', | ||
blog: blog, | ||
}, | ||
]); | ||
const blog = await rssAcceptRepository.save( | ||
RssAcceptFixture.createRssAcceptFixture(), | ||
); | ||
const feeds: Feed[] = []; | ||
for (let i = 1; i <= 2; i++) { | ||
feeds.push(FeedFixture.createFeedFixture(blog, { viewCount: i - 1 }, i)); | ||
} | ||
await feedRepository.save(feeds); | ||
}); | ||
|
||
describe('limit 값을 올바르게 입력하지 않았을 경우', () => { | ||
it('실수를 입력한다.', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.get('/api/statistic/all?limit=1.1') | ||
.set('Cookie', 'sessionId=test1234'); | ||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBe('정수로 입력해주세요.'); | ||
}); | ||
it('문자열을 입력한다.', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.get('/api/statistic/all?limit=test') | ||
.set('Cookie', 'sessionId=test1234'); | ||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBe('정수로 입력해주세요.'); | ||
}); | ||
it('값을 입력 하지 않으면 10개의 데이터만 응답한다.', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()).get( | ||
'/api/statistic/all', | ||
); | ||
|
||
it('음수를 입력한다.', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.get('/api/statistic/all?limit=-100') | ||
.set('Cookie', 'sessionId=test1234'); | ||
expect(response.status).toBe(400); | ||
expect(response.body).toStrictEqual({ | ||
message: 'limit 값은 1 이상이어야 합니다.', | ||
}); | ||
}); | ||
// then | ||
expect(response.status).toBe(200); | ||
expect(response.body.data.map((item) => item.id)).toStrictEqual([2, 1]); | ||
}); | ||
it('양수를 입력하면 제한된 개수의 통계 결과를 응답한다.', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()).get( | ||
'/api/statistic/all?limit=1', | ||
); | ||
|
||
describe('limit 값을 올바르게 입력했을 경우', () => { | ||
it('값을 입력 하지 않는다.', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.get('/api/statistic/all') | ||
.set('Cookie', 'sessionId=test1234'); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toStrictEqual({ | ||
message: '전체 조회수 통계 조회 완료', | ||
data: [ | ||
{ | ||
id: 1, | ||
title: 'test1', | ||
viewCount: 5, | ||
}, | ||
{ | ||
id: 2, | ||
title: 'test2', | ||
viewCount: 4, | ||
}, | ||
{ | ||
id: 3, | ||
title: 'test3', | ||
viewCount: 3, | ||
}, | ||
{ | ||
id: 4, | ||
title: 'test4', | ||
viewCount: 2, | ||
}, | ||
{ | ||
id: 5, | ||
title: 'test5', | ||
viewCount: 1, | ||
}, | ||
], | ||
}); | ||
}); | ||
it('양수를 입력한다.', async () => { | ||
const response = await request(app.getHttpServer()) | ||
.get('/api/statistic/all?limit=1') | ||
.set('Cookie', 'sessionId=test1234'); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toStrictEqual({ | ||
message: '전체 조회수 통계 조회 완료', | ||
data: [ | ||
{ | ||
id: 1, | ||
title: 'test1', | ||
viewCount: 5, | ||
}, | ||
], | ||
}); | ||
}); | ||
// then | ||
expect(response.status).toBe(200); | ||
expect(response.body.data.map((item) => item.id)).toStrictEqual([2]); | ||
}); | ||
}); |
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