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.
Fixed broken unit test test_config.py and test_naive_completion.py (d…
- Loading branch information
Gene Lee
authored
Aug 2, 2019
1 parent
8223ae9
commit fdd541d
Showing
6 changed files
with
182 additions
and
112 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,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) |
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,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 |