Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
_TIMEONLY_W_MICROS = "%H:%M:%S.%f"
_PROJECT_PREFIX_PATTERN = re.compile(
r"""
(?P<project_id>\S+\:[^.]+)\.(?P<dataset_id>[^.]+)(?:$|\.(?P<custom_id>[^.]+)$)
^(?P<project_id>[^:\s]+:[^.\s]+)\.(?P<dataset_id>[^.]+)(?:$|\.(?P<custom_id>[^.]+)$)
""",
re.VERBOSE,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def _extract_types(
operation,
extra_type_sub=re.compile(
r"""
(%*) # Extra %s. We'll deal with these in the replacement code
(?<!%)(%*) # Extra %s. We'll deal with these in the replacement code

% # Beginning of replacement, %s, %(...)s

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
),
state_parse_params_option=OrderedDict(
PY_STRING=r"(?P<PY_STRING>(?:{})|(?:{}))".format( # single and double quoted strings
r"'(?:[^'\\]|\.)*'", r'"(?:[^"\\]|\.)*"'
r"'(?:[^'\\]|\\.)*'", r'"(?:[^"\\]|\\.)*"'
),
PARAMS_OPT_SPEC=r"(?P<PARAMS_OPT_SPEC>--params(?=\s|=|--|$))",
PARAMS_OPT_EQ=r"(?P<PARAMS_OPT_EQ>=)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,65 @@ def lexer_class():
return Lexer


def test_empy_input(lexer_class):
from google.cloud.bigquery.magics.line_arg_parser import TokenType
@pytest.fixture(scope="session")
def token_cls():
from google.cloud.bigquery.magics.line_arg_parser.lexer import Token

return Token


@pytest.fixture(scope="session")
def token_type():
from google.cloud.bigquery.magics.line_arg_parser import TokenType

return TokenType


def test_empy_input(lexer_class, token_cls, token_type):
lexer = lexer_class("")

tokens = list(lexer)

assert tokens == [token_cls(token_type.EOL, lexeme="", pos=0)]


@pytest.mark.parametrize(
"raw_string",
[
r"'it\'s'",
r'"say \"hi\""',
r"'a\\b'",
r"'trailing\\'",
r'"trailing\\"',
],
)
def test_py_string_tokenizes_escaped_characters(
lexer_class, token_cls, token_type, raw_string
):
lexer = lexer_class(f"--params {{'k': {raw_string}}}")

tokens = list(lexer)

assert token_cls(token_type.PY_STRING, lexeme=raw_string, pos=15) in tokens


@pytest.mark.parametrize(
"bad_string,expected_quote_token,expected_quote",
[
(r"'unterminated\'", "SQUOTE", "'"),
(r'"unterminated\"', "DQUOTE", '"'),
],
)
def test_py_string_does_not_match_unterminated_escaped_quote(
lexer_class, token_cls, token_type, bad_string, expected_quote_token, expected_quote
):
lexer = lexer_class(f"--params {{'k': {bad_string}}}")

tokens = list(lexer)

assert tokens == [Token(TokenType.EOL, lexeme="", pos=0)]
assert (
token_cls(
getattr(token_type, expected_quote_token), lexeme=expected_quote, pos=15
)
in tokens
)
35 changes: 34 additions & 1 deletion packages/google-cloud-bigquery/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import decimal
import json
import os
import time
import warnings
import pytest
import packaging
import unittest
from unittest import mock

import google.api_core
from google.cloud.bigquery._helpers import _isinstance_or_raise
from google.cloud.bigquery._helpers import _isinstance_or_raise, _split_id


@pytest.mark.skipif(
Expand Down Expand Up @@ -1214,3 +1215,35 @@ def test__valid_isinstance_or_raise(self, value, dtype, none_allowed, expected):
def test__invalid_isinstance_or_raise(self, value, dtype, none_allowed, expected):
with expected:
_isinstance_or_raise(value, dtype, none_allowed=none_allowed)


@pytest.mark.parametrize(
"full_id,expected",
[
("my-project.my_dataset", ["my-project", "my_dataset"]),
("my-project.my_dataset.my_model", ["my-project", "my_dataset", "my_model"]),
(
"google.com:my-project.my_dataset",
["google.com:my-project", "my_dataset"],
),
(
"google.com:my-project.my_dataset.my_model",
["google.com:my-project", "my_dataset", "my_model"],
),
],
)
def test_split_id_valid_inputs(full_id, expected):
parts = _split_id(full_id)

assert parts == expected


def test_split_id_many_colons_avoids_polynomial_backtracking():
malformed_id = "a:" * 10000 + "b"

start = time.monotonic()
parts = _split_id(malformed_id)
elapsed = time.monotonic() - start

assert parts == [malformed_id]
assert elapsed < 0.5
13 changes: 13 additions & 0 deletions packages/google-cloud-bigquery/tests/unit/test_dbapi_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

import functools
import operator as op
import time
import unittest
from unittest import mock

import google.cloud.bigquery.table as bq_table
import pytest
from google.api_core import exceptions
from google.cloud.bigquery.dbapi.cursor import _extract_types

from tests.unit.helpers import _to_pyarrow

Expand Down Expand Up @@ -940,3 +942,14 @@ def test__extract_types_fail(match, inp):

with pytest.raises(exceptions.ProgrammingError, match=match):
et(inp)


def test__extract_types_many_consecutive_percents_avoids_backtracking():
operation = "%" * 8000 + "x"

start = time.monotonic()
result = _extract_types(operation)
elapsed = time.monotonic() - start

assert result == (operation, None)
assert elapsed < 0.5
Loading