From 86c711707682dafc8e42d27bff24dc97bf3c5286 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sun, 3 Jan 2016 11:29:34 +0530 Subject: [PATCH] Redis fail tests in gui test + todo update --- pytest_gui_status/status_gui/todo.txt | 24 +++--- tests/test_plugin/test_gui.py | 120 ++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 tests/test_plugin/test_gui.py diff --git a/pytest_gui_status/status_gui/todo.txt b/pytest_gui_status/status_gui/todo.txt index 4b222f9..2b2793f 100644 --- a/pytest_gui_status/status_gui/todo.txt +++ b/pytest_gui_status/status_gui/todo.txt @@ -1,16 +1,20 @@ Todo ------ -1. Allow config for template -2. Add well-explained readme -3. use logging.debug / log file for more prints -4. Add gui tests -5. Use config.pluginmanager.hasplugin('plugin_name') when needed -6. Release on pypi -7. Test on Appveyor - install redis via chocolatey -8. Give icon to gui + preferably dynamic with status -9. Add redis sudden fail tests - shutdown or crash after startup -\ -module shouldnt crash/ work incorrectly, should exit with error message +1. Add redis sudden fail tests - shutdown or crash after startup\ + - module shouldnt crash/ work incorrectly, should exit with error message +2 Tests with setup/teardown/class when needed* +3 Redis test port from env* +4 Faster Tests wiith timeout, if required by extra api to params* +5. Allow config for template +6. Add cli doc for --show_status_gui +7. Add well-explained readme +8. use logging.debug / log file for more prints +9. Add gui tests +10. Use config.pluginmanager.hasplugin('plugin_name') when needed +11. Release on pypi +12. Test on Appveyor - install redis via chocolatey +13. Give icon to gui + preferably dynamic with status Done diff --git a/tests/test_plugin/test_gui.py b/tests/test_plugin/test_gui.py new file mode 100644 index 0000000..b6a1d36 --- /dev/null +++ b/tests/test_plugin/test_gui.py @@ -0,0 +1,120 @@ +import pytest_gui_status.status_plugin.plugin as status_plugin +import pytest_gui_status.status_gui.gui_backend as gui_backend + +from mock import patch, MagicMock +import redis +import pytest + +REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1 + + +@patch.dict("os.environ", + {"PYTEST_STATUS_PORT": str(REDIS_TEST_PORT)}) +def test_redis_fail_1(tmpdir): + # load new redis port + reload(gui_backend) + reload(status_plugin) + + # dont start redis. Also if redis running, stop it + redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) + try: + assert redis_db.ping() + redis_db.shutdown() + except redis.exceptions.ConnectionError: + pass + except AssertionError: + pass + + # mock Controller requirements and app_gui.stop + fake_app_gui = MagicMock() + with pytest.raises(redis.exceptions.ConnectionError) as error_info: + gui_backend.Controller(fake_app_gui).redraw() + + assert error_info.value.message == "Cant connect to this socket. Is redis running?\n" \ + + "Stopping pytest_status_gui" + + +@patch.dict("os.environ", + {"PYTEST_STATUS_PORT": str(REDIS_TEST_PORT)}) +def test_redis_fail_2(tmpdir): + # load new redis port + reload(gui_backend) + reload(status_plugin) + + # start Redis + status_plugin.Helpers.on_start(tmpdir.strpath) + + # make PYTEST_STATUS_DB value corrupt/incorrect + redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) + redis_db.set("PYTEST_STATUS_DB", "0") + + # mock Controller requirements and app_gui.stop + fake_app_gui = MagicMock() + with pytest.raises(redis.exceptions.ConnectionError) as error_info: + gui_backend.Controller(fake_app_gui).redraw() + + assert error_info.value.message == "Redis is running on this port, but it is not related to pytest status\n"\ + + "Stopping pytest_status_gui" + + fake_app_gui.stop.assert_called_with() + + # cleanup or in next test it cant be started there + redis_db.shutdown() + + +@patch.dict("os.environ", + {"PYTEST_STATUS_PORT": str(REDIS_TEST_PORT)}) +def test_redis_fail_3(tmpdir): + # load new redis port + reload(gui_backend) + reload(status_plugin) + + # start Redis + status_plugin.Helpers.on_start(tmpdir.strpath) + + # delete PYTEST_STATUS_DB + redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) + redis_db.delete("PYTEST_STATUS_DB") + + # mock Controller requirements and app_gui.stop + fake_app_gui = MagicMock() + + with pytest.raises(redis.exceptions.ConnectionError) as error_info: + gui_backend.Controller(fake_app_gui).redraw() + + assert error_info.value.message == "Redis is running on this port, but it is not related to pytest status\n"\ + + "Stopping pytest_status_gui" + + fake_app_gui.stop.assert_called_with() + + # cleanup or in next test it cant be started there + redis_db.shutdown() + + +@patch.dict("os.environ", + {"PYTEST_STATUS_PORT": str(REDIS_TEST_PORT)}) +def test_redis_fail_4(tmpdir): + # load new redis port + reload(gui_backend) + reload(status_plugin) + + # start Redis + status_plugin.Helpers.on_start(tmpdir.strpath) + + redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) + + # mock Controller requirements and app_gui.stop + # provide invalid path + fake_app_gui = MagicMock() + fake_app_gui.dir_name = r"Invalid/Path" + + with pytest.raises(redis.exceptions.ConnectionError) as error_info: + gui_backend.Controller(fake_app_gui).redraw() + + assert error_info.value.message == "dir_name = {dir_name} not found in redis db".format( + dir_name=fake_app_gui.dir_name) + + fake_app_gui.stop.assert_called_with() + + # cleanup or in next test it cant be started there + redis_db.shutdown()