Showing posts with label Unit Tests. Show all posts
Showing posts with label Unit Tests. Show all posts

Sep 29, 2011

MySQL Unit Testing and Re-factoring Verification

If your software is highly dependent on a database, data intensive processes re-factoring verification can be a pain in the a$$. Yet, you probably would like to avoid a production change that will result in a total mess in your data.

Verification and Automation is Easier w/MySQL
MySQL helps us verifying the change by using a MD5 hashing. Please notice that the following works only on the Linux platform.

MD5 Hash Based Verification Step By Step

  1. Backup the baseline table before running any process: CREATE TABLE class_baseline SELECT * FROM class;
  2. Run the original process.
  3. Copy the results of the old process to a new table : CREATE TABLE class_original SELECT * FROM class;
  4. If needed, TRUNCATE the table or restore the baseline table results TRUNCATE class; INSERT INTO class SELECT * FROM class_baseline;
  5. Run the modified process.
  6. Change the pager to MD5. The result is a single short MD5 hash instead of the regular screen output: pager md5sum -.
  7. Perform a select statement on the original table. Don't forget to drop from the SELECT statement any auto enumerator or time stamp fields that are being changed in any table modification/insert. If your process re-factoring changes the insert order, don't forget to perform an ORDER BY to avoid the case of same content/different order: SELECT class_name, class_location from class_original ORDER BY class_name, class_location;
  8. Perform the same on the modified process result: SELECT class_name, class_location FROM class ORDER BY class_name, class_location;
  9. Compare the two results. If your re-factoring did change the data itself, you should receive identical HASH results.
  10. Recover to regular screen output mode using the nopager command.
Bottom Line
Re-factoring is much simple with MD5 hashing

Keep Performing,

Jan 20, 2010

Refactor Your Code w/o Fear

When you perform a performance boost in your system, many times you need to refactor part of the code, without changing the functionality. When you did not write the code, or it is just too complex, it might be a little bit risky and a very good test plan should be designed in order to assure that logic was not broken while boosting the performance.
When we have Unit Tests in the system it is much simpler (since someone else already done the hard work). Therefore, if your business plan is ambitious, the safe way is starting with a simple system with Unit Tests, and plan for refactoring.
I have been in a short seminar by the industry experts Dror Helper and Gil Zilberfeld from TypeMock that presented several aspects and lessons in TDD, Unit Tests and Mocks. I dropped here some of the lessons that they presented:
  1. Make a single assert per function
  2. Name the test function FunctionName_TestDescription_ExpectedResult
  3. Use the appropriate Assert method in order get most information from the failure (don’t use always IsTrue)
  4. Keep tests simple (up to 5-6 lines each)
  5. Test (usually) only public methods
  6. Code to test, or at least in some cases use re factoring to ease problematic issues (such as pass in date to the function/class when the tested function is based on date.
  7. The AAA test structure:
    1. Arrange – set up the method/function
    2. Act – run the function
    3. Assert – call the assert method
Keep Performing,
Moshe Kaplan



ShareThis

Intense Debate Comments

Ratings and Recommendations