1+ using JDEV . EFMigrationRuntimeSchema ;
2+
3+ namespace ef_migration_runtimeschema . Tests
4+ {
5+ public class RewriteTest
6+ {
7+
8+ const string completeClassString = @"
9+ public partial class AddUserName : Migration
10+
11+
12+ /// <inheritdoc />
13+ protected override void Up(MigrationBuilder migrationBuilder)
14+ {
15+ migrationBuilder.AddColumn<string>(
16+ name: ""FullName"",
17+ schema:""schema"",
18+ table: ""Customers"",
19+ nullable: true);
20+ }
21+ }
22+ " ;
23+ const string interfaceName = "InterfaceName" ;
24+
25+ [ Fact ]
26+ public void ShouldAddConstructor ( )
27+ {
28+ var constructorClassString = @"
29+ public partial class AddUserName : Migration
30+ {
31+ }
32+ " ;
33+
34+ var final = @"
35+ public partial class AddUserName : Migration
36+ {
37+ private readonly InterfaceName _schema;
38+
39+ /// <inheritdoc />
40+ public AddUserName(InterfaceName schema)
41+ {
42+ _schema = schema;
43+ }
44+ }
45+ " ;
46+
47+ var result = MigrationCommand . RewriteSyntaxtNode ( interfaceName , "testPath" , constructorClassString ) ;
48+ var str = result . ToFullString ( ) ;
49+
50+ Assert . Equal ( final , str , ignoreLineEndingDifferences : true ) ;
51+ }
52+
53+ [ Fact ]
54+ public void ShouldNotAddTwoConstructors ( )
55+ {
56+ var constructorClassString = @"
57+ public partial class AddUserName : Migration
58+ {
59+ private readonly InterfaceName _schema;
60+
61+ /// <inheritdoc />
62+ public AddUserName(InterfaceName schema)
63+ {
64+ _schema = schema;
65+ }
66+ }
67+ " ;
68+
69+ var final = @"
70+ public partial class AddUserName : Migration
71+ {
72+ private readonly InterfaceName _schema;
73+
74+ /// <inheritdoc />
75+ public AddUserName(InterfaceName schema)
76+ {
77+ _schema = schema;
78+ }
79+ }
80+ " ;
81+
82+ var result = MigrationCommand . RewriteSyntaxtNode ( interfaceName , "testPath" , constructorClassString ) ;
83+ var str = result . ToFullString ( ) ;
84+
85+ Assert . Equal ( final , str , ignoreLineEndingDifferences : true , ignoreWhiteSpaceDifferences : true ) ;
86+ }
87+
88+
89+ [ Fact ]
90+ public void ShouldReplaceSchemaNameInAddColumn ( )
91+ {
92+ var classString = @"
93+ public partial class AddUserName : Migration
94+ {
95+
96+ /// <inheritdoc />
97+ protected override void Up(MigrationBuilder migrationBuilder)
98+ {
99+ migrationBuilder.AddColumn<string>(
100+ name: ""FullName"",
101+ schema:""schema"",
102+ table: ""Customers"",
103+ nullable: true);
104+ }
105+ }
106+ " ;
107+
108+ var final = @"
109+ public partial class AddUserName : Migration
110+ {
111+ private readonly InterfaceName _schema;
112+
113+ /// <inheritdoc />
114+ public AddUserName(InterfaceName schema)
115+ {
116+ _schema = schema;
117+ }
118+
119+ /// <inheritdoc />
120+ protected override void Up(MigrationBuilder migrationBuilder)
121+ {
122+ migrationBuilder.AddColumn<string>(
123+ name: ""FullName"",
124+ schema:_schema.Schema,
125+ table: ""Customers"",
126+ nullable: true);
127+ }
128+ }
129+ " ;
130+
131+ var result = MigrationCommand . RewriteSyntaxtNode ( interfaceName , "testPath" , classString ) ;
132+ var str = result . ToFullString ( ) ;
133+
134+ Assert . Equal ( final , str , ignoreLineEndingDifferences : true ) ;
135+ }
136+
137+
138+
139+ [ Fact ]
140+ public void ShouldReplaceSchemaInMethodEnsureSchema ( )
141+ {
142+ var classString = @"
143+ public partial class AddUserName : Migration
144+ {
145+
146+ /// <inheritdoc />
147+ protected override void Up(MigrationBuilder migrationBuilder)
148+ {
149+ migrationBuilder.EnsureSchema(
150+ name: ""schema"");
151+ }
152+ }
153+ " ;
154+
155+ var final = @"
156+ public partial class AddUserName : Migration
157+ {
158+ private readonly InterfaceName _schema;
159+
160+ /// <inheritdoc />
161+ public AddUserName(InterfaceName schema)
162+ {
163+ _schema = schema;
164+ }
165+
166+ /// <inheritdoc />
167+ protected override void Up(MigrationBuilder migrationBuilder)
168+ {
169+ migrationBuilder.EnsureSchema(
170+ name: _schema.Schema);
171+ }
172+ }
173+ " ;
174+
175+ var result = MigrationCommand . RewriteSyntaxtNode ( interfaceName , "testPath" , classString ) ;
176+ var str = result . ToFullString ( ) ;
177+
178+ Assert . Equal ( final , str , ignoreLineEndingDifferences : true ) ;
179+ }
180+
181+ }
182+ }
0 commit comments