PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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