|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests for the <c>OBJECT_ID(name [, type])</c> scalar function. Returns the |
| 8 | +/// table's stable per-database int id (assigned at CREATE, fresh on |
| 9 | +/// DROP-then-recreate) or NULL when not found / wrong type / malformed name. |
| 10 | +/// Probed against SQL Server 2025 (2026-05-11). |
| 11 | +/// </summary> |
| 12 | +[TestClass] |
| 13 | +public sealed class ObjectIdTests |
| 14 | +{ |
| 15 | + [TestMethod] |
| 16 | + public void ObjectId_ExistingTable_ReturnsInt() |
| 17 | + { |
| 18 | + using var reader = new Simulation().ExecuteReader(""" |
| 19 | + create table foo (id int); |
| 20 | + select object_id('foo') as id |
| 21 | + """); |
| 22 | + IsTrue(reader.Read()); |
| 23 | + AreEqual(typeof(int), reader.GetFieldType(0)); |
| 24 | + IsFalse(reader.IsDBNull(0)); |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public void ObjectId_MissingTable_ReturnsNull() |
| 29 | + { |
| 30 | + using var reader = new Simulation().ExecuteReader("select object_id('nope') as id"); |
| 31 | + IsTrue(reader.Read()); |
| 32 | + IsTrue(reader.IsDBNull(0)); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void ObjectId_NullName_ReturnsNull() |
| 37 | + { |
| 38 | + using var reader = new Simulation().ExecuteReader("select object_id(null) as id"); |
| 39 | + IsTrue(reader.Read()); |
| 40 | + IsTrue(reader.IsDBNull(0)); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void ObjectId_NullType_ReturnsNull() |
| 45 | + { |
| 46 | + // Probe-confirmed: a NULL type filter propagates NULL even when the |
| 47 | + // table exists — real SQL Server treats it as "no match". |
| 48 | + using var reader = new Simulation().ExecuteReader(""" |
| 49 | + create table foo (id int); |
| 50 | + select object_id('foo', null) as id |
| 51 | + """); |
| 52 | + IsTrue(reader.Read()); |
| 53 | + IsTrue(reader.IsDBNull(0)); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void ObjectId_UserType_Matches() |
| 58 | + => IsFalseDbNull(new Simulation().ExecuteReader(""" |
| 59 | + create table foo (id int); |
| 60 | + select object_id('foo', 'U') as id |
| 61 | + """)); |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void ObjectId_UserTypeLowercase_Matches() |
| 65 | + => IsFalseDbNull(new Simulation().ExecuteReader(""" |
| 66 | + create table foo (id int); |
| 67 | + select object_id('foo', 'u') as id |
| 68 | + """)); |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void ObjectId_TypeWithWhitespace_ReturnsNull() |
| 72 | + { |
| 73 | + // Probe-confirmed: real SQL Server is whitespace-sensitive on the |
| 74 | + // type filter; ' U ' returns NULL. |
| 75 | + using var reader = new Simulation().ExecuteReader(""" |
| 76 | + create table foo (id int); |
| 77 | + select object_id('foo', ' U ') as id |
| 78 | + """); |
| 79 | + IsTrue(reader.Read()); |
| 80 | + IsTrue(reader.IsDBNull(0)); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void ObjectId_WrongType_ReturnsNull() |
| 85 | + { |
| 86 | + using var reader = new Simulation().ExecuteReader(""" |
| 87 | + create table foo (id int); |
| 88 | + select object_id('foo', 'V') as id |
| 89 | + """); |
| 90 | + IsTrue(reader.Read()); |
| 91 | + IsTrue(reader.IsDBNull(0)); |
| 92 | + } |
| 93 | + |
| 94 | + [TestMethod] |
| 95 | + public void ObjectId_InvalidType_ReturnsNull() |
| 96 | + { |
| 97 | + using var reader = new Simulation().ExecuteReader(""" |
| 98 | + create table foo (id int); |
| 99 | + select object_id('foo', 'XX') as id |
| 100 | + """); |
| 101 | + IsTrue(reader.Read()); |
| 102 | + IsTrue(reader.IsDBNull(0)); |
| 103 | + } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void ObjectId_TwoPartName_Works() |
| 107 | + => IsFalseDbNull(new Simulation().ExecuteReader(""" |
| 108 | + create schema audit; |
| 109 | + create table audit.bar (id int); |
| 110 | + select object_id('audit.bar', 'U') as id |
| 111 | + """)); |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void ObjectId_UnqualifiedResolvesToDbo() |
| 115 | + => AreEqual(new Simulation().ExecuteScalar(""" |
| 116 | + create table dbo.foo (id int); |
| 117 | + select object_id('dbo.foo') as id |
| 118 | + """), new Simulation().ExecuteScalar(""" |
| 119 | + create table dbo.foo (id int); |
| 120 | + select object_id('foo') as id |
| 121 | + """)); |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void ObjectId_BracketedName_Works() |
| 125 | + { |
| 126 | + var bareId = (int)new Simulation().ExecuteScalar(""" |
| 127 | + create table foo (id int); |
| 128 | + select object_id('foo') as id |
| 129 | + """)!; |
| 130 | + var bracketedId = (int)new Simulation().ExecuteScalar(""" |
| 131 | + create table foo (id int); |
| 132 | + select object_id('[dbo].[foo]') as id |
| 133 | + """)!; |
| 134 | + // Same Simulation would be more direct but our infra creates two — |
| 135 | + // assert both are non-null ints. The cross-Simulation values differ. |
| 136 | + AreNotEqual(0, bareId); |
| 137 | + AreNotEqual(0, bracketedId); |
| 138 | + } |
| 139 | + |
| 140 | + [TestMethod] |
| 141 | + public void ObjectId_BracketsAndDots_SingleSimulation() |
| 142 | + { |
| 143 | + using var reader = new Simulation().ExecuteReader(""" |
| 144 | + create table foo (id int); |
| 145 | + select object_id('foo') as a, object_id('[dbo].[foo]') as b, object_id('dbo.foo') as c |
| 146 | + """); |
| 147 | + IsTrue(reader.Read()); |
| 148 | + var a = reader.GetInt32(0); |
| 149 | + AreEqual(a, reader.GetInt32(1)); |
| 150 | + AreEqual(a, reader.GetInt32(2)); |
| 151 | + } |
| 152 | + |
| 153 | + [TestMethod] |
| 154 | + public void ObjectId_ThreePartCorrectDb_Works() |
| 155 | + => IsFalseDbNull(new Simulation().ExecuteReader(""" |
| 156 | + create table foo (id int); |
| 157 | + select object_id('simulated.dbo.foo', 'U') as id |
| 158 | + """)); |
| 159 | + |
| 160 | + [TestMethod] |
| 161 | + public void ObjectId_ThreePartWrongDb_ReturnsNull() |
| 162 | + { |
| 163 | + using var reader = new Simulation().ExecuteReader(""" |
| 164 | + create table foo (id int); |
| 165 | + select object_id('baddb.dbo.foo', 'U') as id |
| 166 | + """); |
| 167 | + IsTrue(reader.Read()); |
| 168 | + IsTrue(reader.IsDBNull(0)); |
| 169 | + } |
| 170 | + |
| 171 | + [TestMethod] |
| 172 | + public void ObjectId_FourPartName_ReturnsNull() |
| 173 | + { |
| 174 | + // Linked-server names aren't modeled — real SQL Server also returns |
| 175 | + // NULL for a 4-part name pointing at a non-existent linked server. |
| 176 | + using var reader = new Simulation().ExecuteReader(""" |
| 177 | + create table foo (id int); |
| 178 | + select object_id('linked.simulated.dbo.foo', 'U') as id |
| 179 | + """); |
| 180 | + IsTrue(reader.Read()); |
| 181 | + IsTrue(reader.IsDBNull(0)); |
| 182 | + } |
| 183 | + |
| 184 | + [TestMethod] |
| 185 | + public void ObjectId_StableAcrossCalls() |
| 186 | + { |
| 187 | + using var reader = new Simulation().ExecuteReader(""" |
| 188 | + create table foo (id int); |
| 189 | + select object_id('foo') as a, object_id('foo') as b |
| 190 | + """); |
| 191 | + IsTrue(reader.Read()); |
| 192 | + AreEqual(reader.GetInt32(0), reader.GetInt32(1)); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public void ObjectId_DistinctAcrossTables() |
| 197 | + { |
| 198 | + using var reader = new Simulation().ExecuteReader(""" |
| 199 | + create table foo (id int); |
| 200 | + create table bar (id int); |
| 201 | + select object_id('foo') as foo, object_id('bar') as bar |
| 202 | + """); |
| 203 | + IsTrue(reader.Read()); |
| 204 | + AreNotEqual(reader.GetInt32(0), reader.GetInt32(1)); |
| 205 | + } |
| 206 | + |
| 207 | + [TestMethod] |
| 208 | + public void ObjectId_FreshAfterDropAndRecreate() |
| 209 | + { |
| 210 | + // Probe-confirmed against SQL Server 2025: drop-then-recreate yields |
| 211 | + // a fresh int id, the prior value is gone. |
| 212 | + using var reader = new Simulation().ExecuteReader(""" |
| 213 | + create table foo (id int); |
| 214 | + declare @before int = object_id('foo'); |
| 215 | + drop table foo; |
| 216 | + create table foo (id int); |
| 217 | + select @before as before_id, object_id('foo') as after_id |
| 218 | + """); |
| 219 | + IsTrue(reader.Read()); |
| 220 | + AreNotEqual(reader.GetInt32(0), reader.GetInt32(1)); |
| 221 | + } |
| 222 | + |
| 223 | + [TestMethod] |
| 224 | + public void ObjectId_VariableArgument_RuntimeEvaluated() |
| 225 | + { |
| 226 | + using var reader = new Simulation().ExecuteReader(""" |
| 227 | + create table foo (id int); |
| 228 | + declare @n nvarchar(100) = 'foo'; |
| 229 | + select object_id(@n) as id |
| 230 | + """); |
| 231 | + IsTrue(reader.Read()); |
| 232 | + IsFalse(reader.IsDBNull(0)); |
| 233 | + } |
| 234 | + |
| 235 | + [TestMethod] |
| 236 | + public void ObjectId_AfterDrop_IsNull() |
| 237 | + { |
| 238 | + using var reader = new Simulation().ExecuteReader(""" |
| 239 | + create table foo (id int); |
| 240 | + drop table foo; |
| 241 | + select object_id('foo') as id |
| 242 | + """); |
| 243 | + IsTrue(reader.Read()); |
| 244 | + IsTrue(reader.IsDBNull(0)); |
| 245 | + } |
| 246 | + |
| 247 | + [TestMethod] |
| 248 | + public void ObjectId_TempTable_Resolves() |
| 249 | + { |
| 250 | + // Divergence from real SQL Server (documented quirk): the simulator |
| 251 | + // routes # leaves to the connection's temp dict regardless of the |
| 252 | + // current db, so OBJECT_ID('#foo') finds the session's temp table |
| 253 | + // directly rather than requiring tempdb..#foo. |
| 254 | + using var reader = new Simulation().ExecuteReader(""" |
| 255 | + create table #foo (id int); |
| 256 | + select object_id('#foo') as id |
| 257 | + """); |
| 258 | + IsTrue(reader.Read()); |
| 259 | + IsFalse(reader.IsDBNull(0)); |
| 260 | + } |
| 261 | + |
| 262 | + [TestMethod] |
| 263 | + public void ObjectId_IfExistsIdiom_Works() |
| 264 | + { |
| 265 | + // The dominant real-world use of OBJECT_ID is the safe-drop pattern. |
| 266 | + // Confirm it threads end-to-end against an existing table. |
| 267 | + _ = new Simulation().ExecuteNonQuery(""" |
| 268 | + create table foo (id int); |
| 269 | + if object_id('dbo.foo', 'U') is not null drop table foo; |
| 270 | + create table foo (id int) |
| 271 | + """); |
| 272 | + } |
| 273 | + |
| 274 | + [TestMethod] |
| 275 | + public void ObjectId_IfExistsIdiom_NoExistingTable() |
| 276 | + => _ = new Simulation().ExecuteNonQuery(""" |
| 277 | + if object_id('dbo.foo', 'U') is not null drop table foo; |
| 278 | + create table foo (id int) |
| 279 | + """); |
| 280 | + |
| 281 | + [TestMethod] |
| 282 | + public void ObjectId_EmptyString_ReturnsNull() |
| 283 | + { |
| 284 | + using var reader = new Simulation().ExecuteReader("select object_id('') as id"); |
| 285 | + IsTrue(reader.Read()); |
| 286 | + IsTrue(reader.IsDBNull(0)); |
| 287 | + } |
| 288 | + |
| 289 | + [TestMethod] |
| 290 | + public void ObjectId_TempdbDotDotHash_Resolves() |
| 291 | + => IsFalseDbNull(new Simulation().ExecuteReader(""" |
| 292 | + create table #foo (id int); |
| 293 | + select object_id('tempdb..#foo') as id |
| 294 | + """)); |
| 295 | + |
| 296 | + [TestMethod] |
| 297 | + public void ObjectId_ResultTypeIsInt32() |
| 298 | + { |
| 299 | + using var reader = new Simulation().ExecuteReader(""" |
| 300 | + create table foo (id int); |
| 301 | + select object_id('foo') as id |
| 302 | + """); |
| 303 | + IsTrue(reader.Read()); |
| 304 | + AreEqual(typeof(int), reader.GetFieldType(0)); |
| 305 | + } |
| 306 | + |
| 307 | + [TestMethod] |
| 308 | + public void ObjectId_TooManyArgs_RaisesMsg174() |
| 309 | + => new Simulation().AssertSqlError("select object_id('foo', 'U', 'extra')", 174); |
| 310 | + |
| 311 | + private static void IsFalseDbNull(DbDataReader reader) |
| 312 | + { |
| 313 | + using (reader) |
| 314 | + { |
| 315 | + IsTrue(reader.Read()); |
| 316 | + IsFalse(reader.IsDBNull(0)); |
| 317 | + } |
| 318 | + } |
| 319 | +} |
0 commit comments