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/1main
commit
0b1da5c5b9
@ -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…
Reference in new issue