Beispiel #1
0
def test_drop_database_raises_operational_error():
    client = mock.Mock()
    client.rmdir.side_effect = EtcdKeyNotFound
    tree = SQLTree()
    tree.db = 'foo'
    with pytest.raises(OperationalError):
        drop_database(client, tree)
Beispiel #2
0
def test_insert_duplicate_raises(mock_get_pk_field):
    mock_get_pk_field.return_value = Column('id')

    etcd_client = mock.Mock()
    # etcd_client.read.side_effect =
    tree = SQLTree()

    # {"query_type": "INSERT",
    # "db": null,
    # "query":
    # "insert into bar(id, name) values(1, 'aaa')",
    # "table": "bar",
    # "expressions": [],
    # "success": true,
    # "fields": {"id": "1", "name": "aaa"},
    # "order": {"direction": "ASC", "by": null},
    # "limit": null,
    # "where": null,
    # "options": {}}
    tree.table = 'bar'
    tree.fields = {"id": "1", "name": "aaa"}

    with pytest.raises(IntegrityError):
        # noinspection PyTypeChecker
        insert(etcd_client, tree, 'foo')
Beispiel #3
0
def test_prepare_columns(expressions, cs):
    tree = SQLTree()
    tree.expressions = expressions
    actual_cs = prepare_columns(tree)
    print('Expected: %s' % cs)
    print('Actual: %s' % actual_cs)
    assert actual_cs == cs
Beispiel #4
0
def test_group_result_set(rs, row, expressions, result):
    tree = SQLTree()
    tree.expressions = expressions
    actual = group_result_set(etcdb_count, rs, row, tree, 0)
    print('Expected: %s' % result)
    print('Actual: %s' % actual)
    assert actual == result
Beispiel #5
0
def test_show_tables(content, rows):

    response = mock.Mock()
    response.content = content
    etcd_response = EtcdResult(response)

    etcd_client = mock.Mock()
    etcd_client.read.return_value = etcd_response

    cols = ColumnSet()
    cols.add(Column('Tables_in_foo'))

    # print(cols)

    rs = ResultSet(cols, rows)
    tree = SQLTree()
    tree.db = 'foo'
    tree.options['full'] = False
    # noinspection PyTypeChecker
    result = show_tables(etcd_client, tree, tree.db)

    print("Expected: \n%s" % rs)
    print("Actual: \n%s" % result)
    # noinspection PyTypeChecker
    assert result == rs
Beispiel #6
0
def test_drop_table_calls_rmdir(db_sql_tree, db_arg):
    tree = SQLTree()
    tree.db = db_sql_tree
    tree.table = 'bar'

    etcd_client = mock.Mock()

    drop_table(etcd_client, tree, db=db_arg)

    etcd_client.rmdir.assert_called_once_with('/foo/bar', recursive=True)
Beispiel #7
0
def test_show_tables(mock_client, db, payload, result, cursor):
    response = mock.MagicMock()
    response.content = payload
    etcd_result = EtcdResult(response)
    tree = SQLTree()
    tree.db = db
    tree.options['full'] = False
    mock_client.return_value = etcd_result
    assert cursor._execute_show_tables(tree) == result
    mock_client.assert_called_once_with('/%s' % db)
Beispiel #8
0
def test_drop_table_raises_on_unknown_db():
    tree = SQLTree()
    tree.db = 'foo'
    tree.table = 'bar'

    etcd_client = mock.Mock()
    etcd_client.read.side_effect = EtcdKeyNotFound

    # unknown database
    with pytest.raises(OperationalError):
        drop_table(etcd_client, tree)
Beispiel #9
0
def test_drop_table_if_exists():
    tree = SQLTree()
    tree.db = 'foo'
    tree.table = 'bar'
    tree.options['if_exists'] = True

    etcd_client = mock.Mock()
    etcd_client.rmdir.side_effect = EtcdKeyNotFound

    drop_table(etcd_client, tree, db='foo')

    etcd_client.rmdir.assert_called_once_with('/foo/bar', recursive=True)
Beispiel #10
0
def test_insert(mock_get_pk_field):
    mock_get_pk_field.return_value = Column('id')

    etcd_client = mock.Mock()
    etcd_client.read.side_effect = EtcdKeyNotFound
    tree = SQLTree()
    tree.table = 'bar'
    tree.fields = {"id": "1", "name": "aaa"}
    # noinspection PyTypeChecker
    insert(etcd_client, tree, 'foo')
    etcd_client.write.assert_called_once_with('/foo/bar/1',
                                              json.dumps(tree.fields))
Beispiel #11
0
def test_show_full_tables(mock_client, payload, result, etcdb_connection):
    cursor = etcdb_connection.cursor()
    assert cursor._db == 'foo'
    response = mock.MagicMock()
    response.content = payload
    etcd_result = EtcdResult(response)
    tree = SQLTree()
    tree.db = cursor._db
    tree.options['full'] = True
    mock_client.return_value = etcd_result
    assert cursor._execute_show_tables(tree) == result
    mock_client.assert_called_once_with('/foo')
Beispiel #12
0
def test_expr_in_simple_expr():
    tree = SQLTree()
    tree.expressions = [(('bool_primary', ('predicate',
                                           ('bit_expr',
                                            ('simple_expr',
                                             ('expr', ('bool_primary',
                                                       ('predicate',
                                                        ('bit_expr',
                                                         ('simple_expr',
                                                          ('literal',
                                                           '1')))))))))), 'a')]

    assert eval_expr(None, tree.expressions[0][0]) == ('1', '1')
