|
| 1 | +using PythonScriptRunner.Core.Tests.Infrastructure; |
| 2 | + |
| 3 | +namespace PythonScriptRunner.Core.Tests.Integration; |
| 4 | + |
| 5 | +public sealed class PipelineRunTests(PythonFixture fixture) |
| 6 | +{ |
| 7 | + private const string Generator = """ |
| 8 | + from psrunner import stage |
| 9 | +
|
| 10 | + @stage |
| 11 | + def run(inputs, ctx): |
| 12 | + out = ctx.new_file(inputs["nome"], "text/plain") |
| 13 | + out.path.write_text(inputs["texto"], encoding="utf-8") |
| 14 | + return {"arquivo": out, "tamanho": len(inputs["texto"])} |
| 15 | + """; |
| 16 | + |
| 17 | + private const string Reader = """ |
| 18 | + from psrunner import stage |
| 19 | +
|
| 20 | + @stage |
| 21 | + def run(inputs, ctx): |
| 22 | + conteudo = inputs["arquivo"].read_text() |
| 23 | + return {"resumo": f"{inputs['prefixo']}: {conteudo}", "porta": inputs["porta"]} |
| 24 | + """; |
| 25 | + |
| 26 | + private const string Ok = """ |
| 27 | + from psrunner import stage |
| 28 | +
|
| 29 | + @stage |
| 30 | + def run(inputs, ctx): |
| 31 | + return {"valor": inputs["valor"] + 1} |
| 32 | + """; |
| 33 | + |
| 34 | + private string CalculoProgressao => Path.Combine(fixture.RepositoryRoot, "tests", "fixtures", "calculo-progressao"); |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task Spec003_AC01_CalculoProgressao_ProducesTheExpectedNumbers() |
| 38 | + { |
| 39 | + // Arrange |
| 40 | + await using var host = RunnerHost.Create(fixture, b => b.AddFileCatalog(CalculoProgressao)); |
| 41 | + |
| 42 | + // Act |
| 43 | + await using var result = await host.Runner.RunPipelineAsync("calculo-progressao", new |
| 44 | + { |
| 45 | + a1 = 1, |
| 46 | + razao = 2, |
| 47 | + n = 4, |
| 48 | + tipo = "geometrica", |
| 49 | + coeficientes = new[] { 1.0, 0.0, 1.0 }, |
| 50 | + }, cancellationToken: TestContext.Current.CancellationToken); |
| 51 | + |
| 52 | + // Assert |
| 53 | + result.Status.ShouldBe(RunStatus.Succeeded, result.Error?.Message); |
| 54 | + result.Kind.ShouldBe(RunKind.Pipeline); |
| 55 | + result.GetOutput<double>("soma").ShouldBe(89); |
| 56 | + result.GetOutput<double>("media").ShouldBe(22.25); |
| 57 | + result.GetOutput<double>("desvio_padrao").ShouldBe(25.3118, 1e-4); |
| 58 | + var tabela = result.GetOutput<List<Linha>>("tabela"); |
| 59 | + tabela.ShouldBe([new(1, 1, 2), new(2, 2, 5), new(3, 4, 17), new(4, 8, 65)]); |
| 60 | + result.Steps.Select(s => (s.StepKey, s.Status)).ShouldBe( |
| 61 | + [ |
| 62 | + ("termos", StepStatus.Succeeded), |
| 63 | + ("polinomio", StepStatus.Succeeded), |
| 64 | + ("estatisticas", StepStatus.Succeeded), |
| 65 | + ("tabela", StepStatus.Succeeded), |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task Spec003_AC02_StepCanReadAnyEarlierStep() |
| 71 | + { |
| 72 | + await using var host = RunnerHost.Create(fixture, b => b.AddFileCatalog(CalculoProgressao)); |
| 73 | + |
| 74 | + await using var result = await host.Runner.RunPipelineAsync("calculo-progressao", new |
| 75 | + { |
| 76 | + a1 = 3, |
| 77 | + razao = 2, |
| 78 | + n = 3, |
| 79 | + coeficientes = new[] { 0.0, 1.0 }, |
| 80 | + }, cancellationToken: TestContext.Current.CancellationToken); |
| 81 | + |
| 82 | + // 'tabela' (step 4) reads 'termos' from step 1, skipping steps 2 and 3; tipo uses its default (aritmetica). |
| 83 | + result.GetStepOutput<double[]>("termos", "termos").ShouldBe([3, 5, 7]); |
| 84 | + result.GetStepOutput<List<Linha>>("tabela", "tabela").Select(l => l.X).ShouldBe([3, 5, 7]); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async Task Spec003_AC03_AC04_AC09_ConstantsImplicitBindingsAndFilesBetweenSteps() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + await using var host = RunnerHost.Create(fixture, b => b |
| 92 | + .AddStage("gerar", s => s.Script(Generator) |
| 93 | + .Input("texto", FieldType.String).Input("nome", FieldType.String) |
| 94 | + .Output("arquivo", FieldType.File).Output("tamanho", FieldType.Integer)) |
| 95 | + .AddStage("ler", s => s.Script(Reader) |
| 96 | + .Input("arquivo", FieldType.File).Input("prefixo", FieldType.String).Input("porta", FieldType.Integer) |
| 97 | + .Output("resumo", FieldType.String).Output("porta", FieldType.Integer)) |
| 98 | + .AddPipeline("arquivo-entre-passos", p => p |
| 99 | + .Input("texto", FieldType.String) |
| 100 | + .Input("prefixo", FieldType.String) |
| 101 | + .Step("gerar", "gerar@1", s => s.Bind("nome", From.Constant("dados.txt"))) |
| 102 | + .Step("ler", "ler@1", s => s |
| 103 | + .Bind("prefixo", From.Pipeline("prefixo")) |
| 104 | + .Bind("porta", From.Constant(465))) |
| 105 | + .Output("arquivo", From.Step("gerar", "arquivo")) |
| 106 | + .Output("resumo", From.Step("ler", "resumo")) |
| 107 | + .Output("porta", From.Step("ler", "porta")))); |
| 108 | + |
| 109 | + // Act |
| 110 | + await using var result = await host.Runner.RunPipelineAsync("arquivo-entre-passos", new { texto = "olá", prefixo = "conteúdo" }, |
| 111 | + cancellationToken: TestContext.Current.CancellationToken); |
| 112 | + |
| 113 | + // Assert |
| 114 | + result.Status.ShouldBe(RunStatus.Succeeded, result.Error?.Message); |
| 115 | + result.GetOutput<int>("porta").ShouldBe(465); |
| 116 | + result.GetOutput<string>("resumo").ShouldBe("conteúdo: olá"); |
| 117 | + var file = result.GetFile("arquivo"); |
| 118 | + file.FileName.ShouldBe("dados.txt"); |
| 119 | + File.ReadAllText(file.LocalPath!).ShouldBe("olá"); |
| 120 | + Should.Throw<KeyNotFoundException>(() => result.GetOutput<int>("tamanho")); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task Spec003_AC07_FailedStep_SkipsTheRest() |
| 125 | + { |
| 126 | + await using var host = RunnerHost.Create(fixture, b => WithChain(b, failing: """ |
| 127 | + from psrunner import StageError, stage |
| 128 | +
|
| 129 | + @stage |
| 130 | + def run(inputs, ctx): |
| 131 | + raise StageError("divergiu", code="DIV") |
| 132 | + """)); |
| 133 | + |
| 134 | + await using var result = await host.Runner.RunPipelineAsync("cadeia", new { valor = 1 }, cancellationToken: TestContext.Current.CancellationToken); |
| 135 | + |
| 136 | + result.Status.ShouldBe(RunStatus.Failed); |
| 137 | + result.Steps.Select(s => s.Status).ShouldBe([StepStatus.Succeeded, StepStatus.Failed, StepStatus.Skipped, StepStatus.Skipped]); |
| 138 | + result.Error!.StepKey.ShouldBe("polinomio"); |
| 139 | + result.Error.Code.ShouldBe("DIV"); |
| 140 | + result.Outputs.ShouldBeEmpty(); |
| 141 | + result.GetStepOutput<int>("termos", "valor").ShouldBe(2); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public async Task Spec003_AC08_RetryInsideAStep_CompletesThePipeline() |
| 146 | + { |
| 147 | + var counter = Path.Combine(Path.GetTempPath(), "psr-tests", $"counter-{Guid.NewGuid():N}.txt"); |
| 148 | + await using var host = RunnerHost.Create(fixture, b => WithChain(b, failing: $$""" |
| 149 | + from pathlib import Path |
| 150 | + from psrunner import StageError, stage |
| 151 | +
|
| 152 | + @stage |
| 153 | + def run(inputs, ctx): |
| 154 | + counter = Path(r"{{counter}}") |
| 155 | + count = int(counter.read_text()) + 1 if counter.exists() else 1 |
| 156 | + counter.write_text(str(count)) |
| 157 | + if count == 1: |
| 158 | + raise StageError("falha transitória") |
| 159 | + return {"valor": inputs["valor"] + 1} |
| 160 | + """, retries: 1)); |
| 161 | + |
| 162 | + await using var result = await host.Runner.RunPipelineAsync("cadeia", new { valor = 1 }, cancellationToken: TestContext.Current.CancellationToken); |
| 163 | + |
| 164 | + result.Status.ShouldBe(RunStatus.Succeeded, result.Error?.Message); |
| 165 | + result.Steps[1].Attempts.Count.ShouldBe(2); |
| 166 | + result.GetOutput<int>("valor").ShouldBe(5); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public async Task Spec003_AC11_Cancellation_StopsTheStepAndSkipsTheRest() |
| 171 | + { |
| 172 | + await using var host = RunnerHost.Create(fixture, b => WithChain(b, failing: """ |
| 173 | + import time |
| 174 | + from psrunner import stage |
| 175 | +
|
| 176 | + @stage |
| 177 | + def run(inputs, ctx): |
| 178 | + time.sleep(60) |
| 179 | + return {"valor": 0} |
| 180 | + """)); |
| 181 | + using var cancellation = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); |
| 182 | + cancellation.CancelAfter(TimeSpan.FromSeconds(3)); |
| 183 | + |
| 184 | + await using var result = await host.Runner.RunPipelineAsync("cadeia", new { valor = 1 }, cancellationToken: cancellation.Token); |
| 185 | + |
| 186 | + result.Status.ShouldBe(RunStatus.Cancelled); |
| 187 | + result.Steps.Select(s => s.Status).ShouldBe([StepStatus.Succeeded, StepStatus.Cancelled, StepStatus.Skipped, StepStatus.Skipped]); |
| 188 | + } |
| 189 | + |
| 190 | + [Fact] |
| 191 | + public async Task Spec003_AC12_FolderPipeline_WithMissingStageVersion_FailsToLoad() |
| 192 | + { |
| 193 | + // Arrange |
| 194 | + using var catalog = new TempCatalog(); |
| 195 | + catalog.AddStage("eco", Scripts.EchoManifest, Scripts.Echo); |
| 196 | + var pipelines = Path.Combine(catalog.Root, "pipelines"); |
| 197 | + Directory.CreateDirectory(pipelines); |
| 198 | + await File.WriteAllTextAsync(Path.Combine(pipelines, "quebrada.pipeline.json"), """ |
| 199 | + { |
| 200 | + "key": "quebrada", "version": 1, "name": "Quebrada", |
| 201 | + "inputs": [ { "name": "texto", "type": "string" } ], |
| 202 | + "steps": [ { "key": "eco", "stage": "eco@2" } ] |
| 203 | + } |
| 204 | + """, TestContext.Current.CancellationToken); |
| 205 | + await using var host = RunnerHost.Create(fixture, b => b.AddFileCatalog(catalog.Root)); |
| 206 | + |
| 207 | + // Act |
| 208 | + var exception = await Should.ThrowAsync<PsrConfigurationException>(() => |
| 209 | + host.Runner.RunPipelineAsync("quebrada", new { texto = "x" }, cancellationToken: TestContext.Current.CancellationToken)); |
| 210 | + |
| 211 | + // Assert |
| 212 | + exception.Message.ShouldContain("PIPE_STAGE_NOT_PUBLISHED"); |
| 213 | + exception.Message.ShouldContain("eco@2"); |
| 214 | + } |
| 215 | + |
| 216 | + [Fact] |
| 217 | + public async Task Spec003_AC13_FluentAndFilePipelines_BehaveTheSame() |
| 218 | + { |
| 219 | + // Arrange: the same stages, with the pipeline from the JSON file or from C#. |
| 220 | + using var stagesOnly = new TempCatalog(); |
| 221 | + CopyDirectory(Path.Combine(CalculoProgressao, "stages"), Path.Combine(stagesOnly.Root, "stages")); |
| 222 | + await using var fromFile = RunnerHost.Create(fixture, b => b.AddFileCatalog(CalculoProgressao)); |
| 223 | + await using var fromCode = RunnerHost.Create(fixture, b => b |
| 224 | + .AddFileCatalog(stagesOnly.Root) |
| 225 | + .AddPipeline("calculo-progressao", p => p |
| 226 | + .Name("Cálculo de progressão") |
| 227 | + .Input("a1", FieldType.Number) |
| 228 | + .Input("razao", FieldType.Number) |
| 229 | + .Input("n", FieldType.Integer) |
| 230 | + .Input("tipo", FieldType.Enum("aritmetica", "geometrica"), required: false, @default: "aritmetica") |
| 231 | + .Input("coeficientes", FieldType.ListOf(FieldType.Number)) |
| 232 | + .Step("termos", "progressao-termos@1") |
| 233 | + .Step("polinomio", "polinomio-avaliar@1", s => s |
| 234 | + .Bind("coeficientes", From.Pipeline("coeficientes")) |
| 235 | + .Bind("x", From.Step("termos", "termos"))) |
| 236 | + .Step("estatisticas", "estatisticas@1", s => s.Bind("valores", From.Step("polinomio", "valores"))) |
| 237 | + .Step("tabela", "montar-tabela@1", s => s |
| 238 | + .Bind("x", From.Step("termos", "termos")) |
| 239 | + .Bind("y", From.Step("polinomio", "valores"))) |
| 240 | + .Output("soma", From.Step("estatisticas", "soma")) |
| 241 | + .Output("media", From.Step("estatisticas", "media")) |
| 242 | + .Output("desvio_padrao", From.Step("estatisticas", "desvio_padrao")) |
| 243 | + .Output("tabela", From.Step("tabela", "tabela")))); |
| 244 | + var inputs = new { a1 = 2, razao = 3, n = 5, tipo = "aritmetica", coeficientes = new[] { 1.0, 1.0 } }; |
| 245 | + |
| 246 | + // Act |
| 247 | + var fileReport = await Validate(fromFile); |
| 248 | + var codeReport = await Validate(fromCode); |
| 249 | + await using var fileResult = await fromFile.Runner.RunPipelineAsync("calculo-progressao", inputs, cancellationToken: TestContext.Current.CancellationToken); |
| 250 | + await using var codeResult = await fromCode.Runner.RunPipelineAsync("calculo-progressao", inputs, cancellationToken: TestContext.Current.CancellationToken); |
| 251 | + |
| 252 | + // Assert |
| 253 | + fileReport.Errors.ShouldBe(codeReport.Errors); |
| 254 | + fileReport.Warnings.ShouldBe(codeReport.Warnings); |
| 255 | + fileReport.IsValid.ShouldBeTrue(); |
| 256 | + fileResult.Status.ShouldBe(RunStatus.Succeeded, fileResult.Error?.Message); |
| 257 | + fileResult.Outputs.ToJsonString().ShouldBe(codeResult.Outputs.ToJsonString()); |
| 258 | + } |
| 259 | + |
| 260 | + [Fact] |
| 261 | + public async Task IntegerOutput_FeedsADecimalInput_AsAnExactDecimal() |
| 262 | + { |
| 263 | + await using var host = RunnerHost.Create(fixture, b => b |
| 264 | + .AddStage("contar", s => s.Script(""" |
| 265 | + from psrunner import stage |
| 266 | +
|
| 267 | + @stage |
| 268 | + def run(inputs, ctx): |
| 269 | + return {"quantidade": 5} |
| 270 | + """).Output("quantidade", FieldType.Integer)) |
| 271 | + .AddStage("preco", s => s.Script(""" |
| 272 | + from decimal import Decimal |
| 273 | + from psrunner import stage |
| 274 | +
|
| 275 | + @stage |
| 276 | + def run(inputs, ctx): |
| 277 | + assert isinstance(inputs["quantidade"], Decimal) |
| 278 | + return {"total": inputs["quantidade"] * Decimal("0.10")} |
| 279 | + """).Input("quantidade", FieldType.Decimal).Output("total", FieldType.Decimal)) |
| 280 | + .AddPipeline("venda", p => p.Step("contar", "contar@1").Step("preco", "preco@1").Output("total", From.Step("preco", "total")))); |
| 281 | + |
| 282 | + await using var result = await host.Runner.RunPipelineAsync("venda", null, cancellationToken: TestContext.Current.CancellationToken); |
| 283 | + |
| 284 | + result.Status.ShouldBe(RunStatus.Succeeded, result.Error?.Message); |
| 285 | + result.GetOutput<decimal>("total").ShouldBe(0.50m); |
| 286 | + } |
| 287 | + |
| 288 | + [Fact] |
| 289 | + public async Task UnknownPipeline_AndInvalidInputs_AreReportedBeforeRunning() |
| 290 | + { |
| 291 | + await using var host = RunnerHost.Create(fixture, b => b.AddFileCatalog(CalculoProgressao)); |
| 292 | + |
| 293 | + await Should.ThrowAsync<PipelineNotFoundException>(() => host.Runner.RunPipelineAsync("inexistente", null, cancellationToken: TestContext.Current.CancellationToken)); |
| 294 | + var invalid = await Should.ThrowAsync<InputValidationException>(() => |
| 295 | + host.Runner.RunPipelineAsync("calculo-progressao", new { a1 = 1 }, cancellationToken: TestContext.Current.CancellationToken)); |
| 296 | + invalid.Errors.Select(e => e.Field).ShouldBe(["razao", "n", "coeficientes"]); |
| 297 | + } |
| 298 | + |
| 299 | + private static IPythonScriptRunnerBuilder WithChain(IPythonScriptRunnerBuilder builder, string failing, int retries = 0) => builder |
| 300 | + .AddStage("incremento", s => s.Script(Ok).Input("valor", FieldType.Integer).Output("valor", FieldType.Integer)) |
| 301 | + .AddStage("instavel", s => s.Script(failing).Input("valor", FieldType.Integer).Output("valor", FieldType.Integer) |
| 302 | + .Retry(retries, TimeSpan.FromMilliseconds(50))) |
| 303 | + .AddPipeline("cadeia", p => p |
| 304 | + .Input("valor", FieldType.Integer) |
| 305 | + .Step("termos", "incremento@1") |
| 306 | + .Step("polinomio", "instavel@1") |
| 307 | + .Step("estatisticas", "incremento@1") |
| 308 | + .Step("tabela", "incremento@1") |
| 309 | + .Output("valor", From.Step("tabela", "valor"))); |
| 310 | + |
| 311 | + private static async Task<ValidationReport> Validate(RunnerHost host) |
| 312 | + { |
| 313 | + var catalog = host.Services.GetRequiredService<ICatalog>(); |
| 314 | + var pipeline = await catalog.FindPipelineAsync("calculo-progressao", null, TestContext.Current.CancellationToken); |
| 315 | + return await host.Services.GetRequiredService<IPipelineValidator>().ValidateAsync(pipeline!, TestContext.Current.CancellationToken); |
| 316 | + } |
| 317 | + |
| 318 | + private static void CopyDirectory(string source, string target) |
| 319 | + { |
| 320 | + foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories)) |
| 321 | + { |
| 322 | + var destination = Path.Combine(target, Path.GetRelativePath(source, file)); |
| 323 | + Directory.CreateDirectory(Path.GetDirectoryName(destination)!); |
| 324 | + File.Copy(file, destination); |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + public sealed record Linha(long I, double X, double Y); |
| 329 | +} |
0 commit comments