Skip to content

Commit

Permalink
Merge pull request #252 from boostcampwm-2024/test/today-statistic-api
Browse files Browse the repository at this point in the history
✅ test: Today Statistic Test 작성
  • Loading branch information
Jo-Minseok authored Dec 2, 2024
2 parents 1974882 + ccd32af commit 7f40bea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 134 deletions.
2 changes: 1 addition & 1 deletion server/test/fixture/feed.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class FeedFixture {
path: `https://test.com/test${index}`,
thumbnail: `https://test.com/test${index}.png`,
},
rssAccept,
blog: rssAccept,
});
return Object.assign(feed, overwrites);
}
Expand Down
50 changes: 50 additions & 0 deletions server/test/statistic/dto/statistic-query.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { validate } from 'class-validator';
import { StatisticQueryDto } from '../../../src/statistic/dto/statistic-query.dto';

describe('StatisticQueryDto', () => {
it('실수를 입력한다.', async () => {
// given
const dto = new StatisticQueryDto();
dto.limit = 1.1;

// when
const errors = await validate(dto);

// then
expect(errors.length).toBe(1);
expect(errors[0].constraints).toHaveProperty(
'isInt',
'정수로 입력해주세요.',
);
});
it('문자열을 입력한다.', async () => {
// given
const dto = new StatisticQueryDto();
dto.limit = 'test' as unknown as number;

// when
const errors = await validate(dto);

// then
expect(errors.length).toBe(1);
expect(errors[0].constraints).toHaveProperty(
'isInt',
'정수로 입력해주세요.',
);
});
it('음수를 입력한다.', async () => {
// given
const dto = new StatisticQueryDto();
dto.limit = -1;

// when
const errors = await validate(dto);

// then
expect(errors.length).toBe(1);
expect(errors[0].constraints).toHaveProperty(
'min',
'limit 값은 1 이상이어야 합니다.',
);
});
});
154 changes: 21 additions & 133 deletions server/test/statistic/today.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { redisKeys } from '../../src/common/redis/redis.constant';
import { RssAcceptFixture } from '../fixture/rssAccept.fixture';
import { FeedRepository } from '../../src/feed/feed.repository';
import { RssAcceptRepository } from '../../src/rss/rss.repository';
import { FeedFixture } from '../fixture/feed.fixture';

describe('Today view count statistic E2E Test : GET /api/statistic/today', () => {
let app: INestApplication;
Expand All @@ -16,143 +17,30 @@ describe('Today view count statistic E2E Test : GET /api/statistic/today', () =>
const redisService = app.get(RedisService);
const [blog] = await Promise.all([
rssAcceptRepository.save(RssAcceptFixture.createRssAcceptFixture()),
redisService.redisClient.set('auth:test1234', 'test'),
redisService.redisClient.zadd(
redisKeys.FEED_TREND_KEY,
'1',
5,
'2',
4,
'3',
3,
'4',
2,
'5',
1,
),
]);
await feedRepository.save([
{
id: 1,
createdAt: '2024-11-26 09:00:00',
title: 'test1',
path: 'test1',
thumbnail: 'https://test.com/test.png',
blog: blog,
},
{
id: 2,
createdAt: '2024-11-26 09:00:00',
title: 'test2',
path: 'test2',
thumbnail: 'https://test.com/test.png',
blog: blog,
},
{
id: 3,
createdAt: '2024-11-26 09:00:00',
title: 'test3',
path: 'test3',
thumbnail: 'https://test.com/test.png',
blog: blog,
},
{
id: 4,
createdAt: '2024-11-26 09:00:00',
title: 'test4',
path: 'test4',
thumbnail: 'https://test.com/test.png',
blog: blog,
},
{
id: 5,
createdAt: '2024-11-26 09:00:00',
title: 'test5',
path: 'test5',
thumbnail: 'https://test.com/test.png',
blog: blog,
},
redisService.redisClient.zadd(redisKeys.FEED_TREND_KEY, 5, '1', 4, '2'),
]);
for (let i = 1; i <= 2; i++) {
await feedRepository.save(FeedFixture.createFeedFixture(blog, {}, i));
}
});
it('값을 입력 하지 않아 10개의 데이터만 요청한다.', async () => {
// when
const response = await request(app.getHttpServer()).get(
'/api/statistic/today',
);

describe('limit 값을 올바르게 입력하지 않았을 경우', () => {
it('실수를 입력한다.', async () => {
const response = await request(app.getHttpServer())
.get('/api/statistic/today?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/today?limit=test')
.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/today?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([1, 2]);
});
it('양수를 입력하여 제한된 통계를 요청한다.', async () => {
// when
const response = await request(app.getHttpServer()).get(
'/api/statistic/today?limit=1',
);

describe('limit 값을 올바르게 입력했을 경우', () => {
it('값을 입력 하지 않는다.', async () => {
const response = await request(app.getHttpServer())
.get('/api/statistic/today')
.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/today?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([1]);
});
});

0 comments on commit 7f40bea

Please sign in to comment.