|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Behavioral tests for the parse-and-store-but-no-search xml surface |
| 7 | +/// (<c>xml</c> as a column type, <c>xml(schema_collection)</c> binding, |
| 8 | +/// <c>CREATE/DROP XML SCHEMA COLLECTION</c>, <c>CREATE [PRIMARY] XML |
| 9 | +/// INDEX</c>, plus <c>sys.xml_schema_collections</c> / <c>sys.xml_indexes</c>). |
| 10 | +/// XPath / XQuery methods (<c>.value()</c> / <c>.nodes()</c> / <c>.query()</c> |
| 11 | +/// / <c>.exist()</c> / <c>.modify()</c>) raise <see cref="NotSupportedException"/> |
| 12 | +/// at execute time. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class XmlTests |
| 16 | +{ |
| 17 | + [TestMethod] |
| 18 | + public void XmlColumn_AcceptsAndRoundTrips() |
| 19 | + { |
| 20 | + var sim = new Simulation(); |
| 21 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 22 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<root><child>hi</child></root>')"); |
| 23 | + var read = sim.ExecuteScalar("select body from dbo.doc where id = 1"); |
| 24 | + AreEqual("<root><child>hi</child></root>", read); |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public void XmlColumn_NullStoresAsNull() |
| 29 | + { |
| 30 | + var sim = new Simulation(); |
| 31 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml null)"); |
| 32 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, null)"); |
| 33 | + AreEqual(DBNull.Value, sim.ExecuteScalar("select body from dbo.doc")); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void SysColumns_ReportsXmlTypeIdentity() |
| 38 | + { |
| 39 | + var sim = new Simulation(); |
| 40 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 41 | + AreEqual("xml", sim.ExecuteScalar(@" |
| 42 | + select t.name from sys.columns c |
| 43 | + join sys.types t on t.user_type_id = c.user_type_id |
| 44 | + where c.object_id = object_id('dbo.doc') and c.name = 'body'")); |
| 45 | + } |
| 46 | + |
| 47 | + [TestMethod] |
| 48 | + public void CreateXmlSchemaCollection_Succeeds() |
| 49 | + { |
| 50 | + var sim = new Simulation(); |
| 51 | + _ = sim.ExecuteNonQuery(@"create xml schema collection xsc1 as N'<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><xsd:element name=""root"" type=""xsd:string"" /></xsd:schema>'"); |
| 52 | + AreEqual(1, sim.ExecuteScalar("select count(*) from sys.xml_schema_collections where name = 'xsc1'")); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void CreateXmlSchemaCollection_QualifiedSchema_Succeeds() |
| 57 | + { |
| 58 | + var sim = new Simulation(); |
| 59 | + _ = sim.ExecuteNonQuery("create schema audit"); |
| 60 | + _ = sim.ExecuteNonQuery("create xml schema collection audit.xsc1 as N'<xsd:schema/>'"); |
| 61 | + var schemaId = sim.ExecuteScalar("select schema_id from sys.xml_schema_collections where name = 'xsc1'"); |
| 62 | + var auditId = sim.ExecuteScalar("select schema_id from sys.schemas where name = 'audit'"); |
| 63 | + AreEqual(auditId, schemaId); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void CreateXmlSchemaCollection_DuplicateName_Raises219() |
| 68 | + { |
| 69 | + var sim = new Simulation(); |
| 70 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 71 | + _ = sim.AssertSqlError("create xml schema collection xsc1 as N'<xsd:schema/>'", 219); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public void XmlColumn_WithSchemaCollection_Binds() |
| 76 | + { |
| 77 | + var sim = new Simulation(); |
| 78 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 79 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml(xsc1))"); |
| 80 | + // The binding round-trips through INSERT/SELECT — payload still stores |
| 81 | + // as raw text since no XSD validation runs. |
| 82 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<hi/>')"); |
| 83 | + AreEqual("<hi/>", sim.ExecuteScalar("select body from dbo.doc")); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void XmlColumn_WithContentDiscriminator_Parses() |
| 88 | + { |
| 89 | + var sim = new Simulation(); |
| 90 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 91 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml(content xsc1))"); |
| 92 | + AreEqual(1, sim.ExecuteScalar("select count(*) from sys.tables where name = 'doc'")); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void XmlColumn_WithDocumentDiscriminator_Parses() |
| 97 | + { |
| 98 | + var sim = new Simulation(); |
| 99 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 100 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml(document xsc1))"); |
| 101 | + AreEqual(1, sim.ExecuteScalar("select count(*) from sys.tables where name = 'doc'")); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void XmlColumn_WithUnknownCollection_Raises208() |
| 106 | + { |
| 107 | + var sim = new Simulation(); |
| 108 | + _ = sim.AssertSqlError("create table dbo.doc (id int, body xml(no_such_collection))", 208); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public void DropXmlSchemaCollection_Removes() |
| 113 | + { |
| 114 | + var sim = new Simulation(); |
| 115 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 116 | + _ = sim.ExecuteNonQuery("drop xml schema collection xsc1"); |
| 117 | + AreEqual(0, sim.ExecuteScalar("select count(*) from sys.xml_schema_collections")); |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + public void CreatePrimaryXmlIndex_Succeeds() |
| 122 | + { |
| 123 | + var sim = new Simulation(); |
| 124 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int not null primary key, body xml)"); |
| 125 | + _ = sim.ExecuteNonQuery("create primary xml index pxml_doc on dbo.doc(body)"); |
| 126 | + AreEqual(1, sim.ExecuteScalar("select count(*) from sys.xml_indexes")); |
| 127 | + AreEqual("XML", sim.ExecuteScalar("select type_desc from sys.xml_indexes where name = 'pxml_doc'")); |
| 128 | + } |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public void CreateSecondaryXmlIndex_PathValueProperty_Stores() |
| 132 | + { |
| 133 | + var sim = new Simulation(); |
| 134 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int not null primary key, body xml)"); |
| 135 | + _ = sim.ExecuteNonQuery("create primary xml index pxml_doc on dbo.doc(body)"); |
| 136 | + _ = sim.ExecuteNonQuery("create xml index sxml_path on dbo.doc(body) using xml index pxml_doc for path"); |
| 137 | + _ = sim.ExecuteNonQuery("create xml index sxml_value on dbo.doc(body) using xml index pxml_doc for value"); |
| 138 | + _ = sim.ExecuteNonQuery("create xml index sxml_property on dbo.doc(body) using xml index pxml_doc for property"); |
| 139 | + AreEqual(4, sim.ExecuteScalar("select count(*) from sys.xml_indexes")); |
| 140 | + AreEqual("PATH", sim.ExecuteScalar("select secondary_type_desc from sys.xml_indexes where name = 'sxml_path'")); |
| 141 | + AreEqual("VALUE", sim.ExecuteScalar("select secondary_type_desc from sys.xml_indexes where name = 'sxml_value'")); |
| 142 | + AreEqual("PROPERTY", sim.ExecuteScalar("select secondary_type_desc from sys.xml_indexes where name = 'sxml_property'")); |
| 143 | + } |
| 144 | + |
| 145 | + [TestMethod] |
| 146 | + public void SecondaryXmlIndex_UsingId_LinksToPrimary() |
| 147 | + { |
| 148 | + var sim = new Simulation(); |
| 149 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int not null primary key, body xml)"); |
| 150 | + _ = sim.ExecuteNonQuery("create primary xml index pxml_doc on dbo.doc(body)"); |
| 151 | + _ = sim.ExecuteNonQuery("create xml index sxml_path on dbo.doc(body) using xml index pxml_doc for path"); |
| 152 | + var primaryId = sim.ExecuteScalar("select index_id from sys.xml_indexes where name = 'pxml_doc'"); |
| 153 | + var secondaryUsingId = sim.ExecuteScalar("select using_xml_index_id from sys.xml_indexes where name = 'sxml_path'"); |
| 154 | + AreEqual(primaryId, secondaryUsingId); |
| 155 | + } |
| 156 | + |
| 157 | + [TestMethod] |
| 158 | + public void CreateXmlIndex_DuplicateName_Raises2714() |
| 159 | + { |
| 160 | + var sim = new Simulation(); |
| 161 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int not null primary key, body xml)"); |
| 162 | + _ = sim.ExecuteNonQuery("create primary xml index pxml on dbo.doc(body)"); |
| 163 | + _ = sim.AssertSqlError("create primary xml index pxml on dbo.doc(body)", 2714); |
| 164 | + } |
| 165 | + |
| 166 | + [TestMethod] |
| 167 | + public void XmlValue_Method_RaisesNotSupportedAtExecute() |
| 168 | + { |
| 169 | + var sim = new Simulation(); |
| 170 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 171 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<r><c>hi</c></r>')"); |
| 172 | + var ex = ThrowsExactly<NotSupportedException>(() => |
| 173 | + sim.ExecuteScalar("select body.value('(/r/c)[1]', 'nvarchar(50)') from dbo.doc")); |
| 174 | + Contains(".value()", ex.Message); |
| 175 | + } |
| 176 | + |
| 177 | + [TestMethod] |
| 178 | + public void XmlQuery_Method_RaisesNotSupportedAtExecute() |
| 179 | + { |
| 180 | + var sim = new Simulation(); |
| 181 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 182 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<r/>')"); |
| 183 | + var ex = ThrowsExactly<NotSupportedException>(() => |
| 184 | + sim.ExecuteScalar("select body.query('/r') from dbo.doc")); |
| 185 | + Contains(".query()", ex.Message); |
| 186 | + } |
| 187 | + |
| 188 | + [TestMethod] |
| 189 | + public void XmlExist_Method_RaisesNotSupportedAtExecute() |
| 190 | + { |
| 191 | + var sim = new Simulation(); |
| 192 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 193 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<r/>')"); |
| 194 | + var ex = ThrowsExactly<NotSupportedException>(() => |
| 195 | + sim.ExecuteScalar("select body.exist('/r') from dbo.doc")); |
| 196 | + Contains(".exist()", ex.Message); |
| 197 | + } |
| 198 | + |
| 199 | + [TestMethod] |
| 200 | + public void CreateViewWithXmlMethod_Succeeds_FailsAtExecute() |
| 201 | + { |
| 202 | + // CREATE VIEW body parses cleanly (proc/view bodies parse for name |
| 203 | + // resolution but XML methods defer their NotSupportedException to |
| 204 | + // run-time). The query against the view fails on first row. |
| 205 | + var sim = new Simulation(); |
| 206 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 207 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<r/>')"); |
| 208 | + _ = sim.ExecuteNonQuery("create view dbo.v_doc as select body.value('(/r)[1]', 'nvarchar(50)') as v from dbo.doc"); |
| 209 | + _ = ThrowsExactly<NotSupportedException>(() => |
| 210 | + sim.ExecuteScalar("select v from dbo.v_doc")); |
| 211 | + } |
| 212 | + |
| 213 | + [TestMethod] |
| 214 | + public void XmlSchemaCollections_StartsAt65536() |
| 215 | + { |
| 216 | + // Probe-confirmed: SQL Server's first user xml_collection_id is 65536. |
| 217 | + var sim = new Simulation(); |
| 218 | + _ = sim.ExecuteNonQuery("create xml schema collection xsc1 as N'<xsd:schema/>'"); |
| 219 | + AreEqual(65536, sim.ExecuteScalar("select xml_collection_id from sys.xml_schema_collections where name = 'xsc1'")); |
| 220 | + } |
| 221 | + |
| 222 | + [TestMethod] |
| 223 | + public void CastXmlToNvarchar_Succeeds() |
| 224 | + { |
| 225 | + var sim = new Simulation(); |
| 226 | + _ = sim.ExecuteNonQuery("create table dbo.doc (id int, body xml)"); |
| 227 | + _ = sim.ExecuteNonQuery("insert into dbo.doc values (1, N'<r/>')"); |
| 228 | + AreEqual("<r/>", sim.ExecuteScalar("select cast(body as nvarchar(max)) from dbo.doc")); |
| 229 | + } |
| 230 | +} |
0 commit comments