2010-09-29 86 views
1

我目前正试图将我的数据库表从MyISAM移动到InnoDB。我遇到了服务器上运行的导致一些错误的请求和cron作业的计时问题。我很确定交易支持会帮助我解决问题。因此我正在转向InnoDB。SQL Alchemy +测试网络服务器与InnoDB失败

我有一套测试调用我们的webservices REST API并接收XML响应。测试套件非常全面,它使用Python编写,并使用SQLAlchemy从数据库查询信息。但是,当我将系统中的表从MyISAM更改为InnoDB时,测试开始失败。但是,测试不会失败,因为系统不工作,它们失败,因为ORM没有正确查询我正在测试的数据库中的行。当我浏览代码时,我看到了正确的结果,但ORM并没有返回正确的结果。

基本流程是:

class UnitTest(unittest.TestCase):                    
    def setUp(self):    
     # Create a test object in DB that gets affected by the web server                     
     testObject = Obj(foo='one')                    
     self.testId = testObject.id 

     session.add(testObject)                     
     session.commit()                       

    def tearDown(self): 
     # Clean up after the test 
     testObject = session.query(Obj).get(self.testId)               

     session.delete(testObject)                    
     session.commit() 

    def test_web_server(self): 
     # Ensure the initial state of the object.                     
     objects = session.query(Obj).get(self.testId)  
     assert objects.foo == 'one'                    

     # This will make a simple HTTP get call on an url that will modify the DB 
     response = server.request.increment_foo(self.testId) 

     # This one fails, the object still has a foo of 'one'              
     # When I stop here in a debugger though, and look at the database, 
     # The row in question actually has the correct value in the database. 
     # ???? 
     objects = session.query(Obj).get(self.testId)                
     assert objects.foo == 'two' 

使用MyISAM表存储该对象和该测试将通过。但是,当我更改为InnoDB表时,此测试不会通过。更有趣的是,当我在调试器中遍历代码时,我可以看到数据库具有我所期望的,因此它在Web服务器代码中不是问题。我已经尝试过几乎所有的expire_all,autoflush,autocommit等等的组合,并且仍然无法通过此测试。

如有必要,我可以提供更多信息。

感谢, 康拉德

回答

1

的问题是,你把self.testId = testObject.id之前新对象添加到会话,冲洗和SQLAlchemy的ID分配给它的线。因此self.testId总是None。将此行移至session.commit()以下。