|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the <c>CHOOSE(index, val1, val2, ...)</c> scalar function — the |
| 7 | +/// 1-based variadic picker. Result type is the joint promotion of the value |
| 8 | +/// arms (CASE-style branch-type unification); out-of-range and NULL index |
| 9 | +/// both return typed NULL. |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public sealed class ChooseTests |
| 13 | +{ |
| 14 | + [TestMethod] |
| 15 | + public void Choose_FirstValue_ReturnsFirst() |
| 16 | + => AreEqual("a", new Simulation().ExecuteScalar("select choose(1, 'a', 'b', 'c')")); |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + public void Choose_MiddleValue_ReturnsThat() |
| 20 | + => AreEqual("b", new Simulation().ExecuteScalar("select choose(2, 'a', 'b', 'c')")); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Choose_LastValue_ReturnsLast() |
| 24 | + => AreEqual("c", new Simulation().ExecuteScalar("select choose(3, 'a', 'b', 'c')")); |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Choose_IndexZero_ReturnsNull() |
| 28 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select choose(0, 'a', 'b', 'c')")); |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void Choose_IndexNegative_ReturnsNull() |
| 32 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select choose(-1, 'a', 'b', 'c')")); |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void Choose_IndexBeyondList_ReturnsNull() |
| 36 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select choose(4, 'a', 'b', 'c')")); |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void Choose_NullIndex_ReturnsNull() |
| 40 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select choose(cast(null as int), 'a', 'b', 'c')")); |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void Choose_PickedValueIsNull_ReturnsNull() |
| 44 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select choose(2, 'a', cast(null as varchar(10)), 'c')")); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void Choose_IntegerValues_ReturnsInt() |
| 48 | + => AreEqual(20, new Simulation().ExecuteScalar("select choose(2, 10, 20, 30)")); |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void Choose_MixedTypes_PromotesToHigher() |
| 52 | + => AreEqual(20m, new Simulation().ExecuteScalar("select choose(2, cast(10 as decimal(10, 2)), 20, 30)")); |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void Choose_OnlyIndexNoValues_RaisesMsg174() |
| 56 | + => new Simulation().AssertSqlError("select choose(1)", 174); |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Choose_FromTableValues_RoundTrips() |
| 60 | + => AreEqual("medium", new Simulation().ExecuteScalar(""" |
| 61 | + create table sizes (id int); |
| 62 | + insert sizes values (2); |
| 63 | + select choose(id, 'small', 'medium', 'large') from sizes |
| 64 | + """)); |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void Choose_StringIndexCoerced_Works() |
| 68 | + => AreEqual("b", new Simulation().ExecuteScalar("select choose('2', 'a', 'b', 'c')")); |
| 69 | +} |
0 commit comments