blob: c6853ae27399cf917f19d9f228d818ff64766a00 [file] [log] [blame]
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import pytest
import pyignite_dbapi
from tests.util import server_addresses_invalid, server_addresses_basic
@pytest.mark.parametrize('address', [server_addresses_basic, server_addresses_basic[0]])
def test_connection_success(address):
conn = pyignite_dbapi.connect(address=address, timeout=1)
assert conn is not None
conn.close()
@pytest.mark.parametrize('address', [server_addresses_basic, server_addresses_basic[0]])
def test_connection_get_cursor(address):
with pyignite_dbapi.connect(address=address, timeout=1) as conn:
assert conn is not None
cursor = conn.cursor()
assert cursor.connection is conn
cursor.close()
@pytest.mark.parametrize('address', [server_addresses_invalid, server_addresses_invalid[0]])
def test_connection_fail(address):
with pytest.raises(pyignite_dbapi.OperationalError) as err:
pyignite_dbapi.connect(address=address, timeout=1)
assert err.match('Failed to establish connection with the cluster.')
ERR_MSG_WRONG_TYPE = "Only a string or a list of strings are allowed in 'address' parameter"
ERR_MSG_EMPTY = "No addresses provided to connect"
@pytest.mark.parametrize('address,err_msg', [
(123, ERR_MSG_WRONG_TYPE),
([123], ERR_MSG_WRONG_TYPE),
([server_addresses_basic[0], 123], ERR_MSG_WRONG_TYPE),
([], ERR_MSG_EMPTY),
('', ERR_MSG_EMPTY),
([''], ERR_MSG_EMPTY),
])
def test_connection_wrong_arg(address, err_msg):
with pytest.raises(pyignite_dbapi.InterfaceError) as err:
pyignite_dbapi.connect(address=address, timeout=1)
assert err.match(err_msg)
@pytest.mark.parametrize("interval", [2.0, 20.0, 0.0001])
def test_heartbeat_enabled(table_name, drop_table_cleanup, interval):
row_count = 10
with pyignite_dbapi.connect(address=server_addresses_basic[0], heartbeat_interval=interval) as conn:
with conn.cursor() as cursor:
cursor.execute(f"create table {table_name}(id int primary key, data varchar)")
for key in range(row_count):
cursor.execute(f"insert into {table_name} values({key}, 'data-{key*2}')")
assert cursor.rowcount == 1
data_out = {}
for key in range(row_count):
cursor.execute(f"select id, data from {table_name} WHERE id = ?", [key])
data_out[key] = cursor.fetchone()
if len(data_out) == 5:
time.sleep(7)
assert len(data_out) == row_count
def test_heartbeat_disabled(table_name, drop_table_cleanup):
row_count = 10
with pyignite_dbapi.connect(address=server_addresses_basic[0], heartbeat_interval=0) as conn:
with conn.cursor() as cursor:
cursor.execute(f"create table {table_name}(id int primary key, data varchar)")
for key in range(row_count):
cursor.execute(f"insert into {table_name} values({key}, 'data-{key*2}')")
assert cursor.rowcount == 1
data_out = {}
with pytest.raises(pyignite_dbapi.OperationalError) as err:
for key in range(row_count):
cursor.execute(f"select id, data from {table_name} where id = ?", [key])
data_out[key] = cursor.fetchone()
if len(data_out) == 5:
time.sleep(7)
assert err.match("(Connection closed by the server|Can not send a message to the server due to connection error)")