My database doesn't insert immediately

160 Views Asked by At

I am a trying to apply Test Driven Development to producing a web page and am having trouble using my database. I am developing using Cake 1.1, PHP5 and SimpleTest to unit test.

My problem is that I have a test that:

  1. Inserts a row using a direct SQL statement
  2. Tests if that row was inserted (by using a function in my main program.)
  3. Deletes the row that was inserted.

Both steps 1 and 3 work fine but step 2 fails.

It's important to realize that if I run the test without deleting the row (I commented that out) and then ran the test again but first eliminating step 1 (once again simply commenting that line) and the test passed without a problem.

I also tried running the SQL statements directly (one after another in the editor) in PHPMyAdmin and they work perfectly.

3

There are 3 best solutions below

1
On

Are all steps executed in the same transaction?

When not, step 2 isn't able to see the result of step 1 until step 1 commits.

0
On

It does sound like your insert is happening inside a transaction that has not yet been committed when you attempt the test.

Can you give some sample code?

0
On

Are you sure you are committing the transaction in step 1?