summaryrefslogtreecommitdiffstats
path: root/tests/test_rowlimit.py
blob: da916b4dac746d753f7812770556685cc507370d (plain)
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
import pytest
from unittest.mock import Mock

from pgcli.main import PGCli


# We need this fixtures because we need PGCli object to be created
# after test collection so it has config loaded from temp directory


@pytest.fixture(scope="module")
def default_pgcli_obj():
    return PGCli()


@pytest.fixture(scope="module")
def DEFAULT(default_pgcli_obj):
    return default_pgcli_obj.row_limit


@pytest.fixture(scope="module")
def LIMIT(DEFAULT):
    return DEFAULT + 1000


@pytest.fixture(scope="module")
def over_default(DEFAULT):
    over_default_cursor = Mock()
    over_default_cursor.configure_mock(rowcount=DEFAULT + 10)
    return over_default_cursor


@pytest.fixture(scope="module")
def over_limit(LIMIT):
    over_limit_cursor = Mock()
    over_limit_cursor.configure_mock(rowcount=LIMIT + 10)
    return over_limit_cursor


@pytest.fixture(scope="module")
def low_count():
    low_count_cursor = Mock()
    low_count_cursor.configure_mock(rowcount=1)
    return low_count_cursor


def test_row_limit_with_LIMIT_clause(LIMIT, over_limit):
    cli = PGCli(row_limit=LIMIT)
    stmt = "SELECT * FROM students LIMIT 1000"

    result = cli._should_limit_output(stmt, over_limit)
    assert result is False

    cli = PGCli(row_limit=0)
    result = cli._should_limit_output(stmt, over_limit)
    assert result is False


def test_row_limit_without_LIMIT_clause(LIMIT, over_limit):
    cli = PGCli(row_limit=LIMIT)
    stmt = "SELECT * FROM students"

    result = cli._should_limit_output(stmt, over_limit)
    assert result is True

    cli = PGCli(row_limit=0)
    result = cli._should_limit_output(stmt, over_limit)
    assert result is False


def test_row_limit_on_non_select(over_limit):
    cli = PGCli()
    stmt = "UPDATE students SET name='Boby'"
    result = cli._should_limit_output(stmt, over_limit)
    assert result is False

    cli = PGCli(row_limit=0)
    result = cli._should_limit_output(stmt, over_limit)
    assert result is False