What do you mean?

January 27, 2010


‘The Power of a Common Business Language’ outlines the important of having a common set of key business terms and definitions within an enterprise.

It’s author asserts – the major challenges when deploying information systems are not technical – but are in having a “common business language”.

I am sure many of you can think of many examples within your own organisations where issues are caused by not having one.

Often the same term is used to mean different thing or differing terms are used for the same thing – leading to:

  • confusion
  • data quality issues
  • increased costs
  • the potential loss of opportunities to increase revenue

What is the best approach to take?
Most would agree that having a common business language will provide benefit – but there are a number of different approaches that can be taken.

I have recently been involved in setting up a centralised data dictionary/metadata repository – to start to record all key business terms/definitions.

The approach has been to build this up iteratively – collecting and reviewing as and when necessary, usually through project work.

I would be very interested to hear from anyone who has been involved in tackling this issue within an enterprise – the approach you took – what worked – what didn’t?


01B Database testing framework – feature set for MS SQL and MySQL

December 10, 2009


Overview
The last blog – 01A Database testing framework – what it does – showed the capabilities provided by the framework.

This blog will look at the supported SQL dbms and the testing features provided for each.

Click to see the complete set of blogs in this series

Feature set

MS SQL 2K MS SQL 2K5 MS SQL 2K8 MySQL 5.1
Tables
Views
Sprocs
Triggers N/A
Synonyms N/A N/A
Expected data sets

What next
The next blog in the series – 02 Database testing framework – how it works – will provide an overview into how it works.

Click to see the complete set of blogs in this series


Technical debt – what about data quality debt?

November 21, 2009

 He says: “In this metaphor, doing things the quick and dirty way sets us up with a technical debt, which is similar to a financial debt. Like a financial debt, the technical debt incurs interest payments, which come in the form of the extra effort that we have to do in future development because of the quick and dirty design choice. We can choose to continue paying the interest, or we can pay down the principal by refactoring the quick and dirty design into the better design. Although it costs to pay down the principal, we gain by reduced interest payments in the future.”  He also explains the use of the ‘Technical debt quadrant’

Is the debt Inadvertant or deliberate

Is it a reckless or prudent debt? Can you afford to pay the debt back in the long run? 

Is it worth having a similar metaphor – ‘data quality debt’? 

It might be useful when discussing data quality and data management issues within project teams and with the wider business. 

So what constitutes data quality debt? 

Often the sort of decisions taken in projects that lead to: 

  • the quality of data degrading overtime.
  • having to continually support the system to mitigate against data issues.
  • having to carry out expensive data cleansing exercises.

Some examples of data quality debt: 

  • Not identitying who in the business is responsible for the data.
    • If no one  is responsible  for keeping it up to date it will degrade overtime.
  • Not having clear definitions of the data and it’s purpose.
    • If you dont’  - semantic integration with other systems will be an issue and the data is likely to be misused.
  • Setting a column in table to null - even though business requirements say it should be compulsory.
    • Some rows in the initial data import didn’t have this data – it was easier to set it to null rather than go back to the business.
  • Not having any data quality profiling/metrics - to ensure data quality is maintained over time. 
    • ‘If you cant measure it you cant manage it’.

These are only few examples I am sure there are many more! 


07 Database testing framework – auto create tests for all existing tables

November 19, 2009


Overview
The last blog – 06 Database testing framework – testing a change to an existing table – showed how to:

  • produce a set of base line unit tests for an existing table
  • how to use this approach when making a change to a table

This blog will look at how to auto create a base line set of tests for ALL of your existing SQL tables.

This will normally be done if you have a legacy database and what to get started.

A BAT file will be created which will query your test database and output a C# test file for each table in the database.

Click to see the complete set of blogs in this series

Steps
1. Create a BAT file to auto create all of the SQL table tests.

Go to misc\AdventureWorksDatabase\SQL\BATAndSQLScripts02_AutoCreateTest_TablesOrViews.sql

Enter the name of database to be tested

