@@ -1687,6 +1687,66 @@ def test_given_exception_for_other_model_when_compiling_selection_then_exception
16871687 assert payload ["diagnostics" ] == []
16881688
16891689
1690+ @pytest .mark .parametrize (
1691+ "test_case" ,
1692+ [RulesIntegrationTestCase ("typed unselected model stays outside rule scope" , 0 , "" )],
1693+ ids = lambda case : case .description ,
1694+ )
1695+ def test_given_typed_unselected_model_when_planning_selection_then_rules_use_selected_scope (
1696+ test_case : RulesIntegrationTestCase ,
1697+ tmp_path : Path ,
1698+ capsys : pytest .CaptureFixture [str ],
1699+ ) -> None :
1700+ (tmp_path / "sqlbuild_project.toml" ).write_text (
1701+ 'name = "orders"\n adapter = "duckdb"\n \n [rules]\n select = ["SQBRCONTRACT105"]\n ' ,
1702+ encoding = "utf-8" ,
1703+ )
1704+ sources : Path = tmp_path / "sources"
1705+ sources .mkdir ()
1706+ (sources / "customers.yml" ).write_text (
1707+ """sources:
1708+ - name: raw_customers
1709+ schema: raw
1710+ table: customers
1711+ columns:
1712+ - name: customer_id
1713+ type: VARCHAR
1714+ """ ,
1715+ encoding = "utf-8" ,
1716+ )
1717+ models : Path = tmp_path / "models"
1718+ models .mkdir ()
1719+ (models / "orders.sql" ).write_text (
1720+ "MODEL (contract enforced, columns (order_id (type INTEGER)));\n "
1721+ "SELECT CAST(1 AS INTEGER) AS order_id\n " ,
1722+ encoding = "utf-8" ,
1723+ )
1724+ (models / "customers.sql" ).write_text (
1725+ """MODEL (
1726+ contract enforced,
1727+ columns (customer_id (type VARCHAR)),
1728+ );
1729+
1730+ WITH source_rows AS (
1731+ SELECT customer_id FROM __source("raw_customers")
1732+ ), final AS (
1733+ SELECT customer_id FROM source_rows
1734+ )
1735+ SELECT customer_id FROM final
1736+ """ ,
1737+ encoding = "utf-8" ,
1738+ )
1739+
1740+ exit_code : int = main (["--project-dir" , str (tmp_path ), "plan" , "--json" , "--select" , "orders" ])
1741+ payload : dict [str , object ] = json .loads (capsys .readouterr ().out )
1742+
1743+ assert exit_code == test_case .expected_exit_code
1744+ assert payload ["selected_count" ] == 1
1745+ assert [model ["name" ] for model in cast (list [dict [str , object ]], payload ["models" ])] == [
1746+ "orders"
1747+ ]
1748+
1749+
16901750@pytest .mark .parametrize (
16911751 "test_case" ,
16921752 [RulesIntegrationTestCase ("subject folder precedes model layer" , 1 , "SQBRPROJECT102" )],
0 commit comments