-
Notifications
You must be signed in to change notification settings - Fork 14
오단비 1주차 과제 #5
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
base: main
Are you sure you want to change the base?
오단비 1주차 과제 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||
package doit.jpastudy2.repository; | ||||||
|
||||||
import org.springframework.data.jpa.repository.JpaRepository; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 부분 이렇게 작성하셔도 동작은 하는데, 보통은 <Entity Class 이름, 그 클래스의 PK 타입>으로 작성하는 편입니다.
Suggested change
|
||||||
public interface ArmyRepository extends JpaRepository<Army, Long> { | ||||||
|
||||||
// 쿼리 메소드 패턴은 다음과 같다. | ||||||
// [ ] = Optional | ||||||
// ( ) = 조건 | ||||||
// find + [ ] + By + (조건) | ||||||
|
||||||
Army findByDescription(String description); | ||||||
Army findByName(String name); | ||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package doit.jpastudy2.repository; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
|
||
public class Personal { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
|
||
@Column(name="군번") | ||
private int id; | ||
|
||
@Column(name = "출생년도") | ||
private int birthYear; | ||
|
||
@Column(name = "생일") | ||
private int birthDay; | ||
|
||
@Column(name="전화번호") | ||
private String phoneNumber; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package doit.jpastudy2.repository; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
|
@@ -12,97 +10,88 @@ | |
|
||
@Transactional // 테스트 케이스에 이 어노테이션이 있으면, 테스트가 끝나면 롤백을 해준다. ( 데이터베이스 초기화 ) | ||
@SpringBootTest // 스프링 컨테이너를 이용한 테스트 | ||
class CategoryRepositoryTest { | ||
class ArmyRepositoryTest { | ||
|
||
@Autowired // 스프링이 관리하는 빈을 주입받는다. | ||
private CategoryRepository categoryRepository; | ||
private ArmyRepository categoryRepository; | ||
|
||
@DisplayName("save 테스트") | ||
@Test | ||
void test() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
Army army1 = Army.builder() | ||
.description("이발병") | ||
.build(); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
Army army2 = Army.builder() | ||
.description("통신병") | ||
.build(); | ||
|
||
// When | ||
categoryRepository.save(category1); | ||
categoryRepository.save(category2); | ||
categoryRepository.save(army1); | ||
categoryRepository.save(army2); | ||
|
||
// Then | ||
List<Category> categories = categoryRepository.findAll(); | ||
List<Army> categories = categoryRepository.findAll(); | ||
Assertions.assertThat(categories).hasSize(2); | ||
Assertions.assertThat(categories.get(0).getType()).isEqualTo("양식"); | ||
Assertions.assertThat(categories.get(0).getDescription()).isEqualTo("데이트"); | ||
Assertions.assertThat(categories.get(1).getDescription()).isEqualTo("통신병"); | ||
} | ||
|
||
@DisplayName("Description을 이용한 조회") | ||
@Test | ||
void findByDescription() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
Army army1 = Army.builder() | ||
.description("행정병") | ||
.build(); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
Army army2 = Army.builder() | ||
.description("운전병") | ||
.build(); | ||
|
||
categoryRepository.save(category1); | ||
categoryRepository.save(category2); | ||
categoryRepository.save(army1); | ||
categoryRepository.save(army2); | ||
|
||
// When | ||
Category result1 = categoryRepository.findByDescription("철가방"); | ||
Category result2 = categoryRepository.findByDescription("데이트"); | ||
Army result1 = categoryRepository.findByDescription("통신병"); | ||
Army result2 = categoryRepository.findByDescription("운전병"); | ||
|
||
// Then | ||
Assertions.assertThat(result1).isNull(); | ||
Assertions.assertThat(result2).isNotNull(); | ||
Assertions.assertThat(result2.getType()).isEqualTo("양식"); | ||
} | ||
|
||
@DisplayName("description과 type을 이용한 조회") | ||
@DisplayName("name을 이용한 조회") | ||
@Test | ||
void findByTypeAndDescription() { | ||
void findByName() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
Army army1 = Army.builder() | ||
.name("박준혁") | ||
.build(); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
Army army2 = Army.builder() | ||
.name("노승현") | ||
.build(); | ||
|
||
Category category3 = Category.builder() | ||
.type("중식") | ||
.description("철가방") | ||
Army army3 = Army.builder() | ||
.name("설만수") | ||
.build(); | ||
|
||
Category category4 = Category.builder() | ||
.type("미식") | ||
.description("축구ㅋㅋ") | ||
Army army4 = Army.builder() | ||
.name("현서호") | ||
.build(); | ||
|
||
categoryRepository.saveAll(List.of(category1, category2, category3, category4)); | ||
|
||
categoryRepository.saveAll(List.of(army1, army2, army3, army4)); | ||
|
||
// When | ||
Category result1 = categoryRepository.findByTypeAndDescription("양식", "데이트"); | ||
Category result2 = categoryRepository.findByTypeAndDescription("중식", "데이트"); // null | ||
Category result3 = categoryRepository.findByTypeAndDescription("미식", "축구ㅋㅋ"); | ||
Army result1 = categoryRepository.findByName("오단비"); | ||
Army result2 = categoryRepository.findByName("박준혁"); | ||
Army result3 = categoryRepository.findByName("노승현"); | ||
|
||
// Then | ||
Assertions.assertThat(result1.getType()).isEqualTo("양식"); | ||
Assertions.assertThat(result2).isNull(); | ||
Assertions.assertThat(result3.getDescription()).isEqualTo("축구ㅋㅋ"); | ||
Assertions.assertThat(result1).isNull(); | ||
Assertions.assertThat(result2).isNotNull(); | ||
Assertions.assertThat(result3.getName()).isEqualTo("노승현"); | ||
Comment on lines
+93
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굿굿굿입니다. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주제가 재밌네요ㅎㅎ..
Column명을 한국어로 작성하여도 동작은 정상적으로 하지만, 보통은 영어로 적는 편이 관례입니다.
참고하시면 될 것 같아요!