比较两个Sqlalchemy表对象与Python单元

2024-03-28 15:57:22 发布

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

我试图查看两个表对象是否匹配,并找到了关于Python的__eq__函数的文档,但不确定如何在代码中使用它。下面是我正在测试的代码:

def get_table(self, table_name, select_stmt=None):
    """
        This method gets a table from the data connection using SQLAlchemy's reflection capability. Columns in the
        select_stmt are included, with all other table columns excluded.
        :param table_name: The name of the table being reflected.
        :type table_name: str
        :param select_stmt: The fields being included in the query. Default None.
        :type select_stmt:  str
        :returns:  SQLAlchemy Table object
    """
    table = Table(table_name, self.meta)
    self.log.debug("Reflecting table %s" % table_name)

    if select_stmt == "all_columns":
        select_stmt = None

    self.insp.reflecttable(table, select_stmt)

    return table

我的测试目前看起来像:

^{pr2}$

Tags: columnsthe代码nameinselfnoneparam
1条回答
网友
1楼 · 发布于 2024-03-28 15:57:22

我终于发现了怎么做。问题是结果也返回了不匹配的对象值。通过将测试更改为

self.assertCountEqual(given_result.columns._data, expected_result.columns._data)

我得到了需要验证的表结构。在

相关问题 更多 >