eg SET @dbName = ‘AdventureWorks’

Change th namespace

eg SET @NAMESPACE = ‘AdventureWorks.DatabaseTest.Tests.Tables’

2. Run the sql script

The text that is outputted should be inserted into the correct section of the following BAT file

misc\AdventureWorksDatabase\SQL\BATAndSQLScripts06_AutoCreateTest_Tables.bat

3. Update the BAT file

Set the correct serverName

SET SQLServerName=ServerName

If required the output directory can be changed ie where the C# files will be created – by changing:

SET ParentDir=C:\Temp\New\Tables\

4. Run the BAT file

A C# test file should be created for each of the SQL tables in the database.

Copy the files from

‘C:\Temp\New\Tables\’

to DIR\src\AdventureWorksDatabaseTest\Tests\Tables

5. In VS use ‘Add – existing item’ to add these tests to the database test project.

Rebuild the project.

You now have a base line set of tests for all existing SQL tables in your database.

Summary
This blog has shown how to:

  • produce a set of base line unit tests for an existing table
  • how to use this approach when making a change to a table

What next
The next blog in the series – 08 Database testing framework – testing the creation of a new table – as per the title – will look at auto creating unit tests for all tables in an existing database.

Click to see the complete set of blogs in this series


06 Database testing framework – testing a change to an existing table

November 18, 2009


Overview
The last blog – 05 Database testing framework – running your first tests – showed how to:

  • the final steps in setting up the unit testing visual studio (VS) solution
  • running the test SQL helper scripts
  • running the first tests against the database

This blog will look at:

  • how to auto create a base line set of tests for an existing SQL table
  • using a ‘test first’ type approach when making a change to the table

Click to see the complete set of blogs in this series

Steps
1. The AdventureWorks database contains a table – HumanResources.Employee

The image below shows the columns and properties in this table:

Employee table properties

Employee table properties

2. To auto create the unit tests for this table

Note from this point ‘DIR’ is a replacement for ‘C:\Projects\AdventureWorks\’.

Open the following file:

DIR\misc\AdventureWorksDatabase\SQL\DatabaseTestingHelperScripts
08_SQLDatabase_ManuallyCreateTests.sql

Run the following sproc – note that the name of the database to test is ‘AdventureWorks’.

EXEC dbo.sp_Sys_UnitTestGenerator_Table
‘AdventureWorks’
, ‘HumanResources’
, ‘Employee’
, ‘AdventureWorks.DatabaseTest.Tests.Tables’
, 10
, 0

3. The output of the above will contain details on the table – columns and their properties, indexes, constraints and trigggers.

Paste it into a new VS C# file AdventureWorksDatabaseTest\Tests\Tables\Employee.cs.

It should look something like the following:

Employee table unit test

Employee table unit test

Read through the test declarations/expectations in the C# file.

Compare the expectations to those in the image ‘Employee table properties’ in Point 1 above.

They should be the same ie correct number of columns, constraints etc.

4. Rebuild the test project and open MBUnit UI

Run the tests for Tables\Employee.cs. All should pass.

This is the base set of tests for the current table – if something is altered on the table, without updating the expectations, then one or more of the tests should fail.

5. Taking a ‘test first – TDD’ type approach when making a change to a table

We want to add a new column to this table – eg:

PassportNumber – int and nullable

6. First update the test expectations.

For a new column there are three things to change

A) expectedColumnCount = 16; to expectedColumnCount = 17;

B) Include PassportNumber column name in the expectedColumList
expectedColumnList = “PassportNumber,EmployeeID,etc”;

C) Add the expected column properties to test ‘T10_ColumnProperties’

[Row("PassportNumber", "17,YES,int,N/A")]
public void T10_ColumnProperties(string column, string expectedProperties)

This is entered in the format

[Row("ColumnName", "Ordinal number,Nullable - YES/NO,datatype,string length")]

As this is an int – string length is entered as – N/A.

The new ‘expectations’ have now been set.

7. To quickly check what columns are currently in the table – run this sql script

