简体   繁体   中英

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

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;
    }

I have wrapped the same helper method above to use asserts. It helps in debugging a unit test.

private void AssertTableRecordsAreEqual(DataTable expectedTable, DataTable actualTable)
    {
        Assert.IsNotNull(actualTable, "Table is empty");
        Assert.AreEqual(expectedTable.Columns.Count, actualTable.Columns.Count, "Number of columns in actual and expected tables are different");
        Assert.AreEqual(expectedTable.Rows.Count, actualTable.Rows.Count, "Number of records in actual and expected tables are different");
        Assert.IsFalse(expectedTable.Columns.Cast<DataColumn>().Any(dc => !actualTable.Columns.Contains(dc.ColumnName)), "Table column names are different");

        for (int i = 0; i <= expectedTable.Rows.Count - 1; i++)
        {
            Assert.IsFalse(expectedTable.Columns.Cast<DataColumn>().Any(dc1 => expectedTable.Rows[i][dc1.ColumnName].ToString() != actualTable.Rows[i][dc1.ColumnName].ToString()), "Table row value is different");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM