-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathMySqlAnnotationCodeGeneratorTest.cs
More file actions
119 lines (97 loc) · 4.6 KB
/
MySqlAnnotationCodeGeneratorTest.cs
File metadata and controls
119 lines (97 loc) · 4.6 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
// Copyright (c) Pomelo Foundation. All rights reserved.
// Licensed under the MIT. See LICENSE in the project root for license information.
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Moq;
using Pomelo.EntityFrameworkCore.MySql.Design.Internal;
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;
using Xunit;
namespace Pomelo.EntityFrameworkCore.MySql.Design
{
/// <summary>
/// Tests for <see cref="MySqlAnnotationCodeGenerator"/> ensuring that generated
/// MethodCallCodeFragment instances produce fluent chaining output instead of static method calls.
/// </summary>
/// <remarks>
/// Fixes: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1990
/// </remarks>
public class MySqlAnnotationCodeGeneratorTest
{
[Fact]
public void GenerateFluentApiCalls_for_property_identity_column_has_null_MethodInfo()
{
// Arrange
var modelBuilder = new ModelBuilder();
modelBuilder.Entity("TestEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation(MySqlAnnotationNames.ValueGenerationStrategy, MySqlValueGenerationStrategy.IdentityColumn);
});
var model = modelBuilder.FinalizeModel();
var property = model.FindEntityType("TestEntity")!.FindProperty("Id")!;
var annotations = property.GetAnnotations()
.ToDictionary(a => a.Name, a => a);
var generator = CreateGenerator();
// Act
var fragments = generator.GenerateFluentApiCalls(property, annotations);
// Assert
var identityFragment = fragments.FirstOrDefault(f => f.Method == "UseMySqlIdentityColumn");
Assert.NotNull(identityFragment);
// Key assertion: MethodInfo must be null for fluent chaining
// When MethodInfo is null, EF Core's CSharpSnapshotGenerator outputs fluent chaining
// instead of static method calls like MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(...)
Assert.Null(identityFragment.MethodInfo);
}
[Fact]
public void GenerateFluentApiCalls_for_property_computed_column_has_null_MethodInfo()
{
// Arrange
var modelBuilder = new ModelBuilder();
modelBuilder.Entity("TestEntity", b =>
{
b.Property<int>("ComputedValue")
.HasAnnotation(MySqlAnnotationNames.ValueGenerationStrategy, MySqlValueGenerationStrategy.ComputedColumn);
});
var model = modelBuilder.FinalizeModel();
var property = model.FindEntityType("TestEntity")!.FindProperty("ComputedValue")!;
var annotations = property.GetAnnotations()
.ToDictionary(a => a.Name, a => a);
var generator = CreateGenerator();
// Act
var fragments = generator.GenerateFluentApiCalls(property, annotations);
// Assert
var computedFragment = fragments.FirstOrDefault(f => f.Method == "UseMySqlComputedColumn");
Assert.NotNull(computedFragment);
Assert.Null(computedFragment.MethodInfo);
}
[Fact]
public void GenerateFluentApiCalls_for_model_auto_increment_has_null_MethodInfo()
{
// Arrange
var modelBuilder = new ModelBuilder();
modelBuilder.HasAnnotation(
MySqlAnnotationNames.ValueGenerationStrategy,
MySqlValueGenerationStrategy.IdentityColumn);
var model = modelBuilder.FinalizeModel();
var annotations = model.GetAnnotations()
.ToDictionary(a => a.Name, a => a);
var generator = CreateGenerator();
// Act
var fragments = generator.GenerateFluentApiCalls(model, annotations);
// Assert
var autoIncrementFragment = fragments.FirstOrDefault(f => f.Method == "AutoIncrementColumns");
Assert.NotNull(autoIncrementFragment);
Assert.Null(autoIncrementFragment.MethodInfo);
}
private static MySqlAnnotationCodeGenerator CreateGenerator()
{
var typeMappingSourceMock = new Mock<IRelationalTypeMappingSource>();
var dependencies = new AnnotationCodeGeneratorDependencies(typeMappingSourceMock.Object);
return new MySqlAnnotationCodeGenerator(dependencies);
}
}
}