|
20 | 20 | MacroExpanderRouter, |
21 | 21 | PatternMacroExpander, |
22 | 22 | SimpleMacroExpander, |
| 23 | + ParameterAwareMacroExpander |
23 | 24 | ) |
24 | 25 |
|
25 | 26 |
|
@@ -160,3 +161,71 @@ def test_basic_expand(): |
160 | 161 | assert expanded == "abcdef alpha.bravo ghijkl" |
161 | 162 | un_expanded = expander.un_expand(pathlib.Path("abc.sql"), expanded) |
162 | 163 | assert un_expanded == input_text |
| 164 | + |
| 165 | +def test_complex_1(): |
| 166 | + expander = MacroExpanderRouter( |
| 167 | + { |
| 168 | + "*.sql": ParameterAwareMacroExpander( |
| 169 | + pattern="(\\[\\$\\w+\\])", |
| 170 | + value_stripper='__bq__\\d+__(.*)__bq__\\d+__', |
| 171 | + mapping={"[$table1]": "__bq__0__ABCDEF__bq__0__"}, |
| 172 | + source_bind_generator=lambda arg: "@"+arg, |
| 173 | + target_bind_generator=lambda arg: arg |
| 174 | + ) |
| 175 | + } |
| 176 | + ) |
| 177 | + |
| 178 | + input_text="SELECT * FROM WXYZ.[$table1]" |
| 179 | + expanded = expander.expand(pathlib.Path("abc.sql"), input_text) |
| 180 | + assert expanded == "SELECT * FROM WXYZ.__bq__0__ABCDEF__bq__0__" |
| 181 | + un_expanded = expander.un_expand(pathlib.Path("abc.sql"), expanded) |
| 182 | + assert un_expanded == input_text |
| 183 | + |
| 184 | +def test_complex_2(): |
| 185 | + expander = MacroExpanderRouter( |
| 186 | + { |
| 187 | + "*.sql": ParameterAwareMacroExpander( |
| 188 | + pattern="\\[\\$(\\w+)\\]", |
| 189 | + value_stripper='__bq__\\d+__(.*)__bq__\\d+__', |
| 190 | + mapping={ |
| 191 | + "table1": "__bq__0__ABCDEF__bq__0__", |
| 192 | + "limit_val1": "__bq__1__5__bq__1__" |
| 193 | + }, |
| 194 | + source_bind_generator=lambda arg: "@"+arg, |
| 195 | + target_bind_generator=lambda arg: arg |
| 196 | + ) |
| 197 | + } |
| 198 | + ) |
| 199 | + |
| 200 | + input_text = "SELECT * FROM WXYZ.[$table1] LIMIT [$limit_val1]" |
| 201 | + expanded = expander.expand(pathlib.Path("abc.sql"), input_text) |
| 202 | + assert expanded == "SELECT * FROM WXYZ.__bq__0__ABCDEF__bq__0__ LIMIT @__bq__1__5__bq__1__" |
| 203 | + expanded = "SELECT * FROM WXYZ.__bq__0__ABCDEF__bq__0__ LIMIT __bq__1__5__bq__1__" |
| 204 | + un_expanded = expander.un_expand(pathlib.Path("abc.sql"), expanded) |
| 205 | + assert un_expanded == input_text |
| 206 | + |
| 207 | +def test_unexpand_after_database_added(): |
| 208 | + expander = MacroExpanderRouter( |
| 209 | + { |
| 210 | + "*.sql": ParameterAwareMacroExpander( |
| 211 | + pattern="(\\[\\$\\w+\\])", |
| 212 | + value_stripper='__bq__\\d+__(.*)__bq__\\d+__', |
| 213 | + mapping={ |
| 214 | + "[$table]": "__bq__0__table__bq__0__", |
| 215 | + }, |
| 216 | + source_bind_generator=lambda arg: "@"+arg, |
| 217 | + target_bind_generator=lambda arg: arg |
| 218 | + ) |
| 219 | + } |
| 220 | + ) |
| 221 | + |
| 222 | + input_text = "CREATE TABLE [$table](a INT64);" |
| 223 | + expanded = expander.expand(pathlib.Path("abc.sql"), input_text) |
| 224 | + assert expanded == "CREATE TABLE __bq__0__table__bq__0__(a INT64);" |
| 225 | + expanded = "CREATE TABLE db_name.__bq__0__table__bq__0__(a INT64);" |
| 226 | + un_expanded = expander.un_expand(pathlib.Path("abc.sql"), expanded) |
| 227 | + expected_output = "CREATE TABLE db_name.[$table](a INT64);" |
| 228 | + assert un_expanded == expected_output |
| 229 | + |
| 230 | + |
| 231 | + |
0 commit comments