1- using System . Text . RegularExpressions ;
2-
3- namespace AutoMapper . IntegrationTests . ExplicitExpansion ;
4-
5- public class DisableExplicitExpansion : IntegrationTest < DisableExplicitExpansion . DatabaseInitializer >
6- {
7- public class Source
8- { [ Key , DatabaseGenerated ( DatabaseGeneratedOption . None ) ]
9- public Int32 Desc { get ; set ; }
10- public String Name { get ; set ; }
11- public String Code { get ; set ; }
12- }
13- public class Dto
14- {
15- public String Name { get ; set ; }
16- public String Code { get ; set ; }
17- public Nullable < int > Desc { get ; set ; }
18- }
19-
20- public class Context : LocalDbContext
21- {
22- public List < string > Log = new List < string > ( ) ;
23-
24- protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
25- {
26- optionsBuilder . LogTo ( s => Log . Add ( s ) ) ;
27- base . OnConfiguring ( optionsBuilder ) ;
28- }
29-
30- public DbSet < Source > Sources { get ; set ; }
31-
32- public string GetLastSelectSqlLogEntry ( ) => Log . LastOrDefault ( _ => _ . Contains ( "SELECT" ) ) ;
33- }
34-
35- private static readonly IQueryable < Source > _iq = new List < Source > {
36- new Source ( ) { Name = "Name1" , Code = "Code1" , Desc = - 12 } ,
37- } . AsQueryable ( ) ;
38-
39- private static readonly Source _iqf = _iq . First ( ) ;
40-
41- public class DatabaseInitializer : DropCreateDatabaseAlways < Context >
42- {
43- protected override void Seed ( Context context )
44- {
45- context . Sources . Add ( _iqf ) ;
46- base . Seed ( context ) ;
47- }
48- }
49-
50- protected override MapperConfiguration CreateConfiguration ( ) => new ( cfg =>
51- {
52- cfg . CreateMap < Source , Dto > ( )
53- . ForMember ( dto => dto . Code , conf => conf . ExplicitExpansion ( false ) )
54- . ForAllMembers ( conf => conf . ExplicitExpansion ( ) )
55- ; } ) ;
56-
57- [ Fact ]
58- public void Should_CodeBeExpanded ( ) {
59- using ( var ctx = new Context ( ) ) {
60- var dto = ProjectTo < Dto > ( ctx . Sources ) . ToList ( ) . First ( ) ;
61- var sqlSelect = ctx . GetLastSelectSqlLogEntry ( ) ;
62- sqlSelect . SqlFromShouldStartWith ( nameof ( ctx . Sources ) ) ;
63- sqlSelect . ShouldNotContain ( "JOIN" ) ;
64-
65- sqlSelect . SqlShouldNotSelectColumn ( nameof ( _iqf . Name ) ) ; dto . Name . ShouldBeNull ( ) ;
66- sqlSelect . SqlShouldSelectColumn ( nameof ( _iqf . Code ) ) ; dto . Code . ShouldBe ( _iqf . Code ) ;
67- sqlSelect . SqlShouldNotSelectColumn ( nameof ( _iqf . Desc ) ) ; dto . Desc . ShouldBeNull ( ) ;
68- }
69- }
70-
71- [ Fact ]
72- public void Should_NameAndCodeBeExpanded ( ) {
73- using ( var ctx = new Context ( ) ) {
74- var dto = ProjectTo < Dto > ( ctx . Sources , null , _ => _ . Name ) . First ( ) ;
75- var sqlSelect = ctx . GetLastSelectSqlLogEntry ( ) ;
76- sqlSelect . SqlFromShouldStartWith ( nameof ( ctx . Sources ) ) ;
77- sqlSelect . ShouldNotContain ( "JOIN" ) ;
78-
79- dto . Name . ShouldBe ( _iqf . Name ) ; sqlSelect . SqlShouldSelectColumn ( nameof ( _iqf . Name ) ) ;
80- dto . Code . ShouldBe ( _iqf . Code ) ; sqlSelect . SqlShouldSelectColumn ( nameof ( _iqf . Code ) ) ;
81- dto . Desc . ShouldBeNull ( ) ; sqlSelect . SqlShouldNotSelectColumn ( nameof ( _iqf . Desc ) ) ;
82- }
83- }
84-
85- }
86-
87- public class ConstructorNoExplicitExpansion : IntegrationTest < ConstructorNoExplicitExpansion . DatabaseInitializer > {
88- public class Entity {
89- public int Id { get ; set ; }
90- public string Name { get ; set ; }
91- }
92- record Dto ( string Name ) { }
93- public class Context : LocalDbContext {
94- public DbSet < Entity > Entities { get ; set ; }
95- }
96- public class DatabaseInitializer : DropCreateDatabaseAlways < Context > {
97- protected override void Seed ( Context context ) {
98- context . Entities . Add ( new ( ) { Name = "Name" } ) ;
99- base . Seed ( context ) ;
100- }
101- }
102- protected override MapperConfiguration CreateConfiguration ( ) => new ( c => c . CreateProjection < Entity , Dto > ( ) . ForCtorParam ( "Name" , o => o . ExplicitExpansion ( false ) ) ) ;
103- [ Fact ]
104- public void Should_work ( ) {
105- using var context = new Context ( ) ;
106- var dto = ProjectTo < Dto > ( context . Entities ) . Single ( ) ;
107- dto . Name . ShouldBe ( "Name" ) ;
108- dto = ProjectTo < Dto > ( context . Entities , null , d => d . Name ) . Single ( ) ;
109- dto . Name . ShouldBe ( "Name" ) ;
110- }
111- }
1+ namespace AutoMapper . IntegrationTests . ExplicitExpansion ;
1122
1133public class ConstructorWithInheritanceNoExplicitExpansion : IntegrationTest < ConstructorWithInheritanceNoExplicitExpansion . DatabaseInitializer > {
1144 public class Entity {
@@ -117,36 +7,32 @@ public class Entity {
1177 }
1188
1199 public class SubEntity : Entity {
120- public string Caption { get ; set ; }
12110 }
12211
123- record Dto ( string Name ) { }
124- record SubDto ( string Name , string Caption ) : Dto ( Name ) { }
12+ record Dto ( string Name ) ;
13+ record SubDto ( string Name ) : Dto ( Name ) { }
12514
12615 public class Context : LocalDbContext {
127- public DbSet < SubEntity > Entities { get ; set ; }
16+ public DbSet < Entity > Entities { get ; set ; }
17+ public DbSet < SubEntity > SubEntities { get ; set ; }
12818 }
12919 public class DatabaseInitializer : DropCreateDatabaseAlways < Context > {
13020 protected override void Seed ( Context context ) {
131- context . Entities . Add ( new ( ) { Name = "Name" , Caption = "Caption" } ) ;
21+ context . Entities . Add ( new ( ) { Name = "base" } ) ;
22+ context . SubEntities . Add ( new ( ) { Name = "derived" } ) ;
13223 base . Seed ( context ) ;
13324 }
13425 }
13526 protected override MapperConfiguration CreateConfiguration ( ) => new ( c => {
13627 c . CreateMap < Entity , Dto > ( ) . ForCtorParam ( "Name" , o => o . ExplicitExpansion ( ) ) ;
137- c . CreateMap < SubEntity , SubDto > ( )
138- . IncludeBase < Entity , Dto > ( )
139- . ForCtorParam ( "Name" , o => o . ExplicitExpansion ( false ) )
140- . ForCtorParam ( "Caption" , o => o . ExplicitExpansion ( false ) ) ;
28+ c . CreateMap < SubEntity , SubDto > ( ) . IncludeBase < Entity , Dto > ( ) . ForCtorParam ( "Name" , o => o . ExplicitExpansion ( false ) ) ;
14129 } ) ;
14230 [ Fact ]
14331 public void Should_work ( ) {
14432 using var context = new Context ( ) ;
145- var dto = ProjectTo < SubDto > ( context . Entities ) . Single ( ) ;
146- dto . Name . ShouldBe ( "Name" ) ;
147- dto . Caption . ShouldBe ( "Caption" ) ;
148- dto = ProjectTo < SubDto > ( context . Entities , null , d => d . Name , d => d . Caption ) . Single ( ) ;
149- dto . Name . ShouldBe ( "Name" ) ;
150- dto . Caption . ShouldBe ( "Caption" ) ;
33+ var dtos = ProjectTo < Dto > ( context . Entities ) . ToList ( ) ;
34+ dtos . Count . ShouldBe ( 2 ) ;
35+ dtos [ 0 ] . ShouldBeOfType < Dto > ( ) . Name . ShouldBeNull ( ) ;
36+ dtos [ 1 ] . ShouldBeOfType < SubDto > ( ) . Name . ShouldBe ( "derived" ) ;
15137 }
15238}
0 commit comments