Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[기능] reviewer api 추가 #7

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@mikro-orm/cli": "^6.3.8",
"@mikro-orm/core": "^6.3.8",
"@mikro-orm/entity-generator": "^6.3.8",
"@mikro-orm/migrations": "^6.3.8",
"@mikro-orm/cli": "6.3.8",
"@mikro-orm/core": "6.3.8",
"@mikro-orm/entity-generator": "6.3.8",
"@mikro-orm/migrations": "6.3.8",
"@mikro-orm/nestjs": "^6.0.2",
"@mikro-orm/postgresql": "^6.3.8",
"@mikro-orm/postgresql": "6.3.8",
"@mikro-orm/sqlite": "6.3.8",
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
Expand Down
841 changes: 816 additions & 25 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { AppService } from './app.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AuthModule } from './auth/auth.module';
import mikroOrmConfig, { testConfig } from './mikro-orm.config';
import { ReviewersModule } from './reviewers/reviewers.module';

@Module({
imports: [
MikroOrmModule.forRoot(
process.env.NODE_ENV === 'test' ? testConfig : mikroOrmConfig,
),
AuthModule,
ReviewersModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
12 changes: 8 additions & 4 deletions src/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { EntityGenerator } from '@mikro-orm/entity-generator';
import { Migrator } from '@mikro-orm/migrations';
import { MikroOrmModuleOptions } from '@mikro-orm/nestjs';
import { defineConfig, Options, PostgreSqlDriver } from '@mikro-orm/postgresql';
import {
defineConfig as postgreSQLConfig,
PostgreSqlDriver,
} from '@mikro-orm/postgresql';
import { defineConfig as sqliteConfig, SqliteDriver } from '@mikro-orm/sqlite';

const config: MikroOrmModuleOptions<PostgreSqlDriver> = {
entities: ['./dist/entities'], // Path to your compiled JS entities
Expand All @@ -22,10 +26,10 @@ const config: MikroOrmModuleOptions<PostgreSqlDriver> = {
extensions: [Migrator, EntityGenerator],
};

export default defineConfig(config);
export default postgreSQLConfig(config);

export const testConfig = defineConfig({
export const testConfig = sqliteConfig({
...config,
allowGlobalContext: true,
dbName: ':memory:',
});
} as unknown as MikroOrmModuleOptions<SqliteDriver>);
12 changes: 12 additions & 0 deletions src/reviewers/reviewers.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { ReviewersService } from './reviewers.service';

@Controller('reviewers')
export class ReviewersController {
constructor(private readonly reviwersService: ReviewersService) {}

@Get()
getAll() {
return this.reviwersService.getAll();
}
}
12 changes: 12 additions & 0 deletions src/reviewers/reviewers.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ReviewersService } from './reviewers.service';
import { ReviewersController } from './reviewers.controller';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Reviewer } from '../entities/reviewer';

@Module({
imports: [MikroOrmModule.forFeature([Reviewer])],
controllers: [ReviewersController],
providers: [ReviewersService],
})
export class ReviewersModule {}
16 changes: 16 additions & 0 deletions src/reviewers/reviewers.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EntityRepository } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { Reviewer } from '../entities/reviewer';

@Injectable()
export class ReviewersService {
constructor(
@InjectRepository(Reviewer)
private readonly reviewerRepository: EntityRepository<Reviewer>,
) {}

getAll() {
return this.reviewerRepository.findAll();
}
}
22 changes: 22 additions & 0 deletions src/reviewers/test/reviewers.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Reviewer } from 'src/entities/reviewer';

