Skip to content

Commit

Permalink
Fixed broken unit test test_config.py and test_naive_completion.py (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Lee authored Aug 2, 2019
1 parent 8223ae9 commit fdd541d
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 112 deletions.
4 changes: 3 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def unit_test():
utility.exec_command(
'pytest --cov mssqlcli '
'tests/test_mssqlcliclient.py '
'tests/test_completion_refresher.py ',
'tests/test_completion_refresher.py '
'tests/test_config.py '
'tests/test_naive_completion.py '
'tests/test_main.py '
'tests/test_fuzzy_completion.py '
'tests/test_rowlimit.py '
Expand Down
14 changes: 14 additions & 0 deletions mssqlcli/mssqlcompleter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import print_function, unicode_literals
import logging
import re
import sys
from itertools import count, repeat, chain
import operator
from collections import namedtuple, defaultdict, OrderedDict
Expand Down Expand Up @@ -405,6 +406,11 @@ def _match(item):
sort_key, type_priority, prio, priority_func(item),
prio2, lexical_priority
)

item = self.ensure_unicode(item)
display_meta = self.ensure_unicode(display_meta)
display = self.ensure_unicode(display)

matches.append(
Match(
completion=Completion(
Expand All @@ -417,6 +423,14 @@ def _match(item):
)
)
return matches

# In Python 3, all strings are sequences of Unicode characters.
# There is a bytes type that holds raw bytes.
# In Python 2, a string may be of type str or of type unicode.
def ensure_unicode(self, word):
if (sys.version_info.major < 3 and isinstance(word, str)):
return word.decode('utf-8')
return word

def case(self, word):
return self.casing.get(word, word)
Expand Down
11 changes: 5 additions & 6 deletions tests/mssqltestutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def create_mssql_cli_options(**nondefault_options):
def shutdown(connection):
connection.shutdown()

def getTempDir(*args):
tempDir = os.path.join(os.path.abspath(__file__), '..')
def getTempPath(*args):
testRoot = os.path.join(os.path.abspath(__file__), '..')
tempPath = os.path.join(testRoot, 'temp')
for arg in args:
tempDir = os.path.join(tempDir, arg)
return os.path.abspath(tempDir)


tempPath = os.path.join(tempPath, arg)
return os.path.abspath(tempPath)
51 changes: 24 additions & 27 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import os
import stat
import unittest
import shutil
from mssqlcli.config import (
ensure_dir_exists,
get_config,
)
from mssqltestutils import getTempPath

import pytest

from mssqlcli.config import ensure_dir_exists
class ConfigTests(unittest.TestCase):

def test_ensure_existing_dir(self):
rcfilePath = getTempPath('subdir', 'rcfile')
get_config(rcfilePath)
# should just not raise
ensure_dir_exists(rcfilePath)
shutil.rmtree(getTempPath())

def test_ensure_file_parent(tmpdir):
subdir = tmpdir.join("subdir")
rcfile = subdir.join("rcfile")
ensure_dir_exists(str(rcfile))

# Below test does not seem to work on windows.
# Commenting this out so that it doesn't fail our test runs.
# Tracked by Github Issue
# def test_ensure_other_create_error(self):
# rcfilePath = getTempPath('subdir', 'rcfile')
# get_config(rcfilePath)

def test_ensure_existing_dir(tmpdir):
rcfile = str(tmpdir.mkdir("subdir").join("rcfile"))
# # trigger an oserror that isn't "directory already exists"
# os.chmod(rcfilePath, stat.S_IREAD)

# should just not raise
ensure_dir_exists(rcfile)

# Below test does not seem to work on windows.
# Commenting this out so that it doesn't fail our test runs.
# Tracked by Github Issue
"""
def test_ensure_other_create_error(tmpdir):
subdir = tmpdir.join("subdir")
rcfile = subdir.join("rcfile")
# trigger an oserror that isn't "directory already exists"
os.chmod(str(tmpdir), stat.S_IREAD)
with pytest.raises(OSError):
ensure_dir_exists(str(rcfile))
"""
# with pytest.raises(OSError):
# ensure_dir_exists(rcfilePath)
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
create_mssql_cli_options,
create_mssql_cli_client,
shutdown,
getTempDir
getTempPath
)
from mssqlcli.mssql_cli import OutputSettings, MssqlFileHistory

Expand Down Expand Up @@ -142,7 +142,7 @@ def test_format_output_auto_expand(self):

def test_missing_rc_dir(self):
try:
rcfilePath = getTempDir('subdir', 'rcfile')
rcfilePath = getTempPath('subdir', 'rcfile')
mssqlcli = create_mssql_cli(mssqlclirc_file=rcfilePath)
assert os.path.exists(rcfilePath)
finally:
Expand Down
210 changes: 134 additions & 76 deletions tests/test_naive_completion.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,138 @@
from __future__ import unicode_literals
import pytest
import unittest
from prompt_toolkit.completion import Completion
from prompt_toolkit.document import Document


@pytest.fixture
def completer():
import mssqlcli.mssqlcompleter as mssqlcompleter
return mssqlcompleter.MssqlCompleter(smart_completion=False)


@pytest.fixture
def complete_event():
from mock import Mock
return Mock()


def test_empty_string_completion(completer, complete_event):
text = ''
position = 0
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
assert result == set(map(Completion, completer.all_completions))


def test_select_keyword_completion(completer, complete_event):
text = 'SEL'
position = len('SEL')
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
assert result == set([Completion(text='SELECT', start_position=-3)])


def test_function_name_completion(completer, complete_event):
text = 'SELECT MA'
position = len('SELECT MA')
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
assert result == set([
Completion(text='MATERIALIZED VIEW', start_position=-2),
Completion(text='MAX', start_position=-2),
Completion(text='MAXEXTENTS', start_position=-2)])


def test_column_name_completion(completer, complete_event):
text = 'SELECT FROM users'
position = len('SELECT ')
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
assert result == set(map(Completion, completer.all_completions))


def test_paths_completion(completer, complete_event):
text = '\i '
position = len(text)
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event,
smart_completion=True))
assert result > set([Completion(text="setup.py", start_position=0)])


