forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociatedMetadataProviderTest.cs
More file actions
365 lines (299 loc) · 14.3 KB
/
Copy pathAssociatedMetadataProviderTest.cs
File metadata and controls
365 lines (299 loc) · 14.3 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.TestCommon;
namespace System.Web.Mvc.Test
{
public class AssociatedMetadataProviderTest
{
// FilterAttributes
[Fact]
public void ReadOnlyAttributeIsFilteredOffWhenContainerTypeIsViewPage()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ViewPage<PropertyModel>), "Model");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.DoesNotContain(parms.Attributes, a => a is ReadOnlyAttribute);
}
[Fact]
public void ReadOnlyAttributeIsFilteredOffWhenContainerTypeIsViewUserControl()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ViewUserControl<PropertyModel>), "Model");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.DoesNotContain(parms.Attributes, a => a is ReadOnlyAttribute);
}
[Fact]
public void ReadOnlyAttributeIsPreservedForReadOnlyModelProperties()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ModelWithReadOnlyProperty), "ReadOnlyProperty");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.Contains(parms.Attributes, a => a is ReadOnlyAttribute);
}
// GetMetadataForProperties
[Fact]
public void GetMetadataForPropertiesNullContainerTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForProperties(new Object(), null),
"containerType");
}
[Fact]
public void GetMetadataForPropertiesCreatesMetadataForAllPropertiesOnModelWithPropertyValues()
{
// Arrange
PropertyModel model = new PropertyModel { LocalAttributes = 42, MetadataAttributes = "hello", MixedAttributes = 21.12 };
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperties(model, typeof(PropertyModel)).ToList(); // Call ToList() to force the lazy evaluation to evaluate
// Assert
CreateMetadataParams local =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "LocalAttributes");
Assert.Equal(typeof(int), local.ModelType);
Assert.Equal(42, local.Model);
Assert.Contains(local.Attributes, a => a is RequiredAttribute);
CreateMetadataParams metadata =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "MetadataAttributes");
Assert.Equal(typeof(string), metadata.ModelType);
Assert.Equal("hello", metadata.Model);
Assert.Contains(metadata.Attributes, a => a is RangeAttribute);
CreateMetadataParams mixed =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "MixedAttributes");
Assert.Equal(typeof(double), mixed.ModelType);
Assert.Equal(21.12, mixed.Model);
Assert.Contains(mixed.Attributes, a => a is RequiredAttribute);
Assert.Contains(mixed.Attributes, a => a is RangeAttribute);
}
[Fact]
public void GetMetadataForPropertiesContainerTest()
{
// Arrange
PropertyModel model = new PropertyModel { LocalAttributes = 42, MetadataAttributes = "hello", MixedAttributes = 21.12 };
EmptyModelMetadataProvider provider = new EmptyModelMetadataProvider();
// Act
List<ModelMetadata> metadata = provider.GetMetadataForProperties(model, typeof(PropertyModel)).ToList();
// Assert
Assert.Equal(3, metadata.Count());
Assert.Same(model, metadata[0].Container);
Assert.Same(model, metadata[1].Container);
Assert.Same(model, metadata[2].Container);
}
[Fact]
public void GetMetadataForPropertyWithNullContainerReturnsMetadataWithNullValuesForProperties()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperties(null, typeof(PropertyModel)).ToList(); // Call ToList() to force the lazy evaluation to evaluate
// Assert
Assert.True(provider.CreateMetadataLog.Any());
foreach (var parms in provider.CreateMetadataLog)
{
Assert.Null(parms.Model);
}
}
// GetMetadataForProperty
[Fact]
public void GetMetadataForPropertyNullContainerTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForProperty(null /* model */, null /* containerType */, "propertyName"),
"containerType");
}
[Fact]
public void GetMetadataForPropertyNullOrEmptyPropertyNameThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
() => provider.GetMetadataForProperty(null /* model */, typeof(object), null /* propertyName */),
"propertyName");
Assert.ThrowsArgumentNullOrEmpty(
() => provider.GetMetadataForProperty(null, typeof(object), String.Empty),
"propertyName");
}
[Fact]
public void GetMetadataForPropertyInvalidPropertyNameThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.Throws<ArgumentException>(
() => provider.GetMetadataForProperty(null, typeof(object), "BadPropertyName"),
"The property System.Object.BadPropertyName could not be found.");
}
[Fact]
public void GetMetadataForPropertyWithLocalAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(int), "LocalAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "LocalAttributes");
// Assert
Assert.Same(metadata, result);
Assert.Contains(provider.CreateMetadataLog.Single().Attributes, a => a is RequiredAttribute);
}
[Fact]
public void GetMetadataForPropertyWithMetadataAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(string), "MetadataAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "MetadataAttributes");
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.PropertyName == "MetadataAttributes");
Assert.Contains(parms.Attributes, a => a is RangeAttribute);
}
[Fact]
public void GetMetadataForPropertyWithMixedAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(double), "MixedAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "MixedAttributes");
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.PropertyName == "MixedAttributes");
Assert.Contains(parms.Attributes, a => a is RequiredAttribute);
Assert.Contains(parms.Attributes, a => a is RangeAttribute);
}
// GetMetadataForType
[Fact]
public void GetMetadataForTypeNullModelTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForType(() => new Object(), null),
"modelType");
}
[Fact]
public void GetMetadataForTypeIncludesAttributesOnType()
{
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, null, null, typeof(TypeModel), null);
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForType(null, typeof(TypeModel));
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.ModelType == typeof(TypeModel));
Assert.Contains(parms.Attributes, a => a is ReadOnlyAttribute);
}
[AdditionalMetadata("ClassName", "ClassValue")]
class ClassWithAdditionalMetadata
{
[AdditionalMetadata("PropertyName", "PropertyValue")]
public int MyProperty { get; set; }
}
[Fact]
public void MetadataAwareAttributeCanModifyTypeMetadata()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
provider.CreateMetadataReturnValue = new ModelMetadata(provider, null, null, typeof(ClassWithAdditionalMetadata), null);
// Act
ModelMetadata metadata = provider.GetMetadataForType(null, typeof(ClassWithAdditionalMetadata));
// Assert
var kvp = metadata.AdditionalValues.Single();
Assert.Equal("ClassName", kvp.Key);
Assert.Equal("ClassValue", kvp.Value);
}
[Fact]
public void MetadataAwareAttributeCanModifyPropertyMetadata()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
provider.CreateMetadataReturnValue = new ModelMetadata(provider, typeof(ClassWithAdditionalMetadata), null, typeof(int), "MyProperty");
// Act
ModelMetadata metadata = provider.GetMetadataForProperty(null, typeof(ClassWithAdditionalMetadata), "MyProperty");
// Assert
var kvp = metadata.AdditionalValues.Single();
Assert.Equal("PropertyName", kvp.Key);
Assert.Equal("PropertyValue", kvp.Value);
}
// Helpers
[MetadataType(typeof(Metadata))]
private class PropertyModel
{
[Required]
public int LocalAttributes { get; set; }
public string MetadataAttributes { get; set; }
[Required]
public double MixedAttributes { get; set; }
private class Metadata
{
[Range(10, 100)]
public object MetadataAttributes { get; set; }
[Range(10, 100)]
public object MixedAttributes { get; set; }
}
}
private class ModelWithReadOnlyProperty
{
public int ReadOnlyProperty { get; private set; }
}
[ReadOnly(true)]
private class TypeModel
{
}
class TestableAssociatedMetadataProvider : AssociatedMetadataProvider
{
public List<CreateMetadataParams> CreateMetadataLog = new List<CreateMetadataParams>();
public ModelMetadata CreateMetadataReturnValue = null;
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType,
Func<object> modelAccessor, Type modelType,
string propertyName)
{
CreateMetadataLog.Add(new CreateMetadataParams
{
Attributes = attributes,
ContainerType = containerType,
Model = modelAccessor == null ? null : modelAccessor(),
ModelType = modelType,
PropertyName = propertyName
});
return CreateMetadataReturnValue;
}
}
class CreateMetadataParams
{
public IEnumerable<Attribute> Attributes { get; set; }
public Type ContainerType { get; set; }
public object Model { get; set; }
public Type ModelType { get; set; }
public string PropertyName { get; set; }
}
}
}