-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: include sqlalchemy 1.4 test in python client testing (#14491)
- Loading branch information
1 parent
2bec2ed
commit 7c3edb1
Showing
7 changed files
with
129 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
39 changes: 17 additions & 22 deletions
39
integration_tests/client-library/python/materializeview.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
psycopg2-binary | ||
pytest | ||
pytest | ||
sqlalchemy-risingwave | ||
SQLAlchemy==1.4.51 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
integration_tests/client-library/python/test_sqlalchemy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from sqlalchemy import Column, BigInteger, Integer, String, create_engine | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker | ||
import pytest | ||
# Create a base class for declarative class definitions | ||
Base = declarative_base() | ||
|
||
|
||
class User(Base): | ||
# Define a simple User class as an example | ||
__tablename__ = 'users' | ||
|
||
id = Column('id', BigInteger, primary_key=True) | ||
name = Column('name', String) | ||
age = Column('age', Integer) | ||
|
||
|
||
# Pytest fixture to create and destroy the database session | ||
@pytest.fixture | ||
def db_session(): | ||
DB_URI = 'risingwave+psycopg2://root@risingwave-standalone:4566/dev' | ||
# Create an SQLAlchemy engine to manage connections to the database | ||
engine = create_engine(DB_URI) | ||
|
||
# The automatically created table is incorrect. The BigInteger will be translated into BIGSERIAL somehow, which is not supported. | ||
create_table = """ | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id BIGINT PRIMARY KEY, | ||
name VARCHAR, | ||
age INTEGER | ||
) | ||
""" | ||
with engine.connect() as conn: | ||
conn.execute(create_table) | ||
conn.execute('SET RW_IMPLICIT_FLUSH=true') | ||
|
||
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
session = Session() | ||
yield session | ||
session.close() | ||
|
||
Base.metadata.drop_all(engine) | ||
|
||
|
||
# Pytest test functions to perform CRUD operations | ||
def test_create_user(db_session): | ||
new_user = User(id=1, name='John Doe', age=30) | ||
db_session.add(new_user) | ||
db_session.commit() | ||
assert new_user.id is not None | ||
|
||
all_users = db_session.query(User).all() | ||
assert len(all_users) > 0 | ||
|
||
|
||
def test_delete_user(db_session): | ||
user_to_delete = db_session.query(User).filter_by(name='John Doe').first() | ||
if user_to_delete: | ||
db_session.delete(user_to_delete) | ||
db_session.commit() | ||
deleted_user = db_session.query(User).get(user_to_delete.id) | ||
assert deleted_user is None |