File: AddMigrationCommandTests.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (163 lines) | stat: -rw-r--r-- 5,807 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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Migrations
{
    using System.Collections.Generic;
    using System.Data.Entity.Migrations.Design;
    using System.Data.Entity.Migrations.Resources;
    using System.Data.Entity.Migrations.Utilities;
    using System.Linq;
    using EnvDTE;
    using Moq;
    using Xunit;

    public class AddMigrationCommandTests
    {
        [Fact]
        public void Writes_scaffolding_message_when_new_migration()
        {
            var command = new TestableAddMigrationCommand();

            command.Execute("M", false, false);

            Assert.Equal(Strings.ScaffoldingMigration("M"), command.Messages.Single());
        }

        [Fact]
        public void Opens_migration_file_when_no_error()
        {
            var command = new TestableAddMigrationCommand();

            command.Execute("M", false, false);

            command.MockDispatcher.Verify(p => p.OpenFile(@".\M"));
        }

        [Fact]
        public void Writes_rescaffolding_message_when_rescaffolding()
        {
            var command = new TestableAddMigrationCommand();

            command.MockToolingFacade
                   .Setup(f => f.Scaffold("M", It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                   .Returns(
                       new ScaffoldedMigration
                       {
                           IsRescaffold = true
                       });

            command.Execute("M", false, false);

            Assert.Equal(Strings.RescaffoldingMigration("M"), command.Messages.Single());
        }

        [Fact]
        public void Writes_code_behind_warning_when_new_migration()
        {
            var command = new TestableAddMigrationCommand();

            command.MockToolingFacade
                   .Setup(f => f.Scaffold("M", It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                   .Returns(
                       new ScaffoldedMigration
                           {
                               MigrationId = "Foo"
                           });

            command.Execute("M", false, false);

            Assert.Equal(Strings.SnapshotBehindWarning("M"), command.Warnings.Single());
        }

        [Fact]
        public void Writes_rescaffold_warning_when_new_migration_name_matches_only_applied_migration()
        {
            var command = new TestableAddMigrationCommand();

            command.MockToolingFacade
                   .Setup(f => f.GetDatabaseMigrations()).Returns(new[] { "201301040020540_M" });

            command.Execute("M", false, false);

            Assert.Equal(
                Strings.DidYouMeanToRescaffold("M", "$InitialDatabase", "M"),
                command.Warnings.Last().Trim());
        }

        [Fact]
        public void Writes_rescaffold_warning_when_new_migration_name_matches_last_applied_migration()
        {
            var command = new TestableAddMigrationCommand();

            command.MockToolingFacade
                   .Setup(f => f.GetDatabaseMigrations())
                   .Returns(new[] { "201301040020540_M2", "201301040020540_M1" });

            command.Execute("M2", false, false);

            Assert.Equal(
                Strings.DidYouMeanToRescaffold("M2", "201301040020540_M1", "M2"),
                command.Warnings.Last().Trim());
        }

        internal class TestableAddMigrationCommand : AddMigrationCommand
        {
            public readonly Mock<ToolingFacade> MockToolingFacade
                = new Mock<ToolingFacade>
                      {
                          DefaultValue = DefaultValue.Mock
                      };

            public readonly Mock<Project> MockProject
                = new Mock<Project>
                      {
                          DefaultValue = DefaultValue.Mock
                      };

            public readonly Mock<DomainDispatcher> MockDispatcher = new Mock<DomainDispatcher>();

            public readonly List<string> Messages = new List<string>();
            public readonly List<string> Warnings = new List<string>();

            public TestableAddMigrationCommand()
            {
                AppDomain.CurrentDomain.SetData("efDispatcher", MockDispatcher.Object);

                var mockFullPathProperty = new Mock<Property>();
                mockFullPathProperty.SetupGet(p => p.Value).Returns(".");

                MockProject.Setup(p => p.Properties.Item("FullPath")).Returns(mockFullPathProperty.Object);

                var mockRootNamespaceProperty = new Mock<Property>();
                mockRootNamespaceProperty.SetupGet(p => p.Value).Returns("N");

                MockProject.Setup(p => p.Properties.Item("RootNamespace")).Returns(mockRootNamespaceProperty.Object);
            }

            public override ToolingFacade GetFacade(string configurationTypeName = null)
            {
                return MockToolingFacade.Object;
            }

            public override Project Project
            {
                get { return MockProject.Object; }
            }

            public override void WriteLine(string message)
            {
                Messages.Add(message);
            }

            public override void WriteWarning(string message)
            {
                Warnings.Add(message);
            }

            protected override string WriteMigration(string name, bool force, ScaffoldedMigration scaffoldedMigration, bool rescaffolding)
            {
                return name;
            }
        }
    }
}