日志,菜单,角色表测试部分完成

This commit is contained in:
summer853300975 2016-11-01 20:48:07 +08:00
parent 60424b81da
commit ce5118ef53
3 changed files with 128 additions and 72 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
@ -6,35 +8,43 @@ namespace Bootstrap.DataAccess.Tests
[TestClass] [TestClass]
public class LogHelperTests public class LogHelperTests
{ {
private Log Log { get; set; }
[TestInitialize]
public void Initialized()
{
Log = new Log() { OperationType = 1, UserID = 1, OperationTime = System.DateTime.Now, TableName = "_测试日志_", BusinessName = "新增测试日志信息", PrimaryKey = "ID", SqlText = "Insert Into Logs", OperationIp = "0.0.0.0" };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "delete from Logs where OperationIp='0'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod] [TestMethod]
public void RetrieveLogsTest() public void RetrieveLogsTest()
{ {
var result = LogHelper.RetrieveLogs("1"); //var resul = LogHelper.RetrieveLogs("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句"); //Assert.IsTrue((resul.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句");
result = LogHelper.RetrieveLogs(); var result = LogHelper.RetrieveLogs();
Assert.IsTrue(result.Count() >= 0, "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句"); Assert.IsTrue(result.Count() >= 0, "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句");
} }
[TestMethod] [TestMethod]
public void saveLogTest() public void saveLogTest()
{ {
Log p = new Log(); Assert.IsTrue(LogHelper.saveLog(Log), "新增日志信息出错,请检查LogHelper的saveLog 方法");
p.OperationType = 1; var logs = LogHelper.RetrieveLogs();
p.UserID = 1; Assert.IsTrue(logs.Count() > 0, "新增日志信息出错,请检查LogHelper的saveLog 方法");
p.OperationTime = System.DateTime.Now;
p.TableName = "日志";
p.BusinessName = "新增日志信息";
p.PrimaryKey = "ID";
p.SqlText = "Insert Into Logs";
p.OperationIp = "0.0.0.0";
var result = LogHelper.saveLog(p);
Assert.IsTrue(result, "新增日志信息出错");
} }
[TestMethod] [TestMethod]
public void DeleteLogTest() public void DeleteLogTest()
{ {
string p = "2"; // 先判断数据环境是否可以删除,没有数据先伪造数据
Assert.IsTrue(LogHelper.DeleteLog(p),"删除日志信息出错"); var log = LogHelper.RetrieveLogs().FirstOrDefault(m => m.OperationType == Log.OperationType);
if (log == null) LogHelper.saveLog(Log);
log = LogHelper.RetrieveLogs().FirstOrDefault(m => m.OperationType == Log.OperationType);
Assert.IsTrue(LogHelper.DeleteLog(log.ID.ToString()), "删除日志信息出错");
} }
} }
} }

View File

@ -9,11 +9,13 @@ namespace Bootstrap.DataAccess.Tests
public class MenuHelperTests public class MenuHelperTests
{ {
private Menu Menu { get; set; } private Menu Menu { get; set; }
private Role Role { get; set; }
[TestInitialize] [TestInitialize]
public void Initialized() public void Initialized()
{ {
Menu = new Menu() { Name = "__测试菜单__", Order = 999 }; Menu = new Menu() { Name = "__测试菜单__", Order = 999, Category = "0" };
Role = new Role() { RoleName = "_测试角色_", Description = "这是一个测试角色", Checked = "0" };
} }
[TestCleanup] [TestCleanup]
public void CleanUp() public void CleanUp()
@ -57,11 +59,28 @@ namespace Bootstrap.DataAccess.Tests
Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()), "MenuHelper.DeleteMenu 方法调用失败"); Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()), "MenuHelper.DeleteMenu 方法调用失败");
} }
[TestMethod()] [TestMethod]
public void RetrieveMenusByUserIdTest() public void SavaRolesByMenuIdTest()
{ {
// UNDONE: 根据代码编写单元测试 var menu = MenuHelper.RetrieveMenus().FirstOrDefault(m => m.Name == Menu.Name);
Assert.IsTrue(true); if (menu == null) MenuHelper.SaveMenu(Menu);
menu = MenuHelper.RetrieveMenus().FirstOrDefault(m => m.Name == Menu.Name);
var role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
if (role == null) RoleHelper.SaveRole(Role);
role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
Assert.IsTrue(RoleHelper.SavaRolesByMenuId(menu.ID, role.ID.ToString()), "保存菜单角色关系失败");
Assert.IsTrue(RoleHelper.RetrieveRolesByMenuId(menu.ID).Count() > 0, string.Format("获取菜单ID={0}的角色关系失败", menu.ID));
//删除数据
string sql = "delete from Navigations where Name='__测试菜单__';";
sql += "delete from Roles where RoleName='_测试角色_';";
sql += string.Format("delete from NavigationRole where NavigationID={0}", menu.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
} }
} }
} }

View File

