返回数据帧的单元测试函数

2024-03-29 08:58:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如果函数返回一个随时都可能更改的数据帧,我如何介绍这些测试?我正在开始测试,我不知道如何用单元测试覆盖我的代码,你建议我怎么做

def update_with_formulario(date):
    df_formulario = _get_formulario(date)
    assert df_formulario is not None, 'Dataframe df_formulario is null'
    df_csv = utilities.get_data_csv_with_pandas('df.csv')
    assert df_csv is not None, 'Dataframe df_csv is null'
    df = df_formulario.merge(df_csv, 'left')
    utilities.write_csv_dataframe(f"{ROOT_WRITE_PATH}df.csv", df)


def _get_formulario(date):
    global cnn
    try:
        repo = Repository()
        cnn = repo.connect()
        query = f"""SELECT DISTINCT fecha,
                                   hora ,
                                   coalesce(valor_copia, valor) AS value
                    FROM table
                    WHERE VARIABLE IN ('variable')
                      AND fecha_data = '{date} 00:00:00'"""
        df_formulario = pd.read_sql_query(query, cnn)
        return df_formulario
    except:
        logging.exception(exc_msg.EXC_PROCEDURE_EXCEPT_MSG)
    finally:
        if cnn:
            cnn.close()
            logging.info(exc_msg.CNN_CLOSED_MSG)

Tags: csvnonedataframedfgetdateisdef