예제 #1
0
 def test_leading_parenthesis():
     sqls = [
         '(',
     ]
     for sql in sqls:
         # No assertion for now; just make sure it doesn't crash
         suggest_type(sql, sql)
예제 #2
0
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', last_keyword='SELECT')
예제 #3
0
    def test_3_statements_2nd_current(self):
        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) == self.cols_etc('b', last_keyword='SELECT')
예제 #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'),
    ])
예제 #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')
    ])
예제 #6
0
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'),
    ])
예제 #7
0
def test_suggests_tables_views_and_schemas(expression):
    suggestions = suggest_type(expression, expression)
    assert set(suggestions) == set([
        Table(schema=None),
        View(schema=None),
        Schema(),
    ])
예제 #8
0
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(),
    ])
예제 #9
0
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)
예제 #10
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))
예제 #11
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)
            ])
    def get_completions(self, document, complete_event, smart_completion=None):
        # pylint: disable=arguments-differ

        word_before_cursor = document.get_word_before_cursor(WORD=True)

        if smart_completion is None:
            smart_completion = self.smart_completion

        # If smart_completion is off then return nothing.
        # Our notion of smart completion is all or none unlike PGCLI and MyCLI.
        matches = []
        if not smart_completion:
            return matches

        suggestions = suggest_type(document.text, document.text_before_cursor)

        for suggestion in suggestions:
            suggestion_type = type(suggestion)
            _logger.debug('Suggestion type: %r', suggestion_type)

            # Map suggestion type to method
            # e.g. 'table' -> self.get_table_matches
            matcher = self.suggestion_matchers[suggestion_type]
            matches.extend(matcher(self, suggestion, word_before_cursor))

        # Sort matches so highest priorities are first
        matches = sorted(matches,
                         key=operator.attrgetter('priority'),
                         reverse=True)

        return [m.completion for m in matches]
예제 #13
0
 def test_suggest_tables_views_schemas_and_functions():
     expressions = ['SELECT * FROM ']
     for expression in expressions:
         suggestions = suggest_type(expression, expression)
         assert set(suggestions) == set(
             [FromClauseItem(schema=None),
              Schema()])
예제 #14
0
 def test_sub_select_table_name_completion():
     expression = 'SELECT * FROM (SELECT * FROM '
     suggestion = suggest_type(expression, expression)
     assert set(suggestion) == set([
         FromClauseItem(schema=None),
         Schema(),
     ])
예제 #15
0
 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(),
     ])
예제 #16
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'),
     ])
예제 #17
0
    def test_distinct_and_order_by_suggestions_with_alias_given():
        test_args = [
            (
                'SELECT DISTINCT x. FROM tbl x JOIN tbl1 y',
                'SELECT DISTINCT x.'
            ),
            (
                'SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.',
                'SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.'
            )
        ]
        for arg in test_args:
            text = arg[0]
            text_before = arg[1]

            suggestions = suggest_type(text, text_before)
            assert set(suggestions) == set([
                Column(
                    table_refs=(TableReference(None, 'tbl', 'x', False),),
                    local_tables=(),
                    qualifiable=False
                ),
                Table(schema='x'),
                View(schema='x'),
                Function(schema='x'),
            ])
예제 #18
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'),
    ])
예제 #19
0
 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(),
     ])
예제 #20
0
def test_suggest_after_join_with_two_tables(expression):
    suggestions = suggest_type(expression, expression)
    tables = tuple([(None, 'foo', None, False), (None, 'bar', None, False)])
    assert set(suggestions) == set([
        FromClauseItem(schema=None, table_refs=tables),
        Join(tables, None),
        Schema(),
    ])
예제 #21
0
def test_suggest_after_join_with_one_table(expression):
    suggestions = suggest_type(expression, expression)
    tables = ((None, 'foo', None, False), )
    assert set(suggestions) == set([
        FromClauseItem(schema=None, table_refs=tables),
        Join(((None, 'foo', None, False), ), None),
        Schema(),
    ])
예제 #22
0
 def test_where_suggests_columns_functions_quoted_table(self):
     expressions = ['SELECT * FROM "tabl" WHERE ']
     for expression in expressions:
         expected = self.cols_etc('tabl',
                                  alias='"tabl"',
                                  last_keyword='WHERE')
         suggestions = suggest_type(expression, expression)
         assert expected == set(suggestions)
예제 #23
0
 def test_dot_suggests_cols_of_a_table_or_schema_qualified_table():
     suggestions = suggest_type('SELECT tabl. FROM tabl', 'SELECT tabl.')
     assert set(suggestions) == set([
         Column(table_refs=((None, 'tabl', None, False), )),
         Table(schema='tabl'),
         View(schema='tabl'),
         Function(schema='tabl'),
     ])
예제 #24
0
def test_dot_suggests_cols_of_an_alias_where(sql):
    suggestions = suggest_type(sql, sql)
    assert set(suggestions) == set([
        Table(schema='t1'),
        View(schema='t1'),
        Column(table_refs=((None, 'tabl1', 't1', False), )),
        Function(schema='t1'),
    ])
예제 #25
0
def test_join_alias_dot_suggests_cols2(sql):
    suggestion = suggest_type(sql, sql)
    assert set(suggestion) == set([
        Column(table_refs=((None, 'def', 'd', False), )),
        Table(schema='d'),
        View(schema='d'),
        Function(schema='d'),
    ])
예제 #26
0
 def test_suggest_qualified_aliasable_tables_and_views():
     expressions = ['UPDATE sch.']
     for expression in expressions:
         suggestions = suggest_type(expression, expression)
         assert set(suggestions) == set([
             Table(schema='sch'),
             View(schema='sch'),
         ])
예제 #27
0
 def test_suggest_qualified_tables_and_views():
     expressions = ['INSERT INTO sch.', 'COPY sch.', 'DESCRIBE sch.']
     for expression in expressions:
         suggestions = suggest_type(expression, expression)
         assert set(suggestions) == set([
             Table(schema='sch'),
             View(schema='sch'),
         ])
예제 #28
0
 def test_ignore_leading_double_quotes():
     sqls = [
         'select * from "',
         'select * from "foo',
     ]
     for sql in sqls:
         suggestions = suggest_type(sql, sql)
         assert FromClauseItem(schema=None) in set(suggestions)
예제 #29
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'),
    ])
예제 #30
0
def test_suggest_columns_after_multiple_joins():
    sql = '''select * from t1
            inner join t2 ON
              t1.id = t2.t1_id
            inner join t3 ON
              t2.id = t3.'''
    suggestions = suggest_type(sql, sql)
    assert Column(table_refs=((None, 't3', None, False), )) in set(suggestions)