From 726ad9cedc24f7f8a5e58b99cfd170e9be61387b Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Thu, 14 Jul 2016 15:54:19 +0200 Subject: [PATCH 1/4] Fix #449 --- tests/{utils => }/test_config_utils.py | 0 tests/utils/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/{utils => }/test_config_utils.py (100%) delete mode 100644 tests/utils/__init__.py diff --git a/tests/utils/test_config_utils.py b/tests/test_config_utils.py similarity index 100% rename from tests/utils/test_config_utils.py rename to tests/test_config_utils.py diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py deleted file mode 100644 index e69de29b..00000000 From b08f6ebffdc2c79e1926dd1e7584274975bacd6b Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Thu, 14 Jul 2016 19:08:55 +0200 Subject: [PATCH 2/4] Add test for config_utils.file_config() --- tests/test_config_utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index 8c8e22d4..3fd042e1 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -1,4 +1,5 @@ import copy +from unittest.mock import mock_open, patch import pytest @@ -9,6 +10,12 @@ from bigchaindb import exceptions ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config) +@pytest.fixture +def ignore_local_config_file(monkeypatch): + from bigchaindb.config_utils import file_config + monkeypatch.setattr('bigchaindb.config_utils.file_config', file_config) + + @pytest.fixture(scope='function', autouse=True) def clean_config(monkeypatch): monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG)) @@ -205,3 +212,11 @@ def test_update_config(monkeypatch): assert bigchaindb.config['database']['host'] == 'test-host' assert bigchaindb.config['database']['name'] == 'bigchaindb_other' assert bigchaindb.config['database']['port'] == 28016 + + +def test_file_config(): + from bigchaindb.config_utils import file_config, CONFIG_DEFAULT_PATH + with patch('builtins.open', mock_open(read_data='{}')) as m: + config = file_config() + m.assert_called_once_with(CONFIG_DEFAULT_PATH) + assert config == {} From 15b9e8d7e872de1d44d920ec9765e1d6c3ec3cd1 Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Thu, 14 Jul 2016 19:27:58 +0200 Subject: [PATCH 3/4] Test config_utils.write_config() --- tests/test_config_utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index 3fd042e1..eb4eb5e5 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -220,3 +220,13 @@ def test_file_config(): config = file_config() m.assert_called_once_with(CONFIG_DEFAULT_PATH) assert config == {} + + +def test_write_config(): + from bigchaindb.config_utils import write_config, CONFIG_DEFAULT_PATH + m = mock_open() + with patch('builtins.open', m): + write_config({}) + m.assert_called_once_with(CONFIG_DEFAULT_PATH, 'w') + handle = m() + handle.write.assert_called_once_with('{}') From 96cc40dcffc90540c9e421255301771b1edf0661 Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Wed, 20 Jul 2016 12:07:04 +0200 Subject: [PATCH 4/4] Add docstring that explains the fixture's purpose --- tests/test_config_utils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index eb4eb5e5..ea58f61a 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -12,6 +12,13 @@ ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config) @pytest.fixture def ignore_local_config_file(monkeypatch): + """ + This fixture's purpose is to override the one under + :module:`tests/conftest.py` so that the original behaviour of + :func:`bigchaindb.config_utils.file_config` is restored, so that it can be + tested. + + """ from bigchaindb.config_utils import file_config monkeypatch.setattr('bigchaindb.config_utils.file_config', file_config)