Esempio n. 1
0
def test_pkey_fields(tocase):
    assert ['a'] == wrappers.wrap(
        sqlparse.parse("create table t1 (a int primary key, b int)")
        [0]).pkey_fields()
    assert ['a', 'b'] == wrappers.wrap(
        sqlparse.parse("create table t1 (a int, b int, primary key (a,b))")
        [0]).pkey_fields()
Esempio n. 2
0
def test_tail(tocase):
    "directly test CreateTable.tail()"
    no, yes = [
        wrappers.wrap(parsed[0])
        for parsed in map(sqlparse.parse, tocase(PARTITION))
    ]
    assert no.tail() == []
    assert tocase('partition by range (a)') == ' '.join(map(str, yes.tail()))
Esempio n. 3
0
def test_column_parser():
    "directly test wrappers.Column.parse() cases"
    for type_ in ['int', 'jsonb', 'text', 'varchar(12)', 'int[]', 'text[]']:
        assert parse_column(type_) == wrappers.ParsedColumn(True, 'x', type_)
        assert parse_column(type_ + ' not null') == wrappers.ParsedColumn(
            True, 'x', type_, not_null=True)
        assert parse_column(type_ + ' unique') == wrappers.ParsedColumn(
            True, 'x', type_, unique=True)
        assert parse_column(type_ + ' default now()') == wrappers.ParsedColumn(
            True, 'x', type_, default='now()')
        assert parse_column(type_ + ' default 20') == wrappers.ParsedColumn(
            True, 'x', type_, default='20')
        assert parse_column(type_ +
                            " default 'hello'") == wrappers.ParsedColumn(
                                True, 'x', type_, default="'hello'")
        assert parse_column(
            type_ + ' not null unique default 20') == wrappers.ParsedColumn(
                True, 'x', type_, not_null=True, unique=True, default='20')
    # make sure it doesn't treat 'primary key' as a column
    assert [
        col.name for col in wrappers.wrap(
            sqlparse.parse("create table t1 (a int, b int, primary key (a,b))")
            [0]).columns()
    ] == ['a', 'b']
Esempio n. 4
0
def test_infinite_loop():
    "this was failing in an early version because 'bool' isn't supported in the parser and the test for 'custom type' was broken by 'default false'"
    wrapped = wrappers.wrap(
        sqlparse.parse("create table t (x bool default false)")[0])
    assert wrapped.columns()[0].parse().type == 'bool'
Esempio n. 5
0
def test_parse_extension(tocase):
    assert wrappers.wrap(
        sqlparse.parse(
            tocase('create extension if not exists "uuid-ossp"'))[0]) is None
Esempio n. 6
0
def test_parse_enum(tocase):
    wrapped = wrappers.wrap(sqlparse.parse(tocase(ENUMS[0]))[0])
    assert wrapped.name == 'letters'
    assert wrapped.values == ['a', 'b']
Esempio n. 7
0
def test_no_index_name(tocase):
    # nobody should be using unnamed indexes with automig, the tool doesn't know the created name, but this is testing for a crash
    wrapped = wrappers.wrap(
        sqlparse.parse(tocase('create index on t1 (a);'))[0])
    assert wrapped.index_name is None
Esempio n. 8
0
def parse_column(type_):
    stmt, = sqlparse.parse(f"create table t1 (x {type_})")
    col, = wrappers.wrap(stmt).columns()
    return col.parse()
Esempio n. 9
0
def test_parse_extension(tocase):
    ext = wrappers.wrap(sqlparse.parse(tocase(CREATE_EXT))[0])
    assert isinstance(ext, wrappers.CreateExtension)
    assert ext.name == '"uuid-ossp"'