Skip to content

Commit

Permalink
Merge pull request dbcli#258 from dbcli/gelee/tf
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Lee authored Jul 29, 2019
2 parents 097ddfa + eff673f commit 4e6af3b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 83 deletions.
1 change: 1 addition & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def unit_test():
utility.exec_command(
'pytest --cov mssqlcli '
'tests/test_mssqlcliclient.py '
'tests/test_completion_refresher.py ',
'tests/test_main.py '
'tests/test_fuzzy_completion.py '
'tests/test_rowlimit.py '
Expand Down
6 changes: 3 additions & 3 deletions mssqlcli/completion_refresher.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def wrapper(wrapped):
return wrapper


@refresher('schemata')
@refresher('schemas')
@decorators.suppress_all_exceptions()
def refresh_schemata(completer, mssqlcliclient):
completer.extend_schemata(mssqlcliclient.get_schemas())
def refresh_schemas(completer, mssqlcliclient):
completer.extend_schemas(mssqlcliclient.get_schemas())


@refresher('tables')
Expand Down
3 changes: 1 addition & 2 deletions mssqlcli/mssqlqueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def get_views():
return '''
SELECT table_schema,
table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_type = 'View'
FROM INFORMATION_SCHEMA.VIEWS
ORDER BY 1, 2'''


Expand Down
151 changes: 73 additions & 78 deletions tests/test_completion_refresher.py
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)

0 comments on commit 4e6af3b

Please sign in to comment.