|
10 | 10 | import jakarta.inject.Inject; |
11 | 11 | import org.junit.jupiter.api.Test; |
12 | 12 |
|
| 13 | +import io.kestra.core.runners.AssetEmit; |
| 14 | + |
13 | 15 | import java.nio.file.Files; |
14 | | -import java.util.HashMap; |
15 | 16 | import java.util.List; |
16 | 17 | import java.util.Map; |
17 | 18 |
|
18 | | -import static java.util.stream.Collectors.toMap; |
19 | 19 | import static org.hamcrest.MatcherAssert.assertThat; |
20 | 20 | import static org.hamcrest.Matchers.*; |
21 | 21 |
|
@@ -71,22 +71,23 @@ void parseManifestWithAssets_shouldEmitModelAssets() throws Exception { |
71 | 71 | assertThat(manifestResult.manifest(), is(notNullValue())); |
72 | 72 | assertThat(runContext.assets().emitted(), hasSize(2)); |
73 | 73 |
|
74 | | - var outputAssets = runContext.assets().emitted().stream() |
75 | | - .flatMap(assetEmit -> assetEmit.outputs().stream()) |
76 | | - .toList(); |
77 | | - |
78 | | - assertThat(outputAssets, hasSize(2)); |
79 | | - var byId = new HashMap<String, io.kestra.core.models.assets.Asset>(); |
80 | | - outputAssets.forEach(asset -> byId.put(asset.getId(), asset)); |
| 74 | + // stg_orders has no inputs and 1 output (fct_orders, its child) |
| 75 | + // fct_orders has 1 input (stg_orders) and no outputs (leaf node) |
| 76 | + var stgOrdersEmit = findEmitWithOutput(runContext.assets().emitted(), "analytics.marts.fct_orders"); |
| 77 | + assertThat("stg_orders emission should exist", stgOrdersEmit, is(notNullValue())); |
| 78 | + assertThat(stgOrdersEmit.inputs(), hasSize(0)); |
| 79 | + assertThat(stgOrdersEmit.outputs(), hasSize(1)); |
81 | 80 |
|
82 | | - assertThat(byId.containsKey("analytics.staging.stg_orders"), is(true)); |
83 | | - assertThat(byId.containsKey("analytics.marts.fct_orders"), is(true)); |
| 81 | + var fctOrdersOutput = stgOrdersEmit.outputs().getFirst(); |
| 82 | + assertThat(fctOrdersOutput.getMetadata().get("system"), is("postgres")); |
| 83 | + assertThat(fctOrdersOutput.getMetadata().get("database"), is("analytics")); |
| 84 | + assertThat(fctOrdersOutput.getMetadata().get("schema"), is("marts")); |
| 85 | + assertThat(fctOrdersOutput.getMetadata().get("name"), is("fct_orders")); |
84 | 86 |
|
85 | | - var stgOrders = byId.get("analytics.staging.stg_orders"); |
86 | | - assertThat(stgOrders.getMetadata().get("system"), is("postgres")); |
87 | | - assertThat(stgOrders.getMetadata().get("database"), is("analytics")); |
88 | | - assertThat(stgOrders.getMetadata().get("schema"), is("staging")); |
89 | | - assertThat(stgOrders.getMetadata().get("name"), is("stg_orders")); |
| 87 | + var fctOrdersEmit = findEmitWithInput(runContext.assets().emitted(), "analytics.staging.stg_orders"); |
| 88 | + assertThat("fct_orders emission should exist", fctOrdersEmit, is(notNullValue())); |
| 89 | + assertThat(fctOrdersEmit.inputs(), hasSize(1)); |
| 90 | + assertThat(fctOrdersEmit.outputs(), hasSize(0)); |
90 | 91 | } |
91 | 92 |
|
92 | 93 | @Test |
@@ -133,18 +134,20 @@ void parseManifestWithAssets_shouldEmitLineageInputs() throws Exception { |
133 | 134 |
|
134 | 135 | ResultParser.parseManifestWithAssets(runContext, manifestFile.toFile()); |
135 | 136 |
|
136 | | - var emittedByOutputId = runContext.assets().emitted().stream() |
137 | | - .collect(toMap( |
138 | | - assetEmit -> assetEmit.outputs().getFirst().getId(), |
139 | | - assetEmit -> assetEmit |
140 | | - )); |
| 137 | + assertThat(runContext.assets().emitted(), hasSize(2)); |
141 | 138 |
|
142 | | - assertThat(emittedByOutputId, hasKey("analytics.marts.my_first_dbt_model")); |
143 | | - assertThat(emittedByOutputId, hasKey("analytics.marts.my_second_dbt_model")); |
| 139 | + // my_first_dbt_model: no inputs, 1 output (my_second_dbt_model) |
| 140 | + var firstModelEmit = findEmitWithOutput(runContext.assets().emitted(), "analytics.marts.my_second_dbt_model"); |
| 141 | + assertThat(firstModelEmit, is(notNullValue())); |
| 142 | + assertThat(firstModelEmit.inputs(), hasSize(0)); |
| 143 | + assertThat(firstModelEmit.outputs(), hasSize(1)); |
144 | 144 |
|
145 | | - var secondModelEmit = emittedByOutputId.get("analytics.marts.my_second_dbt_model"); |
| 145 | + // my_second_dbt_model: 1 input (my_first_dbt_model), no outputs (leaf) |
| 146 | + var secondModelEmit = findEmitWithInput(runContext.assets().emitted(), "analytics.marts.my_first_dbt_model"); |
| 147 | + assertThat(secondModelEmit, is(notNullValue())); |
146 | 148 | assertThat(secondModelEmit.inputs(), hasSize(1)); |
147 | 149 | assertThat(secondModelEmit.inputs().getFirst().id(), is("analytics.marts.my_first_dbt_model")); |
| 150 | + assertThat(secondModelEmit.outputs(), hasSize(0)); |
148 | 151 | } |
149 | 152 |
|
150 | 153 | @Test |
@@ -200,26 +203,55 @@ void parseManifestWithAssets_shouldUseParentMapForLineage() throws Exception { |
200 | 203 |
|
201 | 204 | ResultParser.parseManifestWithAssets(runContext, manifestFile.toFile()); |
202 | 205 |
|
203 | | - var emittedByOutputId = runContext.assets().emitted().stream() |
204 | | - .collect(toMap( |
205 | | - assetEmit -> assetEmit.outputs().getFirst().getId(), |
206 | | - assetEmit -> assetEmit |
207 | | - )); |
| 206 | + assertThat(runContext.assets().emitted(), hasSize(3)); |
208 | 207 |
|
209 | | - // fct_orders should only depend on int_orders (from parent_map), |
210 | | - // NOT on both stg_orders and int_orders (from depends_on.nodes) |
211 | | - var fctOrdersEmit = emittedByOutputId.get("dev.marts.fct_orders"); |
212 | | - assertThat(fctOrdersEmit.inputs(), hasSize(1)); |
213 | | - assertThat(fctOrdersEmit.inputs().getFirst().id(), is("dev.intermediate.int_orders")); |
| 208 | + // DAG: stg_orders → int_orders → fct_orders (parent_map, no transitive edges) |
| 209 | + // Inputs = upstream deps, Outputs = downstream children |
| 210 | + |
| 211 | + // stg_orders: no model inputs (source filtered out), 1 output (int_orders) |
| 212 | + var stgOrdersEmit = findEmitWithOutput(runContext.assets().emitted(), "dev.intermediate.int_orders"); |
| 213 | + assertThat(stgOrdersEmit, is(notNullValue())); |
| 214 | + assertThat(stgOrdersEmit.inputs(), hasSize(0)); |
| 215 | + assertThat(stgOrdersEmit.outputs(), hasSize(1)); |
214 | 216 |
|
215 | | - // int_orders should depend on stg_orders |
216 | | - var intOrdersEmit = emittedByOutputId.get("dev.intermediate.int_orders"); |
| 217 | + // int_orders: 1 input (stg_orders), 1 output (fct_orders) |
| 218 | + var intOrdersEmit = findEmitWithInputAndOutput(runContext.assets().emitted(), |
| 219 | + "dev.staging.stg_orders", "dev.marts.fct_orders"); |
| 220 | + assertThat(intOrdersEmit, is(notNullValue())); |
217 | 221 | assertThat(intOrdersEmit.inputs(), hasSize(1)); |
218 | 222 | assertThat(intOrdersEmit.inputs().getFirst().id(), is("dev.staging.stg_orders")); |
| 223 | + assertThat(intOrdersEmit.outputs(), hasSize(1)); |
| 224 | + assertThat(intOrdersEmit.outputs().getFirst().getId(), is("dev.marts.fct_orders")); |
219 | 225 |
|
220 | | - // stg_orders has no model dependencies (source is filtered out) |
221 | | - var stgOrdersEmit = emittedByOutputId.get("dev.staging.stg_orders"); |
222 | | - assertThat(stgOrdersEmit.inputs(), hasSize(0)); |
| 226 | + // fct_orders: 1 input (int_orders only, from parent_map), no outputs (leaf) |
| 227 | + var fctOrdersEmit = findEmitWithInput(runContext.assets().emitted(), "dev.intermediate.int_orders"); |
| 228 | + assertThat(fctOrdersEmit, is(notNullValue())); |
| 229 | + assertThat(fctOrdersEmit.inputs(), hasSize(1)); |
| 230 | + assertThat(fctOrdersEmit.inputs().getFirst().id(), is("dev.intermediate.int_orders")); |
| 231 | + assertThat(fctOrdersEmit.outputs(), hasSize(0)); |
| 232 | + } |
| 233 | + |
| 234 | + private static AssetEmit findEmitWithOutput(List<AssetEmit> emitted, String outputId) { |
| 235 | + return emitted.stream() |
| 236 | + .filter(e -> e.outputs().stream().anyMatch(o -> o.getId().equals(outputId))) |
| 237 | + .findFirst() |
| 238 | + .orElse(null); |
| 239 | + } |
| 240 | + |
| 241 | + private static AssetEmit findEmitWithInput(List<AssetEmit> emitted, String inputId) { |
| 242 | + return emitted.stream() |
| 243 | + .filter(e -> e.inputs().stream().anyMatch(i -> i.id().equals(inputId))) |
| 244 | + .filter(e -> e.outputs().isEmpty()) |
| 245 | + .findFirst() |
| 246 | + .orElse(null); |
| 247 | + } |
| 248 | + |
| 249 | + private static AssetEmit findEmitWithInputAndOutput(List<AssetEmit> emitted, String inputId, String outputId) { |
| 250 | + return emitted.stream() |
| 251 | + .filter(e -> e.inputs().stream().anyMatch(i -> i.id().equals(inputId))) |
| 252 | + .filter(e -> e.outputs().stream().anyMatch(o -> o.getId().equals(outputId))) |
| 253 | + .findFirst() |
| 254 | + .orElse(null); |
223 | 255 | } |
224 | 256 |
|
225 | 257 | private RunContext mockRunContext() { |
|
0 commit comments