PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Web.Http.OData.Test/OData/Formatter/EdmLibHelpersTests.cs

https://github.com/huyq2002/aspnetwebstack
C# | 216 lines | 184 code | 31 blank | 1 comment | 4 complexity | 9d93679f5c6943b6efbc33aa6168881c MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Data.Linq;
  4. using System.Linq;
  5. using System.Web.Http.OData.Builder;
  6. using System.Web.Http.OData.Formatter.Serialization.Models;
  7. using System.Web.Http.OData.Query.Expressions;
  8. using System.Xml.Linq;
  9. using Microsoft.Data.Edm;
  10. using Microsoft.Data.Edm.Library;
  11. using Microsoft.TestCommon;
  12. namespace System.Web.Http.OData.Formatter
  13. {
  14. public class EdmLibHelpersTests
  15. {
  16. [Theory]
  17. [InlineData(typeof(Customer), "Customer")]
  18. [InlineData(typeof(int), "Int32")]
  19. [InlineData(typeof(IEnumerable<int>), "IEnumerable_1OfInt32")]
  20. [InlineData(typeof(IEnumerable<Func<int, string>>), "IEnumerable_1OfFunc_2OfInt32_String")]
  21. [InlineData(typeof(List<Func<int, string>>), "List_1OfFunc_2OfInt32_String")]
  22. public void EdmFullName(Type clrType, string expectedName)
  23. {
  24. Assert.Equal(expectedName, clrType.EdmName());
  25. }
  26. [Theory]
  27. [InlineData(typeof(char), typeof(string))]
  28. [InlineData(typeof(char?), typeof(string))]
  29. [InlineData(typeof(ushort), typeof(int))]
  30. [InlineData(typeof(uint), typeof(long))]
  31. [InlineData(typeof(ulong), typeof(long))]
  32. [InlineData(typeof(ushort?), typeof(int?))]
  33. [InlineData(typeof(uint?), typeof(long?))]
  34. [InlineData(typeof(ulong?), typeof(long?))]
  35. [InlineData(typeof(char[]), typeof(string))]
  36. [InlineData(typeof(Binary), typeof(byte[]))]
  37. [InlineData(typeof(XElement), typeof(string))]
  38. public void IsNonstandardEdmPrimitive_Returns_True(Type primitiveType, Type mappedType)
  39. {
  40. bool isNonstandardEdmPrimtive;
  41. Type resultMappedType = EdmLibHelpers.IsNonstandardEdmPrimitive(primitiveType, out isNonstandardEdmPrimtive);
  42. Assert.True(isNonstandardEdmPrimtive);
  43. Assert.Equal(mappedType, resultMappedType);
  44. }
  45. [Theory]
  46. [InlineData(typeof(string))]
  47. [InlineData(typeof(short))]
  48. [InlineData(typeof(int))]
  49. [InlineData(typeof(long))]
  50. [InlineData(typeof(bool))]
  51. [InlineData(typeof(byte))]
  52. [InlineData(typeof(sbyte))]
  53. [InlineData(typeof(DateTime))]
  54. [InlineData(typeof(DateTimeOffset))]
  55. [InlineData(typeof(TimeSpan))]
  56. public void IsNonstandardEdmPrimitive_Returns_False(Type primitiveType)
  57. {
  58. bool isNonstandardEdmPrimtive;
  59. Type resultMappedType = EdmLibHelpers.IsNonstandardEdmPrimitive(primitiveType, out isNonstandardEdmPrimtive);
  60. Assert.False(isNonstandardEdmPrimtive);
  61. Assert.Equal(primitiveType, resultMappedType);
  62. }
  63. [Fact]
  64. public void GetEdmType_ReturnsBaseType()
  65. {
  66. IEdmModel model = GetEdmModel();
  67. Assert.Equal(model.GetEdmType(typeof(BaseType)), model.SchemaElements.OfType<IEdmEntityType>().Where(t => t.Name == "BaseType").Single());
  68. }
  69. [Fact]
  70. public void GetEdmType_ReturnsDerivedType()
  71. {
  72. IEdmModel model = GetEdmModel();
  73. Assert.Equal(model.GetEdmType(typeof(DerivedTypeA)), model.SchemaElements.OfType<IEdmEntityType>().Where(t => t.Name == "DerivedTypeA").Single());
  74. Assert.Equal(model.GetEdmType(typeof(DerivedTypeB)), model.SchemaElements.OfType<IEdmEntityType>().Where(t => t.Name == "DerivedTypeB").Single());
  75. }
  76. [Fact]
  77. public void GetEdmType_Returns_NearestDerivedType()
  78. {
  79. IEdmModel model = GetEdmModel();
  80. Assert.Equal(model.GetEdmType(typeof(DerivedTypeAA)), model.SchemaElements.OfType<IEdmEntityType>().Where(t => t.Name == "DerivedTypeA").Single());
  81. }
  82. [Fact]
  83. public void GetEdmType_ReturnsNull_ForUnknownType()
  84. {
  85. IEdmModel model = GetEdmModel();
  86. Assert.Null(model.GetEdmType(typeof(TypeNotInModel)));
  87. }
  88. [Fact]
  89. public void GetEdmType_ReturnsCollection_ForIEnumerableOfT()
  90. {
  91. IEdmModel model = GetEdmModel();
  92. IEdmType edmType = model.GetEdmType(typeof(IEnumerable<BaseType>));
  93. Assert.Equal(EdmTypeKind.Collection, edmType.TypeKind);
  94. Assert.Equal("System.Web.Http.OData.Formatter.BaseType", (edmType as IEdmCollectionType).ElementType.FullName());
  95. }
  96. [Fact]
  97. public void GetEdmType_ReturnsCollection_ForIEnumerableOfSelectExpandWrapperOfT()
  98. {
  99. IEdmModel model = GetEdmModel();
  100. IEdmType edmType = model.GetEdmType(typeof(IEnumerable<SelectExpandWrapper<BaseType>>));
  101. Assert.Equal(EdmTypeKind.Collection, edmType.TypeKind);
  102. Assert.Equal("System.Web.Http.OData.Formatter.BaseType", (edmType as IEdmCollectionType).ElementType.FullName());
  103. }
  104. [Fact]
  105. public void GetEdmType_ReturnsNull_ForRecursiveCollections()
  106. {
  107. IEdmModel model = GetEdmModel();
  108. Assert.Null(model.GetEdmType(typeof(RecursiveCollection)));
  109. }
  110. [Theory]
  111. [InlineData(typeof(string), true)]
  112. [InlineData(typeof(List<int>), true)]
  113. [InlineData(typeof(int[]), true)]
  114. [InlineData(typeof(object), true)]
  115. [InlineData(typeof(Nullable<int>), true)]
  116. [InlineData(typeof(int), false)]
  117. [InlineData(typeof(char), false)]
  118. [InlineData(typeof(IEnumerable<int>), true)]
  119. [InlineData(typeof(ICollection<int>), true)]
  120. [InlineData(typeof(DateTime), false)]
  121. public void IsNullable_RecognizesClassesAndInterfacesAndNullableOfTs(Type type, bool isNullable)
  122. {
  123. Assert.Equal(isNullable, EdmLibHelpers.IsNullable(type));
  124. }
  125. public static TheoryDataSet<IEdmType, bool, Type> ToEdmTypeReferenceTestData
  126. {
  127. get
  128. {
  129. IEdmEntityType entity = new EdmEntityType("NS", "Entity");
  130. IEdmComplexType complex = new EdmComplexType("NS", "Complex");
  131. IEdmPrimitiveType primitive = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32);
  132. IEdmCollectionType collection = new EdmCollectionType(new EdmEntityTypeReference(entity, isNullable: false));
  133. return new TheoryDataSet<IEdmType, bool, Type>
  134. {
  135. { primitive, true, typeof(IEdmPrimitiveTypeReference) },
  136. { primitive, false, typeof(IEdmPrimitiveTypeReference) },
  137. { entity, true, typeof(IEdmEntityTypeReference) },
  138. { entity, false, typeof(IEdmEntityTypeReference) },
  139. { complex, true, typeof(IEdmComplexTypeReference) },
  140. { complex, false, typeof(IEdmComplexTypeReference) },
  141. { collection, true, typeof(IEdmCollectionTypeReference) },
  142. { collection, false, typeof(IEdmCollectionTypeReference) }
  143. };
  144. }
  145. }
  146. [Theory]
  147. [PropertyData("ToEdmTypeReferenceTestData")]
  148. public void ToEdmTypeReference_InstantiatesRightEdmTypeReference(IEdmType edmType, bool isNullable, Type expectedType)
  149. {
  150. IEdmTypeReference result = EdmLibHelpers.ToEdmTypeReference(edmType, isNullable);
  151. Assert.Equal(isNullable, result.IsNullable);
  152. Assert.Equal(edmType, result.Definition);
  153. Assert.IsAssignableFrom(expectedType, result);
  154. }
  155. private static IEdmModel GetEdmModel()
  156. {
  157. ODataModelBuilder modelBuilder = new ODataModelBuilder();
  158. modelBuilder
  159. .Entity<DerivedTypeA>()
  160. .DerivesFrom<BaseType>();
  161. modelBuilder
  162. .Entity<DerivedTypeB>()
  163. .DerivesFrom<BaseType>();
  164. return modelBuilder.GetEdmModel();
  165. }
  166. public class BaseType
  167. {
  168. }
  169. public class DerivedTypeA : BaseType
  170. {
  171. }
  172. public class DerivedTypeB : BaseType
  173. {
  174. }
  175. public class DerivedTypeAA : DerivedTypeA
  176. {
  177. }
  178. public class TypeNotInModel
  179. {
  180. }
  181. public class RecursiveCollection : List<RecursiveCollection>
  182. {
  183. }
  184. }
  185. }