PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Microsoft.Web.Http.Data.Test/DataControllerDescriptionTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 192 lines | 150 code | 21 blank | 21 comment | 0 complexity | 74b499a4daa00774ef41ac4077ac6efb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Web.Http;
  8. using System.Web.Http.Controllers;
  9. using System.Web.Http.Filters;
  10. using Microsoft.Web.Http.Data.EntityFramework;
  11. using Microsoft.Web.Http.Data.EntityFramework.Metadata;
  12. using Microsoft.Web.Http.Data.Test.Models;
  13. using Xunit;
  14. using Assert = Microsoft.TestCommon.AssertEx;
  15. namespace Microsoft.Web.Http.Data.Test
  16. {
  17. public class DataControllerDescriptionTest
  18. {
  19. // verify that the LinqToEntitiesMetadataProvider is registered by default for
  20. // LinqToEntitiesDataController<T> derived types
  21. [Fact]
  22. public void EFMetadataProvider_AttributeInference()
  23. {
  24. HttpConfiguration configuration = new HttpConfiguration();
  25. HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor
  26. {
  27. Configuration = configuration,
  28. ControllerType = typeof(NorthwindEFTestController),
  29. };
  30. DataControllerDescription description = GetDataControllerDescription(typeof(NorthwindEFTestController));
  31. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product));
  32. // verify key attribute
  33. Assert.NotNull(properties["ProductID"].Attributes[typeof(KeyAttribute)]);
  34. Assert.Null(properties["ProductName"].Attributes[typeof(KeyAttribute)]);
  35. // verify StringLengthAttribute
  36. StringLengthAttribute sla = (StringLengthAttribute)properties["ProductName"].Attributes[typeof(StringLengthAttribute)];
  37. Assert.NotNull(sla);
  38. Assert.Equal(40, sla.MaximumLength);
  39. // verify RequiredAttribute
  40. RequiredAttribute ra = (RequiredAttribute)properties["ProductName"].Attributes[typeof(RequiredAttribute)];
  41. Assert.NotNull(ra);
  42. Assert.False(ra.AllowEmptyStrings);
  43. // verify association attribute
  44. AssociationAttribute aa = (AssociationAttribute)properties["Category"].Attributes[typeof(AssociationAttribute)];
  45. Assert.NotNull(aa);
  46. Assert.Equal("Category_Product", aa.Name);
  47. Assert.True(aa.IsForeignKey);
  48. Assert.Equal("CategoryID", aa.ThisKey);
  49. Assert.Equal("CategoryID", aa.OtherKey);
  50. // verify metadata from "buddy class"
  51. PropertyDescriptor pd = properties["QuantityPerUnit"];
  52. sla = (StringLengthAttribute)pd.Attributes[typeof(StringLengthAttribute)];
  53. Assert.NotNull(sla);
  54. Assert.Equal(777, sla.MaximumLength);
  55. EditableAttribute ea = (EditableAttribute)pd.Attributes[typeof(EditableAttribute)];
  56. Assert.False(ea.AllowEdit);
  57. Assert.True(ea.AllowInitialValue);
  58. }
  59. [Fact]
  60. public void EFTypeDescriptor_ExcludedEntityMembers()
  61. {
  62. PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["EntityState"];
  63. Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));
  64. pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["EntityState"];
  65. Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));
  66. pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["SupplierReference"];
  67. Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));
  68. }
  69. [Fact]
  70. public void DescriptionValidation_NonAuthorizationFilter()
  71. {
  72. Assert.Throws<NotSupportedException>(
  73. () => GetDataControllerDescription(typeof(InvalidController_NonAuthMethodFilter)),
  74. String.Format(String.Format(Resource.InvalidAction_UnsupportedFilterType, "InvalidController_NonAuthMethodFilter", "UpdateProduct")));
  75. }
  76. /// <summary>
  77. /// Verify that associated entities are correctly registered in the description when
  78. /// using explicit data contracts
  79. /// </summary>
  80. [Fact]
  81. public void AssociatedEntityTypeDiscovery_ExplicitDataContract()
  82. {
  83. DataControllerDescription description = GetDataControllerDescription(typeof(IncludedAssociationTestController_ExplicitDataContract));
  84. List<Type> entityTypes = description.EntityTypes.ToList();
  85. Assert.Equal(8, entityTypes.Count);
  86. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Order)));
  87. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Order_Detail)));
  88. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Customer)));
  89. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Employee)));
  90. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product)));
  91. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Category)));
  92. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Supplier)));
  93. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Shipper)));
  94. }
  95. /// <summary>
  96. /// Verify that associated entities are correctly registered in the description when
  97. /// using implicit data contracts
  98. /// </summary>
  99. [Fact]
  100. public void AssociatedEntityTypeDiscovery_ImplicitDataContract()
  101. {
  102. DataControllerDescription description = GetDataControllerDescription(typeof(IncludedAssociationTestController_ImplicitDataContract));
  103. List<Type> entityTypes = description.EntityTypes.ToList();
  104. Assert.Equal(3, entityTypes.Count);
  105. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Customer)));
  106. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Order)));
  107. Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Order_Detail)));
  108. }
  109. /// <summary>
  110. /// Verify that DataControllerDescription correctly handles Task returning actions and discovers
  111. /// entity types from those as well (unwrapping the task type).
  112. /// </summary>
  113. [Fact]
  114. public void TaskReturningGetActions()
  115. {
  116. DataControllerDescription desc = GetDataControllerDescription(typeof(TaskReturningGetActionsController));
  117. Assert.Equal(4, desc.EntityTypes.Count());
  118. Assert.True(desc.EntityTypes.Contains(typeof(City)));
  119. Assert.True(desc.EntityTypes.Contains(typeof(CityWithInfo)));
  120. Assert.True(desc.EntityTypes.Contains(typeof(CityWithEditHistory)));
  121. Assert.True(desc.EntityTypes.Contains(typeof(State)));
  122. }
  123. internal static DataControllerDescription GetDataControllerDescription(Type controllerType)
  124. {
  125. HttpConfiguration configuration = new HttpConfiguration();
  126. HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor
  127. {
  128. Configuration = configuration,
  129. ControllerType = controllerType
  130. };
  131. return DataControllerDescription.GetDescription(controllerDescriptor);
  132. }
  133. }
  134. internal class InvalidController_NonAuthMethodFilter : DataController
  135. {
  136. // attempt to apply a non-auth filter
  137. [TestActionFilter]
  138. public void UpdateProduct(Microsoft.Web.Http.Data.Test.Models.EF.Product product)
  139. {
  140. }
  141. // the restriction doesn't apply for non CUD actions
  142. [TestActionFilter]
  143. public IEnumerable<Microsoft.Web.Http.Data.Test.Models.EF.Product> GetProducts()
  144. {
  145. return null;
  146. }
  147. }
  148. internal class TaskReturningGetActionsController : DataController
  149. {
  150. public Task<IEnumerable<City>> GetCities()
  151. {
  152. return null;
  153. }
  154. public Task<State> GetState(string name)
  155. {
  156. return null;
  157. }
  158. }
  159. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
  160. public class TestActionFilterAttribute : ActionFilterAttribute
  161. {
  162. }
  163. internal class IncludedAssociationTestController_ExplicitDataContract : LinqToEntitiesDataController<Microsoft.Web.Http.Data.Test.Models.EF.NorthwindEntities>
  164. {
  165. public IQueryable<Microsoft.Web.Http.Data.Test.Models.EF.Order> GetOrders() { return null; }
  166. }
  167. internal class IncludedAssociationTestController_ImplicitDataContract : DataController
  168. {
  169. public IQueryable<Microsoft.Web.Http.Data.Test.Models.Customer> GetCustomers() { return null; }
  170. }
  171. }