-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: baseEntity 생성 & FeedEntity 골격만 생성
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
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
30 changes: 30 additions & 0 deletions
30
sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt
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,30 @@ | ||
package joryu.sns_service.feed.entity | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.Table | ||
import joryu.sns_service.profile.entity.BaseEntity | ||
|
||
@Table(name = "feed") | ||
@Entity | ||
class Feed( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long, | ||
|
||
content: String, | ||
) : BaseEntity() { | ||
constructor() : this(0, "") | ||
constructor(content: String) : this(0, content) | ||
|
||
@Column(name = "view_count", nullable = false) | ||
var viewCount: Long = 0 | ||
private set | ||
|
||
@Column(name = "content", nullable = false, length = 1000) | ||
var content: String = content | ||
private set | ||
} |
6 changes: 6 additions & 0 deletions
6
sns_service/src/main/kotlin/joryu/sns_service/feed/repository/FeedRepository.kt
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,6 @@ | ||
package joryu.sns_service.feed.repository | ||
|
||
import joryu.sns_service.feed.entity.Feed | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface FeedRepository : JpaRepository<Feed, Long> |
24 changes: 24 additions & 0 deletions
24
sns_service/src/main/kotlin/joryu/sns_service/profile/entity/BaseEntity.kt
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,24 @@ | ||
package joryu.sns_service.profile.entity | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.EntityListeners | ||
import jakarta.persistence.MappedSuperclass | ||
import org.springframework.data.annotation.CreatedDate | ||
import org.springframework.data.annotation.LastModifiedDate | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener | ||
import java.time.LocalDateTime | ||
|
||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener::class) | ||
abstract class BaseEntity { | ||
|
||
@CreatedDate | ||
@Column(name = "create_at", nullable = false, updatable = false) | ||
var createAt: LocalDateTime = LocalDateTime.now() | ||
private set | ||
|
||
@LastModifiedDate | ||
@Column(name = "update_at", nullable = false) | ||
var updateAt: LocalDateTime = LocalDateTime.now() | ||
private set | ||
} |
51 changes: 51 additions & 0 deletions
51
sns_service/src/test/kotlin/joryu/sns_service/feed/entity/FeedEntityTest.kt
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,51 @@ | ||
package joryu.sns_service.feed.entity | ||
|
||
import jakarta.persistence.EntityManager | ||
import joryu.sns_service.feed.repository.FeedRepository | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Disabled | ||
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.time.LocalDateTime | ||
|
||
@Transactional | ||
@SpringBootTest | ||
@Disabled("프로젝트 초기 kotlin entity 테스트용도. springBootTest라 속도가 느려서 disabled.") | ||
class FeedEntityTest { | ||
|
||
@Autowired | ||
lateinit var em: EntityManager | ||
|
||
@Autowired | ||
lateinit var feedRepository: FeedRepository | ||
|
||
@Test | ||
fun `BaseEntity에 createAt, updateAt이 잘 들어가야 한다`() { | ||
val before = LocalDateTime.now() | ||
val feedEntity = feedRepository.save(Feed("123")) | ||
|
||
em.flush() | ||
em.clear() | ||
|
||
val findFeedEntity = feedRepository.getReferenceById(feedEntity.id) | ||
|
||
assertThat(findFeedEntity.createAt).isNotNull() | ||
assertThat(findFeedEntity.createAt).isAfter(before) | ||
assertThat(findFeedEntity.createAt).isBefore(LocalDateTime.now()) | ||
assertThat(findFeedEntity.updateAt).isNotNull() | ||
assertThat(findFeedEntity.updateAt).isAfter(before) | ||
assertThat(findFeedEntity.updateAt).isBefore(LocalDateTime.now()) | ||
} | ||
|
||
@Test | ||
fun `BaseEntity의 Id는 auto increment 되어야 한다`() { | ||
val feedEntity1 = feedRepository.save(Feed("123")) | ||
val feedEntity2 = feedRepository.save(Feed("123")) | ||
val feedEntity3 = feedRepository.save(Feed("123")) | ||
|
||
assertThat(feedEntity1.id).isLessThan(feedEntity2.id) | ||
assertThat(feedEntity2.id).isLessThan(feedEntity3.id) | ||
} | ||
} |