diff options
author | Darik Gamble <darik.gamble@gmail.com> | 2015-02-17 12:35:56 -0500 |
---|---|---|
committer | Darik Gamble <darik.gamble@gmail.com> | 2015-02-17 12:35:56 -0500 |
commit | 22ed9ddb8605abf6c1851cd8318319323c86d551 (patch) | |
tree | 88794369df9f28bd77b186b974afc6e4425dfa80 /tests | |
parent | 896297c4403bc0e7b806d728201aca8215672a23 (diff) |
Autocomplete function names
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_smart_completion_multiple_schemata.py | 61 | ||||
-rw-r--r-- | tests/test_smart_completion_public_schema_only.py | 36 | ||||
-rw-r--r-- | tests/test_sqlcompletion.py | 40 |
3 files changed, 94 insertions, 43 deletions
diff --git a/tests/test_smart_completion_multiple_schemata.py b/tests/test_smart_completion_multiple_schemata.py index d36f4c5a..66653f62 100644 --- a/tests/test_smart_completion_multiple_schemata.py +++ b/tests/test_smart_completion_multiple_schemata.py @@ -3,16 +3,20 @@ from prompt_toolkit.completion import Completion from prompt_toolkit.document import Document metadata = { - 'public': { - 'users': ['id', 'email', 'first_name', 'last_name'], - 'orders': ['id', 'ordered_date', 'status'], - 'select': ['id', 'insert', 'ABC'] - }, - 'custom': { - 'users': ['id', 'phone_number'], - 'products': ['id', 'product_name', 'price'], - 'shipments': ['id', 'address', 'user_id'] - } + 'tables': { + 'public': { + 'users': ['id', 'email', 'first_name', 'last_name'], + 'orders': ['id', 'ordered_date', 'status'], + 'select': ['id', 'insert', 'ABC'] + }, + 'custom': { + 'users': ['id', 'phone_number'], + 'products': ['id', 'product_name', 'price'], + 'shipments': ['id', 'address', 'user_id'] + }}, + 'functions': { + 'public': ['func1', 'func2'], + 'custom': ['func3', 'func4']} } @pytest.fixture @@ -23,16 +27,21 @@ def completer(): schemata, tables, columns = [], [], [] - for schema, tbls in metadata.items(): + for schema, tbls in metadata['tables'].items(): schemata.append(schema) for table, cols in tbls.items(): tables.append((schema, table)) columns.extend([(schema, table, col) for col in cols]) + functions = [(schema, func) + for schema, funcs in metadata['functions'].items() + for func in funcs] + comp.extend_schemata(schemata) comp.extend_tables(tables) comp.extend_columns(columns) + comp.extend_functions(functions) comp.set_search_path(['public']) return comp @@ -70,7 +79,9 @@ def test_suggested_column_names_from_shadowed_visible_table(completer, complete_ Completion(text='id', start_position=0), Completion(text='email', start_position=0), Completion(text='first_name', start_position=0), - Completion(text='last_name', start_position=0)] + + Completion(text='last_name', start_position=0), + Completion(text='func1', start_position=0), + Completion(text='func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_column_names_from_qualified_shadowed_table(completer, complete_event): @@ -82,7 +93,9 @@ def test_suggested_column_names_from_qualified_shadowed_table(completer, complet assert set(result) == set([ Completion(text='*', start_position=0), Completion(text='id', start_position=0), - Completion(text='phone_number', start_position=0)] + + Completion(text='phone_number', start_position=0), + Completion(text='func1', start_position=0), + Completion(text='func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_column_names_from_schema_qualifed_table(completer, complete_event): @@ -100,7 +113,9 @@ def test_suggested_column_names_from_schema_qualifed_table(completer, complete_e Completion(text='*', start_position=0), Completion(text='id', start_position=0), Completion(text='product_name', start_position=0), - Completion(text='price', start_position=0)] + + Completion(text='price', start_position=0), + Completion(text='func1', start_position=0), + Completion(text='func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_column_names_in_function(completer, complete_event): @@ -130,7 +145,9 @@ def test_suggested_table_names_with_schema_dot(completer, complete_event): assert set(result) == set([ Completion(text='users', start_position=0), Completion(text='products', start_position=0), - Completion(text='shipments', start_position=0)]) + Completion(text='shipments', start_position=0), + Completion(text='func3', start_position=0), + Completion(text='func4', start_position=0)]) def test_suggested_column_names_with_qualified_alias(completer, complete_event): """ @@ -167,7 +184,9 @@ def test_suggested_multiple_column_names(completer, complete_event): Completion(text='*', start_position=0), Completion(text='id', start_position=0), Completion(text='product_name', start_position=0), - Completion(text='price', start_position=0)] + + Completion(text='price', start_position=0), + Completion(text='func1', start_position=0), + Completion(text='func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_multiple_column_names_with_alias(completer, complete_event): @@ -223,5 +242,11 @@ def test_table_names_after_from(completer, complete_event): Completion(text='"select"', start_position=0), ]) - - +def test_schema_qualified_function_name(completer, complete_event): + text = 'SELECT custom.func' + postion = len(text) + result = set(completer.get_completions( + Document(text=text, cursor_position=postion), complete_event)) + assert result == set([ + Completion(text='func3', start_position=-len('func')), + Completion(text='func4', start_position=-len('func'))]) diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py index 4c64c825..3f5d4fcc 100644 --- a/tests/test_smart_completion_public_schema_only.py +++ b/tests/test_smart_completion_public_schema_only.py @@ -3,9 +3,11 @@ from prompt_toolkit.completion import Completion from prompt_toolkit.document import Document metadata = { - 'users': ['id', 'email', 'first_name', 'last_name'], - 'orders': ['id', 'ordered_date', 'status'], - 'select': ['id', 'insert', 'ABC'] + 'tables': { + 'users': ['id', 'email', 'first_name', 'last_name'], + 'orders': ['id', 'ordered_date', 'status'], + 'select': ['id', 'insert', 'ABC']}, + 'functions': ['custom_func1', 'custom_func2'] } @pytest.fixture @@ -17,13 +19,16 @@ def completer(): schemata = ['public'] tables, columns = [], [] - for table, cols in metadata.items(): + for table, cols in metadata['tables'].items(): tables.append(('public', table)) columns.extend([('public', table, col) for col in cols]) + functions = [('public', func) for func in metadata['functions']] + comp.extend_schemata(schemata) comp.extend_tables(tables) comp.extend_columns(columns) + comp.extend_functions(functions) comp.set_search_path(['public']) return comp @@ -62,13 +67,22 @@ def test_schema_or_visible_table_completion(completer, complete_event): Completion(text='orders', start_position=0)]) -def test_function_name_completion(completer, complete_event): +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([Completion(text='MAX', start_position=-2)]) +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([ + Completion(text='custom_func1', start_position=-2), + Completion(text='custom_func2', start_position=-2)]) + def test_suggested_column_names_from_visible_table(completer, complete_event): """ Suggest column and function names when selecting from table @@ -86,7 +100,9 @@ def test_suggested_column_names_from_visible_table(completer, complete_event): Completion(text='id', start_position=0), Completion(text='email', start_position=0), Completion(text='first_name', start_position=0), - Completion(text='last_name', start_position=0)] + + Completion(text='last_name', start_position=0), + Completion(text='custom_func1', start_position=0), + Completion(text='custom_func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_column_names_in_function(completer, complete_event): @@ -165,7 +181,9 @@ def test_suggested_multiple_column_names(completer, complete_event): Completion(text='id', start_position=0), Completion(text='email', start_position=0), Completion(text='first_name', start_position=0), - Completion(text='last_name', start_position=0)] + + Completion(text='last_name', start_position=0), + Completion(text='custom_func1', start_position=0), + Completion(text='custom_func2', start_position=0)] + list(map(Completion, completer.functions))) def test_suggested_multiple_column_names_with_alias(completer, complete_event): @@ -271,5 +289,7 @@ def test_auto_escaped_col_names(completer, complete_event): Completion(text='*', start_position=0), Completion(text='id', start_position=0), Completion(text='"insert"', start_position=0), - Completion(text='"ABC"', start_position=0), ] + + Completion(text='"ABC"', start_position=0), + Completion(text='custom_func1', start_position=0), + Completion(text='custom_func2', start_position=0)] + list(map(Completion, completer.functions))) diff --git a/tests/test_sqlcompletion.py b/tests/test_sqlcompletion.py index 760e4dba..4c28d9d4 100644 --- a/tests/test_sqlcompletion.py +++ b/tests/test_sqlcompletion.py @@ -9,20 +9,20 @@ def test_select_suggests_cols_with_visible_table_scope(): suggestions = suggest_type('SELECT FROM tabl', 'SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_select_suggests_cols_with_qualified_table_scope(): suggestions = suggest_type('SELECT FROM sch.tabl', 'SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [('sch', 'tabl', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_where_suggests_columns_functions(): suggestions = suggest_type('SELECT * FROM tabl WHERE ', 'SELECT * FROM tabl WHERE ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_lparen_suggests_cols(): suggestion = suggest_type('SELECT MAX( FROM tbl', 'SELECT MAX(') @@ -33,7 +33,7 @@ def test_select_suggests_cols_and_funcs(): suggestions = suggest_type('SELECT ', 'SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': []}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_from_suggests_tables_and_schemas(): suggestions = suggest_type('SELECT * FROM ', 'SELECT * FROM ') @@ -49,7 +49,7 @@ def test_col_comma_suggests_cols(): suggestions = suggest_type('SELECT a, b, FROM tbl', 'SELECT a, b,') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tbl', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_table_comma_suggests_tables_and_schemas(): suggestions = suggest_type('SELECT a, b FROM tbl1, ', @@ -81,27 +81,30 @@ def test_partially_typed_col_name_suggests_col_names(): 'SELECT * FROM tabl WHERE col_n') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_dot_suggests_cols_of_a_table_or_schema_qualified_table(): suggestions = suggest_type('SELECT tabl. FROM tabl', 'SELECT tabl.') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None)]}, - {'type': 'table', 'schema': 'tabl'}]) + {'type': 'table', 'schema': 'tabl'}, + {'type': 'function', 'schema': 'tabl'}]) def test_dot_suggests_cols_of_an_alias(): suggestions = suggest_type('SELECT t1. FROM tabl1 t1, tabl2 t2', 'SELECT t1.') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': 't1'}, - {'type': 'column', 'tables': [(None, 'tabl1', 't1')]}]) + {'type': 'column', 'tables': [(None, 'tabl1', 't1')]}, + {'type': 'function', 'schema': 't1'}]) def test_dot_col_comma_suggests_cols_or_schema_qualified_table(): suggestions = suggest_type('SELECT t1.a, t2. FROM tabl1 t1, tabl2 t2', 'SELECT t1.a, t2.') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl2', 't2')]}, - {'type': 'table', 'schema': 't2'}]) + {'type': 'table', 'schema': 't2'}, + {'type': 'function', 'schema': 't2'}]) def test_sub_select_suggests_keyword(): suggestion = suggest_type('SELECT * FROM (', 'SELECT * FROM (') @@ -122,7 +125,7 @@ def test_sub_select_col_name_completion(): 'SELECT * FROM (SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'abc', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) @pytest.mark.xfail def test_sub_select_multiple_col_name_completion(): @@ -130,14 +133,15 @@ def test_sub_select_multiple_col_name_completion(): 'SELECT * FROM (SELECT a, ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'abc', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_sub_select_dot_col_name_completion(): suggestions = suggest_type('SELECT * FROM (SELECT t. FROM tabl t', 'SELECT * FROM (SELECT t.') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', 't')]}, - {'type': 'table', 'schema': 't'}]) + {'type': 'table', 'schema': 't'}, + {'type': 'function', 'schema': 't'}]) def test_join_suggests_tables_and_schemas(): suggestion = suggest_type('SELECT * FROM abc a JOIN ', @@ -151,14 +155,16 @@ def test_join_alias_dot_suggests_cols1(): 'SELECT * FROM abc a JOIN def d ON a.') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'abc', 'a')]}, - {'type': 'table', 'schema': 'a'}]) + {'type': 'table', 'schema': 'a'}, + {'type': 'function', 'schema': 'a'}]) def test_join_alias_dot_suggests_cols2(): suggestion = suggest_type('SELECT * FROM abc a JOIN def d ON a.', 'SELECT * FROM abc a JOIN def d ON a.id = d.') assert sorted_dicts(suggestion) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'def', 'd')]}, - {'type': 'table', 'schema': 'd'}]) + {'type': 'table', 'schema': 'd'}, + {'type': 'function', 'schema': 'd'}]) def test_on_suggests_aliases(): suggestions = suggest_type( @@ -194,7 +200,7 @@ def test_2_statements_2nd_current(): 'select * from a; select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'b', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) # Should work even if first statement is invalid suggestions = suggest_type('select * from; select * from ', @@ -212,7 +218,7 @@ def test_2_statements_1st_current(): 'select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'a', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) def test_3_statements_2nd_current(): suggestions = suggest_type('select * from a; select * from ; select * from c', @@ -224,5 +230,5 @@ def test_3_statements_2nd_current(): 'select * from a; select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'b', None)]}, - {'type': 'function'}]) + {'type': 'function', 'schema': []}]) |