Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConventionsHelpersTests.cs « Conventions « Builder « OData « System.Web.Http.OData.Test « test - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0d689966711fbae92265d087b649ccea4022a245 (plain)
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
366
367
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TestCommon;
using Microsoft.TestCommon.Types;
using Moq;

namespace System.Web.Http.OData.Builder.Conventions
{
    public class ConventionsHelpersTests
    {
        public static TheoryDataSet<object, string> GetEntityKeyValue_SingleKey_DifferentDataTypes_Data
        {
            get
            {
                return new TheoryDataSet<object, string>
                {
                    { 1, "1" },
                    { "1", "'1'" },
                    { new DateTime(2012,12,31),"datetime'2012-12-31T00:00:00'" },
                    { new byte[] { 1,2 }, "binary'0102'" },
                    { false, "false" },
                    { true, "true" },
                    { new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd"), "guid'dddddddd-dddd-dddd-dddd-dddddddddddd'" }
                };
            }
        }

        public static TheoryDataSet<object, string> GetUriRepresentationForValue_DataSet
        {
            get
            {
                return new TheoryDataSet<object, string>()
                {
                    { (bool)true, "true" },
                    { (char)'1', "'1'" },
                    { (char?)'1', "'1'" },
                    { (string)"123", "'123'" },
                    { (char[])new char[] { '1', '2', '3' }, "'123'" },
                    { (int)123, "123" },
                    { (short)123, "123" },
                    { (long)123, "123L" },
                    { (ushort)123, "123" },
                    { (uint)123, "123L" },
                    { (ulong)123, "123L" },
                    { (int?)123, "123" },
                    { (short?)123, "123" },
                    { (long?)123, "123L" },
                    { (ushort?)123, "123" },
                    { (uint?)123, "123L" },
                    { (ulong?)123, "123L" },
                    { (float)123.123, "123.12f" },
                    { (double)123.123, "123.123" },
                    { (decimal)123.123, "123.123M" },
                    { Guid.Empty, "guid'00000000-0000-0000-0000-000000000000'" },
                    { DateTime.FromBinary(0), "datetime'0001-01-01T00:00:00'" },
                    { TimeSpan.FromSeconds(86456), "time'P1DT56S'" },
                    { DateTimeOffset.FromFileTime(0), "datetimeoffset'1600-12-31T16:00:00-08:00'" },
                    { SimpleEnum.First, "'First'" },
                    { FlagsEnum.One | FlagsEnum.Two, "'One%2C%20Two'" },
                };
            }
        }

        [Theory]
        [InlineData(typeof(ICollection<string>), typeof(string))]
        [InlineData(typeof(IList<string>), typeof(string))]
        [InlineData(typeof(List<int>), typeof(int))]
        [InlineData(typeof(IsCollection_with_Collections_TestClass), typeof(bool))]
        [InlineData(typeof(IEnumerable<int>), typeof(int))]
        [InlineData(typeof(int[]), typeof(int))]
        [InlineData(typeof(MyCustomCollection), typeof(int))]
        public void IsCollection_with_Collections(Type collectionType, Type elementType)
        {
            Type type;
            Assert.True(collectionType.IsCollection(out type));
            Assert.Equal(elementType, type);
        }

        [Theory]
        [InlineData(typeof(object))]
        [InlineData(typeof(ICollection))]
        [InlineData(typeof(IEnumerable))]
        [InlineData(typeof(string))]
        public void IsCollection_with_NonCollections(Type type)
        {
            Assert.False(type.IsCollection());
        }

        [Fact]
        public void GetProperties_ReturnsProperties_FromBaseAndDerived()
        {
            Mock<IStructuralTypeConfiguration> edmType = new Mock<IStructuralTypeConfiguration>();
            edmType.Setup(t => t.ClrType).Returns(typeof(GetProperties_Derived));

            var properties = ConventionsHelpers.GetAllProperties(edmType.Object);
            var expectedProperties = new string[] { "Base_I", "Base_Complex", "Base_Str", "Derived_I", "Derived_Complex", "Collection", "PrivateSetPublicGet" };

            Assert.Equal(expectedProperties.OrderByDescending(name => name), properties.Select(p => p.Name).OrderByDescending(name => name));
        }

        [Fact]
        public void GetProperties_Ignores_IgnoredProperties()
        {
            Mock<IStructuralTypeConfiguration> edmType = new Mock<IStructuralTypeConfiguration>();
            edmType.Setup(t => t.ClrType).Returns(typeof(GetProperties_Derived));
            edmType.Setup(t => t.IgnoredProperties).Returns(typeof(GetProperties_Derived).GetProperties().Where(p => new string[] { "Base_I", "Derived_I" }.Contains(p.Name)));

            var properties = ConventionsHelpers.GetAllProperties(edmType.Object);
            var expectedProperties = new string[] { "Base_Complex", "Base_Str", "Derived_Complex", "Collection", "PrivateSetPublicGet" };

            Assert.Equal(expectedProperties.OrderByDescending(name => name), properties.Select(p => p.Name).OrderByDescending(name => name));
        }

        [Fact]
        public void GetAllProperties_Ignores_IndexerProperties()
        {
            Mock<IStructuralTypeConfiguration> edmType = new Mock<IStructuralTypeConfiguration>();
            edmType.Setup(t => t.ClrType).Returns(typeof(GetProperties_Derived));

            var properties = ConventionsHelpers.GetAllProperties(edmType.Object).Select(p => p.Name);
            Assert.DoesNotContain("Item", properties);
        }

        [Fact]
        public void GetEntityKeyValue_SingleKey()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = "key" };
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }

        [Theory]
        [PropertyData("GetEntityKeyValue_SingleKey_DifferentDataTypes_Data")]
        public void GetEntityKeyValue_SingleKey_DifferentDataTypes(object value, object expectedValue)
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = value };
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal(expectedValue, keyValue);
        }

        [Fact]
        public void GetEntityKeyValue_MultipleKeys()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }

        [Fact]
        public void GetEntityKeyValue_ThrowsForNullKeys()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = (string)null };
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType.Setup(e => e.Keys).Returns(keys);
            entityType.Setup(e => e.FullName).Returns("FullName");

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object),
                "Key property 'Key' of type 'FullName' is null. Key properties cannot have null values.");
        }

        [Fact]
        public void GetEntityKeyValue_ThrowsForNullKeys_WithMultipleKeys()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "abc", Key2 = "def", Key3 = (string)null };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType.Setup(e => e.Keys).Returns(keys);
            entityType.Setup(e => e.FullName).Returns("EntityType");

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object),
                "Key property 'Key3' of type 'EntityType' is null. Key properties cannot have null values.");
        }

        [Fact]
        public void GetEntityKeyValue_DerivedType()
        {
            // Arrange
            var entityInstance = new { Key = "key" };

            Mock<IEntityTypeConfiguration> baseEntityType = new Mock<IEntityTypeConfiguration>();
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), baseEntityType.Object) };
            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock<IEntityTypeConfiguration> derivedEntityType = new Mock<IEntityTypeConfiguration>();
            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, derivedEntityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }

        [Fact]
        public void GetEntityKeyValue_MultipleKeys_DerivedType()
        {
            // Arrange
            Mock<IEntityTypeConfiguration> baseEntityType = new Mock<IEntityTypeConfiguration>();

            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), baseEntityType.Object),
            };

            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock<IEntityTypeConfiguration> derivedEntityType = new Mock<IEntityTypeConfiguration>();
            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, derivedEntityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }

        [Theory]
        [PropertyData("GetUriRepresentationForValue_DataSet")]
        public void GetUriRepresentationForValue_Works(object value, string result)
        {
            Assert.Equal(
                result,
                ConventionsHelpers.GetUriRepresentationForValue(value));
        }

        private sealed class IsCollection_with_Collections_TestClass : List<bool>
        {
        }

        private class GetKeyProperty_validEntityType_TestClass_Id
        {
            public int Id { get; set; }
        }

        private class GetKeyProperty_validEntityType_TestClass2_ClassName
        {
            public string GetKeyProperty_validEntityType_TestClass2_ClassNameId { get; set; }
        }

        private class GetKeyProperty_InValidEntityType_NoId
        {
            public int IDD { get; set; }
        }

        private class GetKeyProperty_InValidEntityType_ComplexId
        {
            public GetProperties_Complex Id { get; set; }
        }
    }

    class GetProperties_Base
    {
        public int Base_I { get; set; }

        private int Pri { get; set; }

        public int InternalGet { internal get; set; }

        public GetProperties_Complex Base_Complex { get; set; }

        public virtual string Base_Str { get; set; }
    }

    class GetProperties_Derived : GetProperties_Base
    {
        public static int SomeStaticProperty1 { get; set; }

        internal static int SomeStaticProperty2 { get; set; }

        private static int SomeStaticProperty3 { get; set; }

        public string Derived_I { get; set; }

        public string PrivateSetPublicGet { get; private set; }

        public string PrivateGetPublicSet { private get; set; }

        public GetProperties_Complex Derived_Complex { get; set; }

        public int[] Collection { get; private set; }

        public string this[string str]
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }
    }

    class GetProperties_Complex
    {
        public int A { get; set; }
    }

    class MyCustomCollection : List<int>
    {
    }
}