summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/metadata.py79
-rw-r--r--tests/test_smart_completion_multiple_schemata.py256
-rw-r--r--tests/test_smart_completion_public_schema_only.py422
3 files changed, 383 insertions, 374 deletions
diff --git a/tests/metadata.py b/tests/metadata.py
new file mode 100644
index 00000000..da4967b9
--- /dev/null
+++ b/tests/metadata.py
@@ -0,0 +1,79 @@
+from pgcli.packages.function_metadata import FunctionMetadata, ForeignKey
+from prompt_toolkit.completion import Completion
+from functools import partial
+
+escape = lambda name: ('"' + name + '"' if not name.islower() or name in (
+ 'select', 'insert') else name)
+
+def completion(display_meta, text, pos=0):
+ return Completion(text, start_position=pos,
+ display_meta=display_meta)
+
+# The code below is quivalent to
+# def schema(text, pos=0):
+# return completion('schema', text, pos)
+# and so on
+schema, table, view, function, column, keyword, datatype, alias, name_join,\
+ fk_join, join = [partial(completion, display_meta)
+ for display_meta in('schema', 'table', 'view', 'function', 'column',
+ 'keyword', 'datatype', 'table alias', 'name join', 'fk join', 'join')]
+
+def wildcard_expansion(cols, pos=-1):
+ return Completion(cols, start_position=pos, display_meta='columns',
+ display = '*')
+
+class MetaData(object):
+ def __init__(self, metadata):
+ self.metadata = metadata
+ self.get_completer()
+
+ def builtin_functions(self, pos=0):
+ return [function(f, pos) for f in self.completer.functions]
+
+ def builtin_datatypes(self, pos=0):
+ return [datatype(dt, pos) for dt in self.completer.datatypes]
+
+ def keywords(self, pos=0):
+ return [keyword(kw, pos) for kw in self.completer.keywords]
+
+ def get_completer(self):
+ metadata = self.metadata
+ import pgcli.pgcompleter as pgcompleter
+ self.completer = comp = pgcompleter.PGCompleter(smart_completion=True)
+
+ schemata, tables, tbl_cols, views, view_cols = [], [], [], [], []
+
+ for schema, tbls in metadata['tables'].items():
+ schemata.append(schema)
+
+ for table, cols in tbls.items():
+ tables.append((schema, table))
+ # Let all columns be text columns
+ tbl_cols.extend([(schema, table, col, 'text') for col in cols])
+
+ for schema, tbls in metadata.get('views', {}).items():
+ for view, cols in tbls.items():
+ views.append((schema, view))
+ # Let all columns be text columns
+ view_cols.extend([(schema, view, col, 'text') for col in cols])
+
+ functions = [FunctionMetadata(schema, *func_meta)
+ for schema, funcs in metadata['functions'].items()
+ for func_meta in funcs]
+
+ datatypes = [(schema, datatype)
+ for schema, datatypes in metadata['datatypes'].items()
+ for datatype in datatypes]
+
+ foreignkeys = [ForeignKey(*fk) for fks in metadata['foreignkeys'].values()
+ for fk in fks]
+
+ comp.extend_schemata(schemata)
+ comp.extend_relations(tables, kind='tables')
+ comp.extend_relations(views, kind='views')
+ comp.extend_columns(tbl_cols, kind='tables')
+ comp.extend_columns(view_cols, kind='views')
+ comp.extend_functions(functions)
+ comp.extend_datatypes(datatypes)
+ comp.extend_foreignkeys(foreignkeys)
+ comp.set_search_path(['public'])
diff --git a/tests/test_smart_completion_multiple_schemata.py b/tests/test_smart_completion_multiple_schemata.py
index b5ac0d3a..8b80d6fc 100644
--- a/tests/test_smart_completion_multiple_schemata.py
+++ b/tests/test_smart_completion_multiple_schemata.py
@@ -1,7 +1,13 @@
from __future__ import unicode_literals
import pytest
import itertools
-from prompt_toolkit.completion import Completion
+from metadata import (MetaData, alias, name_join, fk_join, join,
+ table,
+ function,
+ column,
+ schema,
+ datatype,
+ wildcard_expansion)
from prompt_toolkit.document import Document
from pgcli.packages.function_metadata import FunctionMetadata, ForeignKey
@@ -38,46 +44,17 @@ metadata = {
'public': ['typ1', 'typ2'],
'custom': ['typ3', 'typ4'],
},
- 'foreignkeys': [
+ 'foreignkeys': {
+ 'custom': [
('public', 'users', 'id', 'custom', 'shipments', 'user_id')
- ],
+ ]},
}
+testdata = MetaData(metadata)
+
@pytest.fixture
def completer():
-
- import pgcli.pgcompleter as pgcompleter
- comp = pgcompleter.PGCompleter(smart_completion=True)
-
- schemata, tables, columns = [], [], []
-
- for schema, tbls in metadata['tables'].items():
- schemata.append(schema)
-
- for table, cols in tbls.items():
- tables.append((schema, table))
- # Let all columns be text columns
- columns.extend([(schema, table, col, 'text') for col in cols])
-
- functions = [FunctionMetadata(schema, *func_meta)
- for schema, funcs in metadata['functions'].items()
- for func_meta in funcs]
-
- datatypes = [(schema, datatype)
- for schema, datatypes in metadata['datatypes'].items()
- for datatype in datatypes]
-
- foreignkeys = [ForeignKey(*fk) for fk in metadata['foreignkeys']]
-
- comp.extend_schemata(schemata)
- comp.extend_relations(tables, kind='tables')
- comp.extend_columns(columns, kind='tables')
- comp.extend_functions(functions)
- comp.extend_datatypes(datatypes)
- comp.extend_foreignkeys(foreignkeys)
- comp.set_search_path(['public'])
-
- return comp
+ return testdata.completer
@pytest.fixture
def complete_event():
@@ -90,14 +67,14 @@ def test_schema_or_visible_table_completion(completer, complete_event):
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function'),
- Completion(text='public', start_position=0, display_meta='schema'),
- Completion(text='custom', start_position=0, display_meta='schema'),
- Completion(text='"Custom"', start_position=0, display_meta='schema'),
- Completion(text='users', start_position=0, display_meta='table'),
- Completion(text='"select"', start_position=0, display_meta='table'),
- Completion(text='orders', start_position=0, display_meta='table')])
+ function('func1'),
+ function('func2'),
+ schema('public'),
+ schema('custom'),
+ schema('"Custom"'),
+ table('users'),
+ table('"select"'),
+ table('orders')])
@pytest.mark.parametrize('table', [
@@ -118,14 +95,14 @@ def test_suggested_column_names_from_shadowed_visible_table(completer, complete_
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column'),
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('email'),
+ column('first_name'),
+ column('last_name'),
+ function('func1'),
+ function('func2')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
def test_suggested_column_names_from_qualified_shadowed_table(completer, complete_event):
@@ -135,12 +112,12 @@ def test_suggested_column_names_from_qualified_shadowed_table(completer, complet
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='phone_number', start_position=0, display_meta='column'),
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('phone_number'),
+ function('func1'),
+ function('func2')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
@pytest.mark.parametrize('text', [
@@ -155,10 +132,10 @@ def test_suggested_join_conditions(completer, complete_event, text):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='users', start_position=0, display_meta='table alias'),
- Completion(text='shipments', start_position=0, display_meta='table alias'),
- Completion(text='shipments.id = users.id', start_position=0, display_meta='name join'),
- Completion(text='shipments.user_id = users.id', start_position=0, display_meta='fk join')])
+ alias('users'),
+ alias('shipments'),
+ name_join('shipments.id = users.id'),
+ fk_join('shipments.user_id = users.id')])
@pytest.mark.parametrize(('query', 'tbl'), itertools.product((
'SELECT * FROM public.{0} RIGHT OUTER JOIN ',
@@ -172,17 +149,16 @@ def test_suggested_joins(completer, complete_event, query, tbl):
result = set(completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
- join = 'custom.shipments ON shipments.user_id = {0}.id'.format(tbl)
assert set(result) == set([
- Completion(text=join, start_position=0, display_meta='join'),
- Completion(text='public', start_position=0, display_meta='schema'),
- Completion(text='custom', start_position=0, display_meta='schema'),
- Completion(text='"Custom"', start_position=0, display_meta='schema'),
- Completion(text='orders', start_position=0, display_meta='table'),
- Completion(text='users', start_position=0, display_meta='table'),
- Completion(text='"select"', start_position=0, display_meta='table'),
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function')])
+ join('custom.shipments ON shipments.user_id = {0}.id'.format(tbl)),
+ schema('public'),
+ schema('custom'),
+ schema('"Custom"'),
+ table('orders'),
+ table('users'),
+ table('"select"'),
+ function('func1'),
+ function('func2')])
def test_suggested_column_names_from_schema_qualifed_table(completer, complete_event):
"""
@@ -196,13 +172,13 @@ def test_suggested_column_names_from_schema_qualifed_table(completer, complete_e
result = set(completer.get_completions(
Document(text=text, cursor_position=position), complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='product_name', start_position=0, display_meta='column'),
- Completion(text='price', start_position=0, display_meta='column'),
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('product_name'),
+ column('price'),
+ function('func1'),
+ function('func2')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
def test_suggested_column_names_in_function(completer, complete_event):
@@ -219,9 +195,9 @@ def test_suggested_column_names_in_function(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event)
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='product_name', start_position=0, display_meta='column'),
- Completion(text='price', start_position=0, display_meta='column')])
+ column('id'),
+ column('product_name'),
+ column('price')])
@pytest.mark.parametrize('text', [
@@ -242,12 +218,12 @@ def test_suggested_table_names_with_schema_dot(completer, complete_event,
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='func3', start_position=start_pos, display_meta='function'),
- Completion(text='users', start_position=start_pos, display_meta='table'),
- Completion(text='"Users"', start_position=start_pos, display_meta='table'),
- Completion(text='products', start_position=start_pos, display_meta='table'),
- Completion(text='shipments', start_position=start_pos, display_meta='table'),
- Completion(text='set_returning_func', start_position=start_pos, display_meta='function'),
+ function('func3', start_pos),
+ table('users', start_pos),
+ table('"Users"', start_pos),
+ table('products', start_pos),
+ table('shipments', start_pos),
+ function('set_returning_func', start_pos),
])
@pytest.mark.parametrize('text', [
@@ -266,8 +242,8 @@ def test_suggested_table_names_with_schema_dot2(completer, complete_event,
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='func4', start_position=start_pos, display_meta='function'),
- Completion(text='projects', start_position=start_pos, display_meta='table')
+ function('func4', start_pos),
+ table('projects', start_pos)
])
def test_suggested_column_names_with_qualified_alias(completer, complete_event):
@@ -283,9 +259,9 @@ def test_suggested_column_names_with_qualified_alias(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='product_name', start_position=0, display_meta='column'),
- Completion(text='price', start_position=0, display_meta='column')])
+ column('id'),
+ column('product_name'),
+ column('price')])
def test_suggested_multiple_column_names(completer, complete_event):
"""
@@ -301,13 +277,13 @@ def test_suggested_multiple_column_names(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='product_name', start_position=0, display_meta='column'),
- Completion(text='price', start_position=0, display_meta='column'),
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('product_name'),
+ column('price'),
+ function('func1'),
+ function('func2')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
def test_suggested_multiple_column_names_with_alias(completer, complete_event):
@@ -324,9 +300,9 @@ def test_suggested_multiple_column_names_with_alias(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='product_name', start_position=0, display_meta='column'),
- Completion(text='price', start_position=0, display_meta='column')])
+ column('id'),
+ column('product_name'),
+ column('price')])
@pytest.mark.parametrize('text', [
'SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON ',
@@ -338,11 +314,11 @@ def test_suggestions_after_on(completer, complete_event, text):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='x', start_position=0, display_meta='table alias'),
- Completion(text='y', start_position=0, display_meta='table alias'),
- Completion(text='y.price = x.price', start_position=0, display_meta='name join'),
- Completion(text='y.product_name = x.product_name', start_position=0, display_meta='name join'),
- Completion(text='y.id = x.id', start_position=0, display_meta='name join')])
+ alias('x'),
+ alias('y'),
+ name_join('y.price = x.price'),
+ name_join('y.product_name = x.product_name'),
+ name_join('y.id = x.id')])
def test_suggested_aliases_after_on_right_side(completer, complete_event):
text = 'SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON x.id = '
@@ -351,8 +327,8 @@ def test_suggested_aliases_after_on_right_side(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='x', start_position=0, display_meta='table alias'),
- Completion(text='y', start_position=0, display_meta='table alias')])
+ alias('x'),
+ alias('y')])
def test_table_names_after_from(completer, complete_event):
text = 'SELECT * FROM '
@@ -361,14 +337,14 @@ def test_table_names_after_from(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='func1', start_position=0, display_meta='function'),
- Completion(text='func2', start_position=0, display_meta='function'),
- Completion(text='public', start_position=0, display_meta='schema'),
- Completion(text='custom', start_position=0, display_meta='schema'),
- Completion(text='"Custom"', start_position=0, display_meta='schema'),
- Completion(text='users', start_position=0, display_meta='table'),
- Completion(text='orders', start_position=0, display_meta='table'),
- Completion(text='"select"', start_position=0, display_meta='table'),
+ schema('public'),
+ schema('custom'),
+ schema('"Custom"'),
+ function('func1'),
+ function('func2'),
+ table('users'),
+ table('orders'),
+ table('"select"'),
])
def test_schema_qualified_function_name(completer, complete_event):
@@ -377,8 +353,8 @@ def test_schema_qualified_function_name(completer, complete_event):
result = set(completer.get_completions(
Document(text=text, cursor_position=postion), complete_event))
assert result == set([
- Completion(text='func3', start_position=-len('func'), display_meta='function'),
- Completion(text='set_returning_func', start_position=-len('func'), display_meta='function')])
+ function('func3', -len('func')),
+ function('set_returning_func', -len('func'))])
@pytest.mark.parametrize('text', [
@@ -392,12 +368,12 @@ def test_schema_qualified_type_name(text, completer, complete_event):
result = completer.get_completions(
Document(text=text, cursor_position=pos), complete_event)
assert set(result) == set([
- Completion(text='typ3', display_meta='datatype'),
- Completion(text='typ4', display_meta='datatype'),
- Completion(text='users', display_meta='table'),
- Completion(text='"Users"', display_meta='table'),
- Completion(text='products', display_meta='table'),
- Completion(text='shipments', display_meta='table'),
+ datatype('typ3'),
+ datatype('typ4'),
+ table('users'),
+ table('"Users"'),
+ table('products'),
+ table('shipments'),
])
@@ -407,7 +383,7 @@ def test_suggest_columns_from_aliased_set_returning_function(completer, complete
result = completer.get_completions(Document(text=sql, cursor_position=pos),
complete_event)
assert set(result) == set([
- Completion(text='x', start_position=0, display_meta='column')])
+ column('x')])
@pytest.mark.parametrize('text', [
'SELECT * FROM custom.set_returning_func()',
@@ -421,8 +397,7 @@ def test_wildcard_column_expansion_with_function(completer, complete_event, text
Document(text=text, cursor_position=pos), complete_event)
col_list = 'x'
- expected = [Completion(text=col_list, start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion(col_list)]
assert expected == completions
@@ -435,8 +410,7 @@ def test_wildcard_column_expansion_with_alias_qualifier(completer, complete_even
Document(text=sql, cursor_position=pos), complete_event)
col_list = 'id, p.product_name, p.price'
- expected = [Completion(text=col_list, start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion(col_list)]
assert expected == completions
@@ -463,8 +437,7 @@ def test_wildcard_column_expansion_with_insert(completer, complete_event, text):
completions = completer.get_completions(
Document(text=text, cursor_position=pos), complete_event)
- expected = [Completion(text='id, ordered_date, status', start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion('id, ordered_date, status')]
assert expected == completions
def test_wildcard_column_expansion_with_table_qualifier(completer, complete_event):
@@ -475,8 +448,7 @@ def test_wildcard_column_expansion_with_table_qualifier(completer, complete_even
Document(text=sql, cursor_position=pos), complete_event)
col_list = 'id, "select"."insert", "select"."ABC"'
- expected = [Completion(text=col_list, start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion(col_list)]
assert expected == completions
@@ -489,8 +461,7 @@ def test_wildcard_column_expansion_with_two_tables(completer, complete_event):
cols = ('"select".id, "select"."insert", "select"."ABC", '
'users.id, users.phone_number')
- expected = [Completion(text=cols, start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion(cols)]
assert completions == expected
@@ -502,8 +473,7 @@ def test_wildcard_column_expansion_with_two_tables_and_parent(completer, complet
Document(text=sql, cursor_position=pos), complete_event)
col_list = 'id, "select"."insert", "select"."ABC"'
- expected = [Completion(text=col_list, start_position=-1,
- display='*', display_meta='columns')]
+ expected = [wildcard_expansion(col_list)]
assert expected == completions
@@ -520,8 +490,8 @@ def test_suggest_columns_from_unquoted_table(completer, complete_event, text):
result = completer.get_completions(Document(text=text, cursor_position=pos),
complete_event)
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='phone_number', start_position=0, display_meta='column')])
+ column('id'),
+ column('phone_number')])
@pytest.mark.parametrize('text', [
'SELECT U. FROM custom."Users" U',
@@ -532,5 +502,5 @@ def test_suggest_columns_from_quoted_table(completer, complete_event, text):
result = completer.get_completions(Document(text=text, cursor_position=pos),
complete_event)
assert set(result) == set([
- Completion(text='userid', start_position=0, display_meta='column'),
- Completion(text='username', start_position=0, display_meta='column')])
+ column('userid'),
+ column('username')])
diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py
index 01c54fe5..562ce38f 100644
--- a/tests/test_smart_completion_public_schema_only.py
+++ b/tests/test_smart_completion_public_schema_only.py
@@ -1,6 +1,13 @@
from __future__ import unicode_literals
import pytest
-from prompt_toolkit.completion import Completion
+from metadata import (MetaData, alias, name_join, fk_join, join, keyword,
+ table,
+ view,
+ function,
+ column,
+ schema,
+ datatype,
+ wildcard_expansion)
from prompt_toolkit.document import Document
from pgcli.packages.function_metadata import FunctionMetadata, ForeignKey
@@ -26,50 +33,13 @@ metadata = {
],
}
-@pytest.fixture
-def completer():
-
- import pgcli.pgcompleter as pgcompleter
- comp = pgcompleter.PGCompleter(smart_completion=True)
-
- schemata = ['public']
- comp.extend_schemata(schemata)
-
- # tables
- tables, columns = [], []
- for table, cols in metadata['tables'].items():
- tables.append(('public', table))
- # Let all columns be text columns
- columns.extend([('public', table, col, 'text') for col in cols])
-
- comp.extend_relations(tables, kind='tables')
- comp.extend_columns(columns, kind='tables')
-
- # views
- views, columns = [], []
- for view, cols in metadata['views'].items():
- views.append(('public', view))
- columns.extend([('public', view, col, 'text') for col in cols])
+metadata = dict((k, {'public': v}) for k, v in metadata.items())
- comp.extend_relations(views, kind='views')
- comp.extend_columns(columns, kind='views')
+testdata = MetaData(metadata)
- # functions
- functions = [FunctionMetadata('public', *func_meta)
- for func_meta in metadata['functions']]
- comp.extend_functions(functions)
-
- # types
- datatypes = [('public', typ) for typ in metadata['datatypes']]
- comp.extend_datatypes(datatypes)
-
- # fks
- foreignkeys = [ForeignKey(*fk) for fk in metadata['foreignkeys']]
- comp.extend_foreignkeys(foreignkeys)
-
- comp.set_search_path(['public'])
-
- return comp
+@pytest.fixture
+def completer():
+ return testdata.completer
@pytest.fixture
def complete_event():
@@ -83,8 +53,7 @@ def test_empty_string_completion(completer, complete_event):
completer.get_completions(
Document(text=text, cursor_position=position),
complete_event))
- assert set(map(lambda x: Completion(x, display_meta='keyword'),
- completer.keywords)) == result
+ assert set(testdata.keywords()) == result
def test_select_keyword_completion(completer, complete_event):
text = 'SEL'
@@ -92,8 +61,7 @@ def test_select_keyword_completion(completer, complete_event):
result = completer.get_completions(
Document(text=text, cursor_position=position),
complete_event)
- assert set(result) == set([Completion(text='SELECT', start_position=-3,
- display_meta='keyword')])
+ assert set(result) == set([keyword('SELECT', -3)])
def test_schema_or_visible_table_completion(completer, complete_event):
@@ -102,15 +70,15 @@ def test_schema_or_visible_table_completion(completer, complete_event):
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='public', start_position=0, display_meta='schema'),
- Completion(text='users', start_position=0, display_meta='table'),
- Completion(text='"Users"', start_position=0, display_meta='table'),
- Completion(text='"select"', start_position=0, display_meta='table'),
- Completion(text='orders', start_position=0, display_meta='table'),
- Completion(text='user_emails', start_position=0, display_meta='view'),
- Completion(text='custom_func1', start_position=0, display_meta='function'),
- Completion(text='custom_func2', start_position=0, display_meta='function'),
- Completion(text='set_returning_func', start_position=0, display_meta='function')])
+ schema('public'),
+ table('users'),
+ table('"Users"'),
+ table('"select"'),
+ table('orders'),
+ view('user_emails'),
+ function('custom_func1'),
+ function('custom_func2'),
+ function('set_returning_func')])
def test_builtin_function_name_completion(completer, complete_event):
@@ -118,8 +86,8 @@ def test_builtin_function_name_completion(completer, complete_event):
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, display_meta='function'),
- Completion(text='MAXEXTENTS', start_position=-2, display_meta='keyword'),
+ assert set(result) == set([function('MAX', -2),
+ keyword('MAXEXTENTS', -2),
])
@@ -140,9 +108,9 @@ def test_user_function_name_completion(completer, complete_event):
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='custom_func1', start_position=-2, display_meta='function'),
- Completion(text='custom_func2', start_position=-2, display_meta='function'),
- Completion(text='CURRENT', start_position=-2, display_meta='keyword'),
+ function('custom_func1', -2),
+ function('custom_func2', -2),
+ keyword('CURRENT', -2),
])
@@ -153,8 +121,8 @@ def test_user_function_name_completion_matches_anywhere(completer,
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
assert set(result) == set([
- Completion(text='custom_func1', start_position=-2, display_meta='function'),
- Completion(text='custom_func2', start_position=-2, display_meta='function')])
+ function('custom_func1', -2),
+ function('custom_func2', -2)])
def test_suggested_column_names_from_visible_table(completer, complete_event):
@@ -170,16 +138,16 @@ def test_suggested_column_names_from_visible_table(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column'),
- Completion(text='custom_func1', start_position=0, display_meta='function'),
- Completion(text='custom_func2', start_position=0, display_meta='function'),
- Completion(text='set_returning_func', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name'),
+ function('custom_func1'),
+ function('custom_func2'),
+ function('set_returning_func')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
@@ -197,11 +165,11 @@ def test_suggested_column_names_in_function(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event)
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column')])
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name')])
def test_suggested_column_names_with_table_dot(completer, complete_event):
"""
@@ -216,11 +184,11 @@ def test_suggested_column_names_with_table_dot(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column')])
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name')])
def test_suggested_column_names_with_alias(completer, complete_event):
"""
@@ -235,11 +203,11 @@ def test_suggested_column_names_with_alias(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column')])
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name')])
def test_suggested_multiple_column_names(completer, complete_event):
"""
@@ -255,16 +223,16 @@ def test_suggested_multiple_column_names(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column'),
- Completion(text='custom_func1', start_position=0, display_meta='function'),
- Completion(text='custom_func2', start_position=0, display_meta='function'),
- Completion(text='set_returning_func', start_position=0, display_meta='function')] +
- list(map(lambda f: Completion(f, display_meta='function'), completer.functions)) +
- list(map(lambda x: Completion(x, display_meta='keyword'), completer.keywords))
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name'),
+ function('custom_func1'),
+ function('custom_func2'),
+ function('set_returning_func')] +
+ list(testdata.builtin_functions() +
+ testdata.keywords())
)
def test_suggested_multiple_column_names_with_alias(completer, complete_event):
@@ -281,11 +249,11 @@ def test_suggested_multiple_column_names_with_alias(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column')])
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name')])
def test_suggested_multiple_column_names_with_dot(completer, complete_event):
"""
@@ -301,11 +269,11 @@ def test_suggested_multiple_column_names_with_dot(completer, complete_event):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='id', start_position=0, display_meta='column'),
- Completion(text='parentid', start_position=0, display_meta='column'),
- Completion(text='email', start_position=0, display_meta='column'),
- Completion(text='first_name', start_position=0, display_meta='column'),
- Completion(text='last_name', start_position=0, display_meta='column')])
+ column('id'),
+ column('parentid'),
+ column('email'),
+ column('first_name'),
+ column('last_name')])
def test_suggest_columns_after_three_way_join(completer, complete_event):
@@ -315,7 +283,7 @@ def test_suggest_columns_after_three_way_join(completer, complete_event):
position = len(text)
result = completer.get_completions(
Document(text=text, cursor_position=position), complete_event)
- assert (Completion(text='id', start_position=0, display_meta='column') in
+ assert (column('id') in
set(result))
@pytest.mark.parametrize('text', [
@@ -338,9 +306,9 @@ def test_suggested_join_conditions(completer, complete_event, text):
Document(text=text, cursor_position=position),
complete_event))
assert set(result) == set([
- Completion(text='u', start_position=0, display_meta='table alias'),
- Completion(text='u2', start_position=0, display_meta='table alias'),
- Completion(text='u2.userid = u.id', start_position=0, display_meta='fk join')])
+ alias('u'),
+ alias('u2'),
+ fk_join('u2.userid = u.id')])
@pytest.mark.parametrize('text', [
'''SELECT *
@@ -356,14 +324,14 @@ def test_suggested_join_conditions_with_same_table_twice(completer, complete_eve
Document(text=text, cursor_position=position),
complete_event)
assert result == [
- Completion(text='u2.userid = u.id', start_position=0, display_meta='fk join'),
- Completion(text='u2.u