forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleProjectTest.cs
More file actions
208 lines (185 loc) · 9.15 KB
/
Copy pathSimpleProjectTest.cs
File metadata and controls
208 lines (185 loc) · 9.15 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NET461
using System;
using System.Collections;
using System.IO;
using System.Linq;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.Tools.TestUtilities;
using Xunit;
namespace Microsoft.EntityFrameworkCore.Tools
{
[Collection("OperationExecutorTests")]
public class SimpleProjectTest : IClassFixture<SimpleProjectTest.SimpleProject>
{
private readonly SimpleProject _project;
public SimpleProjectTest(SimpleProject project)
{
_project = project;
}
private void AssertDefaultMigrationName(IDictionary artifacts)
=> Assert.Contains("namespace SimpleProject.Migrations", File.ReadAllText(artifacts["MigrationFile"] as string));
[Fact]
public void AddMigration()
{
var artifacts = _project.Executor.AddMigration("EmptyMigration", "CustomFolder", "SimpleContext");
Assert.NotNull(artifacts);
Assert.NotNull(artifacts["MigrationFile"]);
Assert.NotNull(artifacts["MetadataFile"]);
Assert.NotNull(artifacts["SnapshotFile"]);
Assert.True(Directory.Exists(Path.Combine(_project.TargetDir, "CustomFolder")));
Assert.Contains("namespace SimpleProject.CustomFolder", File.ReadAllText(artifacts["MigrationFile"] as string));
}
[Fact]
public void AddMigration_output_dir_relative_to_projectdir()
{
var artifacts = _project.Executor.AddMigration("EmptyMigration1", "./CustomFolder", "SimpleContext");
Assert.NotNull(artifacts);
Assert.StartsWith(Path.Combine(_project.TargetDir, "CustomFolder"), artifacts["MigrationFile"] as string);
Assert.Contains("namespace SimpleProject.CustomFolder", File.ReadAllText(artifacts["MigrationFile"] as string));
}
[Fact]
public void AddMigration_output_dir_relative_out_of_to_projectdir()
{
var artifacts = _project.Executor.AddMigration("EmptyMigration1", "../CustomFolder", "SimpleContext");
Assert.NotNull(artifacts);
Assert.StartsWith(Path.GetFullPath(Path.Combine(_project.TargetDir, "../CustomFolder")), artifacts["MigrationFile"] as string);
AssertDefaultMigrationName(artifacts);
}
[Fact]
public void AddMigration_output_dir_absolute_path_in_project()
{
var outputDir = Path.Combine(_project.TargetDir, "A/B/C");
var artifacts = _project.Executor.AddMigration("EmptyMigration1", outputDir, "SimpleContext");
Assert.NotNull(artifacts);
Assert.Equal(Path.Combine(outputDir, Path.GetFileName(artifacts["MigrationFile"] as string)), artifacts["MigrationFile"]);
Assert.Contains("namespace SimpleProject.A.B.C", File.ReadAllText(artifacts["MigrationFile"] as string));
}
[Fact]
public void AddMigration_output_dir_absolute_path_outside_project()
{
var outputDir = Path.GetTempPath();
var artifacts = _project.Executor.AddMigration("EmptyMigration1", outputDir, "SimpleContext");
Assert.NotNull(artifacts);
Assert.StartsWith(outputDir, artifacts["MigrationFile"] as string);
AssertDefaultMigrationName(artifacts);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void AddMigration_handles_empty_output_dir(string outputDir)
{
var artifacts = _project.Executor.AddMigration("EmptyMigration2", outputDir, "SimpleContext");
Assert.NotNull(artifacts);
Assert.StartsWith(Path.Combine(_project.TargetDir, "Migrations"), artifacts["MigrationFile"] as string);
AssertDefaultMigrationName(artifacts);
}
[Fact]
public void ScriptMigration()
{
var sql = _project.Executor.ScriptMigration(null, "InitialCreate", false, "SimpleContext");
Assert.NotEmpty(sql);
}
[Fact]
public void GetContextType()
{
var contextTypeName = _project.Executor.GetContextType("SimpleContext");
Assert.StartsWith("SimpleProject.SimpleContext, ", contextTypeName);
}
[Fact]
public void GetContextTypes()
{
var contextTypes = _project.Executor.GetContextTypes();
Assert.Equal(1, contextTypes.Count());
}
[Fact]
public void GetMigrations()
{
var migrations = _project.Executor.GetMigrations("SimpleContext");
Assert.Equal(1, migrations.Count());
}
[Fact]
public void GetContextInfo_returns_connection_string()
{
var info = _project.Executor.GetContextInfo("SimpleContext");
Assert.Equal(@"(localdb)\MSSQLLocalDB", info["DataSource"]);
Assert.Equal("SimpleProject.SimpleContext", info["DatabaseName"]);
}
public class SimpleProject : IDisposable
{
private readonly TempDirectory _directory = new TempDirectory();
public SimpleProject()
{
var source = new BuildSource
{
TargetDir = TargetDir,
References =
{
BuildReference.ByName("System.Diagnostics.DiagnosticSource", true),
BuildReference.ByName("System.Interactive.Async", true),
BuildReference.ByName("System.Data.SqlClient", true),
BuildReference.ByName("Microsoft.EntityFrameworkCore", true),
BuildReference.ByName("Microsoft.EntityFrameworkCore.Design", true),
BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational", true),
BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational.Design", true),
BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer", true),
BuildReference.ByName("Microsoft.Extensions.Caching.Abstractions", true),
BuildReference.ByName("Microsoft.Extensions.Caching.Memory", true),
BuildReference.ByName("Microsoft.Extensions.Configuration.Abstractions", true),
BuildReference.ByName("Microsoft.Extensions.DependencyInjection", true),
BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions", true),
BuildReference.ByName("Microsoft.Extensions.Logging", true),
BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions", true),
BuildReference.ByName("Microsoft.Extensions.Options", true),
BuildReference.ByName("Remotion.Linq", true)
},
Sources = { @"
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
namespace SimpleProject
{
internal class SimpleContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SimpleProject.SimpleContext;Integrated Security=True"");
}
}
namespace Migrations
{
[DbContext(typeof(SimpleContext))]
[Migration(""20141010222726_InitialCreate"")]
public class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
}
}
}" }
};
var build = source.Build();
File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, build.TargetPath + ".config");
Executor = new AppDomainOperationExecutor(build.TargetPath,
build.TargetPath,
build.TargetDir,
build.TargetDir,
"SimpleProject");
}
public string TargetDir => _directory.Path;
internal IOperationExecutor Executor { get; }
public void Dispose()
{
Executor.Dispose();
_directory.Dispose();
}
}
}
}
#elif NETCOREAPP2_0
#else
#error target frameworks need to be updated.
#endif