Ejemplo n.º 1
0
 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'),
     ])
Ejemplo n.º 2
0
 def test_keyword_after_alter():
     sqls = [
         'ALTER ',
         'ALTER TABLE foo ALTER ',
     ]
     for sql in sqls:
         assert Keyword('ALTER') in set(suggest_type(sql, sql))
Ejemplo n.º 3
0
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'),
    ])
Ejemplo n.º 4
0
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'),
    ])
Ejemplo n.º 5
0
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')
    ])
Ejemplo n.º 6
0
    def test_distinct_and_order_by_suggestions_with_aliases():
        test_args = [(
            'SELECT DISTINCT FROM tbl x JOIN tbl1 y',
            'SELECT DISTINCT',
            'SELECT',
        ),
                     (
                         'SELECT * FROM tbl x JOIN tbl1 y ORDER BY ',
                         'SELECT * FROM tbl x JOIN tbl1 y ORDER BY ',
                         'ORDER BY',
                     )]
        for arg in test_args:
            text = arg[0]
            text_before = arg[1]
            last_keyword = arg[2]

            suggestions = suggest_type(text, text_before)
            assert set(suggestions) == set([
                Column(table_refs=(
                    TableReference(None, 'tbl', 'x', False),
                    TableReference(None, 'tbl1', 'y', False),
                ),
                       local_tables=(),
                       qualifiable=True),
                Function(schema=None),
                Keyword(last_keyword)
            ])
Ejemplo n.º 7
0
def test_sub_select_col_name_completion():
    suggestions = suggest_type('SELECT * FROM (SELECT  FROM abc',
                               'SELECT * FROM (SELECT ')
    assert set(suggestions) == set([
        Column(table_refs=((None, 'abc', None, False), ), qualifiable=True),
        Function(schema=None),
        Keyword('SELECT'),
    ])
Ejemplo n.º 8
0
 def test_sub_select_partial_text_suggests_keyword():
     expressions = [
         'SELECT * FROM (S',
         'SELECT * FROM foo WHERE EXISTS (S',
         'SELECT * FROM foo WHERE bar AND NOT EXISTS (S',
     ]
     for expression in expressions:
         suggestion = suggest_type(expression, expression)
         assert suggestion == (Keyword(), )
Ejemplo n.º 9
0
 def test_alias_suggests_keywords():
     texts = [
         'SELECT foo ', 'SELECT foo FROM bar ', 'SELECT foo AS bar ',
         'SELECT foo bar ', 'SELECT * FROM foo AS bar ',
         'SELECT * FROM foo bar ', 'SELECT foo FROM (SELECT bar '
     ]
     for text in texts:
         suggestions = suggest_type(text, text)
         assert suggestions == (Keyword(), )
Ejemplo n.º 10
0
 def test_named_query_completion():
     test_args = [('\\ns abc SELECT ', 'SELECT ', [
         Column(table_refs=(), qualifiable=True),
         Function(schema=None),
         Keyword('SELECT')
     ]), ('\\ns abc SELECT foo ', 'SELECT foo ', (Keyword(), )),
                  ('\\ns abc SELECT t1. FROM tabl1 t1', 'SELECT t1.', [
                      Table(schema='t1'),
                      View(schema='t1'),
                      Column(table_refs=((None, 'tabl1', 't1', False), )),
                      Function(schema='t1')
                  ])]
     for arg in test_args:
         text = arg[0]
         before = arg[1]
         expected = arg[2]
         suggestions = suggest_type(text, before)
         assert set(expected) == set(suggestions)
Ejemplo n.º 11
0
 def test_distinct_suggests_cols():
     texts = ['SELECT DISTINCT ', 'INSERT INTO foo SELECT DISTINCT ']
     for text in texts:
         suggestions = suggest_type(text, text)
         assert set(suggestions) == set([
             Column(table_refs=(), local_tables=(), qualifiable=True),
             Function(schema=None),
             Keyword('DISTINCT')
         ])
Ejemplo n.º 12
0
 def cols_etc(self, table, schema=None, alias=None, is_function=False, parent=None, \
             last_keyword=None):
     """Returns the expected select-clause suggestions for a single-table
     select."""
     return set([
         Column(table_refs=(TableReference(schema, table, alias, is_function),), \
             qualifiable=True),
         Function(schema=parent),
         Keyword(last_keyword)
     ])
Ejemplo n.º 13
0
    def test_statements_in_function_body():
        texts = [ \
'''
CREATE OR REPLACE FUNCTION func() RETURNS setof int AS $$
SELECT  FROM foo;
SELECT 2 FROM bar;
$$ language sql;
    ''', \
    '''create function func2(int, varchar)
RETURNS text
language sql AS
$func$
SELECT 2 FROM bar;
SELECT  FROM foo;
$func$
    ''',
        '''
CREATE OR REPLACE FUNCTION func() RETURNS setof int AS $func$
SELECT 3 FROM foo;
SELECT 2 FROM bar;
$$ language sql;
create function func2(int, varchar)
RETURNS text
language sql AS
$func$
SELECT 2 FROM bar;
SELECT  FROM foo;
$func$
    ''',
        '''
SELECT * FROM baz;
CREATE OR REPLACE FUNCTION func() RETURNS setof int AS $func$
SELECT  FROM foo;
SELECT 2 FROM bar;
$$ language sql;
create function func2(int, varchar)
RETURNS text
language sql AS
$func$
SELECT 3 FROM bar;
SELECT  FROM foo;
$func$
SELECT * FROM qux;
    ''' \
        ]
        for text in texts:
            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'),
            ])
