1
+ using System ;
2
+ using NUnit . Framework ;
3
+ using ServiceStack . OrmLite ;
4
+ using ServiceStack . OrmLite . Firebird ;
5
+ using ServiceStack . OrmLite . MySql ;
6
+ using System . Collections . Generic ;
7
+ using ServiceStack . Logging ;
8
+ using ServiceStack . Logging . Support . Logging ;
9
+ using System . ComponentModel . DataAnnotations ;
10
+
11
+ namespace ServiceStack . OrmLite . DDLTest
12
+ {
13
+
14
+
15
+ [ TestFixture ( ) ]
16
+ public class Test
17
+ {
18
+ List < Dialect > dialects = new List < Dialect > ( ) ;
19
+
20
+ [ TestFixtureSetUp ]
21
+ public void TestFixtureSetup ( )
22
+ {
23
+ dialects . Add ( new Dialect {
24
+ Provider = FirebirdOrmLiteDialectProvider . Instance ,
25
+ AddColumnString = "ALTER TABLE Model ADD Column1 VARCHAR(128) ;" ,
26
+ AlterColumnString = "ALTER TABLE Model ALTER Column2 VARCHAR(50) ;" ,
27
+ ChangeColumnNameString = "ALTER TABLE Model ALTER OldColumn3 TO Column3 ;" ,
28
+ AddFKString = "ALTER TABLE Child ADD CONSTRAINT JustOneFK FOREIGN KEY (IdModel) REFERENCES Model (Id) ON UPDATE NO ACTION ON DELETE CASCADE;" ,
29
+ CreateIndexString = "CREATE UNIQUE INDEX JustIndexOnColumn3 ON Model(Column3);"
30
+
31
+ } ) ;
32
+ dialects . Add ( new Dialect {
33
+ Provider = MySqlDialectProvider . Instance ,
34
+ AddColumnString = "ALTER TABLE `Model` ADD COLUMN `Column1` VARCHAR(255) NULL;" ,
35
+ AlterColumnString = "ALTER TABLE `Model` MODIFY COLUMN `Column2` VARCHAR(50) NULL;" ,
36
+ ChangeColumnNameString = "ALTER TABLE `Model` CHANGE COLUMN `OldColumn3` `Column3` VARCHAR(255) NULL;" ,
37
+ AddFKString = "ALTER TABLE `Child` ADD CONSTRAINT `JustOneFK` FOREIGN KEY (`IdModel`) REFERENCES `Model` (`Id`) ON UPDATE NO ACTION ON DELETE CASCADE;" ,
38
+ CreateIndexString = "CREATE UNIQUE INDEX `JustIndexOnColumn3` ON `Model`(`Column3`);"
39
+ } ) ;
40
+
41
+ LogManager . LogFactory = new ConsoleLogFactory ( ) ;
42
+ }
43
+
44
+ [ Test ( ) ]
45
+ public void CanAddColumn ( )
46
+ {
47
+ var model = typeof ( Model ) ;
48
+
49
+ foreach ( var d in dialects )
50
+ {
51
+ OrmLiteConfig . DialectProvider = d . Provider ;
52
+ var fielDef = ModelDefinition < Model > . Definition . GetFieldDefinition < Model > ( f => f . Column1 ) ;
53
+ Assert . AreEqual ( d . AddColumnString , ( d . Provider . ToAddColumnStatement ( model , fielDef ) ) ) ;
54
+ }
55
+ }
56
+
57
+ [ Test ( ) ]
58
+ public void CanAAlterColumn ( )
59
+ {
60
+ var model = typeof ( Model ) ;
61
+
62
+ foreach ( var d in dialects )
63
+ {
64
+ OrmLiteConfig . DialectProvider = d . Provider ;
65
+ var fielDef = ModelDefinition < Model > . Definition . GetFieldDefinition < Model > ( f => f . Column2 ) ;
66
+ Assert . AreEqual ( d . AlterColumnString , ( d . Provider . ToAlterColumnStatement ( model , fielDef ) ) ) ;
67
+ }
68
+
69
+ }
70
+
71
+
72
+ [ Test ( ) ]
73
+ public void CanChangeColumnName ( )
74
+ {
75
+ var model = typeof ( Model ) ;
76
+
77
+ foreach ( var d in dialects )
78
+ {
79
+ OrmLiteConfig . DialectProvider = d . Provider ;
80
+ var fielDef = ModelDefinition < Model > . Definition . GetFieldDefinition < Model > ( f => f . Column3 ) ;
81
+ Assert . AreEqual ( d . ChangeColumnNameString , ( d . Provider . ToChangeColumnNameStatement ( model , fielDef , "OldColumn3" ) ) ) ;
82
+
83
+ }
84
+
85
+ }
86
+
87
+ [ Test ( ) ]
88
+ public void CanAddForeignKey ( )
89
+ {
90
+
91
+ foreach ( var d in dialects )
92
+ {
93
+ OrmLiteConfig . DialectProvider = d . Provider ;
94
+ Assert . AreEqual ( d . AddFKString , d . Provider . ToAddForeignKeyStatement < Child , Model > ( f=> f . IdModel , fk=> fk . Id , OnFkOption . NoAction , OnFkOption . Cascade , "JustOneFK" ) ) ;
95
+ }
96
+ }
97
+
98
+
99
+ [ Test ( ) ]
100
+ public void CanCreateIndex ( )
101
+ {
102
+
103
+ foreach ( var d in dialects )
104
+ {
105
+ OrmLiteConfig . DialectProvider = d . Provider ;
106
+ Assert . AreEqual ( d . CreateIndexString , d . Provider . ToCreateIndexStatement < Model > ( f=> f . Column3 , "JustIndexOnColumn3" , true ) ) ;
107
+
108
+ }
109
+
110
+ }
111
+
112
+
113
+ }
114
+
115
+ public class Model
116
+ {
117
+ public Model ( )
118
+ {
119
+ }
120
+ public int Id { get ; set ; }
121
+ public string Column1 { get ; set ; }
122
+ [ StringLength ( 50 ) ]
123
+ public string Column2 { get ; set ; }
124
+ public string Column3 { get ; set ; }
125
+
126
+ }
127
+
128
+ public class Child
129
+ {
130
+ public Child ( ) { }
131
+ public int Id { get ; set ; }
132
+ public int IdModel { get ; set ; }
133
+
134
+ }
135
+
136
+ public class Dialect
137
+ {
138
+ public IOrmLiteDialectProvider Provider { get ; set ; }
139
+ public string AddColumnString { get ; set ; }
140
+ public string AlterColumnString { get ; set ; }
141
+ public string ChangeColumnNameString { get ; set ; }
142
+ public string AddFKString { get ; set ; }
143
+ public string CreateIndexString { get ; set ; }
144
+
145
+ }
146
+ }
0 commit comments