def test_suggested_aliases_after_on(completer, text):
    position = len('SELECT u.name, o.id FROM users u JOIN orders o ON ')
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set([
        alias('u'),
        name_join('o.id = u.id'),
        name_join('o.email = u.email'),
        alias('o')])
def test_join_functions_on_suggests_columns_and_join_conditions(completer):
    text = '''SELECT * FROM set_returning_func() f1
              INNER JOIN set_returning_func() f2 ON f1.'''
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [name_join('y = f2.y'), name_join('x = f2.x')] +
        testdata.columns('set_returning_func', typ='functions')
    )
def test_suggested_tables_after_on(completer, text):
    position = len('SELECT users.name, orders.id FROM users JOIN orders ON ')
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set([
        name_join('orders.id = users.id'),
        name_join('orders.email = users.email'),
        alias('users'),
        alias('orders')
    ])
def test_suggestions_after_on(completer, text):
    position = len('SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON ')
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set([
        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_join_functions_on_suggests_columns_and_join_conditions(completer, complete_event):
    text = '''SELECT * FROM set_returning_func() f1
              INNER JOIN set_returning_func() f2 ON f1.'''
    pos = len(text)
    result = set(completer.get_completions(
        Document(text=text, cursor_position=pos), complete_event))
    assert set(result) == set([
         name_join('y = f2.y'),
         name_join('x = f2.x'),
         ] + testdata.columns('set_returning_func', typ='functions'))
def test_suggested_tables_after_on(completer, complete_event, text):
    position = len('SELECT users.name, orders.id FROM users JOIN orders ON ')
    result = set(completer.get_completions(
        Document(text=text, cursor_position=position),
        complete_event))
    assert set(result) == set([
        name_join('orders.id = users.id'),
        name_join('orders.email = users.email'),
        alias('users'),
        alias('orders')])
def test_suggestions_after_on(completer, complete_event, text):
    position = len('SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON ')
    result = set(completer.get_completions(
        Document(text=text, cursor_position=position),
        complete_event))
    assert set(result) == set([
        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_join_conditions_with_same_table_twice(completer, text):
    result = get_result(completer, text)
    assert result == [
        fk_join('u2.userid = u.id'),
        fk_join('u2.userid = users.id'),
        name_join('u2.userid = "Users".userid'),
        name_join('u2.username = "Users".username'),
        alias('u'),
        alias('u2'),
        alias('users'),
        alias('"Users"')
    ]
def test_suggested_join_conditions_with_same_table_twice(completer, complete_event, text):
    position = len(text)
    result = completer.get_completions(
        Document(text=text, cursor_position=position),
        complete_event)
    assert result == [
        fk_join('u2.userid = u.id'),
        fk_join('u2.userid = users.id'),
        name_join('u2.userid = "Users".userid'),
        name_join('u2.username = "Users".username'),
        alias('"Users"'),
        alias('u'),
        alias('u2'),
        alias('users')]
def test_suggested_join_conditions(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set([
        alias('users'),
        alias('shipments'),
        name_join('shipments.id = users.id'),
        fk_join('shipments.user_id = users.id')])
def test_suggested_join_conditions(completer, complete_event, text):
    position = len(text)
    result = set(completer.get_completions(
        Document(text=text, cursor_position=position),
        complete_event))
    assert set(result) == set([
        alias('users'),
        alias('shipments'),
        name_join('shipments.id = users.id'),
        fk_join('shipments.user_id = users.id')])