So I'm working on some tests where I need to create fixtures data.
Basically I used to have the following pseudo code that worked fine:
public function setUp()
{
// Programmatically create a coupon code
}
// Test methods
public function tearDown()
{
// Programmatically delete the coupon code
}
On Vinai's recommendation, and to make my test easier to manage I decided to change that code to the following so I don't have to manually delete my fixture data anymore:
public function setUp()
{
Mage::getSingleton('core/resource')->getConnection('core_write')->beginTransaction();
// Programmatically create a coupon code
}
// Test methods
public function tearDown()
{
Mage::getSingleton('core/resource')->getConnection('core_write')->rollBack();
}
However, with this code, my tests don't run properly anymore and I get the Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation
errors.
I was wondering if that's the right way of using transaction ?
Is the following order the right one ?
- begin transaction
- make DB changes (create DB data, and call the
save()
method) - rollback transaction
Do I need to call commit()
before rollBack()
?