Codeception / module-db

DB module for Codeception
MIT License
23 stars 24 forks source link

Feat: `haveInDatabase` tear down use row values if primary key values are available #44

Closed JesusTheHun closed 1 year ago

JesusTheHun commented 1 year ago

Right now Db::addInsertedRow() uses the lastInsertId to remove the row during tear down. In some case the primary key is not auto incremented but another column is.

// auto_increment_not_on_pk : [id: int, counter: int auto_increment]
$I->haveInDatabase('auto_increment_not_on_pk', ['id' => 777]);
// created row : [id: 777, counter: 1]

The row being referenced with ['id' => 1] because $driver->lastInsertId() returns 1, and the module assumes it's the PK value.

This PR fixes this behaviour by checking if the provided values cover the primary key. If so, it uses those value, if not it follows the default behaviour.

Naktibalda commented 1 year ago

Thank you

YaakovR commented 1 year ago

@JesusTheHun What about using haveInDatabase on a table that does not have auto increment on any column? Currently, it gives this error: [TypeError] Codeception\Lib\Driver\Db::lastInsertId(): Return value must be of type string, bool returned

Naktibalda commented 1 year ago

yes, it is a bug, please raise pull request to fix it.

JesusTheHun commented 1 year ago

@YaakovR can you tell me what db you are using ? I tried with MySQL which (sadly) returns 0 when no id has been generated.

YaakovR commented 1 year ago

@JesusTheHun SQL Server