Skip to content

Commit 0650716

Browse files
committed
test: 优化单元测试
1 parent cd42561 commit 0650716

File tree

8 files changed

+136
-48
lines changed

8 files changed

+136
-48
lines changed

framework/src/Bing.Data/Bing/Data/Queries/Conditions/DateSegmentCondition.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ public DateSegmentCondition(Expression<Func<TEntity, TProperty>> propertyExpress
2626
{
2727
}
2828

29-
/// <summary>
30-
/// 最小值是否大于最大值
31-
/// </summary>
32-
/// <param name="min">最小值</param>
33-
/// <param name="max">最大值</param>
34-
protected override bool IsMinGreaterMax(DateTime? min, DateTime? max) => min > max;
35-
3629
/// <summary>
3730
/// 获取最小值表达式
3831
/// </summary>

framework/tests/Bing.Datas.Test.Integration/Sql/Builders/SqlServer/Clauses/WhereClauseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public void Test_WhereIfNotEmpty_7()
564564
Expression<Func<Sample, bool>> condition = t => t.Email.Contains("a") && t.IntValue == 1;
565565
AssertHelper.Throws<InvalidOperationException>(() => {
566566
_clause.WhereIfNotEmpty(condition);
567-
}, string.Format("仅允许添加一个条件,条件:{0}", condition));
567+
}, $"仅允许添加一个条件条件:{condition}");
568568
}
569569

570570
#endregion

framework/tests/Bing.EventBus.Tests/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Extensions.Logging;
44
using Xunit.DependencyInjection;
55
using Xunit.DependencyInjection.Logging;
6+
67
namespace Bing.EventBus.Tests
78
{
89
/// <summary>

framework/tests/Bing.MailKit.Tests/MailKitEmailSenderTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public MailKitEmailSenderTest(ITestOutputHelper output) : base(output)
5959
[Fact]
6060
public void Test_SendEmail()
6161
{
62-
var box = new EmailBox
63-
{
64-
Subject = "MailKit 测试发送邮件",
65-
To = _to,
66-
Body = "<p style='color:red'>测试一下红色字体的邮件</p>",
67-
IsBodyHtml = true,
68-
};
69-
this._mailKitEmailSender.Send(box);
62+
//var box = new EmailBox
63+
//{
64+
// Subject = "MailKit 测试发送邮件",
65+
// To = _to,
66+
// Body = "<p style='color:red'>测试一下红色字体的邮件</p>",
67+
// IsBodyHtml = true,
68+
//};
69+
//this._mailKitEmailSender.Send(box);
7070
}
7171

7272
/// <summary>

framework/tests/Bing.Tests/Applications/CrudServiceTest.Save.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using AutoMapper;
77
using Bing.AutoMapper;
8+
using Bing.DependencyInjection;
89
using Bing.Tests.Samples;
910
using NSubstitute;
1011
using Xunit;
@@ -79,6 +80,7 @@ public CrudServiceTest()
7980
_unitOfWork = Substitute.For<IUnitOfWork>();
8081
_repository = Substitute.For<IRepositorySample>();
8182
_service = new CrudServiceSample(_unitOfWork, _repository);
83+
_service.LazyServiceProvider = Substitute.For<ILazyServiceProvider>();
8284
var allAssemblyFinder = new AppDomainAllAssemblyFinder();
8385
var mapperProfileTypeFinder = new MapperProfileTypeFinder(allAssemblyFinder);
8486
var instances = mapperProfileTypeFinder

framework/tests/Bing.Tests/Applications/QueryServiceTest.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
6+
using AutoMapper;
7+
using Bing.AutoMapper;
8+
using Bing.DependencyInjection;
9+
using Bing.ObjectMapping;
10+
using Bing.Reflection;
511
using Bing.Tests.Samples;
612
using NSubstitute;
713
using Xunit;
@@ -54,8 +60,26 @@ public QueryServiceTest()
5460
_entity2 = new EntitySample(_id2) { Name = "B" };
5561
_repository = Substitute.For<IRepositorySample>();
5662
_service = new QueryServiceSample(_repository);
57-
//var mapper = new AutoMapperMapper();
58-
//MapperExtensions.SetMapper(mapper);
63+
_service.LazyServiceProvider = Substitute.For<ILazyServiceProvider>();
64+
var allAssemblyFinder = new AppDomainAllAssemblyFinder();
65+
var mapperProfileTypeFinder = new MapperProfileTypeFinder(allAssemblyFinder);
66+
var instances = mapperProfileTypeFinder
67+
.FindAll()
68+
.Select(type => Bing.Reflection.Reflections.CreateInstance<IObjectMapperProfile>(type))
69+
.ToList();
70+
var configuration = new MapperConfiguration(cfg =>
71+
{
72+
foreach (var instance in instances)
73+
{
74+
75+
Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
76+
instance.CreateMap();
77+
// ReSharper disable once SuspiciousTypeConversion.Global
78+
cfg.AddProfile(instance as Profile);
79+
}
80+
});
81+
var mapper = new AutoMapperObjectMapper(configuration, instances);
82+
ObjectMapperExtensions.SetMapper(mapper);
5983
}
6084

