DataTable collation check

I am working on creating a custom database validation condition in Visual Studio, and I am trying to verify the sort order.

I have no problem verifying that the rows in the DataTable are actually sorted, but I also want to verify that each sort column has at least one group with more than one distinct value.

I maintain a list of integers that define the sorted columns (negative integers indicate the descending sort order), and the absolute value of the integer is the column number.

Let me clarify.

I am writing a unit test condition that tests a sorted DataTable. I can verify that the row is in the correct sort order, but I want to verify that the result set contains data that actually checks the sort. For words, a result set with only one row or any number of duplicate rows is indeed sorted, but this does not actually verify the sorting.

I need to verify that each sort level contains at least two different values ​​in at least one group.

In the past, I used a combination of SQL and .Net code and used the following set of queries to make sure that there was enough data to check the sorting.

SELECT TOP 1 COUNT(DISTINCT column1) 
FROM table 
HAVING COUNT(DISTINCT column1) > 1

SELECT TOP 1 column1, COUNT(DISTINCT column2) 
FROM table 
GROUP BY column1 
HAVING COUNT(DISTINCT column2) > 1

SELECT TOP 1 column1, column2, COUNT(DISTINCT column3)
FROM table
GROUP BY column1, column2
HAVING COUNT(DISTINCT column3) > 1

Where I have a query for each level, and all queries must return a non-empty result set in order to pass the unit test.

: / SQL LINQ?

+3
2

, ,

for (int i = 1; i < rowCount; i++) {
    for (int j = 0; j < sortColCount; j++) {
        int col = colNum(sortCols[j]);
        int d = Compare(GetValue(rows[i], col), GetValue(rows[i-1], col));
        if (d == 0) continue;
        // Insert here
        Assert(d == colDir(sortCols[j]));
    }
}
, , d (, , bool). , , .
0

, DataTable TableNewRow, RowChanging RowChanged / , DataTable , .

:

foreach ( int i = 1 ; i < dt.Rows.Length ; ++i )
{
  DataRow prev = dt.Rows[i-1] ;
  DataRow curr = dt.Rows[i  ] ;

  Assert( CompareRows(prev,curr) <= 0 ) ;

}

CompareRows(prev,curr) : -1 prev < curr, 0 prev == curr +1 prev > curr);

0

Source: https://habr.com/ru/post/1787791/


All Articles