USE AdventureWorks
GO

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA + ‘.’ + TABLE_NAME = ‘HumanResources.Employee’
ORDER BY ORDINAL_POSITION

8. Rebuild the test projects and run tests

As expected there will be a number of test failures as the new columns does not currently exist.

9. Now dev the SQL code so that the unit tests now pass

Add the column to table by running the following SQL:

USE AdventureWorks
GO
ALTER TABLE HumanResources.Employee ADD PassportNumber int NULL
GO

10. Rerun the tests – they should now pass

11. If you don’t want to carry out a ‘test first – TDD’ approach
A) it is possible to make the SQL changes first

B) then update the C# tests for the table using the provided helper sprocs

Both approaches have their benefits – the second especially if you are applying many changes in one go.

But I would recommend taking ‘test first – TDD’ approach.

Writing the tests first ensures that you think about the requirements before writing the SQL change script and applying it

Summary
This blog has shown how to:

  • produce a set of base line unit tests for an existing table
  • how to use this approach when making a change to a table

What next
The next blog in the series – 07 Database testing framework – auto create tests for all existing tables – as per the title – will look at auto creating unit tests for all tables in an existing database.

Click to see the complete set of blogs in this series


05 Database testing framework – running your first tests

November 18, 2009


Overview
The last blog – 04 Database testing framework – setting up the visual studio project template – showed how to:

  • download the database testing framework project template
  • set up the directory structure to test the database

This blog will look at:

  • the final steps in setting up the unit testing visual studio (VS) solution
  • installing the test SQL helper scripts
  • running the first tests against the database

Click to see the complete set of blogs in this series

Steps
1. Create the SQL helper files on the Master database

Note from this point ‘DIR’ is a replacement for ‘C:\Projects\AdventureWorks\’.

Open the following file and run it against the Master database.

DIR\misc\AdventureWorksDatabase\SQL
.2.0.223_DatabaseTesting_SQLScriptFile.sql

This will create a number of ‘helper’ sprocs in the Master database.

These helper sprocs can be used, for example, to auto create C# files.

2. Open up the VS solution.

Carry out a project wide text – ‘find and replace – changing ‘DBTemplate.’ to ‘AdventureWorks.’

Build the project

3. Open MBUnit UI

If you need to install get the latest version from their web site or install from DIR\tools\mbunit2.41.232.0

In the UI go to the menu ‘Assemblies’ – ‘Add assemblies’

Navigate to and attach the following dll:

DIR\src\AdventureWorksDatabaseTest\bin\Debug
\AdventureWorks.DatabaseTest.dll

4. In VS open DIR\src\AdventureWorksDatabaseTest\SQLServer.cs

This tests the expected properties of the SQL server that hosts your database eg default collation.

It reads the expected values from AdventureWorks.DatabaseTest.dll.config

Delete the ‘ignore line’ as highlighted in the image below:

SQL server unit test - delete ignore line

SQL server unit test

Carry out a similar process for:

DIR\src\AdventureWorksDatabaseTest\SQLDatabase.cs

This tests the expected properties of the SQL database eg the number of tables,views and sprocs.

Rebuild the test project.

5. Go back to MBUnit UI.

Run all tests.

You should get results something similar to below:

MBUnit UI - first test run

MBUnit UI - first test run

6. To get the SQL server tests to pass

In DIR\misc\AdventureWorksDatabase\SQL\DatabaseTestingHelperScripts

Run 001B_SQLServerSettings.sql

This outputs the properties of SQL server.

The results can be pasted into the relevant section in the AdventureWorks.DatabaseTest.dll.config as shown below

SQL server section of test dll config

SQL server section of test dll config

In VS rebuild the test project and then run the tests again in MBUnit.

The SQL server tests should now all pass.

7. Getting the SQL database tests to pass

In DIR\misc\AdventureWorksDatabase\SQL\DatabaseTestingHelperScripts

Run 002_SQLDatabase_SQLObjectSettings.sql

