EntropyZero Blogging Server

Welcome to EntropyZero Blogging Server Sign in | Join | Help
in Search

dataFresh - a new database rollback library for unit tests

Quickly put your database into a known state for testing.

  • dataFresh is now Open Source!

    I want to thank everyone for your patience as we prepared the codebase for open access, but the wait is finally over.  dataFresh is now available to the open source community and can be downloaded here. We have decided that though we are sharing our source, we are not providing access to our internal source code repository.  We strongly urge you to use our open source products and enhance them to fit your needs. If you have suggestions or code enhancements, please email them in the form of a patch file to me. mike.brockey@nospam__ent0.com

    I look forward to hearing your comments and seeing your enhancements!!

    Mike

    kick it on DotNetKicks.com

  • How to use dataFresh for database unit testing

    In this post I will walk you through the steps of implenting dataFresh into your testing framework. A few of our users have reported issues but as it turns out, dataFresh was simply improperly being called.  Here is my attempt to provide more documentation. :)

    The first thing you should know is that dataFresh is an API that should be integrated directly into your testing framework.  The console application knows how to pass parameters and simply make calls to the API.  It is only nessessary to use the console application if you wish to manually execute dataFresh commands against your database.

    Step 1 : Prepare your Database

    Before you can call any other dataFresh method you must first prepare your database with the nessesary stored procedures, table and triggers.

    Step 2 : Create a Snapshot

    Whenever your database schema or test data changes you are required to create a snapshot of your database in order to perform a successful rollback.  When using a tool like deltaRunner, you can easilly manage updates to your database and automatically create a new snapshot.

    Step 3 : Run your Test

    You should feel free to write your integration tests that communicate with and make changes to your database.

    Step 4 : Refresh the Database

    A quick call to the RefreshTheDatabase method and dataFresh will very quickly restore only those tables that were modified during your integration test.

    Now in practice, we typically check for modifications and refresh the database during the setup method of our test fixtures.  After the test runs, it has been my experience that there is some benefit of being able to run queries to check the state of the database as it were modified after the test.  We usually use the tear down method and output a "Database Write" message just so that we have something visual.

    We have provided sample code that demonstrates the recommended implementation.

     

     

  • New release of dataFresh -- Database Unit Testing

    There have been a few requests for us to sign our dataFresh assemblies. This is the only change in this release and it is available here.  dataFresh is a database rollback library for unit tests and provides simplified database unit testing for your testing framework.

  • It's easy to use and implement dataFresh!

    Entropy's dataFresh is a toolkit that assists test driven development projects in restoring their database to a known state before each test within a test fixture.  The time consuming effort of having to write tear down methods to clean up the database after running your tests are a thing of the past.

    Our appoach is unlike others as we do not attempt to rip and replace the entire database.  Instead we track database modifications to the table level and only work with those tables that have been modified.


    Preparing your database

    Before you can use dataFresh to refresh your data you will need to prepare your database.  Calling the PrepareDatabaseforDataFresh method will connect to the specified database and will create the dataFresh elements required to track and refresh your data.

    Example:

     string connectionString = "<your connection string>";
     SqlDataFresh dataFresh = new SqlDataFresh(connectionString);
     dataFresh.PrepareDatabaseforDataFresh();


    Create Snapshot

    As part of the call to the PrepareDatabaseforDataFresh method, we create a snapshot of your database that will be used to refresh the database. Alternativly you may request that the call to prepare the database skip the snapshot step, in which case you will want to need to to call the CreateSnapshot method manually before you will be able to refresh the
    database.  You may also override the filepath where there snapshot resides by setting the SnapshotPath property.

    Example:

     string connectionString = "<your connection string>";
     SqlDataFresh dataFresh = new SqlDataFresh(connectionString);
     bool createSnapshot = false;
     dataFresh.PrepareDatabaseforDataFresh(createSnapshot);
     

    Specify the SnapshotPath

    You may tell dataFresh where you want to save the snapshot files of your database by setting the SnapshotPath property of your dataFresh instance. Please be aware that the location will be referenced by and therefore should be accessible to the SQL server.

    Example:

     string connectionString = "<your connection string>";
     SqlDataFresh dataFresh = new SqlDataFresh(connectionString);
     bool createSnapshot = false;
     DirectoryInfo snapshotPath =
      new DirectoryInfo("d:\Sql\Data\Snapshot_DatabaseName");
     dataFresh.PrepareDatabaseforDataFresh(createSnapshot);
     

    HasDatabaseBeenModified Method

    This method will strangely enough return true if your database has been modified since the last refresh command.

    Example:

     string connectionString = "<your connection string>";
     SqlDataFresh dataFresh = new SqlDataFresh(connectionString);
     if(dataFresh.HasDatabaseBeenModified)
     {
      Console.Out("Database has been modified!");
      dataFresh.RefreshTheDatabase();
     }


    RefreshTheDatabase Method

    When called, this method will clear out the modified tables and refresh the database from the snapshot files.  If you specified an alternate location for your snapshot files, please be sure the the SnapshotPath property has been set before calling this command.


    RefreshTheEntireDatabase Method

    When called, this method will clear out and refresh all tables regardless if they have been modified or not. Again, If you specified an alternate location for your snapshot files, please be sure the the SnapshotPath property has been set before calling this command.


    RemoveDataFreshFromDatabase

    When you are ready to move into production, or no longer need the help of dataFresh, simply call the RemoveDataFreshFromDatabase method and we will gladly remove all the dataFresh elements from your database. :)


    dataFresh on the Command Line

    You can integrate dataFresh into your automated builds by using the command line version of dataFresh. Here are some examples of the concepts listed above.

    DataFreshUtil.exe -s 127.0.0.1 -u <user> -p <pwd> -d DataFreshSample -c PREPARE

    DataFreshUtil.exe -s 127.0.0.1 -u <user> -p <pwd> -d DataFreshSample -c REFRESH

    DataFreshUtil.exe -s 127.0.0.1 -u <user> -p <pwd> -d DataFreshSample -c REMOVE

  • dataFresh issues after computer rename!

    We recently had one of our development pairing stations rebuilt.  When we got the new machine it was setup with some random name.  In the spirit of consistency, Stephen and I decided to rename the machine to something more in line with our other pairing workstations.  I've never been a big fan of renaming machines especially when Microsoft SQL Server was already installed.  In this case since we just started working on this box and had not yet installed IIS and basically everything else, we decided that it would be OK.

    We finally got all our software installed and were ready to start developing.  We run our test fixtures that build out the database.  Part of the new database initialization is a dataFresh restore that imports all the lookup data for the system.  It would stop at this point and not provide any error.  It seemed like dataFresh would just freeze.  After a little bit of examining our procedures, I noticed a call to @@ServerName that is used to identity the server for authentication purposes.

    SELECT @@ServerName

    When I executed the above SQL statement, it returned the old name of the machine.  Ah ha!  The @@ServerName function stores the computer name during SQL server installation and must be changed after renaming your computer.  To do this, I used the following SQL statements.

    exec sp_dropserver '<old machine name>', null

    exec sp_addserver '<new machine name>', 'local', null

    <restart MSSQLSERVER service>

    After restarting the server, I ran my tests again and this time dataFresh populated the lookup data, we were back in business!

     

     

  • EntropyZero introduces DataFresh

    EntropyZero introduces dataFresh which is a small utility used on test driven development (TDD) projects that enables you to get your Microsoft SQL Server based database back to a known state between test executions.  The provided API layer can use integrated seemlessly into your test harness.  DataFresh can also be used as a console application and be called directly from build scripts including NAnt or other custom built scripts very easilly.

    DataFresh enhances team productivity by eliminating the need of writing TearDown methods that attempt to remove changed records from your database.  DataFresh uses internal features of Microsoft SQL Server that restore your database back to a known state in a fraction of a second in most cases.

    To start using dataFresh today,simply visit our downloads page and download the latest binaries.  For q quick start, please try out our Test Harness Example.

Powered by Community Server, by Telligent Systems