Skip to content

Commit ca31f1f

Browse files
committed
fix unit tests
1 parent 69cb2f8 commit ca31f1f

File tree

6 files changed

+141
-79
lines changed

6 files changed

+141
-79
lines changed

test/ScheduledTasks.Tests/Client/ScheduleClientImplTests.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ namespace Microsoft.DurableTask.ScheduledTasks.Tests.Client;
1212

1313
public class ScheduleClientImplTests
1414
{
15-
readonly Mock<DurableTaskClient> durableTaskClientMock;
16-
readonly Mock<ILogger> loggerMock;
17-
readonly Mock<DurableEntityClient> entityClientMock;
15+
readonly Mock<DurableTaskClient> durableTaskClient;
16+
readonly Mock<DurableEntityClient> entityClient;
17+
readonly Mock<ILogger> logger;
1818
readonly ScheduleClientImpl client;
1919
readonly string scheduleId = "test-schedule";
2020

2121
public ScheduleClientImplTests()
2222
{
23-
this.durableTaskClientMock = new Mock<DurableTaskClient>();
24-
this.loggerMock = new Mock<ILogger>();
25-
this.entityClientMock = new Mock<DurableEntityClient>();
26-
this.durableTaskClientMock.Setup(c => c.Entities).Returns(this.entityClientMock.Object);
27-
this.client = new ScheduleClientImpl(this.durableTaskClientMock.Object, this.scheduleId, this.loggerMock.Object);
23+
this.durableTaskClient = new Mock<DurableTaskClient>("test", MockBehavior.Strict);
24+
this.entityClient = new Mock<DurableEntityClient>("test", MockBehavior.Strict);
25+
this.logger = new Mock<ILogger>(MockBehavior.Loose);
26+
this.durableTaskClient.Setup(x => x.Entities).Returns(this.entityClient.Object);
27+
this.client = new ScheduleClientImpl(this.durableTaskClient.Object, this.scheduleId, this.logger.Object);
2828
}
2929

3030
[Fact]
3131
public void Constructor_WithNullClient_ThrowsArgumentNullException()
3232
{
3333
// Act & Assert
3434
var ex = Assert.Throws<ArgumentNullException>(() =>
35-
new ScheduleClientImpl(null!, this.scheduleId, this.loggerMock.Object));
35+
new ScheduleClientImpl(null!, this.scheduleId, this.logger.Object));
3636
Assert.Equal("client", ex.ParamName);
3737
}
3838

@@ -43,7 +43,7 @@ public void Constructor_WithInvalidScheduleId_ThrowsArgumentException(string inv
4343
{
4444
// Act & Assert
4545
var ex = Assert.Throws<ArgumentException>(() =>
46-
new ScheduleClientImpl(this.durableTaskClientMock.Object, invalidScheduleId, this.loggerMock.Object));
46+
new ScheduleClientImpl(this.durableTaskClient.Object, invalidScheduleId, this.logger.Object));
4747
Assert.Contains("scheduleId cannot be null or empty", ex.Message, StringComparison.OrdinalIgnoreCase);
4848
}
4949

@@ -52,7 +52,7 @@ public void Constructor_WithNullLogger_ThrowsArgumentNullException()
5252
{
5353
// Act & Assert
5454
var ex = Assert.Throws<ArgumentNullException>(() =>
55-
new ScheduleClientImpl(this.durableTaskClientMock.Object, this.scheduleId, null!));
55+
new ScheduleClientImpl(this.durableTaskClient.Object, this.scheduleId, null!));
5656
Assert.Equal("logger", ex.ParamName);
5757
}
5858

@@ -66,7 +66,7 @@ public async Task DescribeAsync_WhenExists_ReturnsDescription()
6666
ScheduleConfiguration = new ScheduleConfiguration(this.scheduleId, "test-orchestration", TimeSpan.FromMinutes(5))
6767
};
6868

