-
Notifications
You must be signed in to change notification settings - Fork 14
[이나현]1주차 과제 #13
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
Open
nAhYUN22
wants to merge
1
commit into
doit-web-study:main
Choose a base branch
from
nAhYUN22:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[이나현]1주차 과제 #13
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,23 @@ | ||
package doit.jpastudy2.repository; | ||
package doit.jpastudy_hw.repository; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
|
||
@Id // PK임을 나타낸다. | ||
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다. | ||
@Column(name = "category_id") // 컬럼명을 지정한다. | ||
@Column(name = "category_id") | ||
private Long id; | ||
|
||
// @Column(name = "type")이 생략된 경우 필드명이 컬럼명이 된다. snake_case로 변환된다. | ||
private String type; | ||
|
||
// @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다. | ||
private String description; | ||
private String subject; | ||
|
||
@Builder // 빌더 패턴을 사용할 수 있게 한다. | ||
public Category(String description, String type) { | ||
this.description = description; | ||
this.type = type; | ||
public Category(String subject) { | ||
this.subject = subject; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package doit.jpastudy_hw.repository; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class Notice { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
private String writer; | ||
|
||
private String title; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "category_id") // 외래 키 설정 | ||
private Category category; | ||
|
||
public Notice(String writer, String title, Category category) { | ||
this.writer = writer; | ||
this.title = title; | ||
this.category = category; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/doit/jpastudy2/repository/NoticeRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package doit.jpastudy_hw.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long> { | ||
} |
107 changes: 13 additions & 94 deletions
107
src/test/java/doit/jpastudy2/repository/CategoryRepositoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,27 @@ | ||
package doit.jpastudy2.repository; | ||
package doit.jpastudy_hw.repository; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional // 테스트 케이스에 이 어노테이션이 있으면, 테스트가 끝나면 롤백을 해준다. ( 데이터베이스 초기화 ) | ||
@SpringBootTest // 스프링 컨테이너를 이용한 테스트 | ||
class CategoryRepositoryTest { | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Autowired // 스프링이 관리하는 빈을 주입받는다. | ||
@Transactional | ||
@SpringBootTest | ||
class CategoryTest { | ||
@Autowired | ||
private CategoryRepository categoryRepository; | ||
|
||
@DisplayName("save 테스트") | ||
@Test | ||
void test() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
.build(); | ||
void test(){ | ||
Category category = new Category("축구"); | ||
categoryRepository.save(category); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
.build(); | ||
|
||
// When | ||
categoryRepository.save(category1); | ||
categoryRepository.save(category2); | ||
|
||
// Then | ||
List<Category> categories = categoryRepository.findAll(); | ||
Assertions.assertThat(categories).hasSize(2); | ||
Assertions.assertThat(categories.get(0).getType()).isEqualTo("양식"); | ||
Assertions.assertThat(categories.get(0).getDescription()).isEqualTo("데이트"); | ||
} | ||
|
||
@DisplayName("Description을 이용한 조회") | ||
@Test | ||
void findByDescription() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
.build(); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
.build(); | ||
|
||
categoryRepository.save(category1); | ||
categoryRepository.save(category2); | ||
|
||
// When | ||
Category result1 = categoryRepository.findByDescription("철가방"); | ||
Category result2 = categoryRepository.findByDescription("데이트"); | ||
|
||
// Then | ||
Assertions.assertThat(result1).isNull(); | ||
Assertions.assertThat(result2).isNotNull(); | ||
Assertions.assertThat(result2.getType()).isEqualTo("양식"); | ||
} | ||
|
||
@DisplayName("description과 type을 이용한 조회") | ||
@Test | ||
void findByTypeAndDescription() { | ||
// Given | ||
Category category1 = Category.builder() | ||
.type("양식") | ||
.description("데이트") | ||
.build(); | ||
|
||
Category category2 = Category.builder() | ||
.type("한식") | ||
.description("한국인의 정") | ||
.build(); | ||
|
||
Category category3 = Category.builder() | ||
.type("중식") | ||
.description("철가방") | ||
.build(); | ||
|
||
Category category4 = Category.builder() | ||
.type("미식") | ||
.description("축구ㅋㅋ") | ||
.build(); | ||
|
||
categoryRepository.saveAll(List.of(category1, category2, category3, category4)); | ||
|
||
// When | ||
Category result1 = categoryRepository.findByTypeAndDescription("양식", "데이트"); | ||
Category result2 = categoryRepository.findByTypeAndDescription("중식", "데이트"); // null | ||
Category result3 = categoryRepository.findByTypeAndDescription("미식", "축구ㅋㅋ"); | ||
|
||
// Then | ||
Assertions.assertThat(result1.getType()).isEqualTo("양식"); | ||
Assertions.assertThat(result2).isNull(); | ||
Assertions.assertThat(result3.getDescription()).isEqualTo("축구ㅋㅋ"); | ||
System.out.println("categories = " + categories.get(0)); | ||
System.out.println("categories = " + categories.get(0).getSubject()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/doit/jpastudy2/repository/NoticeRepositoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package doit.jpastudy_hw.repository; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
class NoticeTest { | ||
|
||
@Autowired | ||
private NoticeRepository noticeRepository; | ||
|
||
@Autowired | ||
private CategoryRepository categoryRepository; | ||
|
||
@Test | ||
void saveAndRetrieveNotice() { | ||
// 1. 새로운 Category 엔티티 생성 후 저장 | ||
Category category = new Category("Sports"); | ||
categoryRepository.save(category); | ||
|
||
// 2. 새로운 Notice 엔티티 생성 후 저장 | ||
Notice notice = new Notice("John Doe", "Football Match", category); | ||
noticeRepository.save(notice); | ||
|
||
// 3. 저장된 Notice 엔티티 모두 조회 | ||
List<Notice> notices = noticeRepository.findAll(); | ||
|
||
// 4. Notice 엔티티가 잘 저장되었는지 확인 | ||
assertEquals(1, notices.size()); | ||
assertEquals("John Doe", notices.get(0).getWriter()); | ||
assertEquals("Football Match", notices.get(0).getTitle()); | ||
|
||
// 5. Notice와 연결된 Category 확인 | ||
assertNotNull(notices.get(0).getCategory()); | ||
assertEquals("Sports", notices.get(0).getCategory().getSubject()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Junit5 메소드로 직접 검증하신 것 너무 좋습니다!!!!