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

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

View File

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