|
| 1 | +// Fr015CodegenTests — FR-015 @parameterRef callable-wrapper C# codegen emission. |
| 2 | +// |
| 3 | +// A source.rdb @kind:"storedProc" | "tableFunction" entity gets a generated calling |
| 4 | +// method (one <Entity>.callable.cs per callable entity). Per the FR-015 spec, the C# |
| 5 | +// emission uses EF Core FromSqlInterpolated: |
| 6 | +// |
| 7 | +// public static Task<IReadOnlyList<PhaseSummary>> Call(AppDbContext db, PhaseSummaryArgs args) |
| 8 | +// => db.Set<PhaseSummary>() |
| 9 | +// .FromSqlInterpolated($"SELECT * FROM analytics.fn_phase_summary({args.CaseId}, {args.AsOfDate})") |
| 10 | +// .ToListAsync()...; |
| 11 | +// |
| 12 | +// Args bind in the @parameterRef value-object's field DECLARATION order. A callable |
| 13 | +// with no @parameterRef emits a zero-arg overload calling fn_x(). |
| 14 | +// |
| 15 | +// Mirrors the TS reference (codegen-ts templates/callable-file.ts), which emits one |
| 16 | +// call<Entity>(db, args) per callable with the same SQL arg-order contract. |
| 17 | + |
| 18 | +using MetaObjects.Codegen; |
| 19 | +using MetaObjects.Codegen.Generators; |
| 20 | +using MetaObjects.Loader; |
| 21 | +using MetaObjects.Meta; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MetaObjects.Codegen.Tests; |
| 25 | + |
| 26 | +public class Fr015CodegenTests |
| 27 | +{ |
| 28 | + // One value-object (the proc args, two fields in declaration order), a storedProc |
| 29 | + // entity referencing it via @parameterRef, a tableFunction entity, and a zero-arg |
| 30 | + // storedProc entity (no @parameterRef). |
| 31 | + private const string Model = """ |
| 32 | + { "metadata.root": { "package": "acme::analytics", "children": [ |
| 33 | + { "object.value": { "name": "PhaseSummaryArgs", "children": [ |
| 34 | + { "field.int": { "name": "caseId", "@required": true } }, |
| 35 | + { "field.timestamp": { "name": "asOfDate" } } |
| 36 | + ]}}, |
| 37 | + { "object.entity": { "name": "PhaseSummary", "children": [ |
| 38 | + { "source.rdb": { "@kind": "storedProc", "@proc": "fn_phase_summary", "@schema": "analytics", "@parameterRef": "PhaseSummaryArgs" } }, |
| 39 | + { "field.long": { "name": "phaseId" } }, |
| 40 | + { "field.string": { "name": "phaseName" } } |
| 41 | + ]}}, |
| 42 | + { "object.entity": { "name": "ActivePhases", "children": [ |
| 43 | + { "source.rdb": { "@kind": "tableFunction", "@function": "fn_active_phases", "@parameterRef": "PhaseSummaryArgs" } }, |
| 44 | + { "field.long": { "name": "phaseId" } } |
| 45 | + ]}}, |
| 46 | + { "object.entity": { "name": "AllPhases", "children": [ |
| 47 | + { "source.rdb": { "@kind": "storedProc", "@proc": "fn_all_phases" } }, |
| 48 | + { "field.long": { "name": "phaseId" } } |
| 49 | + ]}}, |
| 50 | + { "object.entity": { "name": "Plain", "children": [ |
| 51 | + { "source.rdb": { "@table": "plains" } }, |
| 52 | + { "field.long": { "name": "id" } }, |
| 53 | + { "identity.primary": { "@fields": "id" } } |
| 54 | + ]}} |
| 55 | + ]}} |
| 56 | + """; |
| 57 | + |
| 58 | + private static GenContext Ctx(MetaRoot root) => new() |
| 59 | + { |
| 60 | + Entities = root.Objects(), |
| 61 | + Root = root, |
| 62 | + Config = new GenConfig { OutDir = "/tmp", Namespace = "Acme.Generated", ColumnNamingStrategy = ColumnNamingStrategy.Literal }, |
| 63 | + }; |
| 64 | + |
| 65 | + private static MetaRoot Load() |
| 66 | + { |
| 67 | + var r = new MetaDataLoader().Load([new InMemoryStringSource(Model, id: "fr015.json")]); |
| 68 | + Assert.Empty(r.Errors); |
| 69 | + return r.Root; |
| 70 | + } |
| 71 | + |
| 72 | + private static IReadOnlyList<EmittedFile> Files() |
| 73 | + => new CallableGenerator().Generate(Ctx(Load())).ToList(); |
| 74 | + |
| 75 | + private static string Content(IEnumerable<EmittedFile> files, string path) => |
| 76 | + files.Single(f => f.Path == path).Content; |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Source_ParameterRef_and_IsCallable_reflect_attrs() |
| 80 | + { |
| 81 | + var root = Load(); |
| 82 | + var ps = root.FindObject("PhaseSummary")!.Sources()[0]; |
| 83 | + Assert.True(ps.IsCallable()); |
| 84 | + Assert.Equal("PhaseSummaryArgs", ps.ParameterRef); |
| 85 | + |
| 86 | + var plain = root.FindObject("Plain")!.Sources()[0]; |
| 87 | + Assert.False(plain.IsCallable()); |
| 88 | + Assert.Null(plain.ParameterRef); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void Only_callable_entities_emit_a_callable_file() |
| 93 | + { |
| 94 | + var files = Files(); |
| 95 | + var paths = files.Select(f => f.Path).OrderBy(p => p, StringComparer.Ordinal).ToList(); |
| 96 | + Assert.Equal( |
| 97 | + ["ActivePhases.callable.g.cs", "AllPhases.callable.g.cs", "PhaseSummary.callable.g.cs"], |
| 98 | + paths); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void StoredProc_with_parameterRef_emits_FromSqlInterpolated_in_declaration_order() |
| 103 | + { |
| 104 | + var c = Content(Files(), "PhaseSummary.callable.g.cs"); |
| 105 | + |
| 106 | + // Method signature: db + the args value-object. |
| 107 | + Assert.Contains("AppDbContext db, PhaseSummaryArgs args", c); |
| 108 | + // Returns the projection rows. |
| 109 | + Assert.Contains("Task<IReadOnlyList<PhaseSummary>>", c); |
| 110 | + // FromSqlInterpolated with schema-qualified proc name + args in declaration |
| 111 | + // order (caseId, asOfDate → CaseId, AsOfDate). |
| 112 | + Assert.Contains( |
| 113 | + "FromSqlInterpolated($\"SELECT * FROM analytics.fn_phase_summary({args.CaseId}, {args.AsOfDate})\")", |
| 114 | + c); |
| 115 | + Assert.Contains("db.Set<PhaseSummary>()", c); |
| 116 | + Assert.Contains("ToListAsync", c); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void TableFunction_with_parameterRef_emits_unqualified_proc_name() |
| 121 | + { |
| 122 | + var c = Content(Files(), "ActivePhases.callable.g.cs"); |
| 123 | + // No @schema → bare function name. |
| 124 | + Assert.Contains( |
| 125 | + "FromSqlInterpolated($\"SELECT * FROM fn_active_phases({args.CaseId}, {args.AsOfDate})\")", |
| 126 | + c); |
| 127 | + Assert.Contains("AppDbContext db, PhaseSummaryArgs args", c); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void ZeroArg_callable_emits_no_args_method() |
| 132 | + { |
| 133 | + var c = Content(Files(), "AllPhases.callable.g.cs"); |
| 134 | + // No args parameter. |
| 135 | + Assert.Contains("AppDbContext db)", c); |
| 136 | + Assert.DoesNotContain("args", c); |
| 137 | + // Empty parameter list in the SQL. |
| 138 | + Assert.Contains("FromSqlInterpolated($\"SELECT * FROM fn_all_phases()\")", c); |
| 139 | + Assert.Contains("Task<IReadOnlyList<AllPhases>>", c); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void Registry_exposes_callable_for_csharp() |
| 144 | + { |
| 145 | + Assert.True(GeneratorRegistry.Entries.ContainsKey("callable")); |
| 146 | + var built = GeneratorRegistry.Resolve(["callable"]); |
| 147 | + Assert.Single(built); |
| 148 | + Assert.IsType<CallableGenerator>(built[0]); |
| 149 | + } |
| 150 | +} |
0 commit comments