使用Python unittest比较两个Sqlalchemy表对象

时间:2014-07-06 03:01:13

标签: python unit-testing object

我正在尝试查看两个表对象是否匹配,并找到了有关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

我的测试目前看起来像:

    def test_select_all_data_no_columns(self):
    # Testing that when all columns are selected, None value is passed.
    given_result = DataConnector(self.source).get_table(self.table, "all_columns")
    expected_result = DataConnector(self.source).get_table(self.table)

    self.assertEquals(given_result, expected_result)

1 个答案:

答案 0 :(得分:0)

我终于发现了如何做到这一点。问题是结果也返回了不匹配的对象值。通过将测试更改为

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

我能够得到我需要验证的表格结构。