Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Resetting MySQL auto-increment as tear-down step in Node.js unit test

Resetting MySQL auto-increment as tear-down step in Node.js unit test

Problem

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?

Problem courtesy of: Josh Smith

Solution

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.

Solution courtesy of: Matt Healy

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Resetting MySQL auto-increment as tear-down step in Node.js unit test

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×