6185
/// <summary>

framework/tests/Bing.Tests/Datas/Queries/Criterias/DateSegmentCriteriaTest.cs

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,59 +35,127 @@ public DateSegmentCriteriaTest(ITestOutputHelper output)
3535
{
3636
_output = output;
3737
_min = DateTime.Parse("2000-1-1 10:10:10");
38-
_max = DateTime.Parse("2000-1-2 10:10:10");
38+
_max = DateTime.Parse("2000-1-3 10:10:10");
3939
}
4040

4141
/// <summary>
42-
/// 测试 - 获取查询条件
42+
/// 测试 - 获取查询条件 - 不包含边界
4343
/// </summary>
4444
[Fact]
45-
public void Test_GetPredicate()
45+
public void Test_GetCondition_Neither()
46+
{
47+
var result = new StringBuilder();
48+
result.Append("t => ((t.DateValue >= Convert(Parse(\"2000/1/2 0:00:00\"), DateTime))");
49+
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/3 0:00:00\"), DateTime)))");
50+
51+
var condition= new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Neither);
52+
_output.WriteLine(condition.GetCondition().ToString());
53+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
54+
}
55+
56+
/// <summary>
57+
/// 测试 - 获取查询条件 - 不包含边界【可空】
58+
/// </summary>
59+
[Fact]
60+
public void Test_GetCondition_Neither_With_Nullable()
61+
{
62+
var result = new StringBuilder();
63+
result.Append("t => ((t.NullableDateValue >= Convert(Parse(\"2000/1/2 0:00:00\"), Nullable`1))");
64+
result.Append(" AndAlso (t.NullableDateValue < Convert(Parse(\"2000/1/3 0:00:00\"), Nullable`1)))");
65+
66+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Neither);
67+
_output.WriteLine(condition.GetCondition().ToString());
68+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
69+
}
70+
71+
/// <summary>
72+
/// 测试 - 获取查询条件 - 包含左边
73+
/// </summary>
74+
[Fact]
75+
public void Test_GetCondition_Left()
4676
{
4777
var result = new StringBuilder();
4878
result.Append("t => ((t.DateValue >= Convert(Parse(\"2000/1/1 0:00:00\"), DateTime))");
4979
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/3 0:00:00\"), DateTime)))");
50-
var criteria = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Both);
51-
_output.WriteLine(criteria.GetCondition().ToString());
52-
Assert.Equal(result.ToString(), criteria.GetCondition().ToString());
5380

54-
result = new StringBuilder();
81+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Left);
82+
_output.WriteLine(condition.GetCondition().ToString());
83+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
84+
}
85+
86+
/// <summary>
87+
/// 测试 - 获取查询条件 - 包含左边【可空】
88+
/// </summary>
89+
[Fact]
90+
public void Test_GetCondition_Left_With_Nullable()
91+
{
92+
var result = new StringBuilder();
5593
result.Append("t => ((t.NullableDateValue >= Convert(Parse(\"2000/1/1 0:00:00\"), Nullable`1))");
5694
result.Append(" AndAlso (t.NullableDateValue < Convert(Parse(\"2000/1/3 0:00:00\"), Nullable`1)))");
57-
var criteria2 = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Both);
58-
_output.WriteLine(criteria2.GetCondition().ToString());
59-
Assert.Equal(result.ToString(), criteria2.GetCondition().ToString());
95+
96+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Left);
97+
_output.WriteLine(condition.GetCondition().ToString());
98+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
6099
}
61100

