from __future__ import unicode_literals
import pytest
from metadata import (MetaData, alias, name_join, fk_join, join, keyword,
schema, table, view, function, column, wildcard_expansion)
from prompt_toolkit.document import Document
from prompt_toolkit.completion import Completion
metadata = {
'tables': {
'users': ['id', 'parentid', 'email', 'first_name', 'last_name'],
'Users': ['userid', 'username'],
'orders': ['id', 'ordered_date', 'status', 'email'],
'select': ['id', 'insert', 'ABC']},
'views': {
'user_emails': ['id', 'email']},
'functions': [
['custom_fun', [''], [''], [''], '', False, False, False],
['_custom_fun', [''], [''], [''], '', False, False, False],
['custom_func1', [''], [''], [''], '', False, False, False],
['custom_func2', [''], [''], [''], '', False, False, False],
['set_returning_func', ['x', 'y'], ['integer', 'integer'],
['o', 'o'], '', False, False, True]],
'datatypes': ['custom_type1', 'custom_type2'],
'foreignkeys': [
('public', 'users', 'id', 'public', 'users', 'parentid'),
('public', 'users', 'id', 'public', 'Users', 'userid')
],
}
metadata = dict((k, {'public': v}) for k, v in metadata.items())
testdata = MetaData(metadata)
cased_users_cols = ['ID', 'PARENTID', 'Email', 'First_Name', 'last_name']
cased_users2_cols = ['UserID', 'UserName']
cased_funcs = ['Custom_Fun', '_custom_fun', 'Custom_Func1',
'custom_func2', 'set_returning_func']
cased_tbls = ['Users', 'Orders']
cased_views = ['User_Emails']
casing = (['SELECT', 'PUBLIC'] + cased_funcs + cased_tbls + cased_views
+ cased_users_cols + cased_users2_cols)
# Lists for use in assertions
cased_funcs = [function(f + '()') for f in cased_funcs]
cased_tbls = [table(t) for t in (cased_tbls + ['"Users"', '"select"'])]
cased_rels = [view(t) for t in cased_views] + cased_funcs + cased_tbls
cased_users_cols = [column(c) for c in cased_users_cols]
aliased_rels = [table(t) for t in ('users u', '"Users" U', 'orders o',
'"select" s')] + [view('user_emails ue')] + [function(f) for f in (
'_custom_fun() cf', 'custom_fun() cf', 'custom_func1() cf',
'custom_func2() cf', 'set_returning_func() srf')]
cased_aliased_rels = [table(t) for t in ('Users U', '"Users" U', 'Orders O',
'"select" s')] + [view('User_Emails UE')] + [function(f) for f in (
'_custom_fun() cf', 'Custom_Fun() CF', 'Custom_Func1() CF',
'custom_func2() cf', 'set_returning_func() srf')]
@pytest.fixture
def completer():
return testdata.completer
@pytest.fixture
def cased_completer():
return testdata.get_completer(casing=casing)
@pytest.fixture
def aliased_completer():
return testdata.get_completer({'generate_aliases': True})
@pytest.fixture
def cased_aliased_completer(request):
return testdata.get_completer({'generate_aliases': True}, casing)
@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 set(testdata.keywords()) == result
def test_select_keyword_completion(completer, complete_event):
text = 'SEL'
position = len('SEL')
result = completer.get_completions(
Document(text=text, cursor_position=position),
complete_event)
assert set(result) == set([keyword('SELECT', -3)])
def test_builtin_function_name_completion(completer, complete_event):
text = 'SELECT MA'
position = len('SELECT MA')
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([function('MAX', -2),
keyword('MAXEXTENTS', -2),
])
def test_builtin_function_matches_only_at_start(completer, complete_event):
text = 'SELECT IN'
position = len('SELECT IN')
document = Document(text=text, cursor_position=position)
result = [c.text for c in
completer.get_completions(document, complete_event)]
assert 'MIN' not in result
def test_user_function_name_completion(completer, complete_event):
text = 'SELECT cu'
position = len('SELECT cu')
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
function('custom_fun()', -2),
function('_custom_fun()', -2),
function('custom_func1()', -2),
function