parent
444ff311d8
commit
b3a57701a6
@ -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