Ejemplo n.º 14
0
def test_distinct_and_order_by_suggestions_with_aliases(
        text, text_before, last_keyword):
    suggestions = suggest_type(text, text_before)
    assert set(suggestions) == set([
        Column(table_refs=(
            TableReference(None, 'tbl', 'x', False),
            TableReference(None, 'tbl1', 'y', False),
        ),
               local_tables=(),
               qualifiable=True),
        Function(schema=None),
        Keyword(last_keyword)
    ])
Ejemplo n.º 15
0
    def test_2_statements_2nd_current():
        suggestions = suggest_type('select * from a; select * from ', \
            'select * from a; select * from ')
        assert set(suggestions) == set([
            FromClauseItem(schema=None),
            Schema(),
        ])

        suggestions = suggest_type('select * from a; select  from b', \
            'select * from a; select ')
        assert set(suggestions) == set([
            Column(table_refs=((None, 'b', None, False), ), qualifiable=True),
            Function(schema=None),
            Keyword('SELECT')
        ])

        # Should work even if first statement is invalid
        suggestions = suggest_type('select * from; select * from ', \
            'select * from; select * from ')
        assert set(suggestions) == set([
            FromClauseItem(schema=None),
            Schema(),
        ])
Ejemplo n.º 16
0
def test_statements_with_cursor_before_function_body(text):
    suggestions = suggest_type(text, '')
    assert set(suggestions) == set([
        Keyword(),
    ])
Ejemplo n.º 17
0
 def test_invalid_sql():
     # issue 317
     text = 'selt *'
     suggestions = suggest_type(text, text)
     assert suggestions == (Keyword(), )
Ejemplo n.º 18
0
def test_sub_select_partial_text_suggests_keyword(expression):
    suggestion = suggest_type(expression, expression)
    assert suggestion == (Keyword(), )
Ejemplo n.º 19
0
 def test_specials_included_for_initial_completion():
     initial_texts = ('', '    ', '\t \t', '\n')
     for initial_text in initial_texts:
         suggestions = suggest_type(initial_text, initial_text)
         assert set(suggestions) == set([Keyword(), Special()])
Ejemplo n.º 20
0
 def test_statements_with_cursor_before_function_body(self):
     texts = self.functions
     for text in texts:
         suggestions = suggest_type(text, '')
         assert set(suggestions) == set([Keyword(), Special()])
Ejemplo n.º 21
0
 def test_statements_with_cursor_after_function_body(self):
     texts = self.functions
     for text in texts:
         suggestions = suggest_type(text, text[:text.find('; ') + 1])
         assert set(suggestions) == set([Keyword(), Special()])
Ejemplo n.º 22
0
def test_specials_included_for_initial_completion(initial_text):
    suggestions = suggest_type(initial_text, initial_text)

    assert set(suggestions) == \
        set([Keyword(), Special()])
Ejemplo n.º 23
0
def test_alias_suggests_keywords(text):
    suggestions = suggest_type(text, text)
    assert suggestions == (Keyword(), )
Ejemplo n.º 24
0
@pytest.mark.parametrize('text', [
    'SELECT * FROM foo where created > now() - ',
    'select * from foo where bar ',
])
def test_suggest_where_keyword(text):
    # https://github.com/dbcli/mycli/issues/135
    suggestions = suggest_type(text, text)
    assert set(suggestions) == cols_etc('foo', last_keyword='WHERE')


@pytest.mark.parametrize(
    'text, before, expected',
    [('\\ns abc SELECT ', 'SELECT ', [
        Column(table_refs=(), qualifiable=True),
        Function(schema=None),
        Keyword('SELECT')
    ]), ('\\ns abc SELECT foo ', 'SELECT foo ', (Keyword(), )),
     ('\\ns abc SELECT t1. FROM tabl1 t1', 'SELECT t1.', [
         Table(schema='t1'),
         View(schema='t1'),
         Column(table_refs=((None, 'tabl1', 't1', False), )),
         Function(schema='t1')
     ])])
def test_named_query_completion(text, before, expected):
    suggestions = suggest_type(text, before)
    assert set(expected) == set(suggestions)


def test_select_suggests_fields_from_function():
    suggestions = suggest_type('SELECT  FROM func()', 'SELECT ')
    assert set(suggestions) == cols_etc('func',
Ejemplo n.º 25
0
def test_statements_with_cursor_after_function_body(text):
    suggestions = suggest_type(text, text[:text.find('; ') + 1])
    assert set(suggestions) == set([
        Keyword(),
    ])
Ejemplo n.º 26
0
def test_keyword_after_alter(sql):
    assert Keyword('ALTER') in set(suggest_type(sql, sql))