Skip to content

Commit 644bb01

Browse files
committed
完善 TDengine 数据驱动的导入功能的单元测试。 ⛺
1 parent a6b099c commit 644bb01

File tree

1 file changed

+99
-13
lines changed

1 file changed

+99
-13
lines changed

Zongsoft.Data/drivers/tdengine/test/ImporterTest.cs

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,111 @@ namespace Zongsoft.Data.TDengine.Tests;
1010
[Collection("Database")]
1111
public class ImporterTest(DatabaseFixture database)
1212
{
13+
private const string PREFIX = "$Imported:";
1314
private readonly DatabaseFixture _database = database;
1415

1516
[Fact]
16-
public void TestImport()
17+
public void TestImportModel()
1718
{
1819
const int COUNT = 100;
1920

2021
if(!Global.IsTestingEnabled)
2122
return;
2223

24+
var timestamp = DateTime.Now;
2325
IDataAccess accessor = _database.Accessor;
2426

25-
Assert.NotNull(accessor);
26-
Assert.NotNull(Mapping.Entities);
27-
Assert.NotEmpty(Mapping.Entities);
28-
Assert.True(Mapping.Entities.Contains("GatewayHistory"));
27+
//执行数据导入操作
28+
var count = accessor.Import(GenerateModels(COUNT));
29+
Assert.Equal(COUNT, count);
30+
31+
var models = accessor.Select<Models.GatewayHistory>(
32+
Condition.GreaterThanEqual(nameof(Models.GatewayHistory.Timestamp), timestamp) &
33+
Condition.Like(nameof(Models.GatewayHistory.Text), $"{PREFIX}%"));
34+
35+
count = 0;
36+
foreach(var model in models)
37+
{
38+
Assert.True(model.GatewayId > 0);
39+
Assert.StartsWith(PREFIX, model.Text);
40+
++count;
41+
}
2942

30-
var count = accessor.Import(Generate(COUNT));
3143
Assert.Equal(COUNT, count);
3244
}
3345

3446
[Fact]
35-
public async Task TestImportAsync()
47+
public async Task TestImportModelAsync()
3648
{
3749
const int COUNT = 100;
3850

3951
if(!Global.IsTestingEnabled)
4052
return;
4153

54+
var timestamp = DateTime.Now;
4255
IDataAccess accessor = _database.Accessor;
4356

44-
Assert.NotNull(accessor);
45-
Assert.NotNull(Mapping.Entities);
46-
Assert.NotEmpty(Mapping.Entities);
47-
Assert.True(Mapping.Entities.Contains("GatewayHistory"));
57+
//执行数据导入操作
58+
var count = await accessor.ImportAsync(GenerateModels(COUNT));
59+
Assert.Equal(COUNT, count);
60+
61+
var models = accessor.SelectAsync<Models.GatewayHistory>(
62+
Condition.GreaterThanEqual(nameof(Models.GatewayHistory.Timestamp), timestamp) &
63+
Condition.Like(nameof(Models.GatewayHistory.Text), $"{PREFIX}%"));
64+
65+
count = 0;
66+
await foreach(var model in models)
67+
{
68+
Assert.True(model.GatewayId > 0);
69+
Assert.StartsWith(PREFIX, model.Text);
70+
++count;
71+
}
72+
73+
Assert.Equal(COUNT, count);
74+
}
75+
76+
[Fact]
77+
public async Task TestImportDictionaryAsync()
78+
{
79+
const int COUNT = 100;
80+
81+
if(!Global.IsTestingEnabled)
82+
return;
83+
84+
var timestamp = DateTime.Now;
85+
IDataAccess accessor = _database.Accessor;
86+
87+
//执行数据导入操作
88+
var count = await accessor.ImportAsync(nameof(Models.GatewayHistory), GenerateDictionaries(COUNT));
89+
Assert.Equal(COUNT, count);
90+
91+
var models = accessor.SelectAsync<IDictionary<string, object>>(
92+
nameof(Models.GatewayHistory),
93+
Condition.GreaterThanEqual(nameof(Models.GatewayHistory.Timestamp), timestamp) &
94+
Condition.Like(nameof(Models.GatewayHistory.Text), $"{PREFIX}%"));
95+
96+
count = 0;
97+
await foreach(var model in models)
98+
{
99+
Assert.NotNull(model);
100+
Assert.NotEmpty(model);
101+
102+
Assert.True(model.TryGetValue(nameof(Models.GatewayHistory.GatewayId), out var value));
103+
Assert.NotNull(value);
104+
Assert.True((uint)value > 0);
105+
106+
Assert.True(model.TryGetValue(nameof(Models.GatewayHistory.Text), out value));
107+
Assert.NotNull(value);
108+
Assert.StartsWith(PREFIX, (string)value);
109+
110+
++count;
111+
}
48112

49-
var count = await accessor.ImportAsync(Generate(COUNT));
50113
Assert.Equal(COUNT, count);
51114
}
52115

53116
#region 私有方法
54-
private static IEnumerable<Models.GatewayHistory> Generate(int count = 100)
117+
private static IEnumerable<Models.GatewayHistory> GenerateModels(int count = 100)
55118
{
56119
var timestamp = DateTime.Now;
57120

@@ -66,10 +129,33 @@ public async Task TestImportAsync()
66129
GatewayId = (uint)(i + 1),
67130
MetricId = (ulong)Random.Shared.NextInt64(),
68131
Value = Random.Shared.NextDouble(),
132+
Text = $"{PREFIX}{Random.Shared.Next()}",
69133
FailureCode = failureCode,
70134
FailureMessage = failureMessage,
71135
};
72136
}
73137
}
138+
139+
private static IEnumerable<Dictionary<string, object>> GenerateDictionaries(int count = 100)
140+
{
141+
var timestamp = DateTime.Now;
142+
143+
for(int i = 0; i < count; i++)
144+
{
145+
var failureCode = i > 0 && i % 50 == 0 ? Random.Shared.Next(1, 10) : 0;
146+
var failureMessage = failureCode > 0 ? $"Message #{Math.Abs(Random.Shared.Next()):X}" : null;
147+
148+
yield return new Dictionary<string, object>()
149+
{
150+
{ nameof(Models.GatewayHistory.Timestamp), timestamp.AddMilliseconds(1) },
151+
{ nameof(Models.GatewayHistory.GatewayId), (uint)(i + 1) },
152+
{ nameof(Models.GatewayHistory.MetricId), (ulong)Random.Shared.NextInt64() },
153+
{ nameof(Models.GatewayHistory.Value), Random.Shared.NextDouble() },
154+
{ nameof(Models.GatewayHistory.Text), $"{PREFIX}{Random.Shared.Next()}" },
155+
{ nameof(Models.GatewayHistory.FailureCode), failureCode },
156+
{ nameof(Models.GatewayHistory.FailureMessage), failureMessage },
157+
};
158+
}
159+
}
74160
#endregion
75161
}

0 commit comments

Comments
 (0)