Example #1
0
def test__templater_python_substring_occurances(mainstr, substrings,
                                                positions):
    """Test _substring_occurances."""
    occurances = PythonTemplater._substring_occurances(mainstr, substrings)
    assert isinstance(occurances, dict)
    pos_test = [occurances[substring] for substring in substrings]
    assert pos_test == positions
Example #2
0
def test__templater_python_split_invariants(raw_sliced, literals,
                                            raw_occurances,
                                            templated_occurances,
                                            templated_length, result):
    """Test _split_invariants."""
    resp = list(
        PythonTemplater._split_invariants(raw_sliced, literals, raw_occurances,
                                          templated_occurances,
                                          templated_length))
    # check result
    assert resp == result
Example #3
0
def test__templater_python_large_file_check():
    """Test large file skipping.

    The check is seperately called on each .process() method
    so it makes sense to test a few templaters.
    """
    # First check we can process the file normally without config.
    PythonTemplater().process(in_str="SELECT 1", fname="<string>")
    # Then check we raise a skip exception when config is set low.
    with pytest.raises(SQLFluffSkipFile) as excinfo:
        PythonTemplater().process(
            in_str="SELECT 1",
            fname="<string>",
            config=FluffConfig(overrides={
                "dialect": "ansi",
                "large_file_skip_char_limit": 2
            }, ),
        )

    assert "Length of file" in str(excinfo.value)
Example #4
0
def test__templater_python_slice_template(test, result):
    """Test _slice_template."""
    resp = list(PythonTemplater._slice_template(test))
    # check contigious
    assert "".join(elem[0] for elem in resp) == test
    # check indices
    idx = 0
    for literal, _, pos in resp:
        assert pos == idx
        idx += len(literal)
    # Check total result
    assert resp == result
Example #5
0
def test__templater_python_slice_template(test, result):
    """Test _slice_template."""
    resp = list(PythonTemplater._slice_template(test))
    # check contigious
    assert "".join(elem.raw for elem in resp) == test
    # check indices
    idx = 0
    for raw_file_slice in resp:
        assert raw_file_slice.source_idx == idx
        idx += len(raw_file_slice.raw)
    # Check total result
    assert resp == result
Example #6
0
def test__templater_python_slice_file(raw_file, templated_file, result):
    """Test slice_file."""
    _, resp = PythonTemplater.slice_file(
        raw_file,
        templated_file,
    )
    # Check contigious
    prev_slice = None
    for templated_slice in resp:
        if prev_slice:
            assert templated_slice.source_slice.start == prev_slice[0].stop
            assert templated_slice.templated_slice.start == prev_slice[1].stop
        prev_slice = (templated_slice.source_slice,
                      templated_slice.templated_slice)
    # check result
    assert resp == result
Example #7
0
def test__templater_python_slice_file(raw_file, templated_file, unwrap_wrapped,
                                      result):
    """Test slice_file."""
    _, resp, _ = PythonTemplater.slice_file(
        raw_file,
        templated_file,
        config=FluffConfig(
            configs={"templater": {
                "unwrap_wrapped_queries": unwrap_wrapped
            }}),
    )
    # Check contigious
    prev_slice = None
    for templated_slice in resp:
        if prev_slice:
            assert templated_slice.source_slice.start == prev_slice[0].stop
            assert templated_slice.templated_slice.start == prev_slice[1].stop
        prev_slice = (templated_slice.source_slice,
                      templated_slice.templated_slice)
    # check result
    assert resp == result
Example #8
0
def test__templater_python_split_uniques_coalesce_rest(split_file,
                                                       raw_occurances,
                                                       templated_occurances,
                                                       templated_str, result,
                                                       caplog):
    """Test _split_uniques_coalesce_rest."""
    with caplog.at_level(logging.DEBUG, logger="sqlfluff.templater"):
        resp = list(
            PythonTemplater._split_uniques_coalesce_rest(
                split_file,
                raw_occurances,
                templated_occurances,
                templated_str,
            ))
    # Check contigious
    prev_slice = None
    for elem in result:
        if prev_slice:
            assert elem[1].start == prev_slice[0].stop
            assert elem[2].start == prev_slice[1].stop
        prev_slice = (elem[1], elem[2])
    # check result
    assert resp == result
Example #9
0
def test__templater_python_error():
    """Test error handling in the python templater."""
    t = PythonTemplater(override_context=dict(noblah="foo"))
    instr = PYTHON_STRING
    with pytest.raises(SQLTemplaterError):
        t.process(in_str=instr)
Example #10
0
def test__templater_python_sorted_occurance_tuples(test, result):
    """Test _sorted_occurance_tuples."""
    assert PythonTemplater._sorted_occurance_tuples(test) == result
Example #11
0
def test__templater_python():
    """Test the python templater."""
    t = PythonTemplater(override_context=dict(blah="foo"))
    instr = PYTHON_STRING
    outstr, _ = t.process(in_str=instr)
    assert str(outstr) == "SELECT * FROM foo"