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

테스트 코드 작성 환경 설정 #85

Merged
merged 15 commits into from
Apr 16, 2024
Merged

테스트 코드 작성 환경 설정 #85

merged 15 commits into from
Apr 16, 2024

Conversation

NameIsUser06
Copy link
Member

@NameIsUser06 NameIsUser06 commented Apr 15, 2024

  • 서비스 테스트를 Transactinoal로 처리하기 위한 어노테이션을 구현했습니다
  • Truncate 처리 코드를 구현했습니다
  • yml에 환경변수를 사용하지 않기 위해 더미데이터로 넣어 놨습니다

@transactional 어노테이션을 사용하는 상황

  • 서비스, 레포지토리 등을 테스트하는 상황

truncate를 사용해야 하는 상황

  • 인수테스트와 같이 스프링 컨테이너를 별도의 스레드에서 돌림으로써 @transactional 어노테이션이 적용이 않는 등 이러한 상황

@NameIsUser06 NameIsUser06 linked an issue Apr 15, 2024 that may be closed by this pull request
2 tasks
Copy link

sonarcloud bot commented Apr 15, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@hw9402 hw9402 changed the title Test/#79 테스트 코드 작성 환경 설정 Apr 16, 2024
@Transactional
@SpringBootTest
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public @interface ServiceTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 항상 추상클래스로 구현 받아왔는데, 인터페이스로 처리하는 것도 좋네요!!

@jacobhboy jacobhboy self-requested a review April 16, 2024 05:04
Copy link
Member

@jacobhboy jacobhboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정에 대한 이유도 설명해주셔야할 것 같아요.

Comment on lines +16 to +34
public void beforeEach() {
final List<String> truncateQueries = getTruncateQueries(jdbcTemplate);
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY FALSE");
truncateTables(jdbcTemplate, truncateQueries);
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY TRUE");
}

private List<String> getTruncateQueries(final JdbcTemplate jdbcTemplate) {
return jdbcTemplate.queryForList("SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ' RESTART IDENTITY;') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class);
}

private void truncateTables(final JdbcTemplate jdbcTemplate, final List<String> truncateQueries) {
truncateQueries.forEach(v -> execute(jdbcTemplate, v));
}

private void execute(final JdbcTemplate jdbcTemplate, final String query) {
jdbcTemplate.execute(query);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거의 전체 흐름을 설명해주세요

Copy link
Member Author

@NameIsUser06 NameIsUser06 Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truncate 처리를 코드에서 진행하기 위한 함수입니다

  • 먼저 REFERENTIAL_INTEGRITY는 외래키 제약조건 설정입니다.
  1. 테이블을 순서에 상관없이 truncate 하기 위해 끈 상태에서
  2. INFORMATION_SCHEMA.TABLES라는 곳에서 테이블 이름을 모두 가져옵니다
  3. 그 테이블 이름을 가져옴과 동시에 TRUNCATE TABLE TABLE_NAME RESTART IDENTITY 라는 문자열 형태로 가져옵니다
  4. forEach 형태를 통해 각 테이블 truncate를 실행합니다
  5. 다시 외래키 제약조건을 켭니다

위와같은 형태로 테이블 개수에 상관없이 모든 테이블을 truncate하는 것을 코드 형태로 작성된 클래스입니다

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳굳!

@NameIsUser06 NameIsUser06 merged commit 55dadf8 into master Apr 16, 2024
0 of 2 checks passed
@NameIsUser06 NameIsUser06 deleted the test/#79 branch April 16, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

테스트 작성 형식
4 participants