Skip to content

Commit

Permalink
feat : myPage 메인페이지 정보 및 수정 페이지 정보 요청 api 테스트 구현 #35
Browse files Browse the repository at this point in the history
  • Loading branch information
GeunH committed Nov 29, 2023
1 parent 9839dcc commit c6ef3f3
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 38 deletions.
14 changes: 14 additions & 0 deletions be/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.8'
services:
postgres:
image: postgis/postgis
environment:
POSTGRES_DB: testdb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5433:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
10 changes: 7 additions & 3 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
"typescript": "^5.1.3"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/$1"
},
"moduleFileExtensions": [
"js",
"json",
Expand All @@ -83,7 +88,6 @@
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"coverageDirectory": "../coverage"
}
}
}
7 changes: 4 additions & 3 deletions be/src/user/entities/user.restaurantlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
ManyToOne,
JoinColumn,
PrimaryColumn,
DeleteDateColumn,
} from "typeorm";
import { User } from "./user.entity";
import { RestaurantInfoEntity } from "src/restaurant/entities/restaurant.entity";
import { ReviewInfoEntity } from "src/review/entities/review.entity";
import { RestaurantInfoEntity } from "../../restaurant/entities/restaurant.entity";
import { ReviewInfoEntity } from "../../review/entities/review.entity";

