1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using Microsoft . EntityFrameworkCore . Internal ;
5
+
4
6
namespace Microsoft . EntityFrameworkCore . Design . Internal ;
5
7
6
8
public class DatabaseOperationsTest
@@ -10,16 +12,80 @@ public void Can_pass_null_args()
10
12
{
11
13
// Even though newer versions of the tools will pass an empty array
12
14
// older versions of the tools can pass null args.
15
+ CreateOperations ( null ) ;
16
+ }
17
+
18
+ [ ConditionalFact ]
19
+ [ SqlServerConfiguredCondition ]
20
+ public void ScaffoldContext_throws_exceptions_for_invalid_context_name ( )
21
+ {
22
+ ValidateContextNameInReverseEngineerGenerator ( "Invalid!CSharp*Class&Name" ) ;
23
+ ValidateContextNameInReverseEngineerGenerator ( "1CSharpClassNameCannotStartWithNumber" ) ;
24
+ ValidateContextNameInReverseEngineerGenerator ( "volatile" ) ;
25
+ }
26
+
27
+ private void ValidateContextNameInReverseEngineerGenerator ( string contextName )
28
+ {
29
+ var operations = CreateOperations ( [ ] ) ;
30
+
31
+ Assert . Equal (
32
+ DesignStrings . ContextClassNotValidCSharpIdentifier ( contextName ) ,
33
+ Assert . Throws < ArgumentException > (
34
+ ( ) => operations . ScaffoldContext (
35
+ "Microsoft.EntityFrameworkCore.SqlServer" ,
36
+ "connectionstring" ,
37
+ "" ,
38
+ "" ,
39
+ dbContextClassName : contextName ,
40
+ null ,
41
+ null ,
42
+ "FakeNamespace" ,
43
+ contextNamespace : null ,
44
+ useDataAnnotations : false ,
45
+ overwriteFiles : true ,
46
+ useDatabaseNames : false ,
47
+ suppressOnConfiguring : true ,
48
+ noPluralize : false ) )
49
+ . Message ) ;
50
+ }
51
+
52
+ [ ConditionalFact ]
53
+ public void ScaffoldContext_sets_environment ( )
54
+ {
55
+ var operations = CreateOperations ( [ ] ) ;
56
+ operations . ScaffoldContext (
57
+ "Microsoft.EntityFrameworkCore.SqlServer" ,
58
+ TestEnvironment . DefaultConnection ,
59
+ "" ,
60
+ "" ,
61
+ dbContextClassName : nameof ( TestContext ) ,
62
+ schemas : [ "Empty" ] ,
63
+ null ,
64
+ null ,
65
+ contextNamespace : null ,
66
+ useDataAnnotations : false ,
67
+ overwriteFiles : true ,
68
+ useDatabaseNames : false ,
69
+ suppressOnConfiguring : true ,
70
+ noPluralize : false ) ;
71
+
72
+ Assert . Equal ( "Development" , Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ) ;
73
+ Assert . Equal ( "Development" , Environment . GetEnvironmentVariable ( "DOTNET_ENVIRONMENT" ) ) ;
74
+ }
75
+
76
+ private static DatabaseOperations CreateOperations ( string [ ] args )
77
+ {
13
78
var assembly = MockAssembly . Create ( typeof ( TestContext ) ) ;
14
- _ = new TestDatabaseOperations (
79
+ var operations = new DatabaseOperations (
15
80
new TestOperationReporter ( ) ,
16
81
assembly ,
17
82
assembly ,
18
83
"projectDir" ,
19
84
"RootNamespace" ,
20
85
"C#" ,
21
86
nullable : false ,
22
- args : null ) ;
87
+ args : args ) ;
88
+ return operations ;
23
89
}
24
90
25
91
public class TestContext : DbContext ;
0 commit comments