# Path: pgcli/packages/sqlcompletion.py
# class SqlStatement:
#     def __init__(self, full_text, text_before_cursor):
#     def is_insert(self):
#     def get_tables(self, scope="full"):
#     def get_previous_token(self, token):
#     def get_identifier_schema(self):
#     def reduce_to_prev_keyword(self, n_skip=0):
# def suggest_type(full_text, text_before_cursor):
# def _strip_named_query(txt):
# def _find_function_body(text):
# def _statement_from_function(full_text, text_before_cursor, statement):
# def _split_multiple_statements(full_text, text_before_cursor, parsed):
# def suggest_special(text):
# def suggest_based_on_last_token(token, stmt):
# def _suggest_expression(token_v, stmt):
# def identifies(id, ref):
# def _allow_join_condition(statement):
# def _allow_join(statement):
# SPECIALS_SUGGESTION = {
#     "dT": Datatype,
#     "df": Function,
#     "dt": Table,
#     "dv": View,
#     "sf": Function,
# }
# 
# Path: pgcli/packages/parseutils/tables.py
# def is_subselect(parsed):
# def _identifier_is_function(identifier):
# def extract_from_part(parsed, stop_at_punctuation=True):
# def extract_table_identifiers(token_stream, allow_functions=True):
#     def parse_identifier(item):
# def extract_tables(sql):


from pgcli.packages.sqlcompletion import (
    suggest_type,
    Special,
    Database,
    Schema,
    Table,
    Column,
    View,
    Keyword,
    FromClauseItem,
    Function,
    Datatype,
    Alias,
    JoinCondition,
    Join,
)
from pgcli.packages.parseutils.tables import TableReference
import pytest

@pytest.mark.xfail
def test_sub_select_multiple_col_name_completion():
    suggestions = suggest_type(
        "SELECT * FROM (SELECT a, FROM abc", "SELECT * FROM (SELECT a, "
    )
    assert set(suggestions) == cols_etc("abc")


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) == {
        Column(table_refs=((None, "tabl", "t", False),)),
        Table(schema="t"),
        View(schema="t"),
        Function(schema="t"),
    }


@pytest.mark.parametrize("join_type", ("", "INNER", "LEFT", "RIGHT OUTER"))
@pytest.mark.parametrize("tbl_alias", ("", "foo"))
def test_join_suggests_tables_and_schemas(tbl_alias, join_type):
    text = f"SELECT * FROM abc {tbl_alias} {join_type} JOIN "
    suggestion = suggest_type(text, text)
    tbls = tuple([(None, "abc", tbl_alias or None, False)])
    assert set(suggestion) == {
        FromClauseItem(schema=None, table_refs=tbls),
        Schema(),

# Path: pgcli/packages/sqlcompletion.py
# class SqlStatement:
#     def __init__(self, full_text, text_before_cursor):
#     def is_insert(self):
#     def get_tables(self, scope="full"):
#     def get_previous_token(self, token):
#     def get_identifier_schema(self):
#     def reduce_to_prev_keyword(self, n_skip=0):
# def suggest_type(full_text, text_before_cursor):
# def _strip_named_query(txt):
# def _find_function_body(text):
# def _statement_from_function(full_text, text_before_cursor, statement):
# def _split_multiple_statements(full_text, text_before_cursor, parsed):
# def suggest_special(text):
# def suggest_based_on_last_token(token, stmt):
# def _suggest_expression(token_v, stmt):
# def identifies(id, ref):
# def _allow_join_condition(statement):
# def _allow_join(statement):
# SPECIALS_SUGGESTION = {
#     "dT": Datatype,
#     "df": Function,
#     "dt": Table,
#     "dv": View,
#     "sf": Function,
# }
# 
# Path: pgcli/packages/parseutils/tables.py
# def is_subselect(parsed):
# def _identifier_is_function(identifier):
# def extract_from_part(parsed, stop_at_punctuation=True):
# def extract_table_identifiers(token_stream, allow_functions=True):
#     def parse_identifier(item):
# def extract_tables(sql):


# Path: tests/test_sqlcompletion.py
from pgcli.packages.sqlcompletion import (
    suggest_type,
    Special,
    Database,
    Schema,
    Table,
    Column,
    View,
    Keyword,
    FromClauseItem,
    Function,
    Datatype,
    Alias,
    JoinCondition,
    Join,
)
from pgcli.packages.parseutils.tables import TableReference
import pytest


@pytest.mark.xfail
def test_sub_select_multiple_col_name_completion():
    suggestions = suggest_type(
        "SELECT * FROM (SELECT a, FROM abc", "SELECT * FROM (SELECT a, "
    )
    assert set(suggestions) == cols_etc("abc")


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) == {
        Column(table_refs=((None, "tabl", "t", False),)),
        Table(schema="t"),
        View(schema="t"),
        Function(schema="t"),
    }


@pytest.mark.parametrize("join_type", ("", "INNER", "LEFT", "RIGHT OUTER"))
@pytest.mark.parametrize("tbl_alias", ("", "foo"))
def test_join_suggests_tables_and_schemas(tbl_alias, join_type):
    text = f"SELECT * FROM abc {tbl_alias} {join_type} JOIN "
    suggestion = suggest_type(text, text)
    tbls = tuple([(None, "abc", tbl_alias or None, False)])
    assert set(suggestion) == {
        FromClauseItem(schema=None, table_refs=tbls),
        Schema(),

        Join(tbls, None),