2017-06-06 5 views
1

私はユニットコードをテストしようとしています。私は、MySQLのクエリを与え、その結果をパンダのデータフレームとして返すメソッドを持っています。データベースでは、createdexternal_idのすべての戻り値がNULLであることに注意してください。ここで同一データフレームアサインメントが等しくない - Python Pandas

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) 

てAssertionError: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) 

異なり、列 '作成' を左と右のdatの両方に 'なし' の3つの文字列値が含まれていますアフリカ。なぜそれは同じではないと主張しているのですか?

+1

"None"という文字列ではなく、NoneType ['None'](https://docs.python.org/3/library/constants.html)なのでしょうか? – ayhan

+1

@それは、ありがとう!私はまだNoneTypeに遭遇していなかった。あなたの答えを答えとして掲示してください、私はそれを受け入れます! – OnlyDean

答えて

0

Pythonには、という文字列とは異なる組み込み定数Noneもあります。 docsから:'None'None == 'None')に対するNoneを比較する場合には

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.

結果はFalseになります。したがって、assert_frame_equalは、DataFramesの1つにNoneが含まれ、もう1つが'None'を含む場合、AssertionErrorを発生させます。

関連する問題