Skip to content

Commit

Permalink
更新单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliang-wt committed Jun 25, 2019
1 parent 6c4b7b2 commit d9d6bdd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/WalkingTec.Mvvm.Core/BasePagedListVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public virtual void DoSearch()
}
if (Searcher.Limit == 0)
{
Searcher.Limit = ConfigInfo.RPP;
Searcher.Limit = ConfigInfo?.RPP ?? 20;
}
//根据返回数据的数量,以及预先设定的每页行数来设定数据量和总页数
Searcher.Count = count;
Expand All @@ -724,14 +724,14 @@ public virtual void DoSearch()
{
Searcher.Page = Searcher.PageCount;
}
var test = query.ToList();
EntityList = query.Skip((Searcher.Page - 1) * Searcher.Limit).Take(Searcher.Limit).AsNoTracking().ToList();
}
else //如果不需要分页则直接获取数据
{
EntityList = query.AsNoTracking().ToList();
Searcher.Count = EntityList.Count();
Searcher.Limit = EntityList.Count();
Searcher.PageCount = 1;
Searcher.Page = 1;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/WalkingTec.Mvvm.Core/Support/ExpressionVisitors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ public Expression Modify(Expression expression)
{
//先调用一次Visit,删除所有的where表达式
var rv = Visit(expression);
if(rv.NodeType == ExpressionType.Constant)
{
if ((rv.Type.IsGeneric(typeof(EntityQueryable<>)) || rv.Type.IsGeneric(typeof(EnumerableQuery<>))))
{
var modelType = rv.Type.GenericTypeArguments[0];
ParameterExpression pe = Expression.Parameter(modelType, "x");
Expression left1 = Expression.Constant(1);
Expression right1 = Expression.Constant(1);
Expression trueExp = Expression.Equal(left1, right1);
rv = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { modelType },
rv,
Expression.Lambda(trueExp, new ParameterExpression[] { pe }));

}
}
//将模式设为addMode,再调用一次Visit来添加新的表达式
_addMode = true;
rv = Visit(rv);
Expand Down
81 changes: 81 additions & 0 deletions test/WalkingTec.Mvvm.Core.Test/VM/BasePaedListVMTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,90 @@ public class BasePaedListVMTest
public BasePaedListVMTest()
{
_seed = Guid.NewGuid().ToString();
_studentListVM = new BasePagedListVM<Student, BaseSearcher>();
_studentListVM.DC = new DataContext(_seed, DBTypeEnum.Memory);
for (int i = 1; i <= 20; i++)
{
_studentListVM.DC.Set<Student>().Add(new Student
{
LoginName = "s"+i.ToString("00"),
Password = "p",
Name = "n"+(int)(i/10),
IsValid = true
});
}
_studentListVM.DC.SaveChanges();
}

[TestMethod]
public void SearchTest()
{
_studentListVM.Searcher.Limit = 10;
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.Searcher.Count, 20);
Assert.AreEqual(_studentListVM.Searcher.PageCount, 2);
Assert.AreEqual(_studentListVM.EntityList.Count, 10);
}

[TestMethod]
public void SearchTest2()
{
_studentListVM.Searcher.Limit = 5;
_studentListVM.Searcher.Page = 2;
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.Searcher.Count, 20);
Assert.AreEqual(_studentListVM.Searcher.PageCount, 4);
Assert.AreEqual(_studentListVM.EntityList.Count, 5);
}

[TestMethod]
[DataTestMethod()]
[DataRow(-100)]
[DataRow(-20)]
[DataRow(0)]
public void SearchTest3(int page)
{
_studentListVM.Searcher.Limit = 3;
_studentListVM.Searcher.Page = page;
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.Searcher.Count, 20);
Assert.AreEqual(_studentListVM.Searcher.PageCount, 7);
Assert.AreEqual(_studentListVM.EntityList.Count, 3);
Assert.AreEqual(_studentListVM.Searcher.Page, 1);
}

[TestMethod]
[DataTestMethod()]
[DataRow(100)]
[DataRow(20)]
[DataRow(7)]
public void SearchTest4(int page)
{
_studentListVM.Searcher.Limit = 3;
_studentListVM.Searcher.Page = page;
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.Searcher.Count, 20);
Assert.AreEqual(_studentListVM.Searcher.PageCount, 7);
Assert.AreEqual(_studentListVM.EntityList.Count, 2);
Assert.AreEqual(_studentListVM.Searcher.Page, 7);
}

[TestMethod]
public void SearchWithoutPagingTest()
{
_studentListVM.NeedPage = false;
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.Searcher.Count, 20);
Assert.AreEqual(_studentListVM.Searcher.PageCount, 1);
Assert.AreEqual(_studentListVM.EntityList.Count, 20);
}

[TestMethod]
public void SortTest()
{
_studentListVM.Searcher.SortInfo = new SortInfo() { Direction = SortDir.Desc, Property = "LoginName" };
_studentListVM.DoSearch();
Assert.AreEqual(_studentListVM.EntityList[0].LoginName, "s20");
}
}
}

0 comments on commit d9d6bdd

Please sign in to comment.