Skip to content

Commit

Permalink
test: 테스트 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyeonyy committed Jul 23, 2024
1 parent 85c400e commit 7911373
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
43 changes: 26 additions & 17 deletions backend/src/test/java/mouda/backend/config/DatabaseCleaner.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package mouda.backend.config;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import jakarta.persistence.EntityManager;

@Component
public class DatabaseCleaner {

@Autowired
private JdbcTemplate jdbcTemplate;

public DatabaseCleaner(final JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public void cleanUp() {
String sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC' AND table_type='BASE TABLE'";
List<String> tables = jdbcTemplate.queryForList(sql, String.class);
for (String tableName : tables) {
jdbcTemplate.update("DELETE FROM " + tableName);
jdbcTemplate.update("ALTER TABLE " + tableName + " alter column id restart with 1");
}
}
@Autowired
private EntityManager entityManager;

public DatabaseCleaner(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Transactional
public void cleanUp() {
// String sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC' AND table_type='BASE TABLE'";
// List<String> tables = jdbcTemplate.queryForList(sql, String.class);
// for (String tableName : tables) {
// jdbcTemplate.update("DELETE FROM " + tableName);
// jdbcTemplate.update("ALTER TABLE " + tableName + " alter column id restart with 1");
// }
entityManager.createNativeQuery("DELETE FROM MEMBER").executeUpdate();
entityManager.createNativeQuery("ALTER TABLE MEMBER alter column id restart with 1").executeUpdate();

entityManager.createNativeQuery("DELETE FROM MOIM").executeUpdate();
entityManager.createNativeQuery("ALTER TABLE MOIM alter column id restart with 1").executeUpdate();

entityManager.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ class MemberRepositoryTest {

@Autowired
private MoimRepository moimRepository;
//
// @Autowired
// private DatabaseCleaner databaseCleaner;
//
// @AfterEach
// void cleanUp() {
// databaseCleaner.cleanUp();
// }

@DisplayName("모임에 가입된 맴버의 이름을 반환한다.")
@Test
void findNickNamesByMoimId() {
Member member = new Member("tehah");
Moim moim = new Moim().builder()
Moim moim = Moim.builder()
.build();
Moim saveMoim = moimRepository.save(moim);
member.joinMoim(moim);
member.joinMoim(saveMoim);
memberRepository.save(member);

List<String> participants = memberRepository.findNickNamesByMoimId(saveMoim.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void joinMoim() {
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);
Moim moim = moimService.createMoim(moimCreateRequest);

MoimJoinRequest moimJoinRequest = new MoimJoinRequest(1L, "호기");
moimService.joinMoim(moimJoinRequest);
Expand Down

0 comments on commit 7911373

Please sign in to comment.