2017-06-06 54 views
1

我想單元測試我的代碼。我有一個給定MySQL查詢的方法,將結果作爲熊貓數據框返回。請注意,在數據庫中,createdexternal_id中的所有返回值均爲NULL。下面是測試:相同的Dataframes斷言不等 - Python熊貓

def test_get_data(self): 

    ### SET UP 

    self.report._query = "SELECT * FROM floor LIMIT 3"; 
    self.report._columns = ['id', 'facility_id', 'name', 'created', 'modified', 'external_id'] 
    self.d = {'id': p.Series([1, 2, 3]), 
       'facility_id': p.Series([1, 1, 1]), 
       'name': p.Series(['1st Floor', '2nd Floor', '3rd Floor']), 
       'created': p.Series(['None', 'None', 'None']), 
       'modified': p.Series([datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S'), 
            datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S'), 
            datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S')]), 
       'external_id': p.Series(['None', 'None', 'None']) 
       } 
    self.df = p.DataFrame(data=self.d, columns=['id', 'facility_id', 'name', 'created', 'modified', 'external_id']) 
    self.df.fillna('None') 
    print(self.df) 
    ### CODE UNDER TEST 

    result = self.report.get_data(self.report._cursor_web) 
    print(result) 
    ### ASSERTIONS 

    assert_frame_equal(result, self.df) 

這裏是控制檯輸出(注意在測試代碼的打印語句的手動構造數據幀是在上面,從被測試的函數導出的一個在底部。):

. id facility_id  name created   modified external_id 
0 1   1 1st Floor None 2012-10-06 01:08:27  None 
1 2   1 2nd Floor None 2012-10-06 01:08:27  None 
2 3   1 3rd Floor None 2012-10-06 01:08:27  None 
    id facility_id  name created   modified external_id 
0 1   1 1st Floor None 2012-10-06 01:08:27  None 
1 2   1 2nd Floor None 2012-10-06 01:08:27  None 
2 3   1 3rd Floor None 2012-10-06 01:08:27  None 
F 
====================================================================== 
FAIL: test_get_data (__main__.ReportTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/path/to/file/ReportsTestCase.py", line 46, in test_get_data 
    assert_frame_equal(result, self.df) 
    File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1313, in assert_frame_equal 
obj='DataFrame.iloc[:, {0}]'.format(i)) 
    File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1181, in assert_series_equal 
obj='{0}'.format(obj)) 
    File "pandas/src/testing.pyx", line 59, in pandas._testing.assert_almost_equal (pandas/src/testing.c:4156) 
    File "pandas/src/testing.pyx", line 173, in pandas._testing.assert_almost_equal (pandas/src/testing.c:3274) 
    File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1018, in raise_assert_detail 
raise AssertionError(msg) 

Asse田:DataFrame.iloc [:, 3]是不同的

DataFrame.iloc[:, 3] values are different (100.0 %) 
[left]: [None, None, None] 
[right]: [None, None, None] 

---------------------------------------------------------------------- 
Ran 1 test in 0.354s 

FAILED (failures=1) 

我估計「創造」,列包含在左側和右側都逸「無」的三個字符串值aframes。爲什麼它斷言不平等?

+1

也許其中一個不是字符串「None」,而是NoneType ['None'](https://docs.python.org/3/library/constants.html)? – ayhan

+1

@ayhan就是這樣,謝謝!我還沒有遇到NoneType。請將您的答案作爲答案發布,我會接受它! – OnlyDean

回答

0

Python也有一個內置常量None,它與字符串'None'不同。從docs

None

The sole value of the type NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None are illegal and raise a SyntaxError.

在比較None'None'None == 'None')的情況下,結果會是假的。因此,如果其中一個DataFrame包含None,而另一個包含'None',則assert_frame_equal將引發AssertionError。