def test_suggested_column_names_from_schema_qualifed_table( self, completer): result = result_set(completer, 'SELECT from custom.products', len('SELECT ')) self.assertSetEqual( result, set(TESTDATA.columns_functions_and_keywords('products', 'custom')))
def test_all_schema_objects(self, completer): text = ('SELECT * FROM ') result = result_set(completer, text) # Note: set comparison >= means is superset self.assertTrue(result >= set( [table(x) for x in ('orders', '"select"', 'custom.shipments')] + [function(x + '()') for x in ('func2', 'custom.func3')]))
def test_suggested_cased_always_qualified_column_names(self, completer): text = 'SELECT from users' position = len('SELECT ') cols = [column('users.' + c) for c in cased_users_col_names] result = result_set(completer, text, position) self.assertSetEqual(result, set(cased_funcs + cols + testdata.builtin_functions() + testdata.keywords()))
def test_keyword_after_alter(self, completer): text = 'ALTER TABLE users ALTER ' expected = PGCompletion('COLUMN', start_position=0, display_meta='keyword') completions = result_set(completer, text) self.assertTrue(expected in set(completions))
def test_suggested_joins_quoted_schema_qualified_table( self, completer, text): result = result_set(completer, text) self.assertSetEqual( result, set(testdata.schemas_and_from_clause_items() + [join('public.users ON users.id = "Users".userid')]))
def test_aliased_joins(self, completer, text): result = result_set(completer, text) self.assertSetEqual(result, set(testdata.schemas() + aliased_rels + [ join('"Users" U ON U.userid = Users.id'), join('users u ON u.id = Users.parentid'), join('users u ON u.parentid = Users.id'), ]))
def test_cased_joins(self, completer, text): result = result_set(completer, text) self.assertSetEqual(result, set([schema('PUBLIC')] + cased_rels + [ join('"Users" ON "Users".UserID = Users.ID'), join('Users Users2 ON Users2.ID = Users.PARENTID'), join('Users Users2 ON Users2.PARENTID = Users.ID'), ]))
def test_suggest_columns_from_set_returning_function(self, completer): result = result_set( completer, 'select from set_returning_func()', len('select ') ) self.assertSetEqual(result, set(testdata.columns_functions_and_keywords( 'set_returning_func', typ='functions' )))
def test_suggested_multiple_column_names_with_dot(self, completer): result = result_set( completer, 'SELECT users.id, users. from users u', len('SELECT users.id, users.') ) self.assertSetEqual(result, set(testdata.columns('users')))
def test_suggest_columns_from_aliased_set_returning_function(self, completer): result = result_set( completer, 'select f. from set_returning_func() f', len('select f.') ) self.assertSetEqual(result, set( testdata.columns('set_returning_func', typ='functions') ))
def test_join_functions_using_suggests_common_columns(self, completer): text = '''SELECT * FROM set_returning_func() f1 INNER JOIN set_returning_func() f2 USING (''' result = result_set(completer, text) self.assertSetEqual(result, set( testdata.columns('set_returning_func', typ='functions')) )
def test_suggested_aliases_after_on(self, completer, text): position = len('SELECT u.name, o.id FROM users u JOIN orders o ON ') result = result_set(completer, text, position) self.assertSetEqual(result, set([ alias('u'), name_join('o.id = u.id'), name_join('o.email = u.email'), alias('o')]))
def test_suggested_auto_qualified_column_names_two_tables(self, completer, text): position = text.index(' ') + 1 cols = [column('U.' + c.lower()) for c in cased_users_col_names] cols += [column('"Users".' + c.lower()) for c in cased_users2_col_names] result = result_set(completer, text, position) self.assertSetEqual(result, set( cols + testdata.functions_and_keywords()))
def test_user_function_name_completion_matches_anywhere(self, completer): result = result_set(completer, 'SELECT om') self.assertSetEqual(result, set([ function('custom_fun()', -2), function('_custom_fun()', -2), function('custom_func1()', -2), function('custom_func2()', -2)]))
def test_suggested_joins(self, completer, query, tbl): result = result_set(completer, query.format(tbl)) self.assertSetEqual( result, set(TESTDATA.schemas_and_from_clause_items() + [ join('custom.shipments ON shipments.user_id = {0}.id'.format( tbl)) ]))
def test_suggest_columns_from_aliased_set_returning_function( self, completer): result = result_set(completer, 'select f. from custom.set_returning_func() f', len('select f.')) self.assertSetEqual( result, set(TESTDATA.columns('set_returning_func', 'custom', 'functions')))
def test_join_functions_on_suggests_columns_and_join_conditions(self, completer): text = '''SELECT * FROM set_returning_func() f1 INNER JOIN set_returning_func() f2 ON f1.''' result = result_set(completer, text) self.assertSetEqual(result, set( [name_join('y = f2.y'), name_join('x = f2.x')] + testdata.columns('set_returning_func', typ='functions') ))
def test_builtin_function_name_completion(self, completer): result = result_set(completer, 'SELECT MA') self.assertSetEqual( result, set([ function('MAX', -2), keyword('MAXEXTENTS', -2), keyword('MATERIALIZED VIEW', -2) ]))
def test_schema_qualified_function_name(self, completer): text = 'SELECT custom.func' result = result_set(completer, text) self.assertSetEqual( result, set([ function('func3()', -len('func')), function('set_returning_func()', -len('func')) ]))
def test_suggested_joins(self, completer, text): result = result_set(completer, text) self.assertSetEqual(result, set( testdata.schemas_and_from_clause_items() + [ join('"Users" ON "Users".userid = Users.id'), join('users users2 ON users2.id = Users.parentid'), join('users users2 ON users2.parentid = Users.id'), ] ))
def test_user_function_name_completion(self, completer): result = result_set(completer, 'SELECT cu') self.assertSetEqual(result, set([ function('custom_fun()', -2), function('_custom_fun()', -2), function('custom_func1()', -2), function('custom_func2()', -2), keyword('CURRENT', -2), ]))
def test_suggested_join_conditions(self, completer, text): result = result_set(completer, text) self.assertSetEqual( result, set([ alias('users'), alias('shipments'), name_join('shipments.id = users.id'), fk_join('shipments.user_id = users.id') ]))
def test_suggested_tables_after_on(self, completer, text): position = len( 'SELECT users.name, orders.id FROM users JOIN orders ON ') result = result_set(completer, text, position) self.assertSetEqual(result, set([ name_join('orders.id = users.id'), name_join('orders.email = users.email'), alias('users'), alias('orders') ]))
def test_suggested_table_names_with_schema_dot2(self, completer, text, use_leading_double_quote): if use_leading_double_quote: text += '"' start_position = -1 else: start_position = 0 result = result_set(completer, text) self.assertSetEqual( result, set(TESTDATA.from_clause_items('Custom', start_position)))
def test_table_aliases(self, completer, text): result = result_set(completer, text) self.assertSetEqual( result, set(TESTDATA.schemas() + [ table('users u'), table('orders o' if text == 'SELECT * FROM ' else 'orders o2'), table('"select" s'), function('func1() f'), function('func2() f') ]))
def test_aliases_with_casing(self, completer, text): result = result_set(completer, text) self.assertSetEqual( result, set(CASED_SCHEMAS + [ table('users u'), table('Orders O' if text == 'SELECT * FROM ' else 'Orders O2'), table('"select" s'), function('Func1() F'), function('func2() f') ]))
def test_table_casing(self, completer, text): result = result_set(completer, text) self.assertSetEqual( result, set(CASED_SCHEMAS + [ table('users'), table('Orders'), table('"select"'), function('Func1()'), function('func2()') ]))
def test_set_schema(self, completer): text = ('SET SCHEMA ') result = result_set(completer, text) self.assertSetEqual( result, set([ schema(u"'blog'"), schema(u"'Custom'"), schema(u"'custom'"), schema(u"'public'") ]))
def test_suggest_columns_from_cte(self, completer): result = result_set( completer, 'WITH cte AS (SELECT foo, bar FROM baz) SELECT FROM cte', len('WITH cte AS (SELECT foo, bar FROM baz) SELECT ')) expected = ([ PGCompletion('foo', 0, display_meta='column'), PGCompletion('bar', 0, display_meta='column'), ] + testdata.functions_and_keywords()) self.assertSetEqual(result, set(expected))
def test_suggest_cte_names(self, completer): text = ''' WITH cte1 AS (SELECT a, b, c FROM foo), cte2 AS (SELECT d, e, f FROM bar) SELECT * FROM ''' result = result_set(completer, text) expected = set([ PGCompletion('cte1', 0, display_meta='table'), PGCompletion('cte2', 0, display_meta='table'), ]) self.assertTrue(expected <= result)