jeudi 21 juin 2018

Test where clause in sqlalchemy select object

I am trying to write some functions to build sqlalchemy select statements. For instance:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata

t_test = sa.Table(
    'test', metadata,
    sa.Column('col_1', sa.Text),
    sa.Column('col_2', sa.Float)
)

def create_test_select():
    sa_select = sa.select([t_test.c.col_1, t_test.c.col_2])
    return sa_select

def add_test_col1_where_clause(sa_select, x ):
    sa_select = sa_select.where(t_test.c.col_1 == x)
    return sa_select

I would like to test these functions.

To test create_test_select I would write something like

class Test(unittest.TestCase):    
    def test(self):
         self.assertIn('col1', create_test_select().columns)
         self.assertIn('col2', create_test_select().columns)

How can I test that the function add_test_col1_where_clause? I would like to know that it adds the correct where clause to the select. My initial thought was to examine the where clause in sqlachemy select object but I haven't been able to get this to work.

Any suggestions would be appreciated. Thanks.

Aucun commentaire:

Enregistrer un commentaire