1
0
Fork 0

Fix \d special command and add some tests

This commit is contained in:
Darik Gamble 2015-01-26 08:30:55 -05:00
parent 3abfde4003
commit 7d3f276e83
2 changed files with 32 additions and 1 deletions

View File

@ -86,7 +86,19 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text):
return [{'type': 'alias', 'aliases': alias}]
elif token_v in ('d',): # \d
return [{'type': 'schema'}, {'type': 'table', 'schema': []}]
# Apparently "\d <other>" is parsed by sqlparse as
# Identifer('d', Whitespace, '<other>')
if len(token.tokens) > 2:
other = token.tokens[-1].value
identifiers = other.split('.')
if len(identifiers) == 1:
# "\d table" or "\d schema"
return [{'type': 'schema'}, {'type': 'table', 'schema': []}]
elif len(identifiers) == 2:
# \d schema.table
return [{'type': 'table', 'schema': identifiers[0]}]
else:
return [{'type': 'schema'}, {'type': 'table', 'schema': []}]
elif token_v.lower() in ('c', 'use'): # \c
return [{'type': 'database'}]
elif token_v.endswith(',') or token_v == '=':

View File

@ -0,0 +1,19 @@
from pgcli.packages.sqlcompletion import suggest_type
from test_sqlcompletion import sorted_dicts
def test_d_suggests_tables_and_schemas():
suggestions = suggest_type('\d ', '\d ')
assert sorted_dicts(suggestions) == sorted_dicts([
{'type': 'schema'}, {'type': 'table', 'schema': []}])
suggestions = suggest_type('\d xxx', '\d xxx')
assert sorted_dicts(suggestions) == sorted_dicts([
{'type': 'schema'}, {'type': 'table', 'schema': []}])
def test_d_dot_suggests_schema_qualified_tables():
suggestions = suggest_type('\d myschema.', '\d myschema.')
assert suggestions == [{'type': 'table', 'schema': 'myschema'}]
suggestions = suggest_type('\d myschema.xxx', '\d myschema.xxx')
assert suggestions == [{'type': 'table', 'schema': 'myschema'}]