Merge pull request '1.add ut for TagDao,2.enable auto set createTime and updateTime;' (#1) from add_ut_for_tag into main

Reviewed-on: https://go.indie.cn/git/queyue-backend/luoo_parent/pulls/1
main
foo 11 months ago
commit 0b1da5c5b9

@ -42,6 +42,10 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>app</finalName> <finalName>app</finalName>

@ -6,12 +6,15 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import util.IdWorker; import util.IdWorker;
@SpringBootApplication(scanBasePackages = "com.luoo") @SpringBootApplication(scanBasePackages = "com.luoo")
@EnableEurekaClient @EnableEurekaClient
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients @EnableFeignClients
@EnableCaching @EnableCaching
@EnableJpaAuditing
public class TagApplication { public class TagApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -4,14 +4,21 @@ package com.luoo.tag.pojo;
import lombok.Data; import lombok.Data;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Data @Data
@Entity @Entity
@Table(name = "tb_tag") @Table(name = "tb_tag")
@EntityListeners(AuditingEntityListener.class)
public class Tag implements Serializable { public class Tag implements Serializable {
private static final long serialVersionUID = -5593148272164146667L; private static final long serialVersionUID = -5593148272164146667L;
@ -46,10 +53,12 @@ public class Tag implements Serializable {
/** /**
* *
*/ */
@CreatedDate
private LocalDateTime createTime; private LocalDateTime createTime;
/** /**
* *
*/ */
@LastModifiedDate
private LocalDateTime updateTime; private LocalDateTime updateTime;
} }

@ -0,0 +1,57 @@
package com.luoo.tag.dao;
import static org.junit.Assert.*;
import org.junit.Test;
import java.time.LocalDateTime;
import java.util.stream.IntStream;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import com.luoo.tag.pojo.Tag;
@RunWith(SpringRunner.class)
@DataJpaTest
public class TagDaoTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private TagDao dao;
@Test
public void testPagable() throws InterruptedException {
saveTags();
assertEquals(dao.count(), 100L);
Pageable pageRequest = PageRequest.of(0, 2);
Page<Tag> tags = dao.findAll(pageRequest);
assertEquals(tags.getSize(), 2);
Tag firstTag = tags.iterator().next();
String firstTagId = "tag-0";
assertEquals(firstTag.getId(), firstTagId);
LocalDateTime updateTimeBefore = firstTag.getUpdateTime();
firstTag.setState(1);
entityManager.persistAndFlush(firstTag);
assertEquals(dao.count(), 100L);
LocalDateTime updateTimeAfter = dao.findById(firstTagId).get().getUpdateTime();
assertTrue(updateTimeAfter.isAfter(updateTimeBefore));
}
private void saveTags() {
IntStream.range(0, 100).forEach(id -> {
Tag tag = new Tag();
tag.setId("tag-" + id);
entityManager.persist(tag);
});
}
}
Loading…
Cancel
Save