|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for SQL aggregate functions: COUNT/COUNT_BIG/SUM/AVG/ |
| 8 | +/// MAX/MIN, the statistical family (STDEV, STDEVP, VAR, VARP), |
| 9 | +/// STRING_AGG, CHECKSUM_AGG, APPROX_COUNT_DISTINCT — both standalone and |
| 10 | +/// in combination with GROUP BY / HAVING. Result types and NULL / empty- |
| 11 | +/// input semantics are sourced from probes against SQL Server 2025. |
| 12 | +/// </summary> |
| 13 | +[TestClass] |
| 14 | +public sealed class AggregateTests |
| 15 | +{ |
| 16 | + private static DbConnection Seeded(string schema, string values) |
| 17 | + { |
| 18 | + var connection = new Simulation().CreateOpenConnection(); |
| 19 | + _ = connection.CreateCommand($"create table t ({schema})").ExecuteNonQuery(); |
| 20 | + if (!string.IsNullOrEmpty(values)) |
| 21 | + _ = connection.CreateCommand($"insert into t values {values}").ExecuteNonQuery(); |
| 22 | + return connection; |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Count_Star_CountsRowsIncludingNullColumns() |
| 27 | + { |
| 28 | + using var connection = Seeded("a int", "(1), (2), (null), (3)"); |
| 29 | + AreEqual(4, connection.CreateCommand("select count(*) from t").ExecuteScalar()); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public void Count_Column_SkipsNulls() |
| 34 | + { |
| 35 | + using var connection = Seeded("a int", "(1), (2), (null), (3)"); |
| 36 | + AreEqual(3, connection.CreateCommand("select count(a) from t").ExecuteScalar()); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Count_Distinct_DedupsAndSkipsNulls() |
| 41 | + { |
| 42 | + using var connection = Seeded("a int", "(1), (2), (1), (null), (2)"); |
| 43 | + AreEqual(2, connection.CreateCommand("select count(distinct a) from t").ExecuteScalar()); |
| 44 | + } |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void Count_EmptyInput_ReturnsZero() |
| 48 | + { |
| 49 | + // Only aggregate that doesn't return NULL on empty input. |
| 50 | + using var connection = Seeded("a int", ""); |
| 51 | + AreEqual(0, connection.CreateCommand("select count(*) from t").ExecuteScalar()); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void CountBig_StarAlias_ReturnsBigInt() |
| 56 | + { |
| 57 | + using var connection = Seeded("a int", "(1), (2), (3)"); |
| 58 | + AreEqual(3L, connection.CreateCommand("select count_big(*) from t").ExecuteScalar()); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void Sum_Int_TracksTotalSkippingNulls() |
| 63 | + { |
| 64 | + using var connection = Seeded("a int", "(10), (20), (null), (30)"); |
| 65 | + AreEqual(60, connection.CreateCommand("select sum(a) from t").ExecuteScalar()); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void Sum_Decimal_PreservesScale() |
| 70 | + { |
| 71 | + // SQL Server: SUM(decimal(p, s)) → decimal(38, s). Probed against |
| 72 | + // SQL Server 2025 — scale 2 stays 2. |
| 73 | + using var connection = Seeded("p decimal(10, 2)", "(1.50), (2.50), (3.00)"); |
| 74 | + AreEqual(7.00m, connection.CreateCommand("select sum(p) from t").ExecuteScalar()); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public void Sum_EmptyInput_ReturnsNull() |
| 79 | + { |
| 80 | + using var connection = Seeded("a int", ""); |
| 81 | + AreEqual(DBNull.Value, connection.CreateCommand("select sum(a) from t").ExecuteScalar()); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void Sum_AllNullInput_ReturnsNull() |
| 86 | + { |
| 87 | + using var connection = Seeded("a int", "(null), (null)"); |
| 88 | + AreEqual(DBNull.Value, connection.CreateCommand("select sum(a) from t").ExecuteScalar()); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void Sum_IntOverflow_RaisesMsg8115() |
| 93 | + { |
| 94 | + using var connection = Seeded("a int", "(2147483647), (1)"); |
| 95 | + var ex = Throws<DbException>(() => connection.CreateCommand("select sum(a) from t").ExecuteScalar()); |
| 96 | + AreEqual("8115", ex.Data["HelpLink.EvtID"]); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public void Avg_Int_TruncatesByIntegerDivision() |
| 101 | + { |
| 102 | + // SQL Server: `avg(int)` returns int via integer division. (1+2+2)/3 = 1. |
| 103 | + using var connection = Seeded("a int", "(1), (2), (2)"); |
| 104 | + AreEqual(1, connection.CreateCommand("select avg(a) from t").ExecuteScalar()); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void Avg_Decimal_WidensToDecimal38_6() |
| 109 | + { |
| 110 | + // SQL Server: avg(decimal(p, s)) → decimal(38, max(s, 6)). Scale 2 |
| 111 | + // input → scale 6 output. |
| 112 | + using var connection = Seeded("p decimal(10, 2)", "(1.50), (2.50), (3.00)"); |
| 113 | + var result = connection.CreateCommand("select avg(p) from t").ExecuteScalar(); |
| 114 | + AreEqual(2.333333m, result); |
| 115 | + } |
| 116 | + |
| 117 | + [TestMethod] |
| 118 | + public void Max_OnInt() |
| 119 | + { |
| 120 | + using var connection = Seeded("a int", "(10), (5), (20), (null)"); |
| 121 | + AreEqual(20, connection.CreateCommand("select max(a) from t").ExecuteScalar()); |
| 122 | + } |
| 123 | + |
| 124 | + [TestMethod] |
| 125 | + public void Min_OnInt() |
| 126 | + { |
| 127 | + using var connection = Seeded("a int", "(10), (5), (20), (null)"); |
| 128 | + AreEqual(5, connection.CreateCommand("select min(a) from t").ExecuteScalar()); |
| 129 | + } |
| 130 | + |
| 131 | + [TestMethod] |
| 132 | + public void MaxMin_OnString_ByCollationOrder() |
| 133 | + { |
| 134 | + using var connection = Seeded("s nvarchar(20)", "('alpha'), ('gamma'), ('beta')"); |
| 135 | + AreEqual("gamma", connection.CreateCommand("select max(s) from t").ExecuteScalar()); |
| 136 | + AreEqual("alpha", connection.CreateCommand("select min(s) from t").ExecuteScalar()); |
| 137 | + } |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void MaxMin_EmptyInput_ReturnsNull() |
| 141 | + { |
| 142 | + using var connection = Seeded("a int", ""); |
| 143 | + AreEqual(DBNull.Value, connection.CreateCommand("select max(a) from t").ExecuteScalar()); |
| 144 | + AreEqual(DBNull.Value, connection.CreateCommand("select min(a) from t").ExecuteScalar()); |
| 145 | + } |
| 146 | + |
| 147 | + [TestMethod] |
| 148 | + public void Max_OnText_RaisesMsg8117() |
| 149 | + { |
| 150 | + // SQL Server's Msg 8117: LOB types can't participate in MAX/MIN. |
| 151 | + using var connection = Seeded("t text", "('x')"); |
| 152 | + var ex = Throws<DbException>(() => connection.CreateCommand("select max(t) from t").ExecuteScalar()); |
| 153 | + AreEqual("8117", ex.Data["HelpLink.EvtID"]); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public void Stdev_SingleRow_ReturnsNull() |
| 158 | + { |
| 159 | + // Sample stddev needs n > 1. Single row → divide by zero → NULL. |
| 160 | + using var connection = Seeded("a int", "(5)"); |
| 161 | + AreEqual(DBNull.Value, connection.CreateCommand("select stdev(a) from t").ExecuteScalar()); |
| 162 | + } |
| 163 | + |
| 164 | + [TestMethod] |
| 165 | + public void StdevP_SingleRow_ReturnsZero() |
| 166 | + { |
| 167 | + // Population stddev with n=1 has zero deviation. |
| 168 | + using var connection = Seeded("a int", "(5)"); |
| 169 | + AreEqual(0d, connection.CreateCommand("select stdevp(a) from t").ExecuteScalar()); |
| 170 | + } |
| 171 | + |
| 172 | + [TestMethod] |
| 173 | + public void Var_VarP_OverIntegerColumn() |
| 174 | + { |
| 175 | + // 10, 20, 30: mean=20, sample var = ((10-20)^2 + 0 + (30-20)^2) / 2 = 100. |
| 176 | + // Population var = 200/3 ≈ 66.67. |
| 177 | + using var connection = Seeded("a int", "(10), (20), (30)"); |
| 178 | + AreEqual(100d, connection.CreateCommand("select var(a) from t").ExecuteScalar()); |
| 179 | + var pop = (double)connection.CreateCommand("select varp(a) from t").ExecuteScalar()!; |
| 180 | + IsLessThan(1e-5, Math.Abs(pop - 66.6666666)); |
| 181 | + } |
| 182 | + |
| 183 | + [TestMethod] |
| 184 | + public void StringAgg_ConcatsWithSeparator() |
| 185 | + { |
| 186 | + using var connection = Seeded("s nvarchar(20)", "('a'), ('b'), ('c')"); |
| 187 | + AreEqual("a,b,c", connection.CreateCommand("select string_agg(s, ',') from t").ExecuteScalar()); |
| 188 | + } |
| 189 | + |
| 190 | + [TestMethod] |
| 191 | + public void StringAgg_SkipsNulls() |
| 192 | + { |
| 193 | + using var connection = Seeded("s nvarchar(20)", "('a'), (null), ('b')"); |
| 194 | + AreEqual("a,b", connection.CreateCommand("select string_agg(s, ',') from t").ExecuteScalar()); |
| 195 | + } |
| 196 | + |
| 197 | + [TestMethod] |
| 198 | + public void StringAgg_EmptyInput_ReturnsNull() |
| 199 | + { |
| 200 | + using var connection = Seeded("s nvarchar(20)", ""); |
| 201 | + AreEqual(DBNull.Value, connection.CreateCommand("select string_agg(s, ',') from t").ExecuteScalar()); |
| 202 | + } |
| 203 | + |
| 204 | + [TestMethod] |
| 205 | + public void ChecksumAgg_OrderIndependentFold() |
| 206 | + { |
| 207 | + // CHECKSUM_AGG's semantic guarantee is order-independence; the bit |
| 208 | + // pattern itself isn't pinned (SQL Server's exact algorithm is |
| 209 | + // implementation-defined). The simulator's contract: same multiset |
| 210 | + // of inputs → same checksum. |
| 211 | + using var ascending = Seeded("a int", "(1), (2), (3)"); |
| 212 | + using var reversed = Seeded("a int", "(3), (2), (1)"); |
| 213 | + AreEqual( |
| 214 | + ascending.CreateCommand("select checksum_agg(a) from t").ExecuteScalar(), |
| 215 | + reversed.CreateCommand("select checksum_agg(a) from t").ExecuteScalar()); |
| 216 | + } |
| 217 | + |
| 218 | + [TestMethod] |
| 219 | + public void ApproxCountDistinct_BehavesLikeCountDistinct() |
| 220 | + { |
| 221 | + // Simulator implements APPROX_COUNT_DISTINCT as exact COUNT(DISTINCT) |
| 222 | + // since memory optimization isn't a goal. Still returns bigint. |
| 223 | + using var connection = Seeded("a int", "(1), (2), (1), (null), (3)"); |
| 224 | + AreEqual(3L, connection.CreateCommand("select approx_count_distinct(a) from t").ExecuteScalar()); |
| 225 | + } |
| 226 | + |
| 227 | + [TestMethod] |
| 228 | + public void GroupBy_PartitionsByKey() |
| 229 | + { |
| 230 | + using var connection = Seeded("s nvarchar(20), a int", "('alpha', 1), ('alpha', 2), ('beta', 5), ('beta', 7)"); |
| 231 | + using var reader = connection.CreateCommand("select s, sum(a) from t group by s").ExecuteReader(); |
| 232 | + var totals = new Dictionary<string, int>(); |
| 233 | + while (reader.Read()) |
| 234 | + totals[(string)reader[0]] = (int)reader[1]; |
| 235 | + AreEqual(3, totals["alpha"]); |
| 236 | + AreEqual(12, totals["beta"]); |
| 237 | + } |
| 238 | + |
| 239 | + [TestMethod] |
| 240 | + public void GroupBy_NullKey_OneBucketForNulls() |
| 241 | + { |
| 242 | + // SQL Server: NULL is a valid group key with exactly one bucket. |
| 243 | + using var connection = Seeded("a int, b int", "(null, 1), (null, 2), (1, 5)"); |
| 244 | + using var reader = connection.CreateCommand("select a, sum(b) from t group by a").ExecuteReader(); |
| 245 | + var seen = new List<(object key, int sum)>(); |
| 246 | + while (reader.Read()) |
| 247 | + seen.Add((reader[0], (int)reader[1])); |
| 248 | + HasCount(2, seen); |
| 249 | + } |
| 250 | + |
| 251 | + [TestMethod] |
| 252 | + public void GroupBy_Having_FiltersByAggregatePredicate() |
| 253 | + { |
| 254 | + using var connection = Seeded("s nvarchar(20)", "('alpha'), ('alpha'), ('beta'), ('gamma')"); |
| 255 | + using var reader = connection.CreateCommand("select s, count(*) from t group by s having count(*) > 1").ExecuteReader(); |
| 256 | + var rows = 0; |
| 257 | + while (reader.Read()) |
| 258 | + { |
| 259 | + AreEqual("alpha", reader[0]); |
| 260 | + AreEqual(2, reader[1]); |
| 261 | + rows++; |
| 262 | + } |
| 263 | + AreEqual(1, rows); |
| 264 | + } |
| 265 | +} |
0 commit comments