Resetting MySQL auto-increment as tear-down step in Node.js unit test
I'm currently running unit tests on my DAL in a Node.js app. I'd like to be able to work with a dummy database, testing my CRUD operations with an informal setup/teardown that occurs throughout the test. Setup occurs by doing inserts, for example, and teardown by doing deletes.
I'm testing the database and not a mock/fake because I'd prefer to test complex SQL against a semi-working copy of the database.
The problem I'm running into is that on columns that have
AUTO_INCREMENT-ing, I can't expect any consistency on IDs. By resetting the
AUTO_INCREMENT I could avert this, but then I would be doing a lot of copy-paste coding, inserting a method in all my models.
Is there any way I can do this once and forget about it? Or is there a better practice I'm missing entirely?
You could use MySQL's TRUNCATE TABLE command, which empties a table and resets the AUTO_INCREMENT column. Effectively, it drops the table and recreates it which is faster than deleting all rows.