Beispiel #1
0
def test__templater_param_style(instr, expected_outstr, param_style, values):
    """Test different param_style templating."""
    t = PlaceholderTemplater(override_context={
        **values, "param_style": param_style
    })
    outstr, _ = t.process(in_str=instr, fname="test", config=FluffConfig())
    assert str(outstr) == expected_outstr
Beispiel #2
0
def test__templater_exception():
    """Test the exception raised when variables are missing."""
    t = PlaceholderTemplater(
        override_context=dict(name="'john'", param_style="colon"))
    instr = "SELECT name FROM table WHERE user_id = :user_id"
    with pytest.raises(SQLTemplaterError,
                       match=r"Failure in placeholder templating: 'user_id'"):
        t.process(in_str=instr, fname="test")
Beispiel #3
0
def test__templater_custom_regex():
    """Test custom regex templating."""
    t = PlaceholderTemplater(override_context=dict(
        param_regex="__(?P<param_name>[\\w_]+)__", my_name="john"))
    outstr, _ = t.process(
        in_str="SELECT bla FROM blob WHERE id = __my_name__",
        fname="test",
        config=FluffConfig(),
    )
    assert str(outstr) == "SELECT bla FROM blob WHERE id = john"
Beispiel #4
0
def test__templater_setup():
    """Test the exception raised when config is incomplete or ambiguous."""
    t = PlaceholderTemplater(override_context=dict(name="'john'"))
    with pytest.raises(
            ValueError,
            match=
        ("No param_regex nor param_style was provided to the placeholder templater"
         ),
    ):
        t.process(in_str="SELECT 2+2", fname="test")

    t = PlaceholderTemplater(
        override_context=dict(param_style="bla", param_regex="bli"))
    with pytest.raises(
            ValueError,
            match=
            r"Either param_style or param_regex must be provided, not both",
    ):
        t.process(in_str="SELECT 2+2", fname="test")
Beispiel #5
0
def test__templater_raw():
    """Test the templaters when nothing has to be replaced."""
    t = PlaceholderTemplater(override_context=dict(param_style="colon"))
    instr = "SELECT * FROM {{blah}} WHERE %(gnepr)s OR e~':'"
    outstr, _ = t.process(in_str=instr, fname="test")
    assert str(outstr) == instr
Beispiel #6
0
def test__templater_styles():
    """Test the exception raised when parameter style is unknown."""
    t = PlaceholderTemplater(override_context=dict(param_style="pperccent"))
    with pytest.raises(ValueError, match=r"Unknown param_style"):
        t.process(in_str="SELECT 2+2", fname="test")