2014-07-06 46 views
0

我想看看兩個表對象是否匹配,並找到有關Python的__eq__函數的文檔,但我不確定如何將它與我的代碼一起使用。下面是我測試的代碼:比較兩個Sqlalchemy表對象與Python單元測試

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) 
+0

也許我錯過了實現細節,但它看起來像你正在用函數本身測試'get_tables'。通常你想從功能中分離出你想測試的功能並獨立驗證。做這件事的一個好方法是建立一個測試數據庫,讓你知道其中的數據,然後給這個知識測試,如果該功能通過確保所有條目正確返回正確的東西。 – MrAlias

+0

我正在測試get_tables的類似功能。在一個我特別通過選項「all_columns」。另一方面,我沒有通過選擇。我有其他測試,驗證函數正確返回對象。我想確保在兩個測試選項之間得到相同的結果。 – OpenDataAlex

回答

0

我終於發現瞭如何做到這一點。問題在於結果也返回了不匹配的對象值。通過改變測試

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

我能夠得到我需要驗證的表結構。