Ejemplo n.º 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')
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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))
Ejemplo n.º 5
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)
Ejemplo n.º 6
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))
Ejemplo n.º 7
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))
Ejemplo n.º 8
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
    }