forked from dbcli/mssql-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dbcli#258 from dbcli/gelee/tf
- Loading branch information
Showing
4 changed files
with
78 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,82 @@ | ||
import time | ||
import pytest | ||
import unittest | ||
from mock import Mock, patch | ||
|
||
|
||
@pytest.fixture | ||
def refresher(): | ||
from mssqlcli.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', | ||
'types', 'databases', 'casing', 'functions'] | ||
assert expected_handlers == actual_handlers | ||
|
||
|
||
def test_refresh_called_once(refresher): | ||
""" | ||
:param refresher: | ||
:return: | ||
""" | ||
callbacks = Mock() | ||
pgexecute = Mock() | ||
|
||
with patch.object(refresher, '_bg_refresh') as bg_refresh: | ||
actual = refresher.refresh(pgexecute, callbacks) | ||
from mssqlcli.completion_refresher import CompletionRefresher | ||
|
||
class CompletionRefresherTests(unittest.TestCase): | ||
|
||
def test_ctor(self): | ||
""" | ||
Refresher object should contain a few handlers | ||
:param refresher: | ||
:return: | ||
""" | ||
refresher = CompletionRefresher() | ||
assert len(refresher.refreshers) > 0 | ||
actual_handlers = set(refresher.refreshers.keys()) | ||
expected_handlers = set(['databases', 'schemas', 'tables', 'types', 'views']) | ||
assert expected_handlers == actual_handlers | ||
|
||
def test_refresh_called_once(self): | ||
""" | ||
:param refresher: | ||
:return: | ||
""" | ||
callbacks = Mock() | ||
mssqlcliclient = Mock() | ||
refresher = CompletionRefresher() | ||
|
||
with patch.object(refresher, '_bg_refresh') as bg_refresh: | ||
actual = refresher.refresh(mssqlcliclient, 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(mssqlcliclient, callbacks, None, None) | ||
|
||
def test_refresh_called_twice(self): | ||
""" | ||
If refresh is called a second time, it should be restarted | ||
:param refresher: | ||
:return: | ||
""" | ||
callbacks = Mock() | ||
mssqlcliclient = Mock() | ||
refresher = CompletionRefresher() | ||
|
||
def bg_refresh_mock(*args): | ||
time.sleep(3) # seconds | ||
|
||
refresher._bg_refresh = bg_refresh_mock | ||
|
||
actual1 = refresher.refresh(mssqlcliclient, 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, callbacks, None, | ||
None) | ||
|
||
assert len(actual1) == 1 | ||
assert len(actual1[0]) == 4 | ||
assert actual1[0][3] == 'Auto-completion refresh started in the background.' | ||
|
||
def test_refresh_called_twice(refresher): | ||
""" | ||
If refresh is called a second time, it should be restarted | ||
:param refresher: | ||
:return: | ||
""" | ||
callbacks = Mock() | ||
|
||
pgexecute = Mock() | ||
|
||
def dummy_bg_refresh(*args): | ||
time.sleep(3) # seconds | ||
|
||
refresher._bg_refresh = dummy_bg_refresh | ||
|
||
actual1 = refresher.refresh(pgexecute, 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, 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() | ||
pgexecute.extra_args = {} | ||
actual2 = refresher.refresh(mssqlcliclient, 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(self): | ||
""" | ||
Callbacks must be called | ||
:param refresher: | ||
""" | ||
class MssqlCliClientMock: | ||
def connect_to_database(self): | ||
return 'connectionservicetest', [] | ||
|
||
mssqlcliclient = MssqlCliClientMock() | ||
callbacks = [Mock()] | ||
refresher = CompletionRefresher() | ||
|
||
with patch('pgcli.completion_refresher.PGExecute', pgexecute_class): | ||
# Set refreshers to 0: we're not testing refresh logic here | ||
refresher.refreshers = {} | ||
refresher.refresh(pgexecute, callbacks) | ||
refresher.refresh(mssqlcliclient, callbacks) | ||
time.sleep(1) # Wait for the thread to work. | ||
assert (callbacks[0].call_count == 1) |