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"),
            ]
        )
    )
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"),
            ]
        )
    )
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"),
            ]
        )
    )
def test_outer_table_reference_in_exists_subquery_suggests_columns():
    q = "SELECT * FROM foo f WHERE EXISTS (SELECT 1 FROM bar WHERE f."
    suggestions = suggest_type(q, q)
    assert (
        set(suggestions)
        == set(
            [
                Column(table_refs=((None, "foo", "f", False),)),
                Table(schema="f"),
                View(schema="f"),
                Function(schema="f"),
            ]
        )
    )
def test_join_alias_dot_suggests_cols1(sql):
    suggestions = suggest_type(sql, sql)
    tables = ((None, "abc", "a", False), (None, "def", "d", False))
    assert (
        set(suggestions)
        == set(
            [
                Column(table_refs=((None, "abc", "a", False),)),
                Table(schema="a"),
                View(schema="a"),
                Function(schema="a"),
                JoinCondition(table_refs=tables, parent=(None, "abc", "a", False)),
            ]
        )
    )
def test_sub_select_dot_col_name_completion():
    suggestions = suggest_type(
        "SELECT * FROM (SELECT t. FROM tabl t", "SELECT * FROM (SELECT t."
    )
    assert (
        set(suggestions)
        == set(
            [
                Column(table_refs=((None, "tabl", "t", False),)),
                Table(schema="t"),
                View(schema="t"),
                Function(schema="t"),
            ]
        )
    )
def test_dot_col_comma_suggests_cols_or_schema_qualified_table():
    suggestions = suggest_type(
        "SELECT t1.a, t2. FROM tabl1 t1, tabl2 t2", "SELECT t1.a, t2."
    )
    assert (
        set(suggestions)
        == set(
            [
                Column(table_refs=((None, "tabl2", "t2", False),)),
                Table(schema="t2"),
                View(schema="t2"),
                Function(schema="t2"),
            ]
        )
    )
def test_distinct_and_order_by_suggestions_with_alias_given(text, text_before):
    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"),
            ]
        )
    )
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_suggest_qualified_aliasable_tables_and_views(expression):
    suggestions = suggest_type(expression, expression)
    assert set(suggestions) == set([Table(schema="sch"), View(schema="sch")])
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_schema_scoped_views_tables_suggested_after_slash_d_schema():
    sql = "\\d abc."
    suggestions = suggest_type(sql, sql)
    expected = (Table(schema='abc'), View(schema='abc'))
    assert expected == suggestions
def test_schemas_views_tables_suggested_after_slash_d_arg_with_wildcards():
    sql = "\\d abc??"
    suggestions = suggest_type(sql, sql)
    expected = (Schema(), Table(schema=None), View(schema=None))
    assert expected == suggestions
def test_schemas_views_tables_suggested_after_slash_d():
    sql = "\\d "
    suggestions = suggest_type(sql, sql)
    expected = (Schema(), Table(schema=None), View(schema=None))
    assert expected == suggestions
        (
            "\\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", is_function=True, last_keyword="SELECT")