根据新编写的菜单单元测试,更改了一些验证代码

This commit is contained in:
Argo-Lenovo 2016-10-26 21:51:38 +08:00
parent f111f3bc4e
commit 8814f1a7ad
5 changed files with 56 additions and 47 deletions

View File

@ -6,7 +6,7 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
static class DBAccessManager
public static class DBAccessManager
{
private static Lazy<DBAccess> db = new Lazy<DBAccess>(() => DBAccess.CreateDB("SQL"), true);

View File

@ -1,6 +1,7 @@
using Longbow;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.Data;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Generic;
@ -38,8 +39,8 @@ namespace Bootstrap.DataAccess
ParentId = (int)reader[1],
Name = (string)reader[2],
Order = (int)reader[3],
Icon = (string)reader[4],
Url = LgbConvert.ReadValue((string)reader[5], string.Empty),
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
Url = LgbConvert.ReadValue(reader[5], string.Empty),
Category = (int)reader[6]
});
}
@ -93,9 +94,10 @@ namespace Bootstrap.DataAccess
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
if (string.IsNullOrEmpty(p.Name)) return ret;
if (p.Name.Length > 50) p.Name.Substring(0, 50);
if (p.Icon.Length > 50) p.Icon.Substring(0, 50);
if (p.Url != null) { if (p.Url.Length > 50) p.Url.Substring(0, 50); }
if (p.Icon != null && p.Icon.Length > 50) p.Icon.Substring(0, 50);
if (p.Url != null && p.Url.Length > 50) p.Url.Substring(0, 50);
string sql = p.ID == 0 ?
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category)" :
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category where ID = @ID";
@ -107,8 +109,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", p.Icon, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", p.Url, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccess.ToDBValue(p.Icon), ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccess.ToDBValue(p.Url), ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}

View File

@ -36,8 +36,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Longbow.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" />
<Reference Include="Longbow.V4, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Data" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">

View File

@ -1,62 +1,67 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
{
[TestClass]
public class MenuHelperTests
{
private Menu Menu { get; set; }
[TestInitialize]
public void Initialized()
{
Menu = new Menu() { Name = "__测试菜单__", Order = 999 };
}
[TestCleanup]
public void CleanUp()
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Navigations where Name = '__测试菜单__'"))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
[TestMethod]
public void RetrieveMenusTest()
{
var result = MenuHelper.RetrieveMenus("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的MenuHelper.RetrieveMenus方法调用失败请检查数据库连接或者数据库SQL语句");
result = MenuHelper.RetrieveMenus();
Assert.IsTrue(result.Count() >= 0, "不带参数的MenuHelper.RetrieveMenus方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsTrue(MenuHelper.RetrieveMenus().Count() > 1, "不带参数的MenuHelper.RetrieveMenus方法调用失败");
}
[TestMethod]
public void SaveMenuTest()
{
Menu p = new Menu();
p.ParentId = 1;
p.Name = "测试菜单名称";
p.Order = 0;
p.Icon = "测试菜单Icon";
p.Url = "urlTestAdd";
p.Category = 1;
var result = MenuHelper.SaveMenu(p);
Assert.IsTrue(result, "增加菜单出错");
// 测试插入菜单方法 ID = 0
Assert.IsTrue(MenuHelper.SaveMenu(Menu), "插入菜单操作失败,请检查 MenuHelper.SaveMenu 方法");
var menus = MenuHelper.RetrieveMenus();
Assert.IsTrue(menus.Count() > 0, "插入菜单操作失败,请检查 MenuHelper.SaveMenu 方法");
Menu p1 = new Menu();
p1.ID = 7;
p1.ParentId = 2;
p1.Name = "测试菜单名称1";
p1.Order = 0;
p1.Icon = "测试菜单Icon1";
p1.Url = "urlTestUpdate";
p1.Category = 1;
result = MenuHelper.SaveMenu(p1);
Assert.IsTrue(result, "更新菜单出错");
// 测试更新菜单方法 ID != 0
var menu = menus.FirstOrDefault(m => m.Name == Menu.Name);
menu.Icon = "fa";
Assert.IsTrue(MenuHelper.SaveMenu(menu), string.Format("更新菜单ID = {0} 操作失败,请检查 MenuHelper.SaveMenu 方法", menu.ID));
var dest = MenuHelper.RetrieveMenus(menu.ID.ToString());
Assert.IsTrue(dest.Count() == 1, "带参数的MenuHelper.RetrieveMenus方法调用失败");
Assert.AreEqual(menu.Icon, dest.First().Icon, string.Format("更新菜单ID = {0} 操作失败,请检查 MenuHelper.SaveMenu 方法", menu.ID));
}
[TestMethod]
public void DeleteMenuTest()
{
MenuHelper.SaveMenu(new Menu()
// 先判断数据环境是否可以删除,没有数据先伪造数据
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);
Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()), "MenuHelper.DeleteMenu 方法调用失败");
}
[TestMethod()]
public void RetrieveMenusByUserIdTest()
{
ID = 0,
ParentId = 1,
Name = "菜单删除测试",
Order = 0,
Icon = "测试菜单Icon1",
Url = "urlTestUpdate",
Category = 1
});
var menu = MenuHelper.RetrieveMenus().FirstOrDefault(m => m.Name == "菜单删除测试");
Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()),"删除菜单失败");
Assert.IsTrue(MenuHelper.DeleteMenu("1,2"), "带有参数的MenuHelper.DeleteMenu方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsFalse(MenuHelper.DeleteMenu(string.Empty), "参数为空字符串的MenuHelper.DeleteMenu方法调用失败请检查数据库连接或者数据库SQL语句");
// UNDONE: 根据代码编写单元测试
Assert.IsTrue(true);
}
}
}

View File

@ -149,7 +149,7 @@ CREATE TABLE [dbo].[Navigations](
[ParentId] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Order] [int] NOT NULL,
[Icon] [varchar](50) NOT NULL,
[Icon] [varchar](50) NULL,
[Url] [varchar](50) NULL,
[Category] [int] NOT NULL,
CONSTRAINT [PK_Navigations] PRIMARY KEY CLUSTERED