export const REVIEWER_LIST: Partial<Reviewer>[] = [
{
presona: {
name: 'John Doe',
image:
'https://images.freeimages.com/images/large-previews/83f/paris-1213603.jpg?fmt=webp&w=500',
description: 'hello world',
prompt: 'hello Missing',
},
},
{
presona: {
name: 'James Doe',
image:
'https://images.freeimages.com/images/large-previews/03e/oxford-architecture-1233371.jpg?fmt=webp&w=500',
description: 'hello world2',
prompt: 'hello Missing2',
},
},
];
54 changes: 54 additions & 0 deletions src/reviewers/test/reviewers.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ReviewersController } from '../reviewers.controller';
import { ReviewersService } from '../reviewers.service';
import { getRepositoryToken, MikroOrmModule } from '@mikro-orm/nestjs';
import { Reviewer } from '../../entities/reviewer';
import { testConfig } from '../../mikro-orm.config';
import { REVIEWER_LIST } from './reviewers.common';
import { EntityRepository } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';

describe('ReviwersController', () => {
let controller: ReviewersController;
let reviewerRepository: EntityRepository<Reviewer>;
let orm: MikroORM;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
MikroOrmModule.forRoot(testConfig),
MikroOrmModule.forFeature([Reviewer]),
],
controllers: [ReviewersController],
providers: [ReviewersService],
}).compile();

controller = module.get<ReviewersController>(ReviewersController);
reviewerRepository = module.get<EntityRepository<Reviewer>>(
getRepositoryToken(Reviewer),
);
orm = module.get(MikroORM);
});

beforeEach(async () => {
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('/reviewer (GET)', () => {
it('생성된 reviewers를 조회함', async () => {
// init
await Promise.all(
REVIEWER_LIST.map((reviewer) => reviewerRepository.create(reviewer)),
);
const result = await controller.getAll();

expect(result.length).toEqual(2);
expect(result[0].presona.name).toEqual('John Doe');
});
});
});
50 changes: 50 additions & 0 deletions src/reviewers/test/reviewers.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ReviewersService } from '../reviewers.service';
import { MikroOrmModule, getRepositoryToken } from '@mikro-orm/nestjs';
import { testConfig } from '../../mikro-orm.config';
import { Reviewer } from '../../entities/reviewer';
import { EntityRepository } from '@mikro-orm/postgresql';
import { MikroORM } from '@mikro-orm/sqlite';
import { REVIEWER_LIST } from './reviewers.common';

describe('ReviwersService', () => {
let service: ReviewersService;
let reviewerRepository: EntityRepository<Reviewer>;
let orm: MikroORM;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
MikroOrmModule.forRoot(testConfig),
MikroOrmModule.forFeature([Reviewer]),
],
providers: [ReviewersService],
}).compile();

service = module.get<ReviewersService>(ReviewersService);
reviewerRepository = module.get<EntityRepository<Reviewer>>(
getRepositoryToken(Reviewer),
);
orm = module.get(MikroORM);
});

beforeEach(async () => {
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('생성된 reviewers를 조회함', async () => {
// init
await Promise.all(
REVIEWER_LIST.map((reviewer) => reviewerRepository.create(reviewer)),
);
const result = await service.getAll();

expect(result.length).toEqual(2);
expect(result[0].presona.name).toEqual('John Doe');
});
});
53 changes: 53 additions & 0 deletions test/reviewers.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { EntityRepository } from '@mikro-orm/core';
import { getRepositoryToken } from '@mikro-orm/nestjs';
import { Reviewer } from '../src/entities/reviewer';
import { REVIEWER_LIST } from '../src/reviewers/test/reviewers.common';
import { MikroORM } from '@mikro-orm/sqlite';

describe('ReviewerController (e2e)', () => {
let app: INestApplication;
let orm: MikroORM;
let reviewerRepository: EntityRepository<Reviewer>;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
transform: true,
}),
);

await app.init();

orm = moduleFixture.get(MikroORM);
reviewerRepository = moduleFixture.get<EntityRepository<Reviewer>>(
getRepositoryToken(Reviewer),
);

await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
await Promise.all(
REVIEWER_LIST.map((reviewer) => reviewerRepository.create(reviewer)),
);
await orm.em.flush();
});

describe('/reviewers (GET)', () => {
it('2명의 리뷰어가 생성된다.', async () => {
const response = await request(app.getHttpServer())
.get('/reviewers')
.send()
.expect(200);

expect(response.body.length).toEqual(2);
});
});
});