|
1 | 1 | import typing as t |
2 | 2 | import pytest |
3 | 3 | from sqlmesh_dbt import selectors |
| 4 | +from sqlmesh.core.context import Context |
| 5 | +from pathlib import Path |
4 | 6 |
|
5 | 7 |
|
6 | 8 | @pytest.mark.parametrize( |
@@ -77,3 +79,181 @@ def test_split_unions_and_intersections( |
77 | 79 | expression: str, expected: t.Tuple[t.List[str], t.List[str]] |
78 | 80 | ): |
79 | 81 | assert selectors._split_unions_and_intersections(expression) == expected |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "dbt_select,expected", |
| 86 | + [ |
| 87 | + (["aging"], set()), |
| 88 | + ( |
| 89 | + ["staging"], |
| 90 | + { |
| 91 | + '"jaffle_shop"."main"."stg_customers"', |
| 92 | + '"jaffle_shop"."main"."stg_orders"', |
| 93 | + '"jaffle_shop"."main"."stg_payments"', |
| 94 | + }, |
| 95 | + ), |
| 96 | + (["staging.stg_customers"], {'"jaffle_shop"."main"."stg_customers"'}), |
| 97 | + (["stg_customers.staging"], set()), |
| 98 | + ( |
| 99 | + ["+customers"], |
| 100 | + { |
| 101 | + '"jaffle_shop"."main"."customers"', |
| 102 | + '"jaffle_shop"."main"."stg_customers"', |
| 103 | + '"jaffle_shop"."main"."stg_orders"', |
| 104 | + '"jaffle_shop"."main"."stg_payments"', |
| 105 | + '"jaffle_shop"."main"."raw_customers"', |
| 106 | + '"jaffle_shop"."main"."raw_orders"', |
| 107 | + '"jaffle_shop"."main"."raw_payments"', |
| 108 | + }, |
| 109 | + ), |
| 110 | + (["customers+"], {'"jaffle_shop"."main"."customers"'}), |
| 111 | + ( |
| 112 | + ["customers+", "stg_orders"], |
| 113 | + {'"jaffle_shop"."main"."customers"', '"jaffle_shop"."main"."stg_orders"'}, |
| 114 | + ), |
| 115 | + (["tag:agg"], {'"jaffle_shop"."main"."agg_orders"'}), |
| 116 | + ( |
| 117 | + ["staging.stg_customers", "tag:agg"], |
| 118 | + { |
| 119 | + '"jaffle_shop"."main"."stg_customers"', |
| 120 | + '"jaffle_shop"."main"."agg_orders"', |
| 121 | + }, |
| 122 | + ), |
| 123 | + ( |
| 124 | + ["+tag:agg"], |
| 125 | + { |
| 126 | + '"jaffle_shop"."main"."agg_orders"', |
| 127 | + '"jaffle_shop"."main"."orders"', |
| 128 | + '"jaffle_shop"."main"."stg_orders"', |
| 129 | + '"jaffle_shop"."main"."stg_payments"', |
| 130 | + '"jaffle_shop"."main"."raw_orders"', |
| 131 | + '"jaffle_shop"."main"."raw_payments"', |
| 132 | + }, |
| 133 | + ), |
| 134 | + ( |
| 135 | + ["tag:agg+"], |
| 136 | + { |
| 137 | + '"jaffle_shop"."main"."agg_orders"', |
| 138 | + }, |
| 139 | + ), |
| 140 | + ], |
| 141 | +) |
| 142 | +def test_select_by_dbt_names( |
| 143 | + jaffle_shop_duckdb: Path, |
| 144 | + jaffle_shop_duckdb_context: Context, |
| 145 | + dbt_select: t.List[str], |
| 146 | + expected: t.Set[str], |
| 147 | +): |
| 148 | + (jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text(""" |
| 149 | + {{ config(tags=["agg"]) }} |
| 150 | + select order_date, count(*) as num_orders from {{ ref('orders') }} |
| 151 | + """) |
| 152 | + |
| 153 | + ctx = jaffle_shop_duckdb_context |
| 154 | + ctx.load() |
| 155 | + assert '"jaffle_shop"."main"."agg_orders"' in ctx.models |
| 156 | + |
| 157 | + selector = ctx._new_selector() |
| 158 | + assert selector._dbt_mode |
| 159 | + |
| 160 | + sqlmesh_selector = selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=[]) |
| 161 | + assert sqlmesh_selector |
| 162 | + |
| 163 | + assert selector.expand_model_selections([sqlmesh_selector]) == expected |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.parametrize( |
| 167 | + "dbt_exclude,expected", |
| 168 | + [ |
| 169 | + (["jaffle_shop"], set()), |
| 170 | + ( |
| 171 | + ["staging"], |
| 172 | + { |
| 173 | + '"jaffle_shop"."main"."agg_orders"', |
| 174 | + '"jaffle_shop"."main"."customers"', |
| 175 | + '"jaffle_shop"."main"."orders"', |
| 176 | + '"jaffle_shop"."main"."raw_customers"', |
| 177 | + '"jaffle_shop"."main"."raw_orders"', |
| 178 | + '"jaffle_shop"."main"."raw_payments"', |
| 179 | + }, |
| 180 | + ), |
| 181 | + (["+customers"], {'"jaffle_shop"."main"."orders"', '"jaffle_shop"."main"."agg_orders"'}), |
| 182 | + ( |
| 183 | + ["+tag:agg"], |
| 184 | + { |
| 185 | + '"jaffle_shop"."main"."customers"', |
| 186 | + '"jaffle_shop"."main"."stg_customers"', |
| 187 | + '"jaffle_shop"."main"."raw_customers"', |
| 188 | + }, |
| 189 | + ), |
| 190 | + ], |
| 191 | +) |
| 192 | +def test_exclude_by_dbt_names( |
| 193 | + jaffle_shop_duckdb: Path, |
| 194 | + jaffle_shop_duckdb_context: Context, |
| 195 | + dbt_exclude: t.List[str], |
| 196 | + expected: t.Set[str], |
| 197 | +): |
| 198 | + (jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text(""" |
| 199 | + {{ config(tags=["agg"]) }} |
| 200 | + select order_date, count(*) as num_orders from {{ ref('orders') }} |
| 201 | + """) |
| 202 | + |
| 203 | + ctx = jaffle_shop_duckdb_context |
| 204 | + ctx.load() |
| 205 | + assert '"jaffle_shop"."main"."agg_orders"' in ctx.models |
| 206 | + |
| 207 | + selector = ctx._new_selector() |
| 208 | + assert selector._dbt_mode |
| 209 | + |
| 210 | + sqlmesh_selector = selectors.to_sqlmesh(dbt_select=[], dbt_exclude=dbt_exclude) |
| 211 | + assert sqlmesh_selector |
| 212 | + |
| 213 | + assert selector.expand_model_selections([sqlmesh_selector]) == expected |
| 214 | + |
| 215 | + |
| 216 | +@pytest.mark.parametrize( |
| 217 | + "dbt_select,dbt_exclude,expected", |
| 218 | + [ |
| 219 | + (["jaffle_shop"], ["jaffle_shop"], set()), |
| 220 | + ( |
| 221 | + ["staging"], |
| 222 | + ["stg_customers"], |
| 223 | + { |
| 224 | + '"jaffle_shop"."main"."stg_orders"', |
| 225 | + '"jaffle_shop"."main"."stg_payments"', |
| 226 | + }, |
| 227 | + ), |
| 228 | + ( |
| 229 | + ["staging.stg_customers", "tag:agg"], |
| 230 | + ["tag:agg"], |
| 231 | + { |
| 232 | + '"jaffle_shop"."main"."stg_customers"', |
| 233 | + }, |
| 234 | + ), |
| 235 | + ], |
| 236 | +) |
| 237 | +def test_selection_and_exclusion_by_dbt_names( |
| 238 | + jaffle_shop_duckdb: Path, |
| 239 | + jaffle_shop_duckdb_context: Context, |
| 240 | + dbt_select: t.List[str], |
| 241 | + dbt_exclude: t.List[str], |
| 242 | + expected: t.Set[str], |
| 243 | +): |
| 244 | + (jaffle_shop_duckdb / "models" / "agg_orders.sql").write_text(""" |
| 245 | + {{ config(tags=["agg"]) }} |
| 246 | + select order_date, count(*) as num_orders from {{ ref('orders') }} |
| 247 | + """) |
| 248 | + |
| 249 | + ctx = jaffle_shop_duckdb_context |
| 250 | + ctx.load() |
| 251 | + assert '"jaffle_shop"."main"."agg_orders"' in ctx.models |
| 252 | + |
| 253 | + selector = ctx._new_selector() |
| 254 | + assert selector._dbt_mode |
| 255 | + |
| 256 | + sqlmesh_selector = selectors.to_sqlmesh(dbt_select=dbt_select, dbt_exclude=dbt_exclude) |
| 257 | + assert sqlmesh_selector |
| 258 | + |
| 259 | + assert selector.expand_model_selections([sqlmesh_selector]) == expected |
0 commit comments