Home > Software engineering >  It is possible to check a variable value in a function in testing?
It is possible to check a variable value in a function in testing?

Time:08-18

I have the following function:

def create_tables(tables: list) -> None:
    template = jinja2.Template(
        open("/opt/airflow/etl_scripts/postgres/staging_orientdb_create_tables.sql", "r").read()
    )
    pg_hook = PostgresHook(POSTGRES_CONN_ID)  # this is airflow module for Postgres
    conn = pg_hook.get_conn()
    for table in tables:
        sql = template.render(TABLE_NAME=table)
        with conn.cursor() as cur:
            cur.execute(sql)
    conn.commit()

Is there a solution to check the content of the "execute" or "sql" internal variable? My test looks like this, but because I return nothing, I can't check:

def test_create_tables(mocker):
    pg_mock = MagicMock(name="pg")
    mocker.patch.object(
        PostgresHook,
        "get_conn",
        return_value=pg_mock
    )
    mock_file_content = "CREATE TABLE IF NOT EXISTS {{table}}"
    mocker.patch("builtins.open", mock_open(read_data=mock_file_content))
    create_tables(tables=["mock_table_1", "mock_table_2"])

Thanks,

CodePudding user response:

You cannot access internal variables of a function from the outside.

I strongly suggest refactoring your create_tables function then, if you already see that you want to test a specific part of its algorithm.

You could create a function like get_sql_from_table for example. Then test that separately and mock it out in your test_create_tables.

Although, since all it does is call the template rendering function form an external library, I am not sure, if that is even something you should be testing. You should assume/know that they test their functions themselves.

Same goes for the Postgres functions.

But the general advice stands: If you want to verify that a specific part of your code does what you expect it to do, factor it out into its own unit/function and test that separately.

  • Related