-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
CSharpModelGeneratorTest.cs
59 lines (49 loc) · 2.25 KB
/
CSharpModelGeneratorTest.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Design.Internal;
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal;
public class CSharpModelGeneratorTest
{
[ConditionalFact]
public void Language_works()
{
var generator = CreateGenerator();
var result = generator.Language;
Assert.Equal("C#", result);
}
[ConditionalFact]
public void WriteCode_works()
{
var generator = CreateGenerator();
var modelBuilder = FakeRelationalTestHelpers.Instance.CreateConventionBuilder();
modelBuilder.Entity("TestEntity").Property<int>("Id");
var result = generator.GenerateModel(
modelBuilder.FinalizeModel(designTime: true),
new ModelCodeGenerationOptions
{
ModelNamespace = "TestNamespace",
ContextNamespace = "ContextNameSpace",
ContextDir = Path.Combine("..", "TestContextDir" + Path.DirectorySeparatorChar),
ContextName = "TestContext",
ConnectionString = "Data Source=Test"
});
Assert.Equal(Path.Combine("..", "TestContextDir", "TestContext.cs"), result.ContextFile.Path);
Assert.NotEmpty(result.ContextFile.Code);
Assert.Equal(1, result.AdditionalFiles.Count);
Assert.Equal("TestEntity.cs", result.AdditionalFiles[0].Path);
Assert.NotEmpty(result.AdditionalFiles[0].Code);
}
private static IModelCodeGenerator CreateGenerator()
{
var testAssembly = typeof(CSharpModelGeneratorTest).Assembly;
var reporter = new TestOperationReporter();
return new DesignTimeServicesBuilder(testAssembly, testAssembly, reporter, new string[0])
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>()
.AddSingleton<IProviderConfigurationCodeGenerator, TestProviderCodeGenerator>()
.BuildServiceProvider(validateScopes: true)
.GetServices<IModelCodeGenerator>()
.Last(g => g is CSharpModelGenerator);
}
}