-
Notifications
You must be signed in to change notification settings - Fork 1
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
2주차 Test Code 작성 #2
Open
unanchoi
wants to merge
1
commit into
main
Choose a base branch
from
week2
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
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 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 |
---|---|---|
|
@@ -58,9 +58,10 @@ private void validateAge(final int age) { | |
|
||
// SOPT는 한국인만 가입 가능함. | ||
private void validateName(final String name) { | ||
// if (!name.matches("ㅎ가-힣")) { | ||
// throw new MemberException("한글만 가능합니다."); | ||
// } | ||
if (!name.matches("^[가-힣]*$")) { | ||
throw new MemberException("한글만 가능합니다."); | ||
} | ||
|
||
if (name.length() > MAX_LENGTH) { | ||
throw new MemberException("유저의 이름은 12자를 넘을 수 없습니다."); | ||
} | ||
|
@@ -74,6 +75,6 @@ private void validateNickname(final String nickname) { | |
} | ||
|
||
public void updateSOPT(SOPT sopt) { | ||
this.sopt = sopt; | ||
this.sopt.updateSopt(sopt); | ||
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. 하단에 updateSopt 함수를 만들고 여기서 한번 더 실행하는 이유가 있을까요? |
||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
24 changes: 22 additions & 2 deletions
24
spring-code-for-deploy/src/main/java/com/example/seminar/service/post/PostEditor.java
This file contains 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,15 +1,35 @@ | ||
package com.example.seminar.service.post; | ||
|
||
import com.example.seminar.domain.Post; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.ScrollableResults; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.StatelessSession; | ||
import org.hibernate.Transaction; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class PostEditor { | ||
private final EntityManagerFactory emf; | ||
|
||
public void editContent(Post post, String content) { | ||
post.updateContent(content); | ||
public void editContent(final long postId, String content) { | ||
SessionFactory sessionFactory = emf.unwrap(SessionFactory.class); | ||
StatelessSession session = sessionFactory.openStatelessSession(); | ||
Transaction tx = session.beginTransaction(); | ||
ScrollableResults scroll = session.createQuery("select p from Post p where p.id = :id") | ||
.setParameter("id", postId) | ||
.scroll(); | ||
|
||
while (scroll.next()) { | ||
Post post = (Post) scroll.get(); | ||
System.out.println("post = " + post.getContent()); | ||
post.updateContent(content); | ||
session.update(post); | ||
} | ||
tx.commit(); | ||
session.close(); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
39 changes: 39 additions & 0 deletions
39
spring-code-for-deploy/src/test/java/com/example/seminar/domain/PostTest.java
This file contains 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,10 +1,49 @@ | ||
package com.example.seminar.domain; | ||
|
||
|
||
import com.example.seminar.common.exception.PostException; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@DataJpaTest | ||
@ActiveProfiles("test") | ||
public class PostTest { | ||
|
||
|
||
@Test | ||
@DisplayName("제목이 50자를 넘으면, 게시글을 생성할 수 없다.") | ||
void postWithInvalidTitle() { | ||
// given | ||
String dummyTitle = "a".repeat(51); | ||
// when | ||
|
||
Assertions.assertThatThrownBy( | ||
() -> { | ||
Post post = Post.builder() | ||
.title(dummyTitle) | ||
.member(createMember("오해영")) | ||
.content("내용") | ||
.build(); | ||
} | ||
).isInstanceOf(PostException.class) | ||
.hasMessage("제목은 50자 이하여야 합니다."); | ||
|
||
// then | ||
|
||
} | ||
private Member createMember(String name) { | ||
SOPT sopt = SOPT.builder() | ||
.part(Part.SERVER) | ||
.build(); | ||
return Member.builder() | ||
.age(99) | ||
.name(name) | ||
.sopt(sopt) | ||
.nickname("5hae0") | ||
.build(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spring-code-for-deploy/src/test/java/com/example/seminar/domain/SoptTest.java
This file contains 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,22 @@ | ||
package com.example.seminar.domain; | ||
|
||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SoptTest { | ||
|
||
@Test | ||
@DisplayName("등록되는 SOPT회원은 현재 기수여야 한다.") | ||
void sopt() { | ||
short CURRENT_GENERATION = (short) 34; | ||
|
||
SOPT sopt = SOPT.builder() | ||
.part(Part.SERVER) | ||
.build(); | ||
// then | ||
Assertions.assertThat(sopt.getGeneration()).isEqualTo(CURRENT_GENERATION); | ||
|
||
} | ||
} |
This file contains 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
This file contains 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
57 changes: 57 additions & 0 deletions
57
spring-code-for-deploy/src/test/java/com/example/seminar/service/post/PostEditorTest.java
This file contains 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,57 @@ | ||
package com.example.seminar.service.post; | ||
|
||
|
||
import com.example.seminar.domain.Member; | ||
import com.example.seminar.domain.Part; | ||
import com.example.seminar.domain.Post; | ||
import com.example.seminar.domain.SOPT; | ||
import com.example.seminar.repository.PostJpaRepository; | ||
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.test.context.ActiveProfiles; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
public class PostEditorTest { | ||
|
||
@Autowired | ||
PostEditor postEditor; | ||
|
||
@Autowired | ||
PostSaver postSaver; | ||
|
||
@Autowired | ||
PostJpaRepository postJpaRepository; | ||
|
||
@Autowired | ||
PostRetriever postRetriever; | ||
|
||
@Test | ||
@DisplayName("게시글의 내용을 수정할 수 있다.") | ||
void editContent() { | ||
Member member = Member.builder() | ||
.age(20) | ||
.name("오해영") | ||
.nickname("오해영") | ||
.sopt( | ||
SOPT.builder() | ||
.part(Part.SERVER) | ||
.build() | ||
).build(); | ||
// given | ||
Post post = Post.builder() | ||
.member(member) | ||
.title("제목") | ||
.content("내용") | ||
.build(); | ||
// when | ||
Post savedPost = postJpaRepository.saveAndFlush(post); | ||
postEditor.editContent(savedPost.getId(), "수정된 내용"); | ||
Post findPost = postRetriever.findById(savedPost.getId()); | ||
// then | ||
Assertions.assertThat(findPost.getContent()).isEqualTo("수정된 내용"); | ||
} | ||
} |
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.
정규식 이슈였군요..