def test_alter_well_known_keywords_completion(completer, complete_event):
text = 'ALTER '
position = len(text)
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event,
smart_completion=True))
assert result > set([
Completion(text="DATABASE", display_meta='keyword'),
Completion(text="TABLE", display_meta='keyword'),
Completion(text="SYSTEM", display_meta='keyword'),
])
assert Completion(text="CREATE", display_meta="keyword") not in result
from mock import Mock
import mssqlcli.mssqlcompleter as mssqlcompleter

class NaiveCompletionTests(unittest.TestCase):

def test_empty_string_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = ''
position = 0
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event
)
)
actual = list(filter(lambda e: e.display_meta_text == 'keyword', actual))
actual = set(map(lambda e: e.text, actual))
expected = set(completer.keywords_tree.keys())
assert actual == expected

def test_select_keyword_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = 'SEL'
position = len('SEL')
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event,
)
)
expected = [Completion(text='SELECT', start_position=-3, display_meta="keyword")]
assert self.equals(actual, expected)

def test_function_name_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = 'SELECT MA'
position = len('SELECT MA')
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event
)
)
expected = [Completion(text='MAX', start_position=-2, display_meta="function")]
assert self.equals(actual, expected)

def test_column_name_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = 'SELECT FROM users'
position = len('SELECT ')
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event
)
)
actual = set(map(lambda e: e.text, actual))
expected = set(completer.keywords_tree.keys())
expected.update(completer.functions)
assert actual == expected

def test_paths_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = '\i '
position = len(text)
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event
)
)
expected = [Completion(text="setup.py", start_position=0, display_meta="")]
assert self.contains(actual, expected)

def test_alter_well_known_keywords_completion(self):
completer = self.get_completer()
complete_event = self.get_complete_event()
text = 'ALTER '
position = len(text)
actual = list(
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event
)
)
expected = [
Completion(text="DATABASE", display_meta='keyword'),
Completion(text="TABLE", display_meta='keyword')
]
assert self.contains(actual, expected)
not_expected = [Completion(text="CREATE", display_meta="keyword")]
assert not self.contains(actual, not_expected)

def get_completer(self):
return mssqlcompleter.MssqlCompleter(smart_completion=True)

def get_complete_event(self):
return Mock()

def equals(self, completion_list1, completion_list2):
if len(completion_list1) != len(completion_list2):
return False
for e1 in completion_list1:
theSame = None
for e2 in completion_list2:
if (e1.text == e2.text
and e1.start_position == e2.start_position
and e1.display_meta_text == e2.display_meta_text
):
theSame = e2
break
if theSame is None:
return False
return True

def contains(self, completion_list1, completion_list2):
for e2 in completion_list2:
theSame = None
for e1 in completion_list1:
if (e2.text == e1.text
and e2.start_position == e1.start_position
and e2.display_meta_text == e1.display_meta_text
):
theSame = e1
break
if theSame is None:
return False
return True

0 comments on commit fdd541d

Please sign in to comment.