@ -1,77 +1,104 @@
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
{ {
[TestClass()] [TestClass]
public class RoleTests public class RoleTests
{ {
[TestMethod()] private Role Role { get; set; }
private User User { get; set; }
private Group Group { get; set; }
[TestInitialize]
public void Initialized()
{
Role = new Role() { RoleName = "_测试角色_", Description = "这是一个测试角色", Checked = "0" };
User = new User() { UserName = "_测试用户_", Password = "111", PassSalt = "111", DisplayName = "_测试用户_", Checked = "0" };
Group = new Group() { GroupName = "_测试部门_", Description = "这是一个测试部门", Checked = "0" };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "delete from Roles where RoleName='_测试角色_'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void SaveRoleTest() public void SaveRoleTest()
{ {
Role role1 = new Role() //测试添加角色方法 ID = 0
{ Assert.IsTrue(RoleHelper.SaveRole(Role), "添加角色操作失败请检查RoleHelper.SaveRole方法");
RoleName = "管理员", var roles = RoleHelper.RetrieveRoles();
Description = "可以读写所有内容" Assert.IsTrue(roles.Count() > 0, "添加角色操作失败请检查RoleHelper.SaveRole方法");
}; //测试编辑角色方法 ID != 0
var result1 = RoleHelper.SaveRole(role1); var role = roles.FirstOrDefault(m => m.RoleName == Role.RoleName);
Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败请检查数据库连接或者数据库SQL语句"); role.Description = "这是修改后的测试角色";
Role role2 = new Role() Assert.IsTrue(RoleHelper.SaveRole(role), string.Format("更新角色ID={0}操作失败请检查RoleHelper.SaveRole方法", role.ID));
{ var ret = RoleHelper.RetrieveRoles(role.ID.ToString());
ID = 1, Assert.IsTrue(ret.Count() == 1, "带参数的RoleHelper.RetrieveRoles方法调用失败");
RoleName = "管理员", Assert.AreEqual(role.Description, ret.First().Description, string.Format("更新角色ID={0}操作失败请检查RoleHelper.SaveRole方法", role.ID));
Description = "读写所有内容"
};
var result2 = RoleHelper.SaveRole(role2);
Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败请检查数据库连接或者数据库SQL语句");
} }
[TestMethod()] [TestMethod]
public void RetrieveRoleTest() public void RetrieveRoleTest()
{ {
var result = RoleHelper.RetrieveRoles("1"); var result = RoleHelper.RetrieveRoles();
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句");
result = RoleHelper.RetrieveRoles();
Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句"); Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句");
} }
[TestMethod()] [TestMethod]
public void DeleteRoleTest() public void DeleteRoleTest()
{ {
RoleHelper.SaveRole(new Role()
{ var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "_测试角色_");
ID = 0, if (role == null) RoleHelper.SaveRole(Role);
RoleName = "RoleUnitTest", role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "_测试角色_");
Description = string.Empty Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除角色失败");
});
var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
} }
[TestMethod()] [TestMethod]
public void RetrieveRolesByUserIdTest()
{
var result = RoleHelper.RetrieveRolesByUserId(1);
Assert.IsTrue(result.Count() >= 0, "用户查询角色关系失败!");
}
[TestMethod()]
public void SaveRolesByUserIdTest() public void SaveRolesByUserIdTest()
{ {
var result = RoleHelper.SaveRolesByUserId(1,"3"); var role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
Assert.IsTrue(result == true, "保存用户角色关系失败"); if (role == null) RoleHelper.SaveRole(Role);
} role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
[TestMethod()] var user = UserHelper.RetrieveUsers().FirstOrDefault(m => m.DisplayName == User.DisplayName);
public void RetrieveRoleByGroupIDTest() if (user == null) UserHelper.SaveUser(User);
user = UserHelper.RetrieveUsers().FirstOrDefault(m => m.DisplayName == User.DisplayName);
Assert.IsTrue(RoleHelper.SaveRolesByUserId(user.ID, role.ID.ToString()), "保存用户角色关系失败");
Assert.IsTrue(RoleHelper.RetrieveRolesByUserId(user.ID).Count() >= 1, string.Format("获取用户ID={0}的角色信息失败", user.ID));
//删除数据
string sql = "delete from Roles where RoleName='_测试角色_';";
sql += "delete from Users where UserName='_测试用户_';";
sql += string.Format("delete from UserRole where UserID={0}", user.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{ {
var result = RoleHelper.RetrieveRolesByGroupId(1); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
Assert.IsTrue((result.Count() == 8), "RetrieveRoleByGroupID测试失败");
} }
}
[TestMethod()] [TestMethod]
public void SaveRoleByGroupIDTest() public void SaveRoleByGroupIDTest()
{ {
var result = RoleHelper.SaveRolesByGroupId(1,"1,2,3,4"); var role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
Assert.IsTrue(result, "SaveRoleByGroupID测试失败"); if (role == null) RoleHelper.SaveRole(Role);
role = RoleHelper.RetrieveRoles().FirstOrDefault(m => m.RoleName == Role.RoleName);
var group = GroupHelper.RetrieveGroups().FirstOrDefault(m => m.GroupName == Group.GroupName);
if (group == null) GroupHelper.SaveGroup(Group);
group = GroupHelper.RetrieveGroups().FirstOrDefault(m => m.GroupName == Group.GroupName);
Assert.IsTrue(RoleHelper.SaveRolesByGroupId(group.ID, role.ID.ToString()), "保存部门角色关系失败");
Assert.IsTrue(RoleHelper.RetrieveRolesByGroupId(group.ID).Count() > 0, string.Format("获取部门ID={0}的角色关系失败", group.ID));
//删除数据
string sql = "delete from Roles where RoleName='_测试角色_';";
sql += "delete from Groups where GroupName='_测试部门_';";
sql += string.Format("delete from RoleGroup where GroupID={0}", group.ID);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
} }
} }
} }