File: DbConfigurationFinderTests.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 (121 lines) | stat: -rw-r--r-- 5,076 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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Config
{
    using System.Data.Entity.Resources;
    using System.Data.Entity.TestHelpers;
    using Moq;
    using Xunit;

    public class DbConfigurationFinderTests
    {
        private static readonly Type _normalConfigType = new Mock<DbConfiguration>().Object.GetType();

        public class TryFindConfigurationType
        {
            [Fact]
            public void TryFindConfigurationType_returns_the_configuration_pointed_to_by_the_DbConfigurationTypeAttribute_if_found()
            {
                Assert.Same(
                    typeof(FunctionalTestsConfiguration),
                    new DbConfigurationFinder().TryFindConfigurationType(
                        typeof(FakeContextWithAttribute),
                        new Type[0]));
            }

            [Fact]
            public void TryFindConfigurationType_throws_the_type_returned_by_DbConfigurationTypeAttribute_is_not_a_DbConfiguration()
            {
                Assert.Equal(
                    Strings.CreateInstance_BadDbConfigurationType(typeof(Random).ToString(), typeof(DbConfiguration).ToString()),
                    Assert.Throws<InvalidOperationException>(
                        () => new DbConfigurationFinder().TryFindConfigurationType(
                            typeof(FakeContextWithBadAttribute))).Message);
            }

            [Fact]
            public void TryFindConfigurationType_throws_if_types_list_contains_more_than_one_normal_DbConfigurations()
            {
                Assert.Equal(
                    Strings.MultipleConfigsInAssembly(_normalConfigType.Assembly, typeof(DbConfiguration).Name),
                    Assert.Throws<InvalidOperationException>(
                        () => new DbConfigurationFinder().TryFindConfigurationType(
                            typeof(DbContext),
                            new[] { typeof(object), _normalConfigType, _normalConfigType })).Message);
            }

            [Fact]
            public void TryFindConfigurationType_returns_the_normal_DbConfiguration_if_found()
            {
                Assert.Same(
                    _normalConfigType,
                    new DbConfigurationFinder().TryFindConfigurationType(
                        typeof(DbContext),
                        new[] { typeof(object), _normalConfigType, typeof(object) }));
            }

            [Fact]
            public void TryFindConfigurationType_returns_null_if_no_DbConfiguration_is_found()
            {
                Assert.Null(
                    new DbConfigurationFinder().TryFindConfigurationType(typeof(DbContext), new[] { typeof(object), typeof(object) }));
            }

            [Fact]
            public void TryFindConfigurationType_excludes_generic_types()
            {
                Assert.Null(
                    new DbConfigurationFinder().TryFindConfigurationType(typeof(DbContext), new[] { typeof(GenericConfiguration<>) }));
            }

            [Fact]
            public void TryFindConfigurationType_excludes_abstract_types()
            {
                Assert.Null(
                    new DbConfigurationFinder().TryFindConfigurationType(typeof(DbContext), new[] { typeof(AbstractConfiguration) }));
            }
        }

        public class TryCreateConfiguration
        {
            [Fact]
            public void TryCreateConfiguration_returns_null_if_TryFindConfigurationType_returns_null()
            {
                Assert.Null(new DbConfigurationFinder().TryCreateConfiguration(typeof(DbContext), new[] { typeof(object), typeof(object) }));
            }

            [Fact]
            public void TryCreateConfiguration_creates_instance_of_type_returned_by_TryFindConfigurationType()
            {
                Assert.IsType<FunctionalTestsConfiguration>(
                    new DbConfigurationFinder().TryCreateConfiguration(typeof(DbContext), new[] { typeof(FunctionalTestsConfiguration) }).
                        Owner);
            }

            [Fact]
            public void TryCreateConfiguration_creates_instance_of_type_specified_by_DbConfigurationTypeAttribute()
            {
                Assert.IsType<FunctionalTestsConfiguration>(
                    new DbConfigurationFinder().TryCreateConfiguration(typeof(FakeContextWithAttribute)).Owner);
            }
        }

        public abstract class AbstractConfiguration : DbConfiguration
        {
        }

        public class GenericConfiguration<T> : DbConfiguration
        {
        }

        [DbConfigurationType(typeof(FunctionalTestsConfiguration))]
        public class FakeContextWithAttribute : DbContext
        {
        }

        [DbConfigurationType(typeof(Random))]
        public class FakeContextWithBadAttribute : DbContext
        {
        }
    }
}