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
|
import os
import sys
from itertools import product
from pathlib import Path
from typing import Any
import pytest
from click.testing import CliRunner
from sqlfmt.analyzer import Analyzer
from sqlfmt.mode import Mode
from tests.util import copy_test_data_to_tmp
# make tests module importable
sys.path.append(os.path.dirname(__file__))
def pytest_sessionstart(session: pytest.Session) -> None:
"""
Called after the Session object has been created and
before performing collection and entering the run test loop.
"""
from sqlfmt.cache import clear_cache
from tests.util import delete_results_dir
delete_results_dir()
clear_cache()
@pytest.fixture
def sqlfmt_runner() -> CliRunner:
return CliRunner()
@pytest.fixture
def unset_no_color_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("NO_COLOR", raising=False)
@pytest.fixture
def default_mode(unset_no_color_env: None) -> Mode:
return Mode()
@pytest.fixture
def clickhouse_mode(unset_no_color_env: None) -> Mode:
return Mode(dialect_name="clickhouse")
@pytest.fixture
def verbose_mode(unset_no_color_env: None) -> Mode:
return Mode(verbose=True)
@pytest.fixture
def check_mode(unset_no_color_env: None) -> Mode:
return Mode(check=True)
@pytest.fixture
def verbose_check_mode(unset_no_color_env: None) -> Mode:
return Mode(check=True, verbose=True)
@pytest.fixture
def diff_mode(unset_no_color_env: None) -> Mode:
return Mode(diff=True)
@pytest.fixture
def no_color_diff_mode(unset_no_color_env: None) -> Mode:
return Mode(diff=True, no_color=True)
@pytest.fixture
def reset_cache_mode(unset_no_color_env: None) -> Mode:
return Mode(reset_cache=True)
@pytest.fixture
def no_progressbar_mode(unset_no_color_env: None) -> Mode:
return Mode(no_progressbar=True)
@pytest.fixture
def single_process_mode(unset_no_color_env: None) -> Mode:
return Mode(single_process=True)
@pytest.fixture(params=product([True, False], repeat=4))
def all_output_modes(request: Any, unset_no_color_env: None) -> Mode:
return Mode(
check=request.param[0],
diff=request.param[1],
single_process=request.param[2],
no_progressbar=request.param[3],
)
@pytest.fixture
def default_analyzer(default_mode: Mode) -> Analyzer:
return default_mode.dialect.initialize_analyzer(default_mode.line_length)
@pytest.fixture
def preformatted_dir(tmp_path: Path) -> Path:
"""
Copies the directory of preformatted sql files from the unit test
directory into a temp directory (provided by pytest fixture tmp_path),
and then returns the path to the temp directory.
"""
test_dir = copy_test_data_to_tmp(["fast/preformatted"], tmp_path)
return test_dir
@pytest.fixture
def unformatted_dir(tmp_path: Path) -> Path:
"""
Copies the directory of unformatted sql files from the test/data
directory into a temp directory (provided by pytest fixture tmp_path),
and then returns the path to the temp directory.
"""
test_dir = copy_test_data_to_tmp(["fast/unformatted"], tmp_path)
return test_dir
@pytest.fixture
def error_dir(tmp_path: Path) -> Path:
test_dir = copy_test_data_to_tmp(["fast/errors"], tmp_path)
return test_dir
|