Click here to Skip to main content
15,887,248 members
Articles / Programming Languages / XML

Approach to unit testing of .NET database applications with NDbUnit and XPath queries

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
22 Jun 200712 min read 70K   412   44  
The focus is on how one can test the content of a database to see if it adheres to certain assumptions
using System.Xml;
using NDbUnit.Core;
using NDbUnitXPath.Schemas;
using NUnit.Framework;

namespace NDbUnitQuery
{
    [TestFixture]
    public class DetailedTesting : TestBase
    {
        XmlDocument xmlDocument;
        
        [TestFixtureSetUp]
        public void SetUpFixture()
        {
            SetUpDatabase(
                    ConnectionString,
                    Util.AssemblyResourceStream("Schemas.DBSchema.xsd"),
                    Util.AssemblyResourceStream("TestData.Initial.xml"),
                    DbOperationFlag.CleanInsertIdentity
            );

        }

        [Test]
        public void AroundTheHornOrderedCPUs()
        {
            xmlDocument = ResultInspector.LoadTablesAsXML<DBSchema>(
                ConnectionString,
                false,
                "Customers",
                "Orders",
                "OrderDetails"
            );
            Assert.IsTrue(
                0 < xmlDocument.SelectNodes(
                        "//Orders[" +
                        "       CustomerID=//Customers[CompanyName='Around the Horn']/CustomerID" +
                        "   and OrderID=//OrderDetails[SKU='CPU-64X2']/OrderID" +
                        "]").Count
            );
        }

        [Test]
        public void MichelaHasCreatedExactlyOneOrder()
        {
            xmlDocument = ResultInspector.LoadTablesAsXML<DBSchema>(
                ConnectionString,
                false,
                "Orders",
                "Employees"
            );
            Assert.AreEqual(1, xmlDocument.SelectNodes("//Orders[EmployeeID=//Employees[LastName='Michela']/EmployeeID]").Count);
        }

        [Test]
        public void AroundTheHornHasBigOrders()
        {
            xmlDocument = ResultInspector.LoadTablesAsXML<DBSchema>(
                ConnectionString,
                true,
                "Customers",
                "Orders",
                "OrderDetails"
            );
            Assert.IsTrue(0 < xmlDocument.SelectNodes("//Customers[CompanyName='Around the Horn']/Orders/OrderDetails[Quantity>=100]").Count);
        }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
Serioja is a senior software developer at HintTech and he implements .NET applications on a consultancy basis for different clients.

Comments and Discussions