1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
import pytest
from sqlfmt.exception import SqlfmtBracketError
from sqlfmt.mode import Mode
from sqlfmt.node_manager import NodeManager
from sqlfmt.tokens import Token, TokenType
from tests.util import read_test_data
@pytest.fixture
def node_manager(default_mode: Mode) -> NodeManager:
return NodeManager(default_mode.dialect.case_sensitive_names)
def test_calculate_depth(node_manager: NodeManager) -> None:
select_t = Token(
type=TokenType.UNTERM_KEYWORD,
prefix="",
token="select",
spos=0,
epos=6,
)
select_n = node_manager.create_node(token=select_t, previous_node=None)
assert (select_n.depth, select_n.open_brackets) == ((0, 0), [])
open_paren_t = Token(
type=TokenType.BRACKET_OPEN,
prefix=" ",
token="(",
spos=8,
epos=9,
)
open_paren_n = node_manager.create_node(token=open_paren_t, previous_node=select_n)
assert (open_paren_n.depth, open_paren_n.open_brackets) == ((1, 0), [select_n])
one_t = Token(
type=TokenType.NUMBER,
prefix=" ",
token="1",
spos=10,
epos=11,
)
one_n = node_manager.create_node(token=one_t, previous_node=open_paren_n)
assert (one_n.depth, one_n.open_brackets) == ((2, 0), [select_n, open_paren_n])
close_paren_t = Token(
type=TokenType.BRACKET_CLOSE,
prefix="",
token=")",
spos=11,
epos=12,
)
close_paren_n = node_manager.create_node(token=close_paren_t, previous_node=one_n)
assert (close_paren_n.depth, close_paren_n.open_brackets) == ((1, 0), [select_n])
def test_calculate_depth_exception(node_manager: NodeManager) -> None:
close_paren = Token(
type=TokenType.BRACKET_CLOSE,
prefix="",
token=")",
spos=0,
epos=1,
)
with pytest.raises(SqlfmtBracketError):
_ = node_manager.create_node(close_paren, previous_node=None)
def test_jinja_depth(default_mode: Mode) -> None:
source_string, _ = read_test_data(
"unit_tests/test_node_manager/test_jinja_depth.sql"
)
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
expected = [
(0, 0), # {{ config(materialized="table") }}
(0, 0), # \n
(0, 0), # \n
(0, 0), # {%- set n = 5 -%}
(0, 0), # \n
(0, 0), # with
(1, 0), # \n
(1, 0), # {% for i in range(n) %}
(1, 1), # \n
(1, 1), # dont_do_this_
(1, 1), # {{ i }}
(1, 1), # as
(1, 1), # (
(2, 1), # \n
(2, 1), # {% if foo %}
(2, 2), # \n
(2, 2), # select
(3, 2), # \n
(2, 1), # {% elif bar %}
(2, 2), # \n
(2, 2), # select distinct
(3, 2), # \n
(2, 1), # {% elif baz %}
(2, 2), # \n
(2, 2), # select top 25
(3, 2), # \n
(2, 1), # {% else %}
(2, 2), # \n
(2, 2), # select
(3, 2), # \n
(3, 1), # {% endif %}
(3, 1), # \n
(3, 1), # my_col
(3, 1), # \n
(2, 1), # from
(3, 1), # \n
(3, 1), # {% if i == qux %}
(3, 2), # \n
(3, 2), # zip
(3, 2), # \n
(3, 1), # {% else %}
(3, 2), # \n
(3, 2), # zap
(3, 2), # \n
(3, 1), # {% endif %}
(3, 1), # \n
(1, 1), # )
(1, 1), # {% if not loop.last %}
(1, 2), # ,
(1, 1), # {% endif%}
(1, 1), # \n
(1, 0), # {% endfor %}
(1, 0), # \n
(1, 0), # {% for i in range(n) %}
(1, 1), # \n
(0, 1), # select
(1, 1), # \n
(1, 1), # *
(1, 1), # \n
(0, 1), # from
(1, 1), # \n
(1, 1), # dont_do_this_
(1, 1), # {{ i }}
(1, 1), # \n
(1, 1), # {% if not loop.last -%}
(1, 2), # \n
(0, 2), # union all
(0, 2), # \n
(0, 1), # {%- endif %}
(0, 1), # \n
(0, 0), # {% endfor %}
(0, 0), # \n
]
actual = [node.depth for node in q.nodes]
assert actual == expected
def test_create_node_raises_bracket_error_on_jinja_block_end(
node_manager: NodeManager,
) -> None:
t = Token(
type=TokenType.JINJA_BLOCK_END,
prefix="",
token="{% endif %}",
spos=0,
epos=11,
)
with pytest.raises(SqlfmtBracketError):
_ = node_manager.create_node(t, previous_node=None)
def test_union_depth(default_mode: Mode) -> None:
source_string, _ = read_test_data(
"unit_tests/test_node_manager/test_union_depth.sql"
)
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
expected = [
(0, 0), # select,
(1, 0), # 1,
(1, 0), # \n,
(1, 0), # \n,
(0, 0), # union,
(0, 0), # \n,
(0, 0), # \n,
(0, 0), # select,
(1, 0), # 2,
(1, 0), # \n,
(1, 0), # \n,
(0, 0), # union all,
(0, 0), # \n,
(0, 0), # \n,
(0, 0), # (,
(1, 0), # \n,
(1, 0), # select,
(2, 0), # 3,
(2, 0), # \n,
(0, 0), # ),
(0, 0), # \n'
]
actual_depth = [n.depth for n in q.nodes]
assert actual_depth == expected
def test_capitalization(default_mode: Mode) -> None:
source_string = (
'SELECT A, B, "C", {{ D }}, e, \'f\', \'G\'\nfROM "H"."j" Join I ON k And L\n'
)
expected = (
'select a, b, "C", {{ D }}, e, \'f\', \'G\'\nfrom "H"."j" join i on k and l\n'
)
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert parsed_string == expected
@pytest.mark.parametrize(
"source_string,expected",
[
(
"SELECT A, B, \"C\", {{ D }}, e, 'f', 'G'\n"
'fROM "H"."j" Join I ON k And L\n',
"select A, B, \"C\", {{ D }}, e, 'f', 'G'\n"
'from "H"."j" join I on k and L\n',
),
(
"SELECT toString(1) AS Test_string, toDateTime64('2022-05-25', 3) "
"AS Test_dateTime64, ifNull(null, 'TestNull') as testIf, "
"JSONExtractString('{\"abc\": \"hello\"}', 'abc') as testJSON\n",
"select toString(1) as Test_string, toDateTime64('2022-05-25', 3) "
"as Test_dateTime64, ifNull(null, 'TestNull') as testIf, "
"JSONExtractString('{\"abc\": \"hello\"}', 'abc') as testJSON\n",
),
],
)
def test_capitalization_clickhouse(
source_string: str, expected: str, clickhouse_mode: Mode
) -> None:
q = clickhouse_mode.dialect.initialize_analyzer(
line_length=clickhouse_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert parsed_string == expected
@pytest.mark.parametrize(
"source_string",
[
"OVER",
"IN",
"AND",
"EXCEPT",
"REPLACE",
"UNION",
"SUM",
"BETWEEN",
],
)
def test_capitalization_operators(default_mode: Mode, source_string: str) -> None:
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert parsed_string.rstrip("\n") == source_string.lower()
@pytest.mark.parametrize(
"source_string",
[
"1e4",
"1E4",
"-.1234567E+2BD",
],
)
def test_capitalization_numbers(default_mode: Mode, source_string: str) -> None:
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert parsed_string.rstrip("\n") == source_string.lower()
@pytest.mark.parametrize(
"source_string",
[
"my_schema.my_table\n",
"my_table.*\n",
'"my_table".*\n',
"{{ my_schema }}.my_table\n",
"my_schema.{{ my_table }}\n",
"my_database.my_schema.my_table\n",
'my_schema."my_table"\n',
'"my_schema".my_table\n',
'"my_schema"."my_table"\n',
"my_table.$2\n",
'"my_table".$2\n',
"my_schema.{% if foo %}bar{% else %}baz{% endif %}\n",
"json_field:key_name\n",
'json:"KeyName"\n',
"my_table.json_field:key_name\n",
'"JSONField":"KeyName"::varchar\n',
'"JSONField":"KeyName"::varchar\n',
"my_array[1]\n",
"my_array[1:2]\n",
'"my_array"[1]\n',
'"my_array"[1:2]\n',
],
)
def test_identifier_whitespace(default_mode: Mode, source_string: str) -> None:
"""
Ensure we do not inject spaces into qualified identifier names
"""
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert source_string == parsed_string
@pytest.mark.parametrize(
"source_string",
[
"(my_schema.my_table)\n",
"count(*)\n",
"count(*) over ()\n",
"something in (somthing_else)\n",
"some_array[offset(0)]\n",
"some_array_func(args)[offset(0)]\n",
"(())\n",
"([])\n",
"()[] + foo()[offset(1)]\n",
"using (id)\n",
"foo:['bar.baz']\n",
],
)
def test_bracket_whitespace(default_mode: Mode, source_string: str) -> None:
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert source_string == parsed_string
@pytest.mark.parametrize(
"source_string,expected_string",
[
("union\n\nall", "union all\n"),
("union\n all", "union all\n"),
("union all", "union all\n"),
("select\ntop\n10", "select top 10\n"),
("group by", "group by\n"),
("not\nin", "not in\n"),
("not\n similar \n to", "not similar to\n"),
("right\n outer \n join", "right outer join\n"),
],
)
def test_internal_whitespace(
default_mode: Mode, source_string: str, expected_string: str
) -> None:
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
parsed_string = "".join(str(line) for line in q.lines)
assert parsed_string == expected_string
def test_other_whitespace(default_mode: Mode) -> None:
source_string = "'hi'"
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
assert q.nodes[0].prefix == " "
def test_jinja_whitespace(default_mode: Mode) -> None:
source_string = "{{ foo }}bar {{ foo }} bar{% if foo %} bar {% endif %}"
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
assert all([node.prefix == node.token.prefix for node in q.nodes])
def test_disabled_formatting(default_mode: Mode) -> None:
source_string, _ = read_test_data(
"unit_tests/test_node_manager/test_disabled_formatting.sql"
)
q = default_mode.dialect.initialize_analyzer(
line_length=default_mode.line_length
).parse_query(source_string=source_string)
selects = [n for n in q.nodes if n.is_unterm_keyword]
assert not selects[0].formatting_disabled
assert selects[1].formatting_disabled
assert selects[2].formatting_disabled
assert len(selects[2].formatting_disabled) == 2
assert selects[3].formatting_disabled
assert not selects[4].formatting_disabled
create_publication_line = q.lines[5]
assert create_publication_line.formatting_disabled
assert create_publication_line.nodes
create_token = create_publication_line.nodes[0].token
assert create_token.type is TokenType.DATA
assert create_token in create_publication_line.nodes[0].formatting_disabled
assert len(create_publication_line.nodes[0].formatting_disabled) == 3
semicolon_node = create_publication_line.nodes[-2]
assert create_token not in semicolon_node.formatting_disabled
|