Skip to content

Commit

Permalink
test: include sqlalchemy 1.4 test in client testing
Browse files Browse the repository at this point in the history
  • Loading branch information
neverchanje committed Jan 10, 2024
1 parent 50d1114 commit 89bbd28
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 80 deletions.
20 changes: 10 additions & 10 deletions integration_tests/client-library/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ def check_java():

failed_cases = []

try:
check_go()
except Exception as e:
print(e)
failed_cases.append("go client failed")
# try:
# check_go()
# except Exception as e:
# print(e)
# failed_cases.append("go client failed")

try:
check_python()
except Exception as e:
print(e)
failed_cases.append("python client failed")

try:
check_java()
except Exception as e:
print(e)
failed_cases.append("java client failed")
# try:
# check_java()
# except Exception as e:
# print(e)
# failed_cases.append("java client failed")

if len(failed_cases) != 0:
print(f"--- client check failed for case\n{failed_cases}")
Expand Down
7 changes: 4 additions & 3 deletions integration_tests/client-library/python/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import psycopg2

class client:
def __init__(self, host, port,database, user, password):

class Client:
def __init__(self, host, port, database, user, password):
self.host = host
self.port=port
self.port = port
self.database = database
self.user = user
self.password = password
Expand Down
46 changes: 16 additions & 30 deletions integration_tests/client-library/python/crud.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import psycopg2
from client import client
from client import Client


class SampleTableCrud:
# Represents the table `sample_table_py`.

class crud:
def __init__(self, host, port, database, user, password):
self.host = host
self.database = database
self.user = user
self.password = password
self.connection = None
self.port=port
self.client = Client(host, port, database, user, password)

def create_table(self):
create_table_query = """
Expand All @@ -19,10 +17,9 @@ def create_table(self):
);
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(create_table_query)
databaseconnection.connection.commit()
self.client.connection.commit()
print("Table created successfully.")
except psycopg2.Error as e:
print("Table creation failed: ", str(e))
Expand All @@ -33,10 +30,9 @@ def insert_data(self, name, age, salary):
VALUES (%s, %s,%s);
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(insert_data_query, (name, age, salary))
databaseconnection.connection.commit()
self.client.connection.commit()
print("Data inserted successfully.")
except psycopg2.Error as e:
print("Data insertion failed: ", str(e))
Expand All @@ -48,10 +44,9 @@ def update_data(self, name, salary):
WHERE name=%s;
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(update_data_query, (salary, name))
databaseconnection.connection.commit()
self.client.connection.commit()
print("Data updated successfully.")
except psycopg2.Error as e:
print("Data updation failed: ", str(e))
Expand All @@ -61,10 +56,9 @@ def delete_data(self, name):
DELETE FROM sample_table_py WHERE name='%s';
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(insert_data_query, (name,))
databaseconnection.connection.commit()
self.client.connection.commit()
print("Data deletion successfully.")
except psycopg2.Error as e:
print("Data deletion failed: ", str(e))
Expand All @@ -74,17 +68,9 @@ def table_drop(self):
DROP TABLE sample_table_py;
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(reset_query)
databaseconnection.connection.commit()
self.client.connection.commit()
print("Table Dropped successfully")
except psycopg2.Error as e:
print("Table Drop Failed: ", str(e))

crud_ins=crud(host="risingwave-standalone",
port="4566",
database="dev",
user="root",
password="")
crud_ins.create_table()
Empty file.
39 changes: 17 additions & 22 deletions integration_tests/client-library/python/materializeview.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import psycopg2
from client import client
from crud import crud
from client import Client
from crud import SampleTableCrud


# Represents the materialized view `average_salary_view_py`.
class MaterializeView:
def __init__(self, host, port, database, user, password):
self.host = host
self.database = database
self.user = user
self.password = password
self.connection = None
self.port=port
self.client = Client(host, port, database, user, password)
self.crud = SampleTableCrud(host, port, database, user, password)

def create_mv(self):
crud_ins = crud(self.host, self.port, self.database, self.user, self.password)
crud_ins.create_table()
crud_ins.insert_data("John",25,10000)
crud_ins.insert_data("Shaun",25,11000)
crud_ins.insert_data("Caul",25,14000)
crud_ins.insert_data("Mantis",28,18000)
crud_ins.insert_data("Tony",28,19000)
mv_query="""
self.crud.create_table()
self.crud.insert_data("John", 25, 10000)
self.crud.insert_data("Shaun", 25, 11000)
self.crud.insert_data("Caul", 25, 14000)
self.crud.insert_data("Mantis", 28, 18000)
self.crud.insert_data("Tony", 28, 19000)
mv_query = """
CREATE MATERIALIZED VIEW average_salary_view_py AS
SELECT age, AVG(salary) AS average_salary
FROM sample_table_py
GROUP BY age;
"""
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(mv_query)
databaseconnection.connection.commit()
self.client.connection.commit()
print("MV created successfully.")
except psycopg2.Error as e:
print("MV creation failed: ", str(e))

