From ce5118ef538ececf61f22c14bd7fd3c30cf14a9b Mon Sep 17 00:00:00 2001 From: summer853300975 <853300975@qq.com> Date: Tue, 1 Nov 2016 20:48:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=EF=BC=8C=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=EF=BC=8C=E8=A7=92=E8=89=B2=E8=A1=A8=E6=B5=8B=E8=AF=95=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccessTests/LogHelperTests.cs | 44 ++++--- Bootstrap.DataAccessTests/MenuHelperTests.cs | 29 ++++- Bootstrap.DataAccessTests/RoleHelperTests.cs | 127 +++++++++++-------- 3 files changed, 128 insertions(+), 72 deletions(-) diff --git a/Bootstrap.DataAccessTests/LogHelperTests.cs b/Bootstrap.DataAccessTests/LogHelperTests.cs index 6f4ee3b2..6340614c 100644 --- a/Bootstrap.DataAccessTests/LogHelperTests.cs +++ b/Bootstrap.DataAccessTests/LogHelperTests.cs @@ -1,4 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Data; +using System.Data.Common; using System.Linq; namespace Bootstrap.DataAccess.Tests @@ -6,35 +8,43 @@ namespace Bootstrap.DataAccess.Tests [TestClass] 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] public void RetrieveLogsTest() { - var result = LogHelper.RetrieveLogs("1"); - Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败,请检查数据库连接或者数据库SQL语句"); - result = LogHelper.RetrieveLogs(); + //var resul = LogHelper.RetrieveLogs("1"); + //Assert.IsTrue((resul.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败,请检查数据库连接或者数据库SQL语句"); + var result = LogHelper.RetrieveLogs(); Assert.IsTrue(result.Count() >= 0, "带有参数的LogHelper.RetrieveLogs方法调用失败,请检查数据库连接或者数据库SQL语句"); } [TestMethod] public void saveLogTest() { - Log p = new Log(); - p.OperationType = 1; - p.UserID = 1; - 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, "新增日志信息出错"); + Assert.IsTrue(LogHelper.saveLog(Log), "新增日志信息出错,请检查LogHelper的saveLog 方法"); + var logs = LogHelper.RetrieveLogs(); + Assert.IsTrue(logs.Count() > 0, "新增日志信息出错,请检查LogHelper的saveLog 方法"); } [TestMethod] 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()), "删除日志信息出错"); } } } diff --git a/Bootstrap.DataAccessTests/MenuHelperTests.cs b/Bootstrap.DataAccessTests/MenuHelperTests.cs index 49216fcd..7222d40e 100644 --- a/Bootstrap.DataAccessTests/MenuHelperTests.cs +++ b/Bootstrap.DataAccessTests/MenuHelperTests.cs @@ -9,11 +9,13 @@ namespace Bootstrap.DataAccess.Tests public class MenuHelperTests { private Menu Menu { get; set; } + private Role Role { get; set; } [TestInitialize] 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] public void CleanUp() @@ -57,11 +59,28 @@ namespace Bootstrap.DataAccess.Tests Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()), "MenuHelper.DeleteMenu 方法调用失败"); } - [TestMethod()] - public void RetrieveMenusByUserIdTest() + [TestMethod] + public void SavaRolesByMenuIdTest() { - // UNDONE: 根据代码编写单元测试 - Assert.IsTrue(true); + var menu = MenuHelper.RetrieveMenus().FirstOrDefault(m => m.Name == Menu.Name); + 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); + } } } } diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs index a3245452..5b075556 100644 --- a/Bootstrap.DataAccessTests/RoleHelperTests.cs +++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs @@ -1,77 +1,104 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Data; +using System.Data.Common; using System.Linq; namespace Bootstrap.DataAccess.Tests { - [TestClass()] + [TestClass] 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() { - Role role1 = new Role() - { - RoleName = "管理员", - Description = "可以读写所有内容" - }; - var result1 = RoleHelper.SaveRole(role1); - Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句"); - Role role2 = new Role() - { - ID = 1, - RoleName = "管理员", - Description = "读写所有内容" - }; - var result2 = RoleHelper.SaveRole(role2); - Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句"); + //测试添加角色方法 ID = 0 + Assert.IsTrue(RoleHelper.SaveRole(Role), "添加角色操作失败,请检查RoleHelper.SaveRole方法"); + var roles = RoleHelper.RetrieveRoles(); + Assert.IsTrue(roles.Count() > 0, "添加角色操作失败,请检查RoleHelper.SaveRole方法"); + //测试编辑角色方法 ID != 0 + var role = roles.FirstOrDefault(m => m.RoleName == Role.RoleName); + role.Description = "这是修改后的测试角色"; + Assert.IsTrue(RoleHelper.SaveRole(role), string.Format("更新角色ID={0}操作失败,请检查RoleHelper.SaveRole方法", role.ID)); + var ret = RoleHelper.RetrieveRoles(role.ID.ToString()); + Assert.IsTrue(ret.Count() == 1, "带参数的RoleHelper.RetrieveRoles方法调用失败"); + Assert.AreEqual(role.Description, ret.First().Description, string.Format("更新角色ID={0}操作失败,请检查RoleHelper.SaveRole方法", role.ID)); } - [TestMethod()] + [TestMethod] public void RetrieveRoleTest() { - var result = RoleHelper.RetrieveRoles("1"); - Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); - result = RoleHelper.RetrieveRoles(); + var result = RoleHelper.RetrieveRoles(); Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); } - [TestMethod()] + [TestMethod] public void DeleteRoleTest() { - RoleHelper.SaveRole(new Role() - { - ID = 0, - RoleName = "RoleUnitTest", - Description = string.Empty - }); - var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest"); - Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败"); + + var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "_测试角色_"); + if (role == null) RoleHelper.SaveRole(Role); + role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "_测试角色_"); + Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除角色失败"); } - [TestMethod()] - public void RetrieveRolesByUserIdTest() - { - var result = RoleHelper.RetrieveRolesByUserId(1); - Assert.IsTrue(result.Count() >= 0, "用户查询角色关系失败!"); - } - [TestMethod()] + [TestMethod] public void SaveRolesByUserIdTest() { - var result = RoleHelper.SaveRolesByUserId(1,"3"); - Assert.IsTrue(result == true, "保存用户角色关系失败"); - } + 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); - [TestMethod()] - public void RetrieveRoleByGroupIDTest() - { - var result = RoleHelper.RetrieveRolesByGroupId(1); - Assert.IsTrue((result.Count() == 8), "RetrieveRoleByGroupID测试失败"); - - } + var user = UserHelper.RetrieveUsers().FirstOrDefault(m => m.DisplayName == User.DisplayName); + if (user == null) UserHelper.SaveUser(User); + user = UserHelper.RetrieveUsers().FirstOrDefault(m => m.DisplayName == User.DisplayName); - [TestMethod()] + 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)) + { + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + } + } + [TestMethod] public void SaveRoleByGroupIDTest() { - var result = RoleHelper.SaveRolesByGroupId(1,"1,2,3,4"); - Assert.IsTrue(result, "SaveRoleByGroupID测试失败"); + 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); + 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); + } } } }