Repository: duckdb/duckdb
File: tools/shell/tests/test_shell_basics.py (Python)

Code before:
    result = test.run()
    result.check_stdout(None)

    with open(random_filepath, 'r') as f:
        lines = f.readlines()
        # The 5 extra lines are from the header and duckbox
        assert len(lines) == 2049 + 2 + 3 # range + header + duckbox
        assert lines[0] == '┌───────┐\n'
        assert lines[1] == '│ range │\n'
        assert lines[2] == '│ int64 │\n'
        assert lines[3] == '├───────┤\n'
        assert lines[4] == '│     0 │\n'
        assert lines[-2] == '│  2048 │\n'
        assert lines[-1] == '└───────┘\n'

def test_once(shell, random_filepath):
    test = (
        ShellTest(shell)
        .statement(f".once {random_filepath.as_posix()}")
        .statement("SELECT 43;")
    )
    result = test.run()
    result.stdout = open(random_filepath, 'rb').read()
    result.check_stdout(b'43')

@pytest.mark.skip(reason=".log is not currently implemented. The log file is opened but never written to.")
def test_log(shell, random_filepath):
    test = (
        ShellTest(shell)
        .statement(f".log {random_filepath.as_posix()}")
        .statement("SELECT 42;")
        .statement(".log off")
    )
    result = test.run()
    result.check_stdout('')

def test_mode_ascii(shell):
    test = (
        ShellTest(shell)
        .statement(".mode ascii")
        .statement("SELECT NULL, 42, 'fourty-two', 42.0;")
    )
    result = test.run()
    result.check_stdout('fourty-two')

def test_mode_csv(shell):
    test = (
        ShellTest(shell)
        .statement(".mode csv")
        .statement("SELECT NULL, 42, 'fourty-two', 42.0;")
    )

Review comment (suggestion):
This was a surprising. `.log` doesn't appear to do anything. A log file is opened but nothing ever seems to write to it. Figured it'd be best to skip for now.

Code after:
def test_mode_csv(shell, dot_command):
    args = ['-csv'] if len(dot_command) == 0 else []
    test = (
        ShellTest(shell, args)
        .statement(dot_command)
        .statement("SELECT NULL, 42, 'fourty-two', 42.0;")
    )
    result = test.run()
    result.check_stdout(',fourty-two,')

@pytest.mark.parametrize("dot_command", [
    ".mode column",
    ""
])
def test_mode_column(shell, dot_command):
    args = ['-column'] if len(dot_command) == 0 else []
    test = (
        ShellTest(shell, args)
        .statement(dot_command)
        .statement("SELECT NULL, 42, 'fourty-two', 42.0;")
    )
    result = test.run()
    result.check_stdout('  fourty-two  ')

def test_mode_html(shell):
    test = (
        ShellTest(shell)
        .statement(".mode html")
        .statement("SELECT NULL, 42, 'fourty-two', 42.0;")
    )
    result = test.run()
    result.check_stdout('<td>fourty-two</td>')

@pytest.mark.parametrize("dot_command", [
    ".mode html",
    ""
])
def test_mode_html_escapes(shell, dot_command):
    args = ['-html'] if len(dot_command) == 0 else []
    test = (
        ShellTest(shell, args)
        .statement(dot_command)
        .statement("SELECT '<&>\"\'\'' AS \"&><\"\"\'\";")
    )
    result = test.run()
    result.check_stdout('<tr><th>&amp;&gt;&lt;&quot;&#39;</th>')

def test_mode_tcl_escapes(shell):
    test = (
        ShellTest(shell)
        .statement(".mode tcl")