2014-04-09 3 views
1

코드에있는 메소드를 테스트 할 때 데이터베이스 호출을 스태핑하고 싶습니다. 내가하고 싶은 것은 가치를 돌려 주지만 나는 그걸 멀리 할 수는없는 것 같습니다.Python Mock UnitTest 및 Database

def loadSummary(appModel): 
    stmt = 'Select * from Table' 
    for row in appModel.session.query(*t.columnNames()).from_statement(stmt).all(): 
     t.append(row) 
    return t 

def test_loadSummary(self): 
    appModel = Mock() 
    query = appModel.session.query.return_value 
    query.from_statment.return_value = ['test1', 'test2'] 
    expected = loadSummary(appModel) 

나는 다음과 같은 오류를 얻을

for row in appModel.session.query(*t.columnNames()).from_statement(stmt).all(): 
TypeError: 'Mock' object is not iterable 

그래서 그는 전혀 심지어 쉘 문제없이 작동하지만 메소드로 전달 가져 오지처럼.

>>> appModel.session.query('').from_statment('stmt') 
['test1', 'test2'] 

그때 mock.patch.object 사용하여 시도

class MockAppContoller(object): 
    def from_from_statement(self, stmt): 
     return ['test1', 'test2'] 

def test_loadSummary(self): 
    with mock.patch.object(loadSummary, 'appModel') as mock_appModel: 
     mock_appModel.return_value = MockAppContoller() 

I 얻을

2014-04-09 13:20:53,276 root ERROR Code failed with error: 
<function loadSummary at 0x0D814AF0> does not have the attribute 'appModel' 

가 어떻게이 문제를 해결 얻을 수있는 다음과 같은 오류?

+0

그것은'sqlalchemy' 사용하고있다 모의 사용하는 등 그와 같은 깔끔한하지, 그렇지? – alecxe

+0

예. 나는 SQLAlchemy – Eamonn

답변

2

귀하의 오류가 여기에 나타납니다

query.from_statment.return_value = ['test1', 'test2'] 

은 다음과 같아야합니다

같은 코드

>>> appModel.session.query('').from_statement('stmt') 
['test1', 'test2'] 
를 사용하지 않기 때문에 그것은 당신을 위해 쉘에서 작동
query.from_statement.return_value.all.return_value = ['test1', 'test2'] 

실제로 시도하면 실패합니다

>>> appModel.session.query('').from_statment('stmt').all() 
['test1', 'test2'] 
+0

을 사용하고 있습니다. 언어 : lang-py -> >>> appModel.session.query (''). from_statment ('stmt'). all() [ 'test1', 'test2 '] >>> for appModel.session.query(). from_statement (stmt) .all() : ... 인쇄 x ... 2014-04-09 17 : 01 : 56,276 루트 오류 코드가 오류로 인해 실패했습니다 : '모의 (Mock)'객체가 반복 가능하지 않습니다 그 외에는 여전히 동일한 결과가 나타납니다. 일단 내가 시도하고 다른 개체에 전달하십시오. – Eamonn

+0

'from_statment'에도 맞춤법 오류가 있습니다. 테스트중인 코드와 일치시키기 위해'from_statement'가되어야합니다. – aychedee

+0

와우, 아마추어 실수. 감사합니다 aychedee. – Eamonn

0

내가 생각 해낸 또 다른 해결책이를했지만

class mockAppModel(object): 
    def from_from_statement(self, stmt):  
     t = [] 
     t.appendRow('row1', 'row2') 
     return t 

class mockFromStmt(object): #This is the ONE parameter constructor 
    def __init__(self): 
     self._all = mockAppModel() 

    def all(self): #This is the needed all method 
     return self._all.from_from_statement('') 

class mockQuery(object): #This is the ONE parameter constructor 
    def __init__(self): 
     self._from_statement = mockFromStmt() 

    def from_statement(self, placeHolder): #This is used to mimic the query.from_statement() call 
     return self._from_statement 

class mockSession(object): 
    def __init__(self): 
     self._query = mockQuery() 

    def query(self, *args): #This is used to mimic the session.query call 
     return self._query 

class mockAppModel(object): 
    def __init__(self): 
     self.session = mockSession()