修改用户,部门,字典表单元测试方法

This commit is contained in:
lq_avenue 2016-11-01 19:14:14 +08:00
parent a5b937889f
commit 60424b81da
3 changed files with 205 additions and 93 deletions

View File

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
@ -7,48 +9,54 @@ namespace Bootstrap.DataAccess.Tests
public class DictHelperTests
{
private Dict Dict { get; set; }
[TestInitialize]
public void Initialized()
{
Dict = new Dict() { Category = "__测试菜单__", Name = "__测试子菜单1__", Code = "2" };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Dicts where Category = '__测试菜单__'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void RetrieveDictsTest()
{
SaveDictTest();
var result = DictHelper.RetrieveDicts("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的DictHelper.RetrieveDicts方法调用失败请检查数据库连接或者数据库SQL语句");
result = DictHelper.RetrieveDicts();
Assert.IsTrue(result.Count() >= 0, "不带参数的DictHelper.RetrieveDicts方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsTrue(DictHelper.RetrieveDicts().Count() > 1, "不带参数的DictHelper.RetrieveDicts方法调用失败");
}
[TestMethod]
public void SaveDictTest()
{
Dict p = new Dict();
p.Category = "测试省份";
p.Name = "测试城市";
p.Code = "测试字典";
var result = DictHelper.SaveDict(p);
Assert.IsTrue(result, "增加用户出错");
// 测试插入字典记录方法 ID = 0
Assert.IsTrue(DictHelper.SaveDict(Dict), "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
var dicts = DictHelper.RetrieveDicts();
Assert.IsTrue(dicts.Count() > 0, "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
p.ID = 1;
p.Category = "测试省份22";
p.Name = "测试城市22";
p.Code = "测试字典22";
result = DictHelper.SaveDict(p);
Assert.IsTrue(result, "更新用户出错");
// 测试更新字典记录方法 ID != 0
var dict = dicts.FirstOrDefault(d => d.Category == Dict.Category);
dict.Name = "__测试子菜单2__";
Assert.IsTrue(DictHelper.SaveDict(dict), string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
var dest = DictHelper.RetrieveDicts(dict.ID.ToString());
Assert.IsTrue(dest.Count() == 1, "带参数的DictHelper.RetrieveDicts方法调用失败");
Assert.AreEqual(dict.Name, dest.First().Name, string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
}
[TestMethod]
public void DeleteDictTest()
{
SaveDictTest();
string p = "1";
try
{
DictHelper.DeleteDict(p);
Assert.IsTrue(true);
}
catch
{
Assert.IsTrue(false, "删除用户出错");
}
// 先判断数据环境是否可以删除,没有数据先伪造数据
var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
if (dict == null) DictHelper.SaveDict(Dict);
dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
Assert.IsTrue(DictHelper.DeleteDict(dict.ID.ToString()), "DictHelper.DeleteDict 方法调用失败");
}
}
}

View File

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
@ -6,60 +8,106 @@ namespace Bootstrap.DataAccess.Tests
[TestClass]
public class GroupHelperTests
{
private Group Group { get; set; }
private User User { get; set; }
private Role Role { get; set; }
[TestInitialize]
public void IniInitialized()
{
Group = new Group() { GroupName="_测试部门_",Description="我是很厉害的测试部门"};
User = new User() { UserName = "_测试用户_", Password = "123", PassSalt = "123", DisplayName = "测试者" };
Role = new Role() { RoleName = "_测试角色_", Description = "测试角色" };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Groups where GroupName = '_测试部门_'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void RetrieveGroupsTest()
{
var result = GroupHelper.RetrieveGroups("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的GroupHelper.RetrieveGroups方法调用失败请检查数据库连接或者数据库SQL语句");
result = GroupHelper.RetrieveGroups();
Assert.IsTrue(result.Count() >= 0, "不带参数的GroupHelper.RetrieveGroups方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsTrue(GroupHelper.RetrieveGroups().Count() >= 0, "不带参数的GroupHelper.RetrieveGroups方法调用失败请检查数据库连接或者数据库SQL语句");
}
[TestMethod]
public void SaveGroupTest()
{
Group p = new Group();
p.GroupName = "测试群组2";
p.Description = "测试群组2";
var result = GroupHelper.SaveGroup(p);
Assert.IsTrue(result, "增加用户出错");
p.ID = 4;
p.GroupName = "测试群组22";
p.Description = "测试群组22";
result = GroupHelper.SaveGroup(p);
Assert.IsTrue(result, "更新用户出错");
// 测试插入部门方法 ID=0
Assert.IsTrue(GroupHelper.SaveGroup(Group), "插入部门操作失败请检查GroupHelper.SaveGroup方法");
var groups = GroupHelper.RetrieveGroups();
Assert.IsTrue(groups.Count() > 0, "插入部门操作失败请检查GroupHelper.SaveGroup方法");
//测试更新部门方法 ID!=0
var group = groups.FirstOrDefault(g => g.GroupName == Group.GroupName);
group.Description="我是测试部门";
Assert.IsTrue(GroupHelper.SaveGroup(group), string.Format("更新部门ID={0}操作失败请检查GroupHelper.SaveGroup方法",group.ID));
var ret = GroupHelper.RetrieveGroups(group.ID.ToString());
Assert.IsTrue(ret.Count() == 1, "带参数的GroupHelper.RetrieveGroups方法失败");
Assert.AreEqual(group.Description, ret.First().Description, string.Format("更新部门ID={0}操作失败请检查GroupHelper.SaveGroup方法", group.ID));
}
[TestMethod]
public void DeleteGroupTest()
{
string p = "2";
try
{
GroupHelper.DeleteGroup(p);
Assert.IsTrue(true);
}
catch
{
Assert.IsTrue(false, "删除用户出错");
}
// 先判断数据环境是否可以删除,没有数据先伪造数据
var group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
if (group == null) GroupHelper.SaveGroup(Group);
group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
Assert.IsTrue(GroupHelper.DeleteGroup(group.ID.ToString()), "GroupHelper.DeleteGroup 方法调用失败");
}
[TestMethod()]
public void RetrieveGroupsByUserIdTest()
{
var result = GroupHelper.RetrieveGroupsByUserId(1);
Assert.IsTrue(result.Count() > 0, "根据用户查询群组失败");
}
[TestMethod()]
[TestMethod]
public void SaveGroupsByUserIdTest()
{
var result = GroupHelper.SaveGroupsByUserId(1, "1,2");
Assert.IsTrue(result == true, "保存用户群组关系失败");
var user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
if (user == null) UserHelper.SaveUser(User);
user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
var group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
if (group == null) GroupHelper.SaveGroup(Group);
group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
Assert.IsTrue(GroupHelper.SaveGroupsByUserId(user.ID, group.ID.ToString()), "存储用户部门信息失败");
Assert.IsTrue(GroupHelper.RetrieveGroupsByUserId(user.ID).Count() >= 1, string.Format("获取用户ID={0}的部门失败", user.ID));
//删除数据
string sql = "Delete from Users where UserName = '_测试用户_';";
sql += "Delete from Groups where GroupName='_测试部门_';";
sql += string.Format("Delete from UserGroup where UserID={0};", user.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void SaveGroupsByRoleIdTest()
{
var group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
if (group == null) GroupHelper.SaveGroup(Group);
group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == Role.RoleName);
if (role == null) RoleHelper.SaveRole(Role);
role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == Role.RoleName);
Assert.IsTrue(GroupHelper.SaveGroupsByRoleId(role.ID, group.ID.ToString()), "存储角色部门信息失败");
Assert.IsTrue(GroupHelper.RetrieveGroupsByRoleId(role.ID).Count() >= 1, string.Format("获取角色ID={0}的部门信息失败", role.ID));
//删除数据
string sql = "Delete from Groups where GroupName = '_测试部门_';";
sql += "Delete from Roles where RoleName='_测试角色_';";
sql += string.Format("Delete from RoleGroup where RoleID={0};", role.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
}
}

View File

@ -1,5 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
@ -7,53 +9,107 @@ namespace Bootstrap.DataAccess.Tests
[TestClass]
public class UserHelperTests
{
[TestMethod]
public void RetrieveUsersTest()
private User User { get; set; }
private Role Role { get; set; }
private Group Group { get; set; }
[TestInitialize]
public void Initialized()
{
var result = UserHelper.RetrieveUsers("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的UserHelper.RetrieveUsers方法调用失败请检查数据库连接或者数据库SQL语句");
result = UserHelper.RetrieveUsers();
Assert.IsTrue(result.Count() >= 0, "不带参数的UserHelper.RetrieveUsers方法调用失败请检查数据库连接或者数据库SQL语句");
User = new User() { UserName = "_测试用户_", Password = "123", PassSalt = "123",DisplayName="测试者" };
Role = new Role() { RoleName = "_测试角色_", Description = "测试角色" };
Group = new Group() { GroupName = "_测试部门_", Description = "测试部门" };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Users where UserName = '_测试用户_'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void DeleteUserTest()
public void RetrieveUsersTest()
{
Assert.IsTrue(UserHelper.DeleteUser("1,2"), "带有参数的UserHelper.DeleteUserTest方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsFalse(UserHelper.DeleteUser(string.Empty), "参数为空字符串的UserHelper.DeleteUserTest方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsTrue(UserHelper.RetrieveUsers().Count() >=1, "不带参数的UserHelper.RetrieveUsers方法调用失败请检查数据库连接或者数据库SQL语句");
}
[TestMethod]
public void SaveUserTest()
{
User users = new User();
users.ID = 0;
users.UserName = "lq";
users.Password = "123";
users.PassSalt = "123";
users.DisplayName = "liqi";
var result = UserHelper.SaveUser(users);
Assert.IsTrue(result == true, "新建用户信息失败请检查数据库连接或者数据库SQL语句");
// 测试插入用户方法 ID = 0
Assert.IsTrue(UserHelper.SaveUser(User), "插入用户操作失败请检查UserHelper.SaveUser 方法");
var users = UserHelper.RetrieveUsers();
Assert.IsTrue(users.Count() > 0, "插入用户操作失败请检查UserHelper.SaveUser 方法");
User users1 = new User();
users1.ID = 5;
users1.UserName = "lq";
users1.Password = "123";
users1.PassSalt = "123456";
users1.DisplayName = "Qi Li";
result = UserHelper.SaveUser(users1);
Assert.IsTrue(result == true, "更新用户信息失败请检查数据库连接或者数据库SQL语句");
// 测试更新用户方法 ID != 0
var user = users.FirstOrDefault(u => u.UserName == User.UserName);
user.DisplayName = "测试者2号";
Assert.IsTrue(UserHelper.SaveUser(user), string.Format("更新用户ID={0}操作失败请检查UserHelper.SaveUser方法", user.ID));
var ret = UserHelper.RetrieveUsers(user.ID.ToString());
Assert.IsTrue(ret.Count() == 1, "带参数的UserHelper.RetrieveUsers方法调用失败");
Assert.AreEqual(user.DisplayName, ret.First().DisplayName, string.Format("更新用户ID={0}操作失败请检查UserHelper.SaveUser方法", user.ID));
}
[TestMethod]
public void RetrieveUsersByRoleIdTest(){
IEnumerable<User> result = UserHelper.RetrieveUsersByRoleId(2);
Assert.IsTrue(result.Count() >= 0, "获取该角色的用户信息失败请检查数据库连接或者数据库SQL语句");
public void DeleteUserTest()
{
// 先判断数据环境是否可以删除,没有数据先伪造数据
var user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
if (user == null) UserHelper.SaveUser(User);
user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
Assert.IsTrue(UserHelper.DeleteUser(user.ID.ToString()), "UserHelper.DeleteUserTest方法调用失败");
}
[TestMethod]
public void SaveUsersByRoleIdTest()
{
bool result = UserHelper.SaveUsersByRoleId(2,"2,3");
Assert.IsTrue(result, "获取该角色的用户信息失败请检查数据库连接或者数据库SQL语句");
var user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
if (user == null) UserHelper.SaveUser(User);
user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == Role.RoleName);
if (role == null) RoleHelper.SaveRole(Role);
role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == Role.RoleName);
Assert.IsTrue(UserHelper.SaveUsersByRoleId(role.ID,user.ID.ToString()), "存储角色用户信息失败");
Assert.IsTrue(UserHelper.RetrieveUsersByRoleId(role.ID).Count()>=1, string.Format("获取角色ID={0}的用户信息失败",role.ID));
//删除数据
string sql = "Delete from Users where UserName = '_测试用户_';";
sql += "Delete from Roles where RoleName='_测试角色_';";
sql += string.Format("Delete from UserRole where RoleID={0};", role.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void SaveUsersByGroupIdTest()
{
var user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
if (user == null) UserHelper.SaveUser(User);
user = UserHelper.RetrieveUsers().FirstOrDefault(u => u.UserName == User.UserName);
var group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
if (group == null) GroupHelper.SaveGroup(Group);
group = GroupHelper.RetrieveGroups().FirstOrDefault(g => g.GroupName == Group.GroupName);
Assert.IsTrue(UserHelper.SaveUsersByGroupId(group.ID, user.ID.ToString()), "存储部门用户信息失败");
Assert.IsTrue(UserHelper.RetrieveUsersByGroupId(group.ID).Count() >= 1, string.Format("获取部门ID={0}的用户失败", group.ID));
//删除数据
string sql = "Delete from Users where UserName = '_测试用户_';";
sql += "Delete from Groups where GroupName='_测试部门_';";
sql += string.Format("Delete from UserGroup where GroupID={0};", group.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
}
}