Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed broken unit tests: test_mssqlcliclient.py and test_completion_refresher.py #258

Merged
merged 2 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
geleems marked this conversation as resolved.
Show resolved Hide resolved
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
geleems marked this conversation as resolved.
Show resolved Hide resolved
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)