102 lines
3.7 KiB
C#
102 lines
3.7 KiB
C#
using FluentAssertions;
|
|
using Workflow.Application.Form.Aggregates;
|
|
using Workflow.Domain.Entities;
|
|
using Workflow.Domain.Enums;
|
|
using Xunit;
|
|
|
|
namespace BuildFlow.Workflow.Tests.Form;
|
|
|
|
[Collection("FormTests")]
|
|
public class FormAggregateFieldTests(FormTestFixtureClassFixture fixture)
|
|
{
|
|
[Fact]
|
|
public async Task RefreshAsync_SameInstance_SumsJsonbAmountField()
|
|
{
|
|
var mainFormId = Guid.NewGuid();
|
|
var lineFormId = Guid.NewGuid();
|
|
var instanceId = Guid.NewGuid();
|
|
await using var db = await fixture.CreateDbContextWithSeedAsync(
|
|
nameof(RefreshAsync_SameInstance_SumsJsonbAmountField),
|
|
seed =>
|
|
{
|
|
seed.FormDefinitions.AddRange(
|
|
new FormDefinition
|
|
{
|
|
Id = mainFormId,
|
|
Name = "费用主表",
|
|
Code = "expense-main",
|
|
Status = FormStatus.Published,
|
|
Version = 1,
|
|
SchemaJson = AggregateMainSchema,
|
|
},
|
|
new FormDefinition
|
|
{
|
|
Id = lineFormId,
|
|
Name = "费用明细",
|
|
Code = "expense-line",
|
|
Status = FormStatus.Published,
|
|
Version = 1,
|
|
SchemaJson = """
|
|
{"type":"object","properties":{"amount":{"type":"number","title":"金额","x-component":"InputNumber"}}}
|
|
""",
|
|
});
|
|
|
|
seed.FormData.AddRange(
|
|
new global::Workflow.Domain.Entities.FormData
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
FormDefinitionId = mainFormId,
|
|
InstanceId = instanceId,
|
|
DataJson = """{"requestId":"REQ-001","lineTotal":0}""",
|
|
},
|
|
new global::Workflow.Domain.Entities.FormData
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
FormDefinitionId = lineFormId,
|
|
InstanceId = instanceId,
|
|
DataJson = """{"requestId":"REQ-001","amount":10}""",
|
|
},
|
|
new global::Workflow.Domain.Entities.FormData
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
FormDefinitionId = lineFormId,
|
|
InstanceId = instanceId,
|
|
DataJson = """{"requestId":"REQ-001","amount":15}""",
|
|
});
|
|
});
|
|
|
|
var service = new FormAggregateRefreshService(db);
|
|
|
|
await service.RefreshAsync(mainFormId, instanceId, CancellationToken.None);
|
|
|
|
var main = await db.FormData.FindAsync(db.FormData.Single(f => f.FormDefinitionId == mainFormId).Id);
|
|
main!.DataJson.Should().Contain("\"lineTotal\":25");
|
|
}
|
|
|
|
private const string AggregateMainSchema = """
|
|
{
|
|
"type": "object",
|
|
"properties": {
|
|
"requestId": { "type": "string", "title": "申请编号", "x-component": "Input" },
|
|
"lineTotal": {
|
|
"type": "number",
|
|
"title": "明细总金额",
|
|
"x-component": "InputNumber",
|
|
"x-field-kind": "aggregate",
|
|
"x-derived": {
|
|
"mode": "aggregate",
|
|
"source": {
|
|
"type": "formData",
|
|
"formDefinitionCode": "expense-line",
|
|
"relationPath": "requestId",
|
|
"relationValuePath": "requestId"
|
|
},
|
|
"aggregate": { "function": "sum", "fieldPath": "amount" },
|
|
"refresh": { "trigger": "afterSubmit", "scope": "sameInstance" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
}
|