|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System.ComponentModel.DataAnnotations; |
5 | | -using Microsoft.AspNetCore.Http.Validation; |
6 | | -using System.Collections.Generic; |
7 | 5 | using System.Reflection; |
8 | 6 |
|
9 | | -namespace Microsoft.AspNetCore.Http.Tests; |
| 7 | +namespace Microsoft.AspNetCore.Http.Validation.Tests; |
10 | 8 |
|
11 | 9 | public class ValidatableTypeInfoTests |
12 | 10 | { |
@@ -61,11 +59,27 @@ [new RequiredAttribute()]) |
61 | 59 |
|
62 | 60 | // Assert |
63 | 61 | Assert.NotNull(context.ValidationErrors); |
64 | | - Assert.Equal(4, context.ValidationErrors.Count); |
65 | | - Assert.Contains("Name", context.ValidationErrors.Keys); |
66 | | - Assert.Contains("Age", context.ValidationErrors.Keys); |
67 | | - Assert.Contains("Address.Street", context.ValidationErrors.Keys); |
68 | | - Assert.Contains("Address.City", context.ValidationErrors.Keys); |
| 62 | + Assert.Collection(context.ValidationErrors, |
| 63 | + kvp => |
| 64 | + { |
| 65 | + Assert.Equal("Name", kvp.Key); |
| 66 | + Assert.Equal("The Name field is required.", kvp.Value.First()); |
| 67 | + }, |
| 68 | + kvp => |
| 69 | + { |
| 70 | + Assert.Equal("Age", kvp.Key); |
| 71 | + Assert.Equal("The field Age must be between 0 and 120.", kvp.Value.First()); |
| 72 | + }, |
| 73 | + kvp => |
| 74 | + { |
| 75 | + Assert.Equal("Address.Street", kvp.Key); |
| 76 | + Assert.Equal("The Street field is required.", kvp.Value.First()); |
| 77 | + }, |
| 78 | + kvp => |
| 79 | + { |
| 80 | + Assert.Equal("Address.City", kvp.Key); |
| 81 | + Assert.Equal("The City field is required.", kvp.Value.First()); |
| 82 | + }); |
69 | 83 | } |
70 | 84 |
|
71 | 85 | [Fact] |
@@ -106,8 +120,9 @@ [new RequiredAttribute()]), |
106 | 120 |
|
107 | 121 | // Assert |
108 | 122 | Assert.NotNull(context.ValidationErrors); |
109 | | - Assert.Contains("Salary", context.ValidationErrors.Keys); |
110 | | - Assert.Equal("Salary must be a positive value.", context.ValidationErrors["Salary"].First()); |
| 123 | + var error = Assert.Single(context.ValidationErrors); |
| 124 | + Assert.Equal("Salary", error.Key); |
| 125 | + Assert.Equal("Salary must be a positive value.", error.Value.First()); |
111 | 126 | } |
112 | 127 |
|
113 | 128 | [Fact] |
@@ -156,10 +171,22 @@ [new RangeAttribute(2, 5)]) |
156 | 171 |
|
157 | 172 | // Assert |
158 | 173 | Assert.NotNull(context.ValidationErrors); |
159 | | - Assert.Equal(3, context.ValidationErrors.Count); |
160 | | - Assert.Contains("Make", context.ValidationErrors.Keys); |
161 | | - Assert.Contains("Model", context.ValidationErrors.Keys); |
162 | | - Assert.Contains("Doors", context.ValidationErrors.Keys); |
| 174 | + Assert.Collection(context.ValidationErrors, |
| 175 | + kvp => |
| 176 | + { |
| 177 | + Assert.Equal("Doors", kvp.Key); |
| 178 | + Assert.Equal("The field Doors must be between 2 and 5.", kvp.Value.First()); |
| 179 | + }, |
| 180 | + kvp => |
| 181 | + { |
| 182 | + Assert.Equal("Make", kvp.Key); |
| 183 | + Assert.Equal("The Make field is required.", kvp.Value.First()); |
| 184 | + }, |
| 185 | + kvp => |
| 186 | + { |
| 187 | + Assert.Equal("Model", kvp.Key); |
| 188 | + Assert.Equal("The Model field is required.", kvp.Value.First()); |
| 189 | + }); |
163 | 190 | } |
164 | 191 |
|
165 | 192 | [Fact] |
@@ -214,10 +241,22 @@ [new RequiredAttribute()]), |
214 | 241 |
|
215 | 242 | // Assert |
216 | 243 | Assert.NotNull(context.ValidationErrors); |
217 | | - Assert.Equal(3, context.ValidationErrors.Count); |
218 | | - Assert.Contains("Items[1].ProductName", context.ValidationErrors.Keys); |
219 | | - Assert.Contains("Items[1].Quantity", context.ValidationErrors.Keys); |
220 | | - Assert.Contains("Items[2].Quantity", context.ValidationErrors.Keys); |
| 244 | + Assert.Collection(context.ValidationErrors, |
| 245 | + kvp => |
| 246 | + { |
| 247 | + Assert.Equal("Items[1].ProductName", kvp.Key); |
| 248 | + Assert.Equal("The ProductName field is required.", kvp.Value.First()); |
| 249 | + }, |
| 250 | + kvp => |
| 251 | + { |
| 252 | + Assert.Equal("Items[1].Quantity", kvp.Key); |
| 253 | + Assert.Equal("The field Quantity must be between 1 and 100.", kvp.Value.First()); |
| 254 | + }, |
| 255 | + kvp => |
| 256 | + { |
| 257 | + Assert.Equal("Items[2].Quantity", kvp.Key); |
| 258 | + Assert.Equal("The field Quantity must be between 1 and 100.", kvp.Value.First()); |
| 259 | + }); |
221 | 260 | } |
222 | 261 |
|
223 | 262 | [Fact] |
|
0 commit comments