def drop_mv(self):
mv_drop_query = "DROP materialized view average_salary_view_py;"
try:
databaseconnection = client(self.host, self.port,self.database, self.user, self.password)
cursor=databaseconnection.connect()
cursor = self.client.connect()
cursor.execute(mv_drop_query)
databaseconnection.connection.commit()
self.client.connection.commit()
print("MV dropped successfully.")
except psycopg2.Error as e:
print("MV drop failed: ", str(e))
4 changes: 3 additions & 1 deletion integration_tests/client-library/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
psycopg2-binary
pytest
pytest
sqlalchemy-risingwave
SQLAlchemy==1.4.51
41 changes: 27 additions & 14 deletions integration_tests/client-library/python/test_database.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from client import client
from crud import crud
from client import Client
from crud import SampleTableCrud
from materializeview import MaterializeView


@pytest.fixture
def db_connection():
db = client(
db = Client(
host="risingwave-standalone",
port="4566",
database="dev",
Expand All @@ -19,7 +19,7 @@ def db_connection():

@pytest.fixture
def crud_instance():
return crud(
return SampleTableCrud(
host="risingwave-standalone",
port="4566",
database="dev",
Expand Down Expand Up @@ -48,19 +48,22 @@ def test_disconnect(db_connection):
db_connection.disconnect()
assert db_connection.connection is None


def test_table_creation(crud_instance, db_connection):
cursor = db_connection.connect()
cursor.execute("SET TRANSACTION READ WRITE;")
crud_instance.create_table()

cursor.execute("FLUSH;")
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_name = 'sample_table_py';")
cursor.execute(
"SELECT table_name FROM information_schema.tables WHERE table_name = 'sample_table_py';")
result = cursor.fetchone()[0]
cursor.close()
assert result == 'sample_table_py'


def test_data_insertion(crud_instance, db_connection):
crud_instance.insert_data("John Doe", 25,10000)
crud_instance.insert_data("John Doe", 25, 10000)

cursor = db_connection.connect()
cursor.execute("FLUSH;")
Expand All @@ -71,6 +74,7 @@ def test_data_insertion(crud_instance, db_connection):

assert result == 1


def test_data_updation(crud_instance, db_connection):
crud_instance.update_data("John Doe", 12000)

Expand All @@ -82,56 +86,65 @@ def test_data_updation(crud_instance, db_connection):
cursor.close()
assert result == 12000


def test_data_deletion(crud_instance, db_connection):
crud_instance.delete_data("John Doe")

cursor = db_connection.connect()
cursor.execute("FLUSH;")
cursor.execute("SELECT EXISTS (SELECT 1 FROM sample_table_py WHERE name = 'John Doe');")
cursor.execute(
"SELECT EXISTS (SELECT 1 FROM sample_table_py WHERE name = 'John Doe');")
result = cursor.fetchone()
result = result[0]
cursor.close()

assert result == True


def test_table_drop(crud_instance, db_connection):
crud_instance.table_drop()

cursor = db_connection.connect()
cursor.execute("FLUSH;")
cursor.execute("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'sample_table_py');")
cursor.execute(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'sample_table_py');")
result = cursor.fetchone()
result = result[0]
cursor.close()

assert result is False

def test_mv_creation(mv_instance,db_connection):

def test_mv_creation(mv_instance, db_connection):
mv_instance.create_mv()
cursor = db_connection.connect()
cursor.execute("FLUSH;")
cursor.execute("SELECT EXISTS (SELECT 1 FROM pg_matviews WHERE matviewname = 'average_salary_view_py');")
cursor.execute(
"SELECT EXISTS (SELECT 1 FROM pg_matviews WHERE matviewname = 'average_salary_view_py');")
result = cursor.fetchone()[0]
cursor.close()
assert result is True

def test_mv_updation(db_connection,crud_instance):

def test_mv_updation(db_connection, crud_instance):
crud_instance.insert_data("Stark", 25, 13000)
cursor = db_connection.connect()
cursor.execute("FLUSH;")
cursor.execute("SELECT average_salary FROM average_salary_view_py WHERE age=25;")
cursor.execute(
"SELECT average_salary FROM average_salary_view_py WHERE age=25;")
result = cursor.fetchone()[0]
cursor.close()
# assert result == 11250
assert result == 12000


def test_mv_drop(crud_instance,mv_instance,db_connection):
def test_mv_drop(crud_instance, mv_instance, db_connection):
mv_instance.drop_mv()
crud_instance.table_drop()
cursor = db_connection.connect()
cursor.execute("FLUSH;")
cursor.execute("SELECT EXISTS (SELECT 1 FROM pg_matviews WHERE matviewname = 'average_salary_view_py');")
cursor.execute(
"SELECT EXISTS (SELECT 1 FROM pg_matviews WHERE matviewname = 'average_salary_view_py');")
result = cursor.fetchone()
result = result[0]
cursor.close()
Expand Down
Loading

0 comments on commit 89bbd28

Please sign in to comment.