62101
/// <summary>
63-
/// 测试 - 获取查询条件 - 设置边界
102+
/// 测试 - 获取查询条件 - 包含右边
64103
/// </summary>
65104
[Fact]
66-
public void Test_GetPredicate_Boundary()
105+
public void Test_GetCondition_Right()
67106
{
68107
var result = new StringBuilder();
69-
result.Append("t => ((t.DateValue > Convert(Parse(\"2000/1/1 0:00:00\"), DateTime))");
70-
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/3 0:00:00\"), DateTime)))");
71-
var criteria = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Neither);
72-
Assert.Equal(result.ToString(), criteria.GetCondition().ToString());
108+
result.Append("t => ((t.DateValue >= Convert(Parse(\"2000/1/2 0:00:00\"), DateTime))");
109+
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/4 0:00:00\"), DateTime)))");
110+
111+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Right);
112+
_output.WriteLine(condition.GetCondition().ToString());
113+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
114+
}
73115

74-
result = new StringBuilder();
116+
/// <summary>
117+
/// 测试 - 获取查询条件 - 包含右边【可空】
118+
/// </summary>
119+
[Fact]
120+
public void Test_GetCondition_Right_With_Nullable()
121+
{
122+
var result = new StringBuilder();
123+
result.Append("t => ((t.NullableDateValue >= Convert(Parse(\"2000/1/2 0:00:00\"), Nullable`1))");
124+
result.Append(" AndAlso (t.NullableDateValue < Convert(Parse(\"2000/1/4 0:00:00\"), Nullable`1)))");
125+
126+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Right);
127+
_output.WriteLine(condition.GetCondition().ToString());
128+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
129+
}
130+
131+
/// <summary>
132+
/// 测试 - 获取查询条件 - 包含两边
133+
/// </summary>
134+
[Fact]
135+
public void Test_GetCondition_Both()
136+
{
137+
var result = new StringBuilder();
75138
result.Append("t => ((t.DateValue >= Convert(Parse(\"2000/1/1 0:00:00\"), DateTime))");
76-
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/3 0:00:00\"), DateTime)))");
77-
criteria = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Left);
78-
Assert.Equal(result.ToString(), criteria.GetCondition().ToString());
139+
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/4 0:00:00\"), DateTime)))");
79140

80-
result = new StringBuilder();
81-
result.Append("t => ((t.NullableDateValue > Convert(Parse(\"2000/1/1 0:00:00\"), Nullable`1))");
82-
result.Append(" AndAlso (t.NullableDateValue <= Convert(Parse(\"2000/1/3 0:00:00\"), Nullable`1)))");
83-
var criteria2 = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Right);
84-
Assert.Equal(result.ToString(), criteria2.GetCondition().ToString());
141+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime>(t => t.DateValue, _min, _max, Boundary.Both);
142+
_output.WriteLine(condition.GetCondition().ToString());
143+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
144+
}
85145

86-
result = new StringBuilder();
146+
/// <summary>
147+
/// 测试 - 获取查询条件 - 包含两边【可空】
148+
/// </summary>
149+
[Fact]
150+
public void Test_GetCondition_Both_With_Nullable()
151+
{
152+
var result = new StringBuilder();
87153
result.Append("t => ((t.NullableDateValue >= Convert(Parse(\"2000/1/1 0:00:00\"), Nullable`1))");
88-
result.Append(" AndAlso (t.NullableDateValue <= Convert(Parse(\"2000/1/3 0:00:00\"), Nullable`1)))");
89-
criteria2 = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Both);
90-
Assert.Equal(result.ToString(), criteria2.GetCondition().ToString());
154+
result.Append(" AndAlso (t.NullableDateValue < Convert(Parse(\"2000/1/4 0:00:00\"), Nullable`1)))");
155+
156+
var condition = new DateSegmentCondition<AggregateRootSample, DateTime?>(t => t.NullableDateValue, _min, _max, Boundary.Both);
157+
_output.WriteLine(condition.GetCondition().ToString());
158+
Assert.Equal(result.ToString(), condition.GetCondition().ToString());
91159
}
92160
}
93161
}

framework/tests/Bing.Tests/Datas/Queries/QueryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void Test_Between_Date()
168168
var max = DateTime.Parse("2000-1-2 10:10:10");
169169
var result = new StringBuilder();
170170
result.Append("t => ((t.DateValue >= Convert(Parse(\"2000/1/1 0:00:00\"), DateTime))");
171-
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/3 0:00:00\"), DateTime)))");
171+
result.Append(" AndAlso (t.DateValue < Convert(Parse(\"2000/1/2 0:00:00\"), DateTime)))");
172172

173173
_query.Between(t => t.DateValue, min, max, false);
174174
Assert.Equal(result.ToString(), _query.GetCondition().ToString());

0 commit comments

Comments
 (0)