File: test_special_iocommands.py

package info (click to toggle)
litecli 1.17.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,440 kB
  • sloc: python: 5,364; makefile: 8
file content (89 lines) | stat: -rw-r--r-- 3,410 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
import os
import tempfile

import pytest

import litecli.packages.special
from litecli.packages.special.main import Verbosity, parse_special_command


def test_once_command():
    with pytest.raises(TypeError):
        litecli.packages.special.execute(None, ".once")

    with pytest.raises(OSError):
        litecli.packages.special.execute(None, ".once /proc/access-denied")

    litecli.packages.special.write_once("hello world")  # write without file set
    # keep Windows from locking the file with delete=False
    with tempfile.NamedTemporaryFile(delete=False) as f:
        litecli.packages.special.execute(None, ".once " + f.name)
        litecli.packages.special.write_once("hello world")
        if os.name == "nt":
            assert f.read() == b"hello world\r\n"
        else:
            assert f.read() == b"hello world\n"

        litecli.packages.special.execute(None, ".once -o " + f.name)
        litecli.packages.special.write_once("hello world line 1")
        litecli.packages.special.write_once("hello world line 2")
        f.seek(0)
        if os.name == "nt":
            assert f.read() == b"hello world line 1\r\nhello world line 2\r\n"
        else:
            assert f.read() == b"hello world line 1\nhello world line 2\n"
    # delete=False means we should try to clean up
    try:
        if os.path.exists(f.name):
            os.remove(f.name)
    except Exception as e:
        print(f"An error occurred while attempting to delete the file: {e}")


def test_pipe_once_command():
    with pytest.raises(IOError):
        litecli.packages.special.execute(None, "\\pipe_once")

    with pytest.raises(OSError):
        litecli.packages.special.execute(None, "\\pipe_once /proc/access-denied")

    if os.name == "nt":
        litecli.packages.special.execute(None, '\\pipe_once python -c "import sys; print(len(sys.stdin.read().strip()))"')
        litecli.packages.special.write_pipe_once("hello world")
        litecli.packages.special.unset_pipe_once_if_written()
    else:
        with tempfile.NamedTemporaryFile() as f:
            litecli.packages.special.execute(None, "\\pipe_once tee " + f.name)
            litecli.packages.special.write_pipe_once("hello world")
            litecli.packages.special.unset_pipe_once_if_written()
            f.seek(0)
            assert f.read() == b"hello world\n"


@pytest.mark.parametrize(
    "sql,expected",
    [
        (r"\d table_name", ("\\d", Verbosity.NORMAL, "table_name")),
        (r"\d+ table_name", ("\\d", Verbosity.VERBOSE, "table_name")),
        (r"\?", ("\\?", Verbosity.NORMAL, "")),
        (r"\llm Question", ("\\llm", Verbosity.NORMAL, "Question")),
        (r"\llm-", ("\\llm", Verbosity.SUCCINCT, "")),
        (r"\llm+", ("\\llm", Verbosity.VERBOSE, "")),
    ],
)
def test_parse_special_command(sql, expected):
    """
    Ensure parse_special_command correctly splits the command and mode.
    """
    result = parse_special_command(sql)
    assert result == expected


def test_parse_special_command_edge_cases():
    # mycli-compatible behavior: no ValueError on special characters; it parses leniently.
    sql = r"\llm* Question"
    assert parse_special_command(sql) == ("\\llm*", Verbosity.NORMAL, "Question")

    sql = r"\llm+- Question"
    # '+' in command sets verbosity; strip('+-') removes both suffixes
    assert parse_special_command(sql) == ("\\llm", Verbosity.VERBOSE, "Question")