diff --git a/Bootstrap.DataAccess/DBAccessManager.cs b/Bootstrap.DataAccess/DBAccessManager.cs
index 9f4a4aa5..3dc10e95 100644
--- a/Bootstrap.DataAccess/DBAccessManager.cs
+++ b/Bootstrap.DataAccess/DBAccessManager.cs
@@ -6,7 +6,7 @@ namespace Bootstrap.DataAccess
///
///
///
- static class DBAccessManager
+ public static class DBAccessManager
{
private static Lazy db = new Lazy(() => DBAccess.CreateDB("SQL"), true);
diff --git a/Bootstrap.DataAccess/MenuHelper.cs b/Bootstrap.DataAccess/MenuHelper.cs
index 011cbe9e..e51964eb 100644
--- a/Bootstrap.DataAccess/MenuHelper.cs
+++ b/Bootstrap.DataAccess/MenuHelper.cs
@@ -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);
}
diff --git a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj
index c44aeb8c..8858e0f6 100644
--- a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj
+++ b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj
@@ -36,8 +36,10 @@
4
+
+
diff --git a/Bootstrap.DataAccessTests/MenuHelperTests.cs b/Bootstrap.DataAccessTests/MenuHelperTests.cs
index 9d84cacd..49216fcd 100644
--- a/Bootstrap.DataAccessTests/MenuHelperTests.cs
+++ b/Bootstrap.DataAccessTests/MenuHelperTests.cs
@@ -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()
- {
- 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语句");
+ // 先判断数据环境是否可以删除,没有数据先伪造数据
+ 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()
+ {
+ // UNDONE: 根据代码编写单元测试
+ Assert.IsTrue(true);
}
}
}
diff --git a/DatabaseScripts/Install.sql b/DatabaseScripts/Install.sql
index 0ce4c52d..e0ff9bd0 100644
--- a/DatabaseScripts/Install.sql
+++ b/DatabaseScripts/Install.sql
@@ -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