This will give you counts and lists of the all the SQL objects in the database eg number of tables and their names in the database.

Copy the relevant values in the config file

The database properties should look something like the following:

SQL database properties section of test dll config

SQL database properties section of test dll config

The database SQL object counts and lists should like:

SQL objects in the database section of test dll config

SQL objects in the database section of test dll config

In VS rebuild and run the tests again.

The SQL database tests should now all pass.

8. What happens if someone deletes or creates a SQL object?

If the ‘expectations’ in the config are not also updated then the appropriate tests will fail

Try it – run the following SQL script (remember to delete the table after you have finished)

USE AdventureWorks
GO
CREATE TABLE dbo.TestTableToDelete (Id int NOT NULL )
GO

Run the tests again – the tables count test should failed.

Summary
The VS solution is now setup and configured to test the database.

Going forward the SQL server and database tests will provide a core baseline set of unit tests.

A useful by-product of this approach – is that:

  • the test config become your documentation – it sets out the expectations of what should be in the database.
  • the SQL helper files can be used to compare the same db in different environments to ensure it is consistant.

What next
The next blog in the series – 06 Database testing framework – testing a change to an existing table – will look at:

  • creating a base line set of tests for an existing table
  • taking a ‘test first’ approach when making a change to a table

Click to see the complete set of blogs in this series


04 Database testing framework – setting up the visual studio project template

November 17, 2009


Overview
The last blog – 03 Database testing framework – prereqs – looked at the prereqs required to run the framework.

This blog will look at:

  • downloading the database testing framework project template
  • setting up the visual studio solution
  • configuring it to test the AdventureWorks database

Click to see the complete set of blogs in this series

Steps
1. Download the latest version of the database testing project template

As of Feb 2010 this was – 0.2.0.223_DBTemplate.zip.

2. Extract files to where you want to place the database testing solution/project.

For this example it has been extracted to C:\Projects\ as shown in the image below:

DBTemplate default directory structure

DBTemplate default directory structure

The AdventureWorks database is going to be used as an example.

Therefore the top level dir is renamed from

C:\Projects\DBTemplate\

to C:\Projects\AdventureWorks\

Note from this point ‘DIR’ is used as a replacement for ‘C:\Projects\AdventureWorks\’.

3. Next rename the directories with the name of your production database name.

eg all instances of ‘DBTemplate’ are replaced with ‘AdventureWorks’.

At the end it should be like the following:

AdventureWorks directory structure

AdventureWorks directory structure

4. Rename the visual studion (VS) solution file

DIR\src\DBTemplate.sln to

DIR\src\AdventureWorks.sln

Next open the sln file in notepad

Change

Project(“{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}”) = “DBTemplate.DatabaseTest”, “DBTemplateDatabaseTest\DBTemplate.DatabaseTest.csproj”, “{E66055C1-0A72-4FCB-B57F-B017E91BE0F3}”
EndProject

to

Project(“{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}”) = “AdventureWorks.DatabaseTest”, “AdventureWorksDatabaseTest\AdventureWorks.DatabaseTest.csproj”, “{E66055C1-0A72-4FCB-B57F-B017E91BE0F3}”
EndProject

5. Rename the db test project files

Change DIR\src\AdventureWorksDatabaseTest\DBTemplate.DatabaseTest.csproj

to

DIR\src\AdventureWorksDatabaseTest\AdventureWorks.DatabaseTest.csproj

6. Start VS

Open the solution DIR\src\AdventureWorks.sln

The solution structure should be like the following:

AdventureWorks Visual Studio directory structure

AdventureWorks Visual Studio directory structure

Change the project properties to AdventureWorks.DatabaseTest for both the assembly name and default namespace.

7. Set up config file

Rename DIR\src\AdventureWorksDatabaseTest\bin\Debug\

DBTemplate.DatabaseTest.dll.config

to AdventureWorks.DatabaseTest.dll.config

This config file contains:

  • database connection strings
  • the names and number of expected SQL objects

Open it.

Go to the section

Edit to connect to the database instance that you want to test.