Beispiel #13
0
def test_desc_table_raises():
    etcd_client = mock.Mock()
    etcd_client.read.side_effect = EtcdKeyNotFound
    tree = SQLTree()
    with pytest.raises(ProgrammingError):
        # noinspection PyTypeChecker
        desc_table(etcd_client, tree, 'foo')
Beispiel #14
0
def test_drop_table_raises_error():

    etcd_client = mock.Mock()

    # Database is not selected
    with pytest.raises(OperationalError):
        drop_table(etcd_client, SQLTree())
Beispiel #15
0
def test_dbl_createtbl(mock_client, etcdb_connection):
    mock_client.side_effect = EtcdNodeExist
    cursor = etcdb_connection.cursor()
    tree = SQLTree()

    with pytest.raises(ProgrammingError):
        cursor._execute_create_table(tree)
Beispiel #16
0
def test_create_table(mock_mkdir, mock_write, cursor):
    tree = SQLTree()
    tree.db = 'foo'
    tree.table = 'bar'
    tree.fields = {
        'id': {
            'type': 'INT',
            'options': {
                'nullable': False,
                'primary': True
            }
        }
    }
    cursor._execute_create_table(tree)
    mock_mkdir.assert_called_once_with('/foo/bar')
    mock_write.assert_called_once_with('/foo/bar/_fields',
                                       json.dumps(tree.fields))
Beispiel #17
0
def test_create():
    etcd_client = mock.Mock()
    tree = SQLTree()
    tree.fields = {
        u'id': {
            u'options': {
                u'primary': True,
                u'nullable': False
            },
            u'type': u'INT'
        }
    }
    tree.table = 'bar'
    create_table(etcd_client, tree, db='foo')

    etcd_client.mkdir.assert_called_once_with('/foo/bar')
    etcd_client.write.assert_called_once_with('/foo/bar/_fields',
                                              json.dumps(tree.fields))
Beispiel #18
0
def test_get_pks_with_limit(mock_get_pk, mock_read, payload, limit, result,
                            cursor):
    response = mock.MagicMock()
    response.content = payload
    etcd_result = EtcdResult(response)
    mock_read.return_value = etcd_result
    tree = SQLTree()
    tree.limit = limit

    mock_get_pk.return_value = {
        'id': {
            'type': 'INT',
            'options': {
                'nullable': False,
                'primary': True
            }
        }
    }
    assert cursor._get_pks('foo', 'bar', tree=tree) == result
Beispiel #19
0
def test_create_raises_on_db_doesnt_exist():
    etcd_client = mock.Mock()
    etcd_client.read.side_effect = EtcdKeyNotFound
    tree = SQLTree()
    tree.fields = {
        u'id': {
            u'options': {
                u'primary': True,
                u'nullable': False
            },
            u'type': u'INT'
        }
    }
    tree.table = 'bar'
    with pytest.raises(OperationalError):
        create_table(etcd_client, tree, db='foo')

    etcd_client.read.assert_called_once_with('/foo')

    tree_values = {
        u'db': None,
        u'expressions': [],
        u'fields': {
            u'id': {
                u'options': {
                    u'nullable': True
                },
                u'type': u'INT'
            }
        },
        u'limit': None,
        u'options': {},
        u'order': {
            u'by': None,
            u'direction': u'ASC'
        },
        u'query': u'create table bar (id int)',
        u'query_type': u'CREATE_TABLE',
        u'success': True,
        u'table': u'bar',
        u'where': None
    }
Beispiel #20
0
def test_create_table_raises_error():

    etcd_client = mock.Mock()

    # Database is not selected
    with pytest.raises(OperationalError):
        create_table(etcd_client, SQLTree())

    # primary key must be defined
    tree = SQLTree()
    tree.fields = {u'id': {u'options': {u'nullable': True}, u'type': u'INT'}}
    with pytest.raises(ProgrammingError):
        create_table(etcd_client, tree, db='foo')

    # primary key must be not NULL-able

    tree.fields = {
        u'id': {
            u'options': {
                u'primary': True,
                u'nullable': True
            },
            u'type': u'INT'
        }
    }
    with pytest.raises(ProgrammingError):
        create_table(etcd_client, tree, db='foo')

    # table exists

    etcd_client.mkdir.side_effect = EtcdNodeExist
    tree.fields = {
        u'id': {
            u'options': {
                u'primary': True,
                u'nullable': False
            },
            u'type': u'INT'
        }
    }
    with pytest.raises(OperationalError):
        create_table(etcd_client, tree, db='foo')
Beispiel #21
0
def test_eval_row(cs, row, expressions, result):
    tree = SQLTree()
    tree.expressions = expressions

    assert eval_row(cs, row, tree) == result
Beispiel #22
0
def test_drop_database():
    client = mock.Mock()
    tree = SQLTree()
    tree.db = 'foo'
    drop_database(client, tree)
    client.rmdir.assert_called_once_with('/foo', recursive=True)
Beispiel #23
0
def test_create_table_raises_exception_if_no_db(mock_client, etcdb_connection):
    cursor = etcdb_connection.cursor()
    cursor._db = None
    tree = SQLTree()
    with pytest.raises(OperationalError):
        cursor._execute_create_table(tree)
Beispiel #24
0
import logging

import ply.yacc as yacc
import lexer
from etcdb.sqlparser.sql_tree import SQLTree

tokens = lexer.tokens
_parse_tree = SQLTree()

precedence = (
    ('left', 'AND', 'OR'),
    ('right', 'UNOT'),
)



def p_statement(p):
    """statement : select_statement
        | show_tables_statement
        | create_table_statement
        | create_database_statement
        | show_databases_statement
        | use_database_statement
        | commit_statement
        | set_statement
        | insert_statement
        | delete_statement
        | drop_database_statement
        | drop_table_statement
        | desc_table_statement
        | update_table_statement