dao层修改 单元测试
This commit is contained in:
119
src/test/java/cn/celess/blog/mapper/CategoryMapperTest.java
Normal file
119
src/test/java/cn/celess/blog/mapper/CategoryMapperTest.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package cn.celess.blog.mapper;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Category;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CategoryMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Category category = generateCategory();
|
||||
assertNotNull(category.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Category category = generateCategory();
|
||||
assertFalse(category.isDeleted());
|
||||
int lines = categoryMapper.delete(category.getId());
|
||||
assertNotEquals(0, lines);
|
||||
Category categoryById = categoryMapper.findCategoryById(category.getId());
|
||||
assertTrue(categoryById.isDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Category category = generateCategory();
|
||||
category.setName(randomStr(4));
|
||||
int update = categoryMapper.update(category);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsByName(category.getName()));
|
||||
assertFalse(categoryMapper.existsByName(randomStr(8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryByName() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
assertNotNull(suibi);
|
||||
|
||||
// tag 数据
|
||||
Category shiro = categoryMapper.findCategoryByName("shiro");
|
||||
assertNull(shiro);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryById() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
|
||||
Category categoryById = categoryMapper.findCategoryById(suibi.getId());
|
||||
|
||||
assertEquals(suibi.getName(), categoryById.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
assertNotEquals(0, all);
|
||||
all.forEach(category -> assertTrue(category.isCategory()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllName() {
|
||||
List<String> allName = categoryMapper.getAllName();
|
||||
assertNotEquals(0, allName.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNameById() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getName(), categoryMapper.getNameById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIdByName() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getId(), categoryMapper.getIdByName(category.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestCategory() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
all.sort((o1, o2) -> (int) (o2.getId() - o1.getId()));
|
||||
assertEquals(all.get(0).getId(), categoryMapper.getLastestCategory().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
assertEquals(all.size(), categoryMapper.count());
|
||||
}
|
||||
|
||||
private Category generateCategory() {
|
||||
Category category = new Category(randomStr(4));
|
||||
|
||||
categoryMapper.insert(category);
|
||||
category.setDeleted(false);
|
||||
return category;
|
||||
}
|
||||
}
|
||||
91
src/test/java/cn/celess/blog/mapper/TagMapperTest.java
Normal file
91
src/test/java/cn/celess/blog/mapper/TagMapperTest.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package cn.celess.blog.mapper;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TagMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Tag tag = generateTag();
|
||||
assertNotNull(tag.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Tag tag = generateTag();
|
||||
tag.setName(randomStr(4));
|
||||
int update = tagMapper.update(tag);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Tag tag = generateTag();
|
||||
assertFalse(tag.isDeleted());
|
||||
assertEquals(1, tagMapper.delete(tag.getId()));
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertTrue(tagById.isDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagById() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagByName() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagByName(tag.getName());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
String s = randomStr(4);
|
||||
assertFalse(tagMapper.existsByName(s));
|
||||
Tag tag = new Tag(s);
|
||||
tagMapper.insert(tag);
|
||||
assertTrue(tagMapper.existsByName(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestTag() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
assertEquals(all.get(0).getId(), tagMapper.getLastestTag().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
assertNotEquals(0, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
assertNotEquals(0, tagMapper.count());
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
assertEquals(all.size(), tagMapper.count());
|
||||
}
|
||||
|
||||
private Tag generateTag() {
|
||||
Tag tag = new Tag(randomStr(4));
|
||||
tagMapper.insert(tag);
|
||||
tag.setDeleted(false);
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user