test: 增加任务管理单元测试

This commit is contained in:
Argo Zhang 2020-02-21 14:43:23 +08:00
parent 2454ea3684
commit dd85f12e79
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
2 changed files with 63 additions and 1 deletions

View File

@ -56,7 +56,10 @@ namespace Bootstrap.Admin.Controllers.Api
{
// 判断 Cron 表达式
if (string.IsNullOrEmpty(widget.CronExpression)) return false;
if (_tasks.Any(t => t.Equals(widget.Name, StringComparison.OrdinalIgnoreCase))) return false;
// 系统内置任务禁止更改
// 演示模式下禁止删除内置任务
if (DictHelper.RetrieveSystemModel() && _tasks.Any(t => t.Equals(widget.Name, StringComparison.OrdinalIgnoreCase))) return false;
// 加载任务执行体
// 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类

View File

@ -33,5 +33,64 @@ namespace Bootstrap.Admin.Api
var recv = await Client.PutAsJsonAsync<string, bool>("/api/Tasks/SQL日志?operType=run", "");
Assert.True(recv);
}
[Fact]
public async void Post_Ok()
{
var widget = new TaskWidget();
// widget Cron 表达式为 ”“
var resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
// task executor 不合法
widget.CronExpression = Longbow.Tasks.Cron.Secondly(5);
widget.TaskExecutorName = "UnitTest-Widget";
resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor";
widget.Name = "UnitTest-Task";
resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.True(resp);
// Delete
resp = await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.True(resp);
}
}
[Collection("SystemModel")]
public class TasksSystemModelTest
{
private HttpClient client;
public TasksSystemModelTest(BASystemModelWebHost factory)
{
client = factory.CreateClient("/api/Tasks");
}
[Fact]
public async void Post_Ok()
{
var widget = new TaskWidget();
widget.CronExpression = Longbow.Tasks.Cron.Secondly(5);
widget.Name = "单次任务";
widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor";
// 演示模式下禁止移除系统内置任务
var resp = await client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.False(resp);
widget.Name = "Test-Widget";
resp = await client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.True(resp);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.True(resp);
}
}
}