|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the <c>SCHEMA_NAME([id])</c>, <c>OBJECT_NAME(object_id [, db_id])</c>, |
| 7 | +/// and <c>OBJECT_SCHEMA_NAME(object_id [, db_id])</c> scalar functions — the |
| 8 | +/// id-to-name inverses of <c>SCHEMA_ID</c> / <c>OBJECT_ID</c>. Probed against |
| 9 | +/// SQL Server 2025 (2026-05-13): result type is <c>sysname</c>; NULL argument |
| 10 | +/// or unknown id returns NULL; <c>SCHEMA_NAME()</c> no-arg form returns the |
| 11 | +/// caller's default schema (<c>dbo</c>); the optional <c>database_id</c> |
| 12 | +/// argument on <c>OBJECT_*</c> is accepted-and-ignored (single-database |
| 13 | +/// simulator). |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public sealed class SchemaNameTests |
| 17 | +{ |
| 18 | + [TestMethod] |
| 19 | + public void SchemaName_Dbo_ReturnsDbo() |
| 20 | + => AreEqual("dbo", new Simulation().ExecuteScalar("select schema_name(1)")); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void SchemaName_Sys_ReturnsSys() |
| 24 | + => AreEqual("sys", new Simulation().ExecuteScalar("select schema_name(4)")); |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void SchemaName_InformationSchema_ReturnsInformationSchema() |
| 28 | + => AreEqual("INFORMATION_SCHEMA", new Simulation().ExecuteScalar("select schema_name(3)")); |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void SchemaName_UserSchema_RoundTripsThroughSchemaId() |
| 32 | + => AreEqual("audit", new Simulation().ExecuteScalar("create schema audit; select schema_name(schema_id('audit'))")); |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void SchemaName_NoArg_ReturnsDbo() |
| 36 | + => AreEqual("dbo", new Simulation().ExecuteScalar("select schema_name()")); |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void SchemaName_NullArg_ReturnsNull() |
| 40 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select schema_name(NULL)")); |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void SchemaName_MissingId_ReturnsNull() |
| 44 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select schema_name(99999)")); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void SchemaName_NegativeId_ReturnsNull() |
| 48 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select schema_name(-1)")); |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void ObjectName_ExistingTable_ReturnsLeafName() |
| 52 | + => AreEqual("foo", new Simulation().ExecuteScalar("create table foo (id int); select object_name(object_id('foo'))")); |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void ObjectName_NullArg_ReturnsNull() |
| 56 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select object_name(NULL)")); |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void ObjectName_MissingId_ReturnsNull() |
| 60 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select object_name(99999)")); |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public void ObjectName_WithDbIdArg_IgnoresArg() |
| 64 | + => AreEqual("foo", new Simulation().ExecuteScalar("create table foo (id int); select object_name(object_id('foo'), 1)")); |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void ObjectName_View_Works() |
| 68 | + => AreEqual("v", new Simulation().ExecuteScalar("create table t (id int); create view v as select id from t; select object_name(object_id('v'))")); |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void ObjectName_TableType_ResolvableViaTypeTableObjectId() |
| 72 | + => AreEqual("MyType", new Simulation().ExecuteScalar("create type MyType as table (id int); select object_name(type_table_object_id) from sys.table_types where name = 'MyType'")); |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public void ObjectSchemaName_DefaultSchemaTable_ReturnsDbo() |
| 76 | + => AreEqual("dbo", new Simulation().ExecuteScalar("create table foo (id int); select object_schema_name(object_id('foo'))")); |
| 77 | + |
| 78 | + [TestMethod] |
| 79 | + public void ObjectSchemaName_QualifiedSchemaTable_ReturnsSchema() |
| 80 | + => AreEqual("audit", new Simulation().ExecuteScalar("create schema audit; create table audit.t (id int); select object_schema_name(object_id('audit.t'))")); |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void ObjectSchemaName_NullArg_ReturnsNull() |
| 84 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select object_schema_name(NULL)")); |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void ObjectSchemaName_MissingId_ReturnsNull() |
| 88 | + => AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select object_schema_name(99999)")); |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void ObjectSchemaName_WithDbIdArg_IgnoresArg() |
| 92 | + => AreEqual("dbo", new Simulation().ExecuteScalar("create table foo (id int); select object_schema_name(object_id('foo'), 1)")); |
| 93 | + |
| 94 | + [TestMethod] |
| 95 | + public void RowConstructorIn_RejectedWithMatchingMsg4145() |
| 96 | + => new Simulation().AssertSqlError( |
| 97 | + "create table t (a int, b int); select * from t where (a, b) in ((1, 2))", |
| 98 | + 4145, |
| 99 | + "An expression of non-boolean type specified in a context where a condition is expected, near ','."); |
| 100 | +} |
0 commit comments