Skip to content

Commit

Permalink
Merge pull request #13 from snuhcs-course/add-backend-unit-test
Browse files Browse the repository at this point in the history
서버 unit test 추가 & 친구 리뷰있는 레스토랑 받기
  • Loading branch information
mirlee0304 authored Nov 2, 2023
2 parents ffa1d17 + 7e49d00 commit e4ae942
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 6 deletions.
9 changes: 9 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --runInBand --forceExit",
"test": "jest --runInBand --forceExit --coverage",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down Expand Up @@ -47,6 +47,7 @@
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"string-similarity-js": "^2.1.4",
"ts-mockito": "^2.6.1",
"typeorm": "^0.3.17",
"typeorm-naming-strategies": "^4.1.0"
},
Expand Down
24 changes: 23 additions & 1 deletion backend/src/review/repositories/review.repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CustomRepository } from '../../typeorm-ex/typeorm-ex.decorator';
import { ReviewEntity } from '../models/review.entity';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { FindManyOptions } from 'typeorm/find-options/FindManyOptions';
import { FindOptionsWhere } from 'typeorm/find-options/FindOptionsWhere';
import { UserEntity } from '../../user/models/user.entity';
import { FollowEntity } from '../../user/models/follow.entity';

@CustomRepository(ReviewEntity)
export class ReviewRepository extends Repository<ReviewEntity> {
Expand Down Expand Up @@ -58,6 +59,27 @@ export class ReviewRepository extends Repository<ReviewEntity> {
});
}

async findReviewOfFriends(user: UserEntity) {
const friendsIds = (
await FollowEntity.find({
where: {
user: {
id: user.id,
},
},
relations: {
follower: true,
},
})
).map((follow) => follow.follower.id);

return this.findFull({
user: {
id: In(friendsIds),
},
});
}

findRandomReviews(limit: number) {
return this.createQueryBuilder('review')
.leftJoinAndSelect('review.images', 'image')
Expand Down
24 changes: 23 additions & 1 deletion backend/src/review/review.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { UserEntity } from '../user/models/user.entity';
import { UserRepository } from '../user/repostiories/user.repository';
import { ReviewAdjacentQueryDto } from './dtos/in-dtos/review-adjacent-query.dto';
import { RestaurantListDto } from './dtos/out-dtos/restaurantList.dto';
import { RestaurantRepository } from './repositories/restaurant.repository';
import { In } from 'typeorm';

@ApiTags('reviews')
@Controller('reviews')
Expand All @@ -37,6 +39,7 @@ export class ReviewController {
private s3ImageService: S3ImageService,
private reviewService: ReviewService,
private reviewRepository: ReviewRepository,
private restaurantRepository: RestaurantRepository,
) {}

@UseGuards(JwtAccessGuard)
Expand Down Expand Up @@ -81,7 +84,26 @@ export class ReviewController {
@Req() { user }: UserRequest,
@Query() data: ReviewAdjacentQueryDto,
) {
const restaurants = await this.reviewService.getAdjacentRestaurant(data);
const allRestaurants = await this.restaurantRepository.find({});

const restaurants = this.reviewService.getAdjacentRestaurant(
data,
allRestaurants,
);

return new RestaurantListDto(restaurants);
}

@UseGuards(JwtAccessGuard)
@Get('/friends/restaurants')
async getReviewOfFriends(@Req() { user }: UserRequest) {
const reviewEntities =
await this.reviewRepository.findReviewOfFriends(user);

const restaurantIds = reviewEntities.map((review) => review.restaurant.id);
const restaurants = await this.restaurantRepository.findBy({
id: In(restaurantIds),
});

return new RestaurantListDto(restaurants);
}
Expand Down
7 changes: 5 additions & 2 deletions backend/src/review/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { In } from 'typeorm';
import { ReviewEntity } from './models/review.entity';
import { ReviewAdjacentQueryDto } from './dtos/in-dtos/review-adjacent-query.dto';
import { getDistance } from 'geolib';
import { RestaurantEntity } from './models/restaurant.entity';

@Injectable()
export class ReviewService {
Expand Down Expand Up @@ -40,9 +41,11 @@ export class ReviewService {
}).save();
}

async getAdjacentRestaurant(data: ReviewAdjacentQueryDto) {
getAdjacentRestaurant(
data: ReviewAdjacentQueryDto,
restaurants: RestaurantEntity[],
) {
const { longitude, latitude, distance } = data;
const restaurants = await this.restaurantRepository.find({});
const KILOMETER = 1000;

return restaurants.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ describe('Get adjacent restaurant test', () => {
validateRestaurantList(body);
});

// TODO : unittest
it('멀리 떨어지면 안잡한다', async () => {
restaurant.longitude = 127.0;
restaurant.latitude = 37.018018;
Expand Down
127 changes: 127 additions & 0 deletions backend/src/test/review/adjacent-restaurant.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from '../../app.module';
import { Test } from '@nestjs/testing';
import { DataSource } from 'typeorm';
import { UserEntity } from '../../user/models/user.entity';
import { RestaurantEntity } from '../../review/models/restaurant.entity';
import { ReviewService } from '../../review/review.service';
import { plainToInstance } from 'class-transformer';
import { ReviewAdjacentQueryDto } from '../../review/dtos/in-dtos/review-adjacent-query.dto';

describe('Get adjacent restaurant test', () => {
let testServer: NestExpressApplication;
let reviewService: ReviewService;
let restaurant: RestaurantEntity;

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
restaurant = RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 37.018018,
});
restaurant = RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 50.018018,
});
reviewService = module.get(ReviewService);
});

beforeEach(async () => {});

it('멀리 떨어지면 안잡한다', async () => {
const restaurants = [
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 37.018018,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 50.018018,
}),
];

const adjacentRestaurants = reviewService.getAdjacentRestaurant(
plainToInstance(ReviewAdjacentQueryDto, {
longitude: 127.018018,
latitude: 37.018018,
distance: 1,
}),
restaurants,
);

expect(adjacentRestaurants.length).toBe(1);
});

it('멀리 떨어지면 안잡한다', async () => {
const restaurants = [
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 37.018018,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 50.018018,
}),
];

const adjacentRestaurants = reviewService.getAdjacentRestaurant(
plainToInstance(ReviewAdjacentQueryDto, {
longitude: 127.018018,
latitude: 37.018018,
distance: 1,
}),
restaurants,
);

expect(adjacentRestaurants.length).toBe(1);
});

it('거리 세밀한 체크 2.5km내', async () => {
const restaurants = [
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.046107,
latitude: 37.018018,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.018018,
latitude: 36.995496,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 126.989929,
latitude: 37.018018,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 127.057343,
latitude: 37.018018,
}),
RestaurantEntity.create({
googleMapPlaceId: '123',
longitude: 126.978693,
latitude: 37.018018,
}),
];

const adjacentRestaurants = reviewService.getAdjacentRestaurant(
plainToInstance(ReviewAdjacentQueryDto, {
longitude: 127.018018,
latitude: 37.018018,
distance: 3,
}),
restaurants,
);

expect(adjacentRestaurants.length).toBe(3);
});
});
Loading

0 comments on commit e4ae942

Please sign in to comment.