diff options
author | Dmitry B <oxitnik@gmail.com> | 2017-09-20 23:49:12 +0300 |
---|---|---|
committer | Dmitry B <oxitnik@gmail.com> | 2017-09-20 23:56:57 +0300 |
commit | aa20014213f29c16040ac00fcd6086cf77e829d5 (patch) | |
tree | 6fb766b69a48b589a259e6f0dd325de4bf10c9d5 /tests | |
parent | 915ffb70a70a855bfc8cbc2d3b7ab9828e102775 (diff) |
use temp dir as config location in tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 6 | ||||
-rw-r--r-- | tests/features/environment.py | 9 | ||||
-rw-r--r-- | tests/test_rowlimit.py | 50 |
3 files changed, 53 insertions, 12 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index b9c6c2f2..85433c53 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import os import pytest from utils import (POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, create_db, db_connection, drop_tables) @@ -29,3 +30,8 @@ def executor(connection): @pytest.fixture def exception_formatter(): return lambda e: str(e) + + +@pytest.fixture(scope="session", autouse=True) +def temp_config(tmpdir_factory): + os.environ['XDG_CONFIG_HOME'] = str(tmpdir_factory.mktemp('data')) diff --git a/tests/features/environment.py b/tests/features/environment.py index cdb3c6bf..81c54ad4 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -7,6 +7,8 @@ import sys import db_utils as dbutils import fixture_utils as fixutils import pexpect +import tempfile +import shutil from steps.wrappers import run_cli, wait_prompt @@ -96,6 +98,10 @@ def before_all(context): context.fixture_data = fixutils.read_fixture_files() + context.env_config_home = tempfile.mkdtemp(prefix='pgcli_home_') + + os.environ['XDG_CONFIG_HOME'] = context.env_config_home + def after_all(context): """ @@ -106,6 +112,9 @@ def after_all(context): context.conf['pass'], context.conf['dbname'], context.conf['port']) + # Remove temp config direcotry + shutil.rmtree(context.env_config_home) + # Restore env vars. for k, v in context.pgenv.items(): if k in os.environ and v is None: diff --git a/tests/test_rowlimit.py b/tests/test_rowlimit.py index 8d9a87a5..fd732eec 100644 --- a/tests/test_rowlimit.py +++ b/tests/test_rowlimit.py @@ -1,21 +1,47 @@ from pgcli.main import PGCli from mock import Mock +import pytest -DEFAULT = PGCli().row_limit -LIMIT = DEFAULT + 1000 +@pytest.fixture(scope="module") +def default_pgcli_obj(): + return PGCli() -over_default = Mock() -over_default.configure_mock(rowcount=DEFAULT + 10) -over_limit = Mock() -over_limit.configure_mock(rowcount=LIMIT + 10) +@pytest.fixture(scope="module") +def DEFAULT(default_pgcli_obj): + return default_pgcli_obj.row_limit -low_count = Mock() -low_count.configure_mock(rowcount=1) +@pytest.fixture(scope="module") +def LIMIT(DEFAULT): + return DEFAULT + 1000 -def test_default_row_limit(): + +@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_default_row_limit(low_count, over_default): cli = PGCli() stmt = "SELECT * FROM students" result = cli._should_show_limit_prompt(stmt, low_count) @@ -25,7 +51,7 @@ def test_default_row_limit(): assert result is True -def test_set_row_limit(): +def test_set_row_limit(over_default, over_limit, LIMIT): cli = PGCli(row_limit=LIMIT) stmt = "SELECT * FROM students" result = cli._should_show_limit_prompt(stmt, over_default) @@ -35,7 +61,7 @@ def test_set_row_limit(): assert result is True -def test_no_limit(): +def test_no_limit(over_limit): cli = PGCli(row_limit=0) stmt = "SELECT * FROM students" @@ -43,7 +69,7 @@ def test_no_limit(): assert result is False -def test_row_limit_on_non_select(): +def test_row_limit_on_non_select(over_default): cli = PGCli() stmt = "UPDATE students set name='Boby'" result = cli._should_show_limit_prompt(stmt, None) |