File: conftest.py

package info (click to toggle)
harlequin 2.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,856 kB
  • sloc: python: 12,064; makefile: 44; sql: 6
file content (188 lines) | stat: -rw-r--r-- 4,678 bytes parent folder | download
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
import sys
from contextlib import suppress
from typing import Awaitable, Callable
from unittest.mock import MagicMock

import pytest
from textual.worker import WorkerCancelled

from harlequin.app import Harlequin
from harlequin.autocomplete import HarlequinCompletion


@pytest.fixture(autouse=True)
def no_use_buffer_cache(
    monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
) -> None:
    if "use_cache" in request.keywords:
        return
    monkeypatch.setattr("harlequin.components.code_editor.load_cache", lambda: None)
    monkeypatch.setattr("harlequin.app.write_editor_cache", lambda *_: None)


@pytest.fixture(autouse=True)
def no_use_catalog_cache(
    monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
) -> None:
    if "use_cache" in request.keywords:
        return
    monkeypatch.setattr("harlequin.app.get_catalog_cache", lambda *_: None)
    monkeypatch.setattr("harlequin.app.update_catalog_cache", lambda *_: None)


@pytest.fixture(autouse=True)
def mock_config_loader(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(
        "harlequin.cli.get_config_for_profile", lambda **_: (dict(), [])
    )


@pytest.fixture(autouse=True)
def mock_completions(monkeypatch: pytest.MonkeyPatch) -> None:
    KEYWORDS = [
        "abort",
        "all",
        "alter",
        "always",
        "analyze",
        "and",
        "as",
        "asc",
        "begin",
        "between",
        "by",
        "cascade",
        "case",
        "column",
        "commit",
        "create",
        "database",
        "delete",
        "desc",
        "distinct",
        "drop",
        "else",
        "end",
        "explain",
        "from",
        "group",
        "groups",
        "having",
        "in",
        "inner",
        "insert",
        "intersect",
        "into",
        "is",
        "join",
        "left",
        "like",
        "limit",
        "null",
        "on",
        "order",
        "outer",
        "over",
        "partition",
        "row",
        "savepoint",
        "select",
        "set",
        "table",
        "temp",
        "temporary",
        "then",
        "union",
        "update",
        "using",
        "values",
        "view",
        "when",
        "where",
        "window",
    ]

    FUNCTIONS = [
        ("array_select", "fn"),
        ("count", "agg"),
        ("greatest", "fn"),
        ("least", "fn"),
        ("list_select", "fn"),
        ("sqrt", "fn"),
        ("sum", "agg"),
    ]

    keyword_completions = [
        HarlequinCompletion(
            label=kw_name, type_label="kw", value=kw_name, priority=100, context=None
        )
        for kw_name in KEYWORDS
    ]

    function_completions = [
        HarlequinCompletion(
            label=label, type_label=type_label, value=label, priority=1000, context=None
        )
        for label, type_label in FUNCTIONS
    ]

    completions = [*keyword_completions, *function_completions]
    duckdb_completions = [
        (
            completion.label,
            completion.type_label,
            completion.priority,
            completion.context,
        )
        for completion in completions
    ]
    monkeypatch.setattr(
        "harlequin_sqlite.adapter.get_completion_data",
        lambda *_: completions,
        raising=True,
    )
    monkeypatch.setattr(
        "harlequin_duckdb.adapter.get_completion_data",
        lambda *_: duckdb_completions,
        raising=True,
    )


@pytest.fixture
def mock_pyperclip(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
    mock = MagicMock()
    mock.determine_clipboard.return_value = mock.copy, mock.paste

    def set_paste(x: str) -> None:
        mock.paste.return_value = x

    mock.copy.side_effect = set_paste
    monkeypatch.setattr("textual_textarea.text_editor.pyperclip", mock)

    return mock


@pytest.fixture
def wait_for_workers() -> Callable[[Harlequin], Awaitable[None]]:
    async def wait_for_filtered_workers(app: Harlequin) -> None:
        filtered_workers = [
            w for w in app.workers if w.name != "_database_tree_background_loader"
        ]
        if filtered_workers:
            with suppress(WorkerCancelled):
                await app.workers.wait_for_complete(filtered_workers)

    return wait_for_filtered_workers


@pytest.fixture
def transaction_button_visible() -> Callable[[Harlequin], bool]:
    def fn(app: Harlequin) -> bool:
        """
        Skip snapshot checks for versions of that app showing the autocommit button.
        """
        return (
            sys.version_info >= (3, 12) and "Sqlite" in app.adapter.__class__.__name__
        )

    return fn