|
| 1 | +//! Real models whose unmocked model inputs pull unmocked sources into a scenario. |
| 2 | +
|
| 3 | +use std::collections::{BTreeSet, HashMap}; |
| 4 | + |
| 5 | +use crate::planner_scenario::models::ScenarioGraphInput; |
| 6 | + |
| 7 | +/// Whether `name`, run as a real model, reads a source the scenario does not mock, with the memo. |
| 8 | +fn reaches_unmocked_source( |
| 9 | + name: &str, |
| 10 | + input: &ScenarioGraphInput, |
| 11 | + mut memo: HashMap<String, bool>, |
| 12 | +) -> (bool, HashMap<String, bool>) { |
| 13 | + if let Some(known) = memo.get(name) { |
| 14 | + return (*known, memo); |
| 15 | + } |
| 16 | + memo.insert(name.to_owned(), false); |
| 17 | + let mut reaches: bool = false; |
| 18 | + for (kind, dep) in input.models.get(name).into_iter().flatten() { |
| 19 | + reaches = match kind.as_str() { |
| 20 | + "source" => !input.sources.contains(dep), |
| 21 | + "model" if !input.refs.contains(dep) => { |
| 22 | + let (found, updated) = reaches_unmocked_source(dep, input, memo); |
| 23 | + memo = updated; |
| 24 | + found |
| 25 | + } |
| 26 | + _ => false, |
| 27 | + }; |
| 28 | + if reaches { |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
| 32 | + memo.insert(name.to_owned(), reaches); |
| 33 | + (reaches, memo) |
| 34 | +} |
| 35 | + |
| 36 | +/// Real models that mock some model inputs but leave others, which read unmocked sources, real. |
| 37 | +pub(crate) fn unmocked_boundaries( |
| 38 | + input: &ScenarioGraphInput, |
| 39 | + models: &BTreeSet<String>, |
| 40 | +) -> Vec<String> { |
| 41 | + let mut memo: HashMap<String, bool> = HashMap::new(); |
| 42 | + let mut messages: Vec<String> = Vec::new(); |
| 43 | + for name in models { |
| 44 | + let Some(deps) = input.models.get(name) else { |
| 45 | + continue; |
| 46 | + }; |
| 47 | + let model_deps = || { |
| 48 | + deps.iter() |
| 49 | + .filter(|(kind, _)| matches!(kind.as_str(), "model")) |
| 50 | + }; |
| 51 | + if !model_deps().any(|(_, dep)| input.refs.contains(dep)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + let mut unmocked: Vec<&str> = Vec::new(); |
| 55 | + for (_, dep) in model_deps() { |
| 56 | + if input.refs.contains(dep) |
| 57 | + || !input.models.contains_key(dep) |
| 58 | + || unmocked.contains(&dep.as_str()) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + let (reaches, updated) = reaches_unmocked_source(dep, input, memo); |
| 63 | + memo = updated; |
| 64 | + if reaches { |
| 65 | + unmocked.push(dep); |
| 66 | + } |
| 67 | + } |
| 68 | + if unmocked.is_empty() { |
| 69 | + continue; |
| 70 | + } |
| 71 | + let quoted: Vec<String> = unmocked.iter().map(|dep| format!("'{dep}'")).collect(); |
| 72 | + let ctes: Vec<String> = unmocked |
| 73 | + .iter() |
| 74 | + .map(|dep| format!("__ref__{dep} AS (...)")) |
| 75 | + .collect(); |
| 76 | + messages.push(format!( |
| 77 | + "Scenario '{}': model '{name}' reads {}, which {} not mocked, while its other model inputs are. Add {} named {}.", |
| 78 | + input.name, |
| 79 | + quoted.join(" and "), |
| 80 | + if unmocked.len() == 1 { "is" } else { "are" }, |
| 81 | + if unmocked.len() == 1 { "a CTE" } else { "CTEs" }, |
| 82 | + ctes.join(" and ") |
| 83 | + )); |
| 84 | + } |
| 85 | + messages |
| 86 | +} |
0 commit comments