1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.SqlServer
{
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Xml;
using Xunit;
public class SqlProviderManifestTests
{
[Fact]
public void GetProviderManifest_loads_ProviderManifest_xml()
{
TestReadResource(
SqlProviderManifest.GetProviderManifest(),
"ProviderManifest",
@"http://schemas.microsoft.com/ado/2006/04/edm/providermanifest",
x => x.Name == "Type" && x.GetAttribute("Name") == "geography", true);
}
[Fact]
public void GetStoreSchemaMapping_loads_V2_schema_mapping_xml()
{
TestReadResource(
SqlProviderManifest.GetStoreSchemaMapping("StoreSchemaMapping"),
"Mapping",
@"urn:schemas-microsoft-com:windows:storage:mapping:CS",
x => x.Name == "EntitySetMapping" && x.GetAttribute("Name") == "FunctionReturnTableColumns", false);
}
[Fact]
public void GetStoreSchemaMapping_loads_V3_schema_mapping_xml()
{
TestReadResource(
SqlProviderManifest.GetStoreSchemaMapping("StoreSchemaMappingVersion3"),
"Mapping",
@"urn:schemas-microsoft-com:windows:storage:mapping:CS",
x => x.Name == "EntitySetMapping" && x.GetAttribute("Name") == "FunctionReturnTableColumns", true);
}
[Fact]
public void GetStoreSchemaDescription_loads_V2_schema_xml()
{
// Test it's for V2
TestReadResource(
new SqlProviderManifest("2008").GetStoreSchemaDescription("StoreSchemaDefinition"),
"Schema",
@"http://schemas.microsoft.com/ado/2006/04/edm/ssdl",
x => x.Name == "EntitySet" && x.GetAttribute("Name") == "SFunctionReturnTableColumns", false,
x => x.Name == "Property" && x.GetAttribute("Type") == "ntext", false);
}
[Fact]
public void GetStoreSchemaDescription_loads_V3_schema_xml()
{
// Test it's for V3
TestReadResource(
new SqlProviderManifest("2008").GetStoreSchemaDescription("StoreSchemaDefinitionVersion3"),
"Schema",
@"http://schemas.microsoft.com/ado/2006/04/edm/ssdl",
x => x.Name == "EntitySet" && x.GetAttribute("Name") == "SFunctionReturnTableColumns", true,
x => x.Name == "Property" && x.GetAttribute("Type") == "ntext", false);
}
[Fact]
public void GetStoreSchemaDescription_loads_V2_schema_xml_for_SQL_2000()
{
// Test it's for V2
TestReadResource(
new SqlProviderManifest("2000").GetStoreSchemaDescription("StoreSchemaDefinition"),
"Schema",
@"http://schemas.microsoft.com/ado/2006/04/edm/ssdl",
x => x.Name == "EntitySet" && x.GetAttribute("Name") == "SFunctionReturnTableColumns", false,
x => x.Name == "Property" && x.GetAttribute("Type") == "ntext", true);
}
[Fact]
public void GetStoreSchemaDescription_loads_V3_schema_xml_SQL_2000()
{
TestReadResource(
new SqlProviderManifest("2000").GetStoreSchemaDescription("StoreSchemaDefinitionVersion3"),
"Schema",
@"http://schemas.microsoft.com/ado/2006/04/edm/ssdl",
x => x.Name == "EntitySet" && x.GetAttribute("Name") == "SFunctionReturnTableColumns", true,
x => x.Name == "Property" && x.GetAttribute("Type") == "ntext", true);
}
private static void TestReadResource(
XmlReader xmlReader,
string name,
string namespaceUri,
Func<XmlReader, bool> identify1,
bool expected1,
Func<XmlReader, bool> identify2 = null,
bool expected2 = false)
{
using (xmlReader)
{
while (xmlReader.Read()
&& xmlReader.NodeType != XmlNodeType.Element)
{
}
Assert.Equal(name, xmlReader.Name);
Assert.Equal(namespaceUri, xmlReader.NamespaceURI);
var ident1 = false;
var ident2 = false;
while (xmlReader.Read())
{
if (identify1(xmlReader))
{
ident1 = true;
}
if (identify2 != null
&& identify2(xmlReader))
{
ident2 = true;
}
}
Assert.Equal(expected1, ident1);
if (identify2 != null)
{
Assert.Equal(expected2, ident2);
}
}
}
[Fact]
public void GetStoreType_throws_for_null_argument()
{
Assert.Throws<ArgumentNullException>(
() => new SqlProviderManifest("2008").GetStoreType(null));
}
[Fact]
public void GetEdmType_throws_for_null_argument()
{
Assert.Throws<ArgumentNullException>(
() => new SqlProviderManifest("2008").GetEdmType(null));
}
[Fact]
public void EscapeLikeArgument_throws_for_null_argument()
{
Assert.Throws<ArgumentNullException>(
() => new SqlProviderManifest("2008").EscapeLikeArgument(null));
}
[Fact]
public void GetStoreTypes_includes_SQL_Server_2008_types_on_SQL_Server_2008()
{
GetStoreTypesTest("2008", expectTypes: true);
}
[Fact]
public void GetStoreTypes_includes_SQL_Server_2008_types_on_SQL_Server_2012()
{
GetStoreTypesTest("2012", expectTypes: true);
}
[Fact]
public void GetStoreTypes_excludes_SQL_Server_2008_types_on_SQL_Server_2005()
{
GetStoreTypesTest("2005", expectTypes: false);
}
[Fact]
public void GetStoreTypes_excludes_SQL_Server_2008_types_on_SQL_Server_2000()
{
GetStoreTypesTest("2000", expectTypes: false);
}
private static void GetStoreTypesTest(string manifestToken, bool expectTypes)
{
var types = new SqlProviderManifest(manifestToken).GetStoreTypes();
var expectedCount = expectTypes ? 1 : 0;
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "time"));
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "date"));
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "datetime2"));
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "datetimeoffset"));
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "geography"));
Assert.Equal(expectedCount, types.Count(t => t.Name.ToLowerInvariant() == "geometry"));
}
[Fact]
public void GetStoreTypes_only_includes_XML_type_on_SQL_Server_2005_and_later()
{
Assert.Equal(0, new SqlProviderManifest("2000").GetStoreTypes().Count(t => t.Name.ToLowerInvariant() == "xml"));
Assert.Equal(1, new SqlProviderManifest("2005").GetStoreTypes().Count(t => t.Name.ToLowerInvariant() == "xml"));
Assert.Equal(1, new SqlProviderManifest("2008").GetStoreTypes().Count(t => t.Name.ToLowerInvariant() == "xml"));
Assert.Equal(1, new SqlProviderManifest("2012").GetStoreTypes().Count(t => t.Name.ToLowerInvariant() == "xml"));
}
private static readonly string[] _functionsOnlyIn2008AndUp =
{
"COUNT collection[Edm.DateTimeOffset(Nullable=True,DefaultValue=,Precision=)]",
"COUNT collection[Edm.Time(Nullable=True,DefaultValue=,Precision=)]",
"COUNT_BIG collection[Edm.DateTimeOffset(Nullable=True,DefaultValue=,Precision=)]",
"COUNT_BIG collection[Edm.Time(Nullable=True,DefaultValue=,Precision=)]",
"MAX collection[Edm.Time(Nullable=True,DefaultValue=,Precision=)]",
"MAX collection[Edm.DateTimeOffset(Nullable=True,DefaultValue=,Precision=)]",
"MIN collection[Edm.Time(Nullable=True,DefaultValue=,Precision=)]",
"MIN collection[Edm.DateTimeOffset(Nullable=True,DefaultValue=,Precision=)]",
"DATEADD String Double Time",
"DATEADD String Double DateTimeOffset",
"DATEDIFF String DateTimeOffset DateTimeOffset",
"DATEDIFF String Time Time",
"DATEDIFF String String DateTimeOffset",
"DATEDIFF String String Time",
"DATEDIFF String Time String",
"DATEDIFF String DateTimeOffset String",
"DATEDIFF String Time DateTime",
"DATEDIFF String Time DateTimeOffset",
"DATEDIFF String DateTime Time",
"DATEDIFF String DateTimeOffset Time",
"DATEDIFF String DateTime DateTimeOffset",
"DATEDIFF String DateTimeOffset DateTime",
"DATENAME String Time",
"DATENAME String DateTimeOffset",
"DATEPART String DateTimeOffset",
"DATEPART String Time",
"DAY DateTimeOffset",
"SYSDATETIME",
"SYSUTCDATETIME",
"SYSDATETIMEOFFSET",
"MONTH DateTimeOffset",
"YEAR DateTimeOffset",
"DATALENGTH Time",
"DATALENGTH DateTimeOffset",
"CHECKSUM Time",
"CHECKSUM DateTimeOffset",
"CHECKSUM Time Time",
"CHECKSUM DateTimeOffset DateTimeOffset",
"CHECKSUM DateTimeOffset DateTimeOffset DateTimeOffset",
"CHECKSUM Time Time Time",
"POINTGEOGRAPHY Double Double Int32",
"ASTEXTZM Geography",
"BUFFERWITHTOLERANCE Geography Double Double Boolean",
"ENVELOPEANGLE Geography",
"ENVELOPECENTER Geography",
"FILTER Geography Geography",
"INSTANCEOF Geography String",
"NUMRINGS Geography",
"REDUCE Geography Double",
"RINGN Geography Int32",
"POINTGEOMETRY Double Double Int32",
"ASTEXTZM Geometry",
"BUFFERWITHTOLERANCE Geometry Double Double Boolean",
"INSTANCEOF Geometry String",
"FILTER Geometry Geometry",
"MAKEVALID Geometry",
"REDUCE Geometry Double",
};
private static readonly string[] _functionsOnlyIn2005AndUp =
{
"COUNT collection[Edm.Guid(Nullable=True,DefaultValue=)]",
"COUNT_BIG collection[Edm.Guid(Nullable=True,DefaultValue=)]",
"CHARINDEX String String Int64",
"CHARINDEX Binary Binary Int64",
};
[Fact]
public void GetStoreFunctions_includes_SQL_Server_2008_functions_on_SQL_Server_2008()
{
FunctionsIncludedTest("2008", _functionsOnlyIn2008AndUp);
}
[Fact]
public void GetStoreFunctions_includes_SQL_Server_2008_functions_on_SQL_Server_2012()
{
FunctionsIncludedTest("2012", _functionsOnlyIn2008AndUp);
}
[Fact]
public void GetStoreFunctions_does_not_include_SQL_Server_2008_functions_on_SQL_Server_2005()
{
FunctionsNotIncludedTest("2005", _functionsOnlyIn2008AndUp);
}
[Fact]
public void GetStoreFunctions_does_not_include_SQL_Server_2008_functions_on_SQL_Server_2000()
{
FunctionsNotIncludedTest("2000", _functionsOnlyIn2008AndUp);
}
[Fact]
public void GetStoreFunctions_includes_SQL_Server_2005_functions_on_SQL_Server_2005()
{
FunctionsIncludedTest("2005", _functionsOnlyIn2005AndUp);
}
[Fact]
public void GetStoreFunctions_includes_SQL_Server_2005_functions_on_SQL_Server_2008()
{
FunctionsIncludedTest("2008", _functionsOnlyIn2005AndUp);
}
[Fact]
public void GetStoreFunctions_includes_SQL_Server_2005_functions_on_SQL_Server_2012()
{
FunctionsIncludedTest("2012", _functionsOnlyIn2005AndUp);
}
[Fact]
public void GetStoreFunctions_does_not_include_SQL_Server_2005_functions_on_SQL_Server_2000()
{
FunctionsNotIncludedTest("2000", _functionsOnlyIn2005AndUp);
}
private static void FunctionsIncludedTest(string manifestToken, string[] expectedFunctions)
{
var actualFunctions = new SqlProviderManifest(manifestToken).GetStoreFunctions().Select(FunctionString).ToList();
foreach (var function in expectedFunctions)
{
Assert.Contains(function, actualFunctions);
}
}
private static void FunctionsNotIncludedTest(string manifestToken, string[] notExpectedFunctions)
{
var actualFunctions = new SqlProviderManifest(manifestToken).GetStoreFunctions().Select(FunctionString).ToList();
foreach (var function in actualFunctions)
{
Assert.DoesNotContain(function, notExpectedFunctions);
}
}
private static string FunctionString(EdmFunction function)
{
return function.Name + function.Parameters.Select(p => p.TypeUsage.EdmType.Name).Aggregate("", (a, n) => a + " " + n);
}
}
}
|