def test_2_statements_2nd_current(): suggestions = suggest_type('select * from a; select * from ', 'select * from a; select * from ') assert set(suggestions) == set([ Table(schema=None), View(schema=None), Function(schema=None, filter='for_from_clause'), Schema(), ]) suggestions = suggest_type('select * from a; select from b', 'select * from a; select ') assert set(suggestions) == set([ Column(tables=((None, 'b', None, False),)), Function(schema=None), Keyword() ]) # Should work even if first statement is invalid suggestions = suggest_type('select * from; select * from ', 'select * from; select * from ') assert set(suggestions) == set([ Table(schema=None), View(schema=None), Function(schema=None, filter='for_from_clause'), Schema(), ])
def test_df_suggests_schema_or_function(): suggestions = suggest_type('\\df xxx', '\\df xxx') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'function', 'schema': []}, {'type': 'schema'}]) suggestions = suggest_type('\\df myschema.xxx', '\\df myschema.xxx') assert suggestions == [{'type': 'function', 'schema': 'myschema'}]
def test_2_statements_2nd_current(): suggestions = suggest_type('select * from a; select * from ', 'select * from a; select * from ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'view', 'schema': []}, {'type': 'function', 'schema': [], 'filter': 'is_set_returning'}, {'type': 'schema'}]) suggestions = suggest_type('select * from a; select from b', 'select * from a; select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'b', None, False)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'} ]) # Should work even if first statement is invalid suggestions = suggest_type('select * from; select * from ', 'select * from; select * from ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'view', 'schema': []}, {'type': 'function', 'schema': [], 'filter': 'is_set_returning'}, {'type': 'schema'}])
def test_d_dot_suggests_schema_qualified_tables_or_views(): suggestions = suggest_type('\d myschema.', '\d myschema.') assert suggestions == [{'type': 'table', 'schema': 'myschema'}, {'type': 'view', 'schema': 'myschema'}] suggestions = suggest_type('\d myschema.xxx', '\d myschema.xxx') assert suggestions == [{'type': 'table', 'schema': 'myschema'}, {'type': 'view', 'schema': 'myschema'}]
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_df_suggests_schema_or_function(): suggestions = suggest_type('\\df xxx', '\\df xxx') assert set(suggestions) == set([ Function(schema=None, usage='special'), Schema(), ]) suggestions = suggest_type('\\df myschema.xxx', '\\df myschema.xxx') assert suggestions == (Function(schema='myschema', usage='special'),)
def test_3_statements_2nd_current(): suggestions = suggest_type('select * from a; select * from ; select * from c', 'select * from a; select * from ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'schema'}]) suggestions = suggest_type('select * from a; select from b; select * from c', 'select * from a; select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'b', None)]}, {'type': 'function', 'schema': []}])
def test_2_statements_1st_current(): suggestions = suggest_type('select * from ; select * from b', 'select * from ') assert set(suggestions) == set([ FromClauseItem(schema=None), Schema(), ]) suggestions = suggest_type('select from a; select * from b', 'select ') assert set(suggestions) == cols_etc('a')
def test_3_statements_2nd_current(): suggestions = suggest_type('select * from a; select * from ; select * from c', 'select * from a; select * from ') assert set(suggestions) == set([ FromClauseItem(schema=None), Schema(), ]) suggestions = suggest_type('select * from a; select from b; select * from c', 'select * from a; select ') assert set(suggestions) == cols_etc('b', last_keyword='SELECT')
def test_d_dot_suggests_schema_qualified_tables_or_views(): suggestions = suggest_type('\d myschema.', '\d myschema.') assert set(suggestions) == set([ Table(schema='myschema'), View(schema='myschema'), ]) suggestions = suggest_type('\d myschema.xxx', '\d myschema.xxx') assert set(suggestions) == set([ Table(schema='myschema'), View(schema='myschema'), ])
def test_d_suggests_tables_views_and_schemas(): suggestions = suggest_type('\d ', '\d ') assert set(suggestions) == set([ Schema(), Table(schema=None), View(schema=None), ]) suggestions = suggest_type('\d xxx', '\d xxx') assert set(suggestions) == set([ Schema(), Table(schema=None), View(schema=None), ])
def test_2_statements_1st_current(): suggestions = suggest_type('select * from ; select * from b', 'select * from ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'view', 'schema': []}, {'type': 'schema'}]) suggestions = suggest_type('select from a; select * from b', 'select ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'a', None)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'} ])
def test_handle_unrecognized_kw_generously(): sql = 'SELECT * FROM sessions WHERE session = 1 AND ' suggestions = suggest_type(sql, sql) expected = Column(table_refs=((None, 'sessions', None, False),), qualifiable=True) assert expected in set(suggestions)
def test_sub_select_table_name_completion_with_outer_table(expression): suggestion = suggest_type(expression, expression) tbls = tuple([(None, 'foo', None, False)]) assert set(suggestion) == set([ FromClauseItem(schema=None, table_refs=tbls), Schema(), ])
def test_statements_in_function_body(text): suggestions = suggest_type(text, text[:text.find(' ') + 1]) assert set(suggestions) == set([ Column(table_refs=((None, 'foo', None, False),), qualifiable=True), Function(schema=None), Keyword('SELECT'), ])
def test_table_comma_suggests_tables_and_schemas(): suggestions = suggest_type('SELECT a, b FROM tbl1, ', 'SELECT a, b FROM tbl1, ') assert set(suggestions) == set([ FromClauseItem(schema=None), Schema(), ])
def test_select_suggests_cols_and_funcs(): suggestions = suggest_type('SELECT ', 'SELECT ') assert set(suggestions) == set([ Column(table_refs=(), qualifiable=True), Function(schema=None), Keyword('SELECT'), ])
def test_into_suggests_tables_and_schemas(): suggestion = suggest_type('INSERT INTO ', 'INSERT INTO ') assert set(suggestion) == set([ Table(schema=None), View(schema=None), Schema(), ])
def test_distinct_suggests_cols(text): suggestions = suggest_type(text, text) assert set(suggestions) == set([ Column(table_refs=(), local_tables=(), qualifiable=True), Function(schema=None), Keyword('DISTINCT') ])
def test_col_comma_suggests_cols(): suggestions = suggest_type('SELECT a, b, FROM tbl', 'SELECT a, b,') assert set(suggestions) == set([ Column(table_refs=((None, 'tbl', None, False),), qualifiable=True), Function(schema=None), Keyword('SELECT'), ])
def test_suggests_tables_views_and_schemas(expression): suggestions = suggest_type(expression, expression) assert set(suggestions) == set([ Table(schema=None), View(schema=None), Schema(), ])
def test_suggest_qualified_tables_views_functions_and_joins(expression): suggestions = suggest_type(expression, expression) tbls = tuple([(None, 'foo', None, False)]) assert set(suggestions) == set([ FromClauseItem(schema='sch', table_refs=tbls), Join(tbls, 'sch'), ])
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, False)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'} ])
def test_suggests_tables_views_and_schemas(expression): suggestions = suggest_type(expression, expression) assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'view', 'schema': []}, {'type': 'schema'}, ])
def test_select_suggests_fields_from_function(): suggestions = suggest_type('SELECT FROM func()', 'SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'func', None, True)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'} ])
def test_select_suggests_cols_and_funcs(): suggestions = suggest_type('SELECT ', 'SELECT ') assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': []}, {'type': 'function', 'schema': []}, {'type': 'keyword'}, ])
def test_where_in_suggests_columns(expression): suggestions = suggest_type(expression, expression) assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None, False)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'} ])
def test_sub_select_table_name_completion(expression): suggestion = suggest_type(expression, expression) assert sorted_dicts(suggestion) == sorted_dicts([ {'type': 'table', 'schema': []}, {'type': 'view', 'schema': []}, {'type': 'function', 'schema': [], 'filter': 'is_set_returning'}, {'type': 'schema'}])
def test_join_alias_dot_suggests_cols2(sql): suggestion = suggest_type(sql, sql) assert sorted_dicts(suggestion) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'def', 'd', False)]}, {'type': 'table', 'schema': 'd'}, {'type': 'view', 'schema': 'd'}, {'type': 'function', 'schema': 'd'}])
def test_where_equals_any_suggests_columns_or_keywords(): text = 'SELECT * FROM tabl WHERE foo = ANY(' suggestions = suggest_type(text, text) assert sorted_dicts(suggestions) == sorted_dicts([ {'type': 'column', 'tables': [(None, 'tabl', None, False)]}, {'type': 'function', 'schema': []}, {'type': 'keyword'}])
def test_slash_suggests_special(): suggestions = suggest_type('\\', '\\') assert set(suggestions) == set([Special()])
def test_leading_whitespace_ok(): cmd = '\\dn ' whitespace = ' ' suggestions = suggest_type(whitespace + cmd, whitespace + cmd) assert suggestions == suggest_type(cmd, cmd)
def test_schema_qualified_dT_suggests_datatypes(): text = '\\dT foo.' suggestions = suggest_type(text, text) assert suggestions == (Datatype(schema='foo'), )
def test_distinct_suggests_cols(): suggestions = suggest_type('SELECT DISTINCT ', 'SELECT DISTINCT ') assert suggestions == (Column(tables=()), )
def test_alias_suggests_keywords(text): suggestions = suggest_type(text, text) assert suggestions == (Keyword(), )
def test_ignore_leading_double_quotes(sql): suggestions = suggest_type(sql, sql) assert Table(schema=None) in set(suggestions)
def test_column_keyword_suggests_columns(sql): suggestions = suggest_type(sql, sql) assert set(suggestions) == set([ Column(tables=((None, 'foo', None, False), )), ])
def test_named_query_completion(text, before, expected): suggestions = suggest_type(text, before) assert set(expected) == set(suggestions)
def test_truncate_suggests_tables_and_schemas(): suggestions = suggest_type('TRUNCATE ', 'TRUNCATE ') assert set(suggestions) == set([Table(schema=None), Schema()])
def test_invalid_sql(): # issue 317 text = 'selt *' suggestions = suggest_type(text, text) assert suggestions == (Keyword(), )
def test_dn_suggests_schemata(): suggestions = suggest_type('\\dn ', '\\dn ') assert suggestions == (Schema(), ) suggestions = suggest_type('\\dn xxx', '\\dn xxx') assert suggestions == (Schema(), )
def test_alter_column_type_suggests_types(): q = 'ALTER TABLE foo ALTER COLUMN bar TYPE ' assert set(suggest_type(q, q)) == set( [Datatype(schema=None), Table(schema=None), Schema()])
def test_cast_operator_suggests_types(text): assert set(suggest_type(text, text)) == set( [Datatype(schema=None), Table(schema=None), Schema()])
def test_identifier_suggests_types_in_parentheses(text): assert set(suggest_type(text, text)) == set( [Datatype(schema=None), Table(schema=None), Schema()])
def test_handle_pre_completion_comma_gracefully(text): suggestions = suggest_type(text, text) assert iter(suggestions)
def test_cast_operator_suggests_schema_qualified_types(text): assert set(suggest_type(text, text)) == set( [Datatype(schema='bar'), Table(schema='bar')])
def test_specials_included_for_initial_completion(initial_text): suggestions = suggest_type(initial_text, initial_text) assert set(suggestions) == \ set([Keyword(), Special()])
def test_drop_schema_suggests_schemas(): sql = 'DROP SCHEMA ' assert suggest_type(sql, sql) == (Schema(), )
def test_join_using_suggests_common_columns(col_list): text = 'select * from abc inner join def using (' + col_list assert set(suggest_type(text, text)) == set([ Column(tables=((None, 'abc', None, False), (None, 'def', None, False)), require_last_table=True), ])
def test_drop_schema_qualified_table_suggests_only_tables(): text = 'DROP TABLE schema_name.table_name' suggestions = suggest_type(text, text) assert suggestions == (Table(schema='schema_name'), )
def test_sub_select_partial_text_suggests_keyword(expression): suggestion = suggest_type(expression, expression) assert suggestion == (Keyword(), )
def test_create_db_with_template(): suggestions = suggest_type('create database foo with template ', 'create database foo with template ') assert set(suggestions) == set((Database(), ))
def test_lparen_suggests_cols(): suggestion = suggest_type('SELECT MAX( FROM tbl', 'SELECT MAX(') assert suggestion == [{ 'type': 'column', 'tables': [(None, 'tbl', None, False)] }]
def test_on_suggests_aliases_right_side(sql): suggestions = suggest_type(sql, sql) assert suggestions == (Alias(aliases=( 'a', 'b', )), )
def test_insert_into_lparen_comma_suggests_cols(): suggestions = suggest_type('INSERT INTO abc (id,', 'INSERT INTO abc (id,') assert suggestions == [{'type': 'column', 'tables': [(None, 'abc', None)]}]
def test_insert_into_lparen_comma_suggests_cols(): suggestions = suggest_type('INSERT INTO abc (id,', 'INSERT INTO abc (id,') assert suggestions == (Column(tables=((None, 'abc', None, False), )), )
def test_suggest_qualified_tables_and_views(expression): suggestions = suggest_type(expression, expression) assert set(suggestions) == set([ Table(schema='sch'), View(schema='sch'), ])
def test_c_suggests_databases(command): suggestions = suggest_type(command, command) assert suggestions == (Database(), )
def test_lparen_suggests_cols(): suggestion = suggest_type('SELECT MAX( FROM tbl', 'SELECT MAX(') assert set(suggestion) == set( [Column(tables=((None, 'tbl', None, False), ))])
def test_truncate_suggests_qualified_tables(): suggestions = suggest_type('TRUNCATE sch.', 'TRUNCATE sch.') assert set(suggestions) == set([Table(schema='sch')])