summaryrefslogtreecommitdiffstats
path: root/tests/test_completion_refresher.py
diff options
context:
space:
mode:
authorIryna Cherniavska <i.chernyavska@gmail.com>2015-09-05 18:04:31 -0700
committerIryna Cherniavska <i.chernyavska@gmail.com>2015-09-05 18:31:53 -0700
commit62af9d3a96362b5bd5a2f6fafc0950ce6ee92dbd (patch)
tree4a058c4a91cafdd91c2610352ed3464875067097 /tests/test_completion_refresher.py
parentc9262adf5e680577f53806b2812d3f32aa42ec6b (diff)
Added refresher tests.
Diffstat (limited to 'tests/test_completion_refresher.py')
-rw-r--r--tests/test_completion_refresher.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/test_completion_refresher.py b/tests/test_completion_refresher.py
new file mode 100644
index 00000000..b83cef31
--- /dev/null
+++ b/tests/test_completion_refresher.py
@@ -0,0 +1,88 @@
+import time
+import pytest
+from mock import Mock, patch
+
+
+@pytest.fixture
+def refresher():
+ from pgcli.completion_refresher import CompletionRefresher
+ return CompletionRefresher()
+
+
+def test_ctor(refresher):
+ """
+ Refresher object should contain a few handlers
+ :param refresher:
+ :return:
+ """
+ assert len(refresher.refreshers) > 0
+ actual_handlers = list(refresher.refreshers.keys())
+ expected_handlers = ['schemata', 'tables', 'views', 'functions',
+ 'types', 'databases']
+ assert expected_handlers == actual_handlers
+
+
+def test_refresh_called_once(refresher):
+ """
+
+ :param refresher:
+ :return:
+ """
+ callbacks = Mock()
+ pgexecute = Mock()
+ special = Mock()
+
+ with patch.object(refresher, '_bg_refresh') as bg_refresh:
+ actual = refresher.refresh(pgexecute, special, callbacks)
+ time.sleep(1) # Wait for the thread to work.
+ assert len(actual) == 1
+ assert len(actual[0]) == 4
+ assert actual[0][3] == 'Auto-completion refresh started in the background.'
+ bg_refresh.assert_called_with(pgexecute, special, callbacks)
+
+
+def test_refresh_called_twice(refresher):
+ """
+ If refresh is called a second time, it should be restarted
+ :param refresher:
+ :return:
+ """
+ callbacks = Mock()
+
+ pgexecute = Mock()
+ special = Mock()
+
+ def dummy_bg_refresh(*args):
+ time.sleep(3) # seconds
+
+ refresher._bg_refresh = dummy_bg_refresh
+
+ actual1 = refresher.refresh(pgexecute, special, callbacks)
+ time.sleep(1) # Wait for the thread to work.
+ assert len(actual1) == 1
+ assert len(actual1[0]) == 4
+ assert actual1[0][3] == 'Auto-completion refresh started in the background.'
+
+ actual2 = refresher.refresh(pgexecute, special, callbacks)
+ time.sleep(1) # Wait for the thread to work.
+ assert len(actual2) == 1
+ assert len(actual2[0]) == 4
+ assert actual2[0][3] == 'Auto-completion refresh restarted.'
+
+
+def test_refresh_with_callbacks(refresher):
+ """
+ Callbacks must be called
+ :param refresher:
+ """
+ callbacks = [Mock()]
+ pgexecute_class = Mock()
+ pgexecute = Mock()
+ special = Mock()
+
+ with patch('pgcli.completion_refresher.PGExecute', pgexecute_class):
+ # Set refreshers to 0: we're not testing refresh logic here
+ refresher.refreshers = {}
+ refresher.refresh(pgexecute, special, callbacks)
+ time.sleep(1) # Wait for the thread to work.
+ assert (callbacks[0].call_count == 1)