Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two DataTables (Unit Testing, Integration Testing, C#, TestMethod)

What kind of tests should be carried out in unit testing when comparing Datatables that are supposed to be different and have multiple rows.

    [TestMethod]
    public void ExecuteOutWithMultipleDataTables()
    {
        //Arrange
        int id1 = TestOrderBuilder.New().Build();
        DataTable dtDefault = CreateDefaultDataTable(id1, "OUT", "TableDesc", DateTime.Now);

        //Act
        object[] result = OracleDatabase.ExecuteOut(SqlStatements.Cursor, procedureParameters);
        DataTable dtResults = result[0] as DataTable;

        //Assert
        Assert.IsNotNull(dtDefault);
        Assert.IsNotNull(dtResults);
        Assert.AreNotEqual(dtDefault, dtResults);
        Assert.AreNotSame(dtDefault.Rows[0][0], dtResults.Rows[0][0]);
        Assert.AreNotSame(dtDefault.Rows[0][1], dtResults.Rows[0][1]);
    }

This is an example of some of what I have wrote already, but I am unsure if I am on the right track.

Does anyone have advice?

Mac

like image 327
Mac Avatar asked Mar 05 '13 10:03

Mac


1 Answers

You need to write a helper method if you need to verify each row and column value.

Also , it does not looks like an unit test because it looks you are calling real database rather then mocking it.

may be something like below

private bool IsTableSame(DataTable t1, DataTable t2)
    {
        if (t1 == null)
            return false;
        if (t2 == null)
            return false;
        if (t1.Rows.Count != t2.Rows.Count)
            return false;

        if (t1.Columns.Count != t2.Columns.Count)
            return false;

        if (t1.Columns.Cast<DataColumn>().Any(dc => !t2.Columns.Contains(dc.ColumnName)))
        {
            return false;
        }

        for (int i = 0; i <= t1.Rows.Count-1; i++)
        {
            if (t1.Columns.Cast<DataColumn>().Any(dc1 => t1.Rows[i][dc1.ColumnName].ToString() != t2.Rows[i][dc1.ColumnName].ToString()))
            {
                return false;
            } 
        }

        return true;
    }
like image 159
TalentTuner Avatar answered Nov 09 '22 18:11

TalentTuner