wawawa
wawawa

Reputation: 3355

How to unit test the columns names are valid for a function that reads data with `pandas.read_sql()`?

I have a function to read data from a MySQL database:

def example(mysql_engine) -> DataFrame:
    query = """SELECT col_1 FROM xxx.xxx"""
    df = pandas.read_sql(query, mysql_engine)

    return df

mysql_engine is returned by another function, now I want to write unit tests to validate the column name col_1 and datatype in df, is there an example I can follow? Do I need to set up a real database engine in unit test?

Upvotes: 1

Views: 1416

Answers (1)

Ali Mirferdos
Ali Mirferdos

Reputation: 302

You can use assert col_1 in df.columns for the unit tests.

For the Database engine, you can use a mock object. You can check this link from the standard library.

Upvotes: 0

Related Questions