69-
this.entityClientMock
69+
this.entityClient
7070
.Setup(c => c.GetEntityAsync<ScheduleState>(
7171
It.Is<EntityInstanceId>(id => id.Name == nameof(Schedule) && id.Key == this.scheduleId),
7272
It.IsAny<CancellationToken>()))
@@ -86,7 +86,7 @@ public async Task DescribeAsync_WhenExists_ReturnsDescription()
8686
public async Task DescribeAsync_WhenNotExists_ThrowsScheduleNotFoundException()
8787
{
8888
// Arrange
89-
this.entityClientMock
89+
this.entityClient
9090
.Setup(c => c.GetEntityAsync<ScheduleState>(
9191
It.Is<EntityInstanceId>(id => id.Name == nameof(Schedule) && id.Key == this.scheduleId),
9292
It.IsAny<CancellationToken>()))
@@ -103,22 +103,22 @@ public async Task DeleteAsync_ExecutesDeleteOperation()
103103
// Arrange
104104
string instanceId = "test-instance";
105105

106-
this.durableTaskClientMock
106+
this.durableTaskClient
107107
.Setup(c => c.ScheduleNewOrchestrationInstanceAsync(
108108
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
109109
It.IsAny<ScheduleOperationRequest>(),
110110
It.IsAny<CancellationToken>()))
111111
.ReturnsAsync(instanceId);
112112

113-
this.durableTaskClientMock
113+
this.durableTaskClient
114114
.Setup(c => c.WaitForInstanceCompletionAsync(instanceId, true, It.IsAny<CancellationToken>()))
115115
.ReturnsAsync(new OrchestrationMetadata(nameof(ExecuteScheduleOperationOrchestrator), instanceId));
116116

117117
// Act
118118
await this.client.DeleteAsync();
119119

120120
// Assert
121-
this.durableTaskClientMock.Verify(
121+
this.durableTaskClient.Verify(
122122
c => c.ScheduleNewOrchestrationInstanceAsync(
123123
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
124124
It.Is<ScheduleOperationRequest>(r =>
@@ -135,22 +135,22 @@ public async Task PauseAsync_ExecutesPauseOperation()
135135
// Arrange
136136
string instanceId = "test-instance";
137137

138-
this.durableTaskClientMock
138+
this.durableTaskClient
139139
.Setup(c => c.ScheduleNewOrchestrationInstanceAsync(
140140
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
141141
It.IsAny<ScheduleOperationRequest>(),
142142
It.IsAny<CancellationToken>()))
143143
.ReturnsAsync(instanceId);
144144

145-
this.durableTaskClientMock
145+
this.durableTaskClient
146146
.Setup(c => c.WaitForInstanceCompletionAsync(instanceId, true, It.IsAny<CancellationToken>()))
147147
.ReturnsAsync(new OrchestrationMetadata(nameof(ExecuteScheduleOperationOrchestrator), instanceId));
148148

149149
// Act
150150
await this.client.PauseAsync();
151151

152152
// Assert
153-
this.durableTaskClientMock.Verify(
153+
this.durableTaskClient.Verify(
154154
c => c.ScheduleNewOrchestrationInstanceAsync(
155155
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
156156
It.Is<ScheduleOperationRequest>(r =>
@@ -167,22 +167,22 @@ public async Task ResumeAsync_ExecutesResumeOperation()
167167
// Arrange
168168
string instanceId = "test-instance";
169169

170-
this.durableTaskClientMock
170+
this.durableTaskClient
171171
.Setup(c => c.ScheduleNewOrchestrationInstanceAsync(
172172
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
173173
It.IsAny<ScheduleOperationRequest>(),
174174
It.IsAny<CancellationToken>()))
175175
.ReturnsAsync(instanceId);
176176

177-
this.durableTaskClientMock
177+
this.durableTaskClient
178178
.Setup(c => c.WaitForInstanceCompletionAsync(instanceId, true, It.IsAny<CancellationToken>()))
179179
.ReturnsAsync(new OrchestrationMetadata(nameof(ExecuteScheduleOperationOrchestrator), instanceId));
180180

181181
// Act
182182
await this.client.ResumeAsync();
183183

184184
// Assert
185-
this.durableTaskClientMock.Verify(
185+
this.durableTaskClient.Verify(
186186
c => c.ScheduleNewOrchestrationInstanceAsync(
187187
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
188188
It.Is<ScheduleOperationRequest>(r =>
@@ -204,22 +204,22 @@ public async Task UpdateAsync_ExecutesUpdateOperation()
204204
Interval = TimeSpan.FromMinutes(10)
205205
};
206206

207-
this.durableTaskClientMock
207+
this.durableTaskClient
208208
.Setup(c => c.ScheduleNewOrchestrationInstanceAsync(
209209
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
210210
It.IsAny<ScheduleOperationRequest>(),
211211
It.IsAny<CancellationToken>()))
212212
.ReturnsAsync(instanceId);
213213

214-
this.durableTaskClientMock
214+
this.durableTaskClient
215215
.Setup(c => c.WaitForInstanceCompletionAsync(instanceId, true, It.IsAny<CancellationToken>()))
216216
.ReturnsAsync(new OrchestrationMetadata(nameof(ExecuteScheduleOperationOrchestrator), instanceId));
217217

218218
// Act
219219
await this.client.UpdateAsync(updateOptions);
220220

221221
// Assert
222-
this.durableTaskClientMock.Verify(
222+
this.durableTaskClient.Verify(
223223
c => c.ScheduleNewOrchestrationInstanceAsync(
224224
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
225225
It.Is<ScheduleOperationRequest>(r =>

test/ScheduledTasks.Tests/Client/ScheduledTaskClientImplTests.cs

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.DurableTask;
45
using Microsoft.DurableTask.Client;
56
using Microsoft.DurableTask.Client.Entities;
67
using Microsoft.DurableTask.Entities;
@@ -12,33 +13,33 @@ namespace Microsoft.DurableTask.ScheduledTasks.Tests.Client;
1213

1314
public class ScheduledTaskClientImplTests
1415
{
15-
readonly Mock<DurableTaskClient> durableTaskClientMock;
16-
readonly Mock<ILogger> loggerMock;
17-
readonly Mock<DurableEntityClient> entityClientMock;
16+
readonly Mock<DurableTaskClient> durableTaskClient;
17+
readonly Mock<DurableEntityClient> entityClient;
18+
readonly Mock<ILogger<ScheduledTaskClientImpl>> logger;
1819
readonly ScheduledTaskClientImpl client;
1920

2021
public ScheduledTaskClientImplTests()
2122
{
22-
this.durableTaskClientMock = new Mock<DurableTaskClient>();
23-
this.loggerMock = new Mock<ILogger>();
24-
this.entityClientMock = new Mock<DurableEntityClient>();
25-
this.durableTaskClientMock.Setup(c => c.Entities).Returns(this.entityClientMock.Object);
26-
this.client = new ScheduledTaskClientImpl(this.durableTaskClientMock.Object, this.loggerMock.Object);
23+
this.durableTaskClient = new Mock<DurableTaskClient>("test", MockBehavior.Strict);
24+
this.entityClient = new Mock<DurableEntityClient>("test", MockBehavior.Strict);
25+
this.logger = new Mock<ILogger<ScheduledTaskClientImpl>>(MockBehavior.Loose);
26+
this.durableTaskClient.Setup(x => x.Entities).Returns(this.entityClient.Object);
27+
this.client = new ScheduledTaskClientImpl(this.durableTaskClient.Object, this.logger.Object);
2728
}
2829

2930
[Fact]
3031
public void Constructor_WithNullClient_ThrowsArgumentNullException()
3132
{
3233
// Act & Assert
33-
var ex = Assert.Throws<ArgumentNullException>(() => new ScheduledTaskClientImpl(null!, this.loggerMock.Object));
34+
var ex = Assert.Throws<ArgumentNullException>(() => new ScheduledTaskClientImpl(null!, this.logger.Object));
3435
Assert.Equal("durableTaskClient", ex.ParamName);
3536
}
3637

3738
[Fact]
3839
public void Constructor_WithNullLogger_ThrowsArgumentNullException()
3940
{
4041
// Act & Assert
41-
var ex = Assert.Throws<ArgumentNullException>(() => new ScheduledTaskClientImpl(this.durableTaskClientMock.Object, null!));
42+
var ex = Assert.Throws<ArgumentNullException>(() => new ScheduledTaskClientImpl(this.durableTaskClient.Object, null!));
4243
Assert.Equal("logger", ex.ParamName);
4344
}
4445

@@ -73,15 +74,20 @@ public async Task CreateScheduleAsync_WithValidOptions_CreatesSchedule()
7374
var options = new ScheduleCreationOptions("test-schedule", "test-orchestration", TimeSpan.FromMinutes(5));
7475
string instanceId = "test-instance";
7576

76-
this.durableTaskClientMock
77-
.Setup(c => c.ScheduleNewOrchestrationInstanceAsync(
77+
this.durableTaskClient
78+
.Setup(x => x.ScheduleNewOrchestrationInstanceAsync(
7879
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
79-
It.IsAny<ScheduleOperationRequest>(),
80-
It.IsAny<CancellationToken>()))
80+
It.Is<ScheduleOperationRequest>(r =>
81+
r.EntityId.Name == nameof(Schedule) &&
82+
r.EntityId.Key == options.ScheduleId &&
83+
r.OperationName == nameof(Schedule.CreateSchedule) &&
84+
r.Input == options),
85+
null,
86+
default))
8187
.ReturnsAsync(instanceId);
8288

83-
this.durableTaskClientMock
84-
.Setup(c => c.WaitForInstanceCompletionAsync(instanceId, true, It.IsAny<CancellationToken>()))
89+
this.durableTaskClient
90+
.Setup(x => x.WaitForInstanceCompletionAsync(instanceId, true, default))
8591
.ReturnsAsync(new OrchestrationMetadata(nameof(ExecuteScheduleOperationOrchestrator), instanceId));
8692

8793
// Act
@@ -91,14 +97,16 @@ public async Task CreateScheduleAsync_WithValidOptions_CreatesSchedule()
9197
Assert.NotNull(scheduleClient);
9298
Assert.Equal(options.ScheduleId, scheduleClient.ScheduleId);
9399

94-
this.durableTaskClientMock.Verify(
95-
c => c.ScheduleNewOrchestrationInstanceAsync(
100+
this.durableTaskClient.Verify(
101+
x => x.ScheduleNewOrchestrationInstanceAsync(
96102
It.Is<TaskName>(n => n.Name == nameof(ExecuteScheduleOperationOrchestrator)),
97103
It.Is<ScheduleOperationRequest>(r =>
98104
r.EntityId.Name == nameof(Schedule) &&
99105
r.EntityId.Key == options.ScheduleId &&
100-
r.OperationName == nameof(Schedule.CreateSchedule)),
101-
It.IsAny<CancellationToken>()),
106+
r.OperationName == nameof(Schedule.CreateSchedule) &&
107+
r.Input == options),
108+
null,
109+
default),
102110
Times.Once);
103111
}
104112

@@ -122,10 +130,11 @@ public async Task GetScheduleAsync_WhenExists_ReturnsDescription()
122130
ScheduleConfiguration = new ScheduleConfiguration(scheduleId, "test-orchestration", TimeSpan.FromMinutes(5))
123131
};
124132

125-
this.entityClientMock
126-
.Setup(c => c.GetEntityAsync<ScheduleState>(
133+
this.entityClient
134+
.Setup(x => x.GetEntityAsync<ScheduleState>(
127135
It.Is<EntityInstanceId>(id => id.Name == nameof(Schedule) && id.Key == scheduleId),
128-
It.IsAny<CancellationToken>()))
136+
true,
137+
default))
129138
.ReturnsAsync(new EntityMetadata<ScheduleState>(new EntityInstanceId(nameof(Schedule), scheduleId), state));
130139

131140
// Act
@@ -144,16 +153,64 @@ public async Task GetScheduleAsync_WhenNotExists_ReturnsNull()
144153
// Arrange
145154
string scheduleId = "test-schedule";
146155

147-
this.entityClientMock
148-
.Setup(c => c.GetEntityAsync<ScheduleState>(
156+
this.entityClient
157+
.Setup(x => x.GetEntityAsync<ScheduleState>(
149158
It.Is<EntityInstanceId>(id => id.Name == nameof(Schedule) && id.Key == scheduleId),
150-
It.IsAny<CancellationToken>()))
151-
.ReturnsAsync((EntityMetadata<ScheduleState>)null!);
159+
true,
160+
default))
161+
.ReturnsAsync((EntityMetadata<ScheduleState?>)null);
152162

153163
// Act
154164
var description = await this.client.GetScheduleAsync(scheduleId);
155165

156166
// Assert
157167
Assert.Null(description);
158168
}
169+
170+
[Fact]
171+
public async Task ListSchedulesAsync_ReturnsSchedules()
172+
{
173+
// Arrange
174+
var query = new ScheduleQuery
175+
{
176+
ScheduleIdPrefix = "test",
177+
Status = ScheduleStatus.Active,
178+
PageSize = 10
179+
};
180+
181+
var states = new[]
182+
{
183+
new EntityMetadata<ScheduleState>(
184+
new EntityInstanceId(nameof(Schedule), "test-1"),
185+
new ScheduleState
186+
{
187+
Status = ScheduleStatus.Active,
188+
ScheduleConfiguration = new ScheduleConfiguration("test-1", "test-orchestration", TimeSpan.FromMinutes(5))
189+
}),
190+
new EntityMetadata<ScheduleState>(
191+
new EntityInstanceId(nameof(Schedule), "test-2"),
192+
new ScheduleState
193+
{
194+
Status = ScheduleStatus.Active,
195+
ScheduleConfiguration = new ScheduleConfiguration("test-2", "test-orchestration", TimeSpan.FromMinutes(5))
196+
})
197+
};
198+
199+
this.entityClient
200+
.Setup(x => x.GetAllEntitiesAsync<ScheduleState>(It.IsAny<EntityQuery>()))
201+
.Returns(Pageable.Create<EntityMetadata<ScheduleState>>((continuation, pageSize, cancellation) =>
202+
Task.FromResult(new Page<EntityMetadata<ScheduleState>>(states.ToList(), null))));
203+
204+
// Act
205+
var schedules = new List<ScheduleDescription>();
206+
await foreach (var schedule in this.client.ListSchedulesAsync(query))
207+
{
208+
schedules.Add(schedule);
209+
}
210+
211+
// Assert
212+
Assert.Equal(2, schedules.Count);
213+
Assert.All(schedules, s => Assert.StartsWith("test-", s.ScheduleId));
214+
Assert.All(schedules, s => Assert.Equal(ScheduleStatus.Active, s.Status));
215+
}
159216
}

test/ScheduledTasks.Tests/Entity/ScheduleTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace Microsoft.DurableTask.ScheduledTasks.Tests.Entity;
1010

1111
public class ScheduleTests
1212
{
13-
private readonly Mock<ILogger<Schedule>> mockLogger;
14-
private readonly Mock<TaskEntityContext> mockContext;
15-
private readonly Schedule schedule;
16-
private readonly string scheduleId = "test-schedule";
13+
readonly Mock<ILogger<Schedule>> mockLogger;
14+
readonly Mock<TaskEntityContext> mockContext;
15+
readonly Schedule schedule;
16+
readonly string scheduleId = "test-schedule";
1717

1818
public ScheduleTests()
1919
{
20-
this.mockLogger = new Mock<ILogger<Schedule>>();
21-
this.mockContext = new Mock<TaskEntityContext>();
20+
this.mockLogger = new Mock<ILogger<Schedule>>(MockBehavior.Loose);
21+
this.mockContext = new Mock<TaskEntityContext>(MockBehavior.Strict);
2222
this.schedule = new Schedule(this.mockLogger.Object);
2323
}
2424

@@ -203,4 +203,4 @@ public void RunSchedule_WithInvalidToken_DoesNotRun()
203203
It.IsAny<object>(),
204204
It.IsAny<StartOrchestrationOptions>()), Times.Never);
205205
}
206-
}
206+
}

0 commit comments

Comments
 (0)