File: test_cache.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 (94 lines) | stat: -rw-r--r-- 2,807 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
import pickle
from pathlib import Path
from typing import List

import pytest
from textual.widgets.text_area import Selection

from harlequin import Harlequin
from harlequin.editor_cache import (
    BufferState,
    Cache,
    get_cache_file,
    load_cache,
    write_cache,
)


@pytest.fixture
def buffer_states() -> List[BufferState]:
    return [
        BufferState(
            selection=Selection((0, 3), (0, 3)),
            text="select 1\n",
        ),
        BufferState(
            selection=Selection((0, 0), (0, 0)),
            text="",
        ),
        BufferState(
            selection=Selection((0, 0), (1, 0)),
            text="select\n*\nfrom\nfoo\n",
        ),
    ]


@pytest.fixture
def cache(buffer_states: List[BufferState]) -> Cache:
    return Cache(focus_index=1, buffers=buffer_states)


@pytest.fixture(autouse=True)
def mock_user_cache_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
    monkeypatch.setattr("harlequin.editor_cache.user_cache_dir", lambda **_: tmp_path)
    return tmp_path


@pytest.mark.use_cache
def test_cache_ops(mock_user_cache_dir: Path, cache: Cache) -> None:
    assert mock_user_cache_dir.exists()
    assert len(list(mock_user_cache_dir.iterdir())) == 0
    write_cache(cache)
    children = list(mock_user_cache_dir.iterdir())
    assert len(children) == 1
    assert children[0].suffix == ".pickle"
    assert get_cache_file() == children[0]
    loaded_cache = load_cache()
    assert loaded_cache == cache


@pytest.mark.use_cache
@pytest.mark.asyncio
async def test_harlequin_loads_cache(cache: Cache, app: Harlequin) -> None:
    write_cache(cache)
    async with app.run_test() as pilot:
        while app.editor is None:
            await pilot.pause()
        assert app.editor_collection is not None
        assert app.editor is not None
        assert app.editor_collection.tab_count == len(cache.buffers)
        assert [editor.text for editor in app.editor_collection.all_editors] == [
            buffer.text for buffer in cache.buffers
        ]


@pytest.mark.use_cache
@pytest.mark.asyncio
async def test_harlequin_writes_cache(app: Harlequin) -> None:
    cache_path = get_cache_file()
    assert not cache_path.exists()
    async with app.run_test() as pilot:
        while app.editor is None:
            await pilot.pause()
        assert app.editor_collection is not None
        assert app.editor_collection.tab_count == 1
        app.editor.text = "first"
        await pilot.press("ctrl+n")
        await pilot.pause()
        app.editor.text = "second"
        await pilot.press("ctrl+q")
    assert cache_path.exists()
    with open(cache_path, "rb") as f:
        cache = pickle.load(f)
    assert isinstance(cache, Cache)
    assert [buffer.text for buffer in cache.buffers] == ["first", "second"]