For example

DBNameDev can be changed to AdventureWorks

Remember to also change the servername for your environment.

Change the key in the config file:

key=”DBInstance” value=”DBInstanceName” to

key=”DBInstance” value=”AdventureWorks”

ie the <add name="AdventureWorks" – added above.

Summary
So now the default project template has been set up and is now nearly ready to start database testing.

In the next blog in the series – 05 Database testing framework – running your first tests – we will look at:

  • using the SQL helper files
  • running the first test to test the SQL server properties and the overall SQL database properties

*It is possible to automate most of this configuration but I think it is useful to see how to manually change these settings as it gives an insight into how it works.

What next
The next blog in the series – 05 Database testing framework – running your first tests – will look at the steps required to run the tests.

Click to see the complete set of blogs in this series


03 Database testing framework – prereqs

November 14, 2009


Overview
The last blog – 02 Database testing framework – how it works – provided an overview into how it works.

This blog will look at whats required to use the framework.

Click to see the complete set of blogs in this series

Prereqs
01B Database testing framework – feature set for MS SQL and MySQL – shows the supported SQL dbms.

The framework was primarily built for MS 2K5/8 – some of the SQL helper files need to be adapted for use with MS SQL 2K.

The solution template was built with Visual Studio 2K8 – but it should work with other flavours of VS.

MBUnit and other required external libraries are included in the DBTemplate download.

AdventureWorks 2K5 sample database is used as a working example in these blogs.

What next
The next blog in the series – 04 Database testing framework – setting up the visual studio project template – will look at setting up the datbase test solution.

Click to see the complete set of blogs in this series


02 Database testing framework – how it works

November 14, 2009


Overview
The last blog – 01B Database testing framework – feature set for MS SQL and MySQL – compared the different features supported.

This blog will look provide a brief overview on how the framework works.

Click to see the complete set of blogs in this series

What’s included
The framework consists of a downloadable database testing project template.

This includes:

  • Visual studio solution and database testing project template.
  • Databasetest.dll – library referenced by unit tests – used to test the database.
  • SQL helper files – get exisitng properties of a database and can be used to help auto generate C# unit tests.
  • MBUnit – the unit test framework used.
  • all other support files, libraries, project directory structure required to get started.

So how does it work
If you wanted to test a SQL table you would create C# unit test for this table (templates are provided to get you started).

In this you would set up out your ‘expectations’ of the table to be created:

  • number of columns, their names and properties
  • check constraints
  • triggers etc

Run the tests and as the table has not been created yet – they will fail.

Then create the table – if it correctly meets the required expectations – it will pass.

The Databasetest.dll provides an API that allows the above properties of a database to be tested.

Under the covers – it mainly queries the INFORMATION_SCHEMA views of a database to see if the table is present, the number of columns it has, their properties etc.

With the templates provided – the tests can be written relatively quickly and caters for either ‘test first’ or ‘test after’ approaches.

I hope in later blogs to explain how it works in more detail and to put Databasetest.dll source code on SourceForge.

What next
The next blog in the series – 03 Database testing framework – prereqs – will look at the prereqs required to run the framework.

Click to see the complete set of blogs in this series


01A Database testing framework – what it does

November 14, 2009

Here are some of the key capabilities provided by the database testing framework:

1. Allow development teams to build SQL databases in an iterative fashion.

2. Test the number of SQL objects is as expected
eg the number of tables, views, sprocs, functions, triggers, synonyms etc.

3. Test each sql object has its expected properties
eg each table has the number of expected columns, defaults, check constraints unique keys, foreign keys.

4. Test each sql objects returns the expected set of data eg when querying a table, view or executing a sproc.

5. Test that the SQL server and database has the expected properties eg server version, default collation.

6. Test the permissions on sql objects and the security properties of the overall database.

What next
The next blog in the series – 01B Database testing framework – feature set for MS SQL and MySQL – will compare the current features the framework provides for testing MS SQL and MySQL databases.

Click to see the complete set of blogs in this series