@Entity("user_restaurant_lists")
export class UserRestaurantListEntity {
Expand All @@ -21,7 +22,7 @@ export class UserRestaurantListEntity {
@CreateDateColumn({ name: "created_at", type: "timestamp" })
createdAt: Date;

@Column({ name: "deleted_at", type: "timestamp", nullable: true })
@DeleteDateColumn({ name: "deleted_at", type: "timestamp", nullable: true })
deletedAt: Date | null;

@ManyToOne(() => User)
Expand Down
5 changes: 3 additions & 2 deletions be/src/user/entities/user.wishrestaurantlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
ManyToOne,
JoinColumn,
PrimaryColumn,
DeleteDateColumn,
} from "typeorm";
import { User } from "./user.entity";
import { RestaurantInfoEntity } from "src/restaurant/entities/restaurant.entity";
import { RestaurantInfoEntity } from "../../restaurant/entities/restaurant.entity";

@Entity("user_wishrestaurant_lists")
export class UserWishRestaurantListEntity {
Expand All @@ -20,7 +21,7 @@ export class UserWishRestaurantListEntity {
@CreateDateColumn({ name: "created_at", type: "timestamp" })
createdAt: Date;

@Column({ name: "deleted_at", type: "timestamp", nullable: true })
@DeleteDateColumn({ name: "deleted_at", type: "timestamp", nullable: true })
deletedAt: Date | null;

@ManyToOne(() => User)
Expand Down
157 changes: 127 additions & 30 deletions be/src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,53 @@ import { newDb } from "pg-mem";
import { DataSource, TypeORMError } from "typeorm";
import { User } from "./entities/user.entity";
import { TypeOrmModule } from "@nestjs/typeorm";
import { FollowEntity } from "./entities/user.followList.entity";
import { RestaurantInfoEntity } from "../restaurant/entities/restaurant.entity";
import { UserRestaurantListEntity } from "./entities/user.restaurantlist.entity";
import { UserWishRestaurantListEntity } from "./entities/user.wishrestaurantlist.entity";
import { ReviewInfoEntity } from "../review/entities/review.entity";
import { UserFollowListRepository } from "./user.followList.repository";
import { UserRestaurantListRepository } from "./user.restaurantList.repository";
import { UserWishRestaurantListRepository } from "./user.wishrestaurantList.repository";
import { RestaurantRepository } from "../restaurant/restaurant.repository";
import { ReviewRepository } from "../review/review.repository";

describe("UserService", () => {
let userService: UserService;
let dataSource: DataSource;
beforeAll(async () => {
const db = newDb({ autoCreateForeignKeyIndices: true });

db.public.registerFunction({
name: "current_database",
implementation: () => "test_database",
});

db.public.registerFunction({
name: "version",
implementation: () =>
"PostgreSQL 12.16, compiled by Visual C++ build 1914, 64-bit",
});

dataSource = await db.adapters.createTypeormDataSource({
type: "postgres",
entities: [User],
});

await dataSource.initialize();
await dataSource.synchronize();

beforeEach(async () => {
const userRepository = dataSource.getRepository(User);
await userRepository.query(`DELETE FROM public.user;`);
});
beforeAll(async () => {
const testModule = await Test.createTestingModule({
imports: [
AuthModule,
TypeOrmModule.forRoot(),
TypeOrmModule.forFeature([User]),
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5433,
username: 'user',
password: 'password',
database: 'testdb',
entities: [User, FollowEntity, RestaurantInfoEntity, UserRestaurantListEntity, UserWishRestaurantListEntity, ReviewInfoEntity],
synchronize: true,
}),
TypeOrmModule.forFeature([User, RestaurantRepository, UserRepository, UserRestaurantListRepository, UserFollowListRepository, UserWishRestaurantListRepository, ReviewRepository]),
],
providers: [UserService, UserRepository],
})
.overrideProvider(DataSource)
.useValue(dataSource)
.compile();
providers: [UserService],
}).compile();

userService = testModule.get<UserService>(UserService);
dataSource = testModule.get<DataSource>(DataSource);
});

afterAll(async () => {
await dataSource.destroy();
});

it("should create a new user and return the user data", async () => {
it("회원가입", async () => {
const userInfoDto: UserInfoDto = {
email: "[email protected]",
password: "1234",
Expand All @@ -68,7 +73,7 @@ describe("UserService", () => {
where: { email: "[email protected]" },
});

expect(foundUser).toEqual(
expect(foundUser).toEqual(await
expect.objectContaining({
nickName: "hi",
region: "인천",
Expand All @@ -78,3 +83,95 @@ describe("UserService", () => {
);
});
});

describe("마이페이지", () => {
let userService: UserService;
let dataSource: DataSource;
beforeEach(async () => {
const userRepository = dataSource.getRepository(User);
await userRepository.query(`DELETE FROM public.user;`);
});


beforeAll(async () => {
const testModule = await Test.createTestingModule({
imports: [
AuthModule,
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5433,
username: 'user',
password: 'password',
database: 'testdb',
entities: [User, FollowEntity, RestaurantInfoEntity, UserRestaurantListEntity, UserWishRestaurantListEntity, ReviewInfoEntity],
synchronize: true,
}),
TypeOrmModule.forFeature([User, RestaurantRepository, UserRepository, UserRestaurantListRepository, UserFollowListRepository, UserWishRestaurantListRepository, ReviewRepository]),
],
providers: [UserService],
}).compile();

userService = testModule.get<UserService>(UserService);
dataSource = testModule.get<DataSource>(DataSource);
});

afterAll(async () => {
await dataSource.destroy();
});

it("마이페이지 정보 요청", async () => {
const userInfoDto: UserInfoDto = {
email: "[email protected]",
password: "1234",
provider: " ",
nickName: "hi",
region: "인천",
birthdate: "1999/10/13",
isMale: true
};
const userRepository = dataSource.getRepository(User);
const id = await userRepository.save(userInfoDto);

const mypageInfo = await userService.getMypageUserInfo(id)

expect(mypageInfo).toEqual(await
expect.objectContaining({
"userInfo": {
nickName: "hi",
region: "인천",
birthdate: "1999/10/13",
isMale: true,
},
})
);
});
it("마이페이지 수정 페이지 정보 요청", async () => {
const userInfoDto: UserInfoDto = {
email: "[email protected]",
password: "1234",
provider: " ",
nickName: "hi",
region: "인천",
birthdate: "1999/10/13",
isMale: true
};
const userRepository = dataSource.getRepository(User);
const id = await userRepository.save(userInfoDto);

const mypageInfo = await userService.getMypageUserDetailInfo(id)

expect(mypageInfo).toEqual(await
expect.objectContaining({
"userInfo": {
email: "[email protected]",
nickName: "hi",
provider: " ",
region: "인천",
birthdate: "1999/10/13",
isMale: true,
},
})
);
});
});

0 comments on commit c6ef3f3

Please sign in to comment.