Skip to content

Commit

Permalink
test: global 환경에서 생성 된 container를 재사용하여 테스트할 수 있도록 설정 #72
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 12, 2024
1 parent 1bd8457 commit eaee784
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
3 changes: 0 additions & 3 deletions backend/test/config/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ export default async () => {
const ddlScriptPath = path.resolve(__dirname, '../../resources/sql/DDL.sql');

global.reusedContainer = await new MySqlContainer()
.withUsername('testUser')
.withUserPassword('testPassword')
.withDatabase('testDB')
.withReuse()
.withBindMounts([
{ source: ddlScriptPath, target: '/docker-entrypoint-initdb.d/DDL.sql' },
Expand Down
29 changes: 6 additions & 23 deletions backend/test/place/place.repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql';
import { DataSource } from 'typeorm';
import { PlaceRepository } from '../../src/place/place.repository';
import { createTestDataSource } from '../datasource.config';
import { PlaceFixture } from './place.fixture';
import { initDataSource } from '../config/datasource.config';
import { PlaceFixture } from './fixture/place.fixture';
import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql';

describe('PlaceRepository', () => {
let placeRepository: PlaceRepository;
let testDataSource: DataSource;
let container: StartedMySqlContainer;
let placeRepository: PlaceRepository;

beforeAll(async () => {
container = await new MySqlContainer()
.withUsername('testUser')
.withUserPassword('testPassword')
.withDatabase('testDB')
.start();

testDataSource = await createTestDataSource(container);
await testDataSource.initialize();
placeRepository = new PlaceRepository(testDataSource);
});

afterAll(async () => {
await testDataSource.destroy();
await container.stop();
container = await new MySqlContainer().withReuse().start();
placeRepository = new PlaceRepository(await initDataSource(container));
});

beforeEach(async () => {
await placeRepository.delete({});
});

it('장소 이름이나 주소에 포함된 키워드를 찾아 해당하는 장소를 반환한다.', async () => {
// given
const placesWithParkName = [
{
googlePlaceId: 'googlePlaceId_1',
Expand Down Expand Up @@ -68,7 +53,6 @@ describe('PlaceRepository', () => {
);
await placeRepository.save(places);

// when
const page = 1;
const pageSize = 10;
const query = 'park';
Expand All @@ -78,7 +62,6 @@ describe('PlaceRepository', () => {
pageSize,
);

// then
expect(results.length).toBe(3);

expect(results).toEqual(
Expand Down

0 comments on commit eaee784

Please sign in to comment.