File: AttributeProviderTests.cs

package info (click to toggle)
mono 3.2.8%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 527,964 kB
  • ctags: 623,327
  • sloc: cs: 3,938,236; xml: 1,891,753; ansic: 418,737; java: 59,920; sh: 15,754; makefile: 11,067; sql: 7,956; perl: 2,279; cpp: 1,380; yacc: 1,203; python: 594; asm: 422; sed: 16; php: 1
file content (184 lines) | stat: -rw-r--r-- 6,749 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.ModelConfiguration.Utilities
{
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using Xunit;

    public sealed class AttributeProviderTests
    {
        [Fact]
        public void GetAttributes_returns_empty_enumerable_when_no_property_descriptor()
        {
            var attributes = new AttributeProvider().GetAttributes(new MockPropertyInfo());

            Assert.Equal(0, attributes.Count());
        }

        [Fact]
        public void GetAttributes_returns_correct_set_of_type_attributes()
        {
            var attributes = new AttributeProvider().GetAttributes(typeof(AttributeProviderTestClass));

            Assert.Equal(1, attributes.OfType<DataContractAttribute>().Count());
            Assert.Equal(1, attributes.OfType<TableAttribute>().Count());
        }

        [Fact]
        public void GetAttributes_returns_correct_set_of_empty_type_attributes()
        {
            var attributes = new AttributeProvider().GetAttributes(typeof(AttributeProviderTestEmptyClass));

            Assert.Equal(0, attributes.Count());
        }

        [Fact]
        public void GetAttributes_returns_correct_set_of_property_attributes()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(typeof(AttributeProviderTestClass).GetProperty("MyProp"));

            Assert.Equal(1, attributes.OfType<KeyAttribute>().Count());
            Assert.Equal(1, attributes.OfType<RequiredAttribute>().Count());
            Assert.Equal(1, attributes.OfType<DataMemberAttribute>().Count());
            Assert.Equal(1, attributes.OfType<MaxLengthAttribute>().Count());
        }

        [Fact]
        public void AttributeCollectionFactory_returns_correct_set_of_empty_property_attributes()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(typeof(AttributeProviderTestEmptyClass).GetProperty("MyProp"));

            Assert.Equal(0, attributes.Count());
        }

        [Fact]
        public void GetAttributes_returns_attributes_from_buddy_class()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(typeof(AttributeProviderTestClass).GetProperty("BuddyProp"));

            Assert.Equal(1, attributes.OfType<KeyAttribute>().Count());
        }

        [Fact]
        public void AttributeCollectionFactory_returns_only_property_attributes_for_complex_type()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(typeof(AttributeProviderEntityWithComplexProperty).GetProperty("CT"));

            Assert.Equal(1, attributes.Count());
            Assert.Equal(typeof(AttributeProviderEntityWithComplexProperty), ((CustomValidationAttribute)attributes.First()).ValidatorType);
            Assert.Equal("ValidateProperty", ((CustomValidationAttribute)attributes.First()).Method);
        }

        [Fact]
        public void GetAttributes_returns_correct_set_of_non_public_type_attributes()
        {
            var attributes = new AttributeProvider().GetAttributes(typeof(NonPublicAttributeProviderTestClass));

            Assert.Equal(1, attributes.OfType<DataContractAttribute>().Count());
            Assert.Equal(1, attributes.OfType<TableAttribute>().Count());
        }

        [Fact]
        public void GetAttributes_returns_correct_set_of_non_public_property_attributes()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(
                    typeof(NonPublicAttributeProviderTestClass)
                        .GetProperty("MyProp", BindingFlags.NonPublic | BindingFlags.Instance));

            Assert.Equal(1, attributes.OfType<KeyAttribute>().Count());
            Assert.Equal(1, attributes.OfType<RequiredAttribute>().Count());
            Assert.Equal(1, attributes.OfType<DataMemberAttribute>().Count());
            Assert.Equal(1, attributes.OfType<MaxLengthAttribute>().Count());
        }

        [Fact]
        public void GetAttributes_does_not_return_attributes_from_non_public_buddy_class()
        {
            var attributes = new AttributeProvider()
                .GetAttributes(
                    typeof(NonPublicAttributeProviderTestClass)
                        .GetProperty("BuddyProp", BindingFlags.NonPublic | BindingFlags.Instance));

            Assert.Equal(0, attributes.OfType<KeyAttribute>().Count());
        }

        #region Test Fixtures

        [DataContract]
        [Table("Foo")]
        [MetadataType(typeof(AttributeProviderBuddyClass))]
        public class AttributeProviderTestClass
        {
            [Key]
            [Required]
            [DataMember(Order = 55)]
            [MaxLength(5)]
            public string MyProp { get; set; }

            public int BuddyProp { get; set; }
        }

        public class AttributeProviderTestEmptyClass
        {
            public string MyProp { get; set; }
        }

        public class AttributeProviderBuddyClass
        {
            [Key]
            public int BuddyProp { get; set; }
        }

        [DataContract]
        [Table("Foo")]
        [MetadataType(typeof(NonPublicAttributeProviderBuddyClass))]
        private class NonPublicAttributeProviderTestClass
        {
            [Key]
            [Required]
            [DataMember(Order = 55)]
            [MaxLength(5)]
            private string MyProp { get; set; }

            private int BuddyProp { get; set; }
        }

        public class NonPublicAttributeProviderBuddyClass
        {
            [Key]
            private int BuddyProp { get; set; }
        }

        [ComplexType]
        [CustomValidation(typeof(AttributeProviderComplexType), "ValidateWholeType")]
        public class AttributeProviderComplexType
        {
            public int Data { get; set; }

            public static void ValidateWholeType()
            {
            }
        }

        public class AttributeProviderEntityWithComplexProperty
        {
            [CustomValidation(typeof(AttributeProviderEntityWithComplexProperty), "ValidateProperty")]
            public AttributeProviderComplexType CT { get; set; }

            public static void ValidateProperty()
            {
            }
        }

        #endregion
    }
}