Exemple #1
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')
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')
Exemple #3
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))
Exemple #4
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))
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))
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
    }