File: DataControllerDescriptionTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (194 lines) | stat: -rw-r--r-- 9,151 bytes parent folder | download | duplicates (11)
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Microsoft.Web.Http.Data.EntityFramework;
using Microsoft.Web.Http.Data.EntityFramework.Metadata;
using Microsoft.Web.Http.Data.Test.Models;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace Microsoft.Web.Http.Data.Test
{
    public class DataControllerDescriptionTest
    {
        // verify that the LinqToEntitiesMetadataProvider is registered by default for
        // LinqToEntitiesDataController<T> derived types
        [Fact]
        public void EFMetadataProvider_AttributeInference()
        {
            HttpConfiguration configuration = new HttpConfiguration();
            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor
            {
                Configuration = configuration,
                ControllerType = typeof(NorthwindEFTestController),
            };
            DataControllerDescription description = GetDataControllerDescription(typeof(NorthwindEFTestController));
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product));

            // verify key attribute
            Assert.NotNull(properties["ProductID"].Attributes[typeof(KeyAttribute)]);
            Assert.Null(properties["ProductName"].Attributes[typeof(KeyAttribute)]);

            // verify StringLengthAttribute
            StringLengthAttribute sla = (StringLengthAttribute)properties["ProductName"].Attributes[typeof(StringLengthAttribute)];
            Assert.NotNull(sla);
            Assert.Equal(40, sla.MaximumLength);

            // verify RequiredAttribute
            RequiredAttribute ra = (RequiredAttribute)properties["ProductName"].Attributes[typeof(RequiredAttribute)];
            Assert.NotNull(ra);
            Assert.False(ra.AllowEmptyStrings);

            // verify association attribute
            AssociationAttribute aa = (AssociationAttribute)properties["Category"].Attributes[typeof(AssociationAttribute)];
            Assert.NotNull(aa);
            Assert.Equal("Category_Product", aa.Name);
            Assert.True(aa.IsForeignKey);
            Assert.Equal("CategoryID", aa.ThisKey);
            Assert.Equal("CategoryID", aa.OtherKey);

            // verify metadata from "buddy class"
            PropertyDescriptor pd = properties["QuantityPerUnit"];
            sla = (StringLengthAttribute)pd.Attributes[typeof(StringLengthAttribute)];
            Assert.NotNull(sla);
            Assert.Equal(777, sla.MaximumLength);
            EditableAttribute ea = (EditableAttribute)pd.Attributes[typeof(EditableAttribute)];
            Assert.False(ea.AllowEdit);
            Assert.True(ea.AllowInitialValue);
        }

        [Fact]
        public void EFTypeDescriptor_ExcludedEntityMembers()
        {
            PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["EntityState"];
            Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));

            pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["EntityState"];
            Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));

            pd = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product))["SupplierReference"];
            Assert.True(LinqToEntitiesTypeDescriptor.ShouldExcludeEntityMember(pd));
        }

        [Fact]
        public void DescriptionValidation_NonAuthorizationFilter()
        {
            Assert.Throws<NotSupportedException>(
                () => GetDataControllerDescription(typeof(InvalidController_NonAuthMethodFilter)),
                String.Format(String.Format(Resource.InvalidAction_UnsupportedFilterType, "InvalidController_NonAuthMethodFilter", "UpdateProduct")));
        }

        /// <summary>
        /// Verify that associated entities are correctly registered in the description when
        /// using explicit data contracts
        /// </summary>
        [Fact]
        public void AssociatedEntityTypeDiscovery_ExplicitDataContract()
        {
            DataControllerDescription description = GetDataControllerDescription(typeof(IncludedAssociationTestController_ExplicitDataContract));
            List<Type> entityTypes = description.EntityTypes.ToList();
            Assert.Equal(8, entityTypes.Count);
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Order)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Order_Detail)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Customer)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Employee)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Category)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Supplier)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Shipper)));
        }

        /// <summary>
        /// Verify that associated entities are correctly registered in the description when
        /// using implicit data contracts
        /// </summary>
        [Fact]
        public void AssociatedEntityTypeDiscovery_ImplicitDataContract()
        {
            DataControllerDescription description = GetDataControllerDescription(typeof(IncludedAssociationTestController_ImplicitDataContract));
            List<Type> entityTypes = description.EntityTypes.ToList();
            Assert.Equal(3, entityTypes.Count);
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Customer)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Order)));
            Assert.True(entityTypes.Contains(typeof(Microsoft.Web.Http.Data.Test.Models.Order_Detail)));
        }

        /// <summary>
        /// Verify that DataControllerDescription correctly handles Task returning actions and discovers
        /// entity types from those as well (unwrapping the task type).
        /// </summary>
        [Fact]
        public void TaskReturningGetActions()
        {
            DataControllerDescription desc = GetDataControllerDescription(typeof(TaskReturningGetActionsController));
            Assert.Equal(4, desc.EntityTypes.Count());
            Assert.True(desc.EntityTypes.Contains(typeof(City)));
            Assert.True(desc.EntityTypes.Contains(typeof(CityWithInfo)));
            Assert.True(desc.EntityTypes.Contains(typeof(CityWithEditHistory)));
            Assert.True(desc.EntityTypes.Contains(typeof(State)));
        }

        internal static DataControllerDescription GetDataControllerDescription(Type controllerType)
        {
            HttpConfiguration configuration = new HttpConfiguration();
            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor
            {
                Configuration = configuration,
                ControllerType = controllerType
            };
            return DataControllerDescription.GetDescription(controllerDescriptor);
        }
    }

    internal class InvalidController_NonAuthMethodFilter : DataController
    {
        // attempt to apply a non-auth filter
        [TestActionFilter]
        public void UpdateProduct(Microsoft.Web.Http.Data.Test.Models.EF.Product product)
        {
        }

        // the restriction doesn't apply for non CUD actions
        [TestActionFilter]
        public IEnumerable<Microsoft.Web.Http.Data.Test.Models.EF.Product> GetProducts()
        {
            return null;
        }
    }

    internal class TaskReturningGetActionsController : DataController
    {
        public Task<IEnumerable<City>> GetCities()
        {
            return null;
        }

        public Task<State> GetState(string name)
        {
            return null;
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class TestActionFilterAttribute : ActionFilterAttribute
    {
    }

    internal class IncludedAssociationTestController_ExplicitDataContract : LinqToEntitiesDataController<Microsoft.Web.Http.Data.Test.Models.EF.NorthwindEntities>
    {
        public IQueryable<Microsoft.Web.Http.Data.Test.Models.EF.Order> GetOrders() { return null; }
    }

    internal class IncludedAssociationTestController_ImplicitDataContract : DataController
    {
        public IQueryable<Microsoft.Web.Http.Data.Test.Models.Customer> GetCustomers() { return null; }
    }
}