@@ -79,57 +79,51 @@ public CustomSqlTests(DialectContext context) : base(context) {}
79
79
public void Can_create_field_with_custom_sql ( )
80
80
{
81
81
OrmLiteConfig . BeforeExecFilter = cmd => cmd . GetDebugString ( ) . Print ( ) ;
82
-
83
- using ( var db = OpenDbConnection ( ) )
84
- {
85
- db . DropAndCreateTable < PocoTable > ( ) ;
86
82
87
- var createTableSql = db . GetLastSql ( ) . NormalizeSql ( ) ;
83
+ using var db = OpenDbConnection ( ) ;
84
+ db . DropAndCreateTable < PocoTable > ( ) ;
88
85
89
- createTableSql . Print ( ) ;
86
+ var createTableSql = db . GetLastSql ( ) . NormalizeSql ( ) ;
90
87
91
- if ( Dialect != Dialect . Firebird )
92
- {
93
- Assert . That ( createTableSql , Does . Contain ( "charcolumn char(20) null" ) ) ;
94
- Assert . That ( createTableSql , Does . Contain ( "decimalcolumn decimal(18,4) null" ) ) ;
95
- }
96
- else
97
- {
98
- Assert . That ( createTableSql , Does . Contain ( "charcolumn char(20)" ) ) ;
99
- Assert . That ( createTableSql , Does . Contain ( "decimalcolumn decimal(18,4)" ) ) ;
100
- }
88
+ createTableSql . Print ( ) ;
89
+
90
+ if ( Dialect != Dialect . Firebird )
91
+ {
92
+ Assert . That ( createTableSql , Does . Contain ( "charcolumn char(20) null" ) ) ;
93
+ Assert . That ( createTableSql , Does . Contain ( "decimalcolumn decimal(18,4) null" ) ) ;
94
+ }
95
+ else
96
+ {
97
+ Assert . That ( createTableSql , Does . Contain ( "charcolumn char(20)" ) ) ;
98
+ Assert . That ( createTableSql , Does . Contain ( "decimalcolumn decimal(18,4)" ) ) ;
101
99
}
102
100
}
103
101
104
102
[ Test ]
105
103
public void Does_execute_CustomSql_before_table_created ( )
106
104
{
107
- using ( var db = OpenDbConnection ( ) )
105
+ using var db = OpenDbConnection ( ) ;
106
+ try
108
107
{
109
- try
110
- {
111
- db . CreateTable < ModelWithPreCreateSql > ( ) ;
112
- Assert . Fail ( "Should throw" ) ;
113
- }
114
- catch ( Exception )
115
- {
116
- Assert . That ( ! db . TableExists ( "ModelWithPreCreateSql" . SqlColumn ( DialectProvider ) ) ) ;
117
- }
108
+ db . CreateTable < ModelWithPreCreateSql > ( ) ;
109
+ Assert . Fail ( "Should throw" ) ;
110
+ }
111
+ catch ( Exception )
112
+ {
113
+ Assert . That ( ! db . TableExists ( "ModelWithPreCreateSql" . SqlColumn ( DialectProvider ) ) ) ;
118
114
}
119
115
}
120
116
121
117
[ Test ]
122
118
[ IgnoreDialect ( Dialect . AnyOracle | Dialect . AnyPostgreSql , "multiple SQL statements need to be wrapped in an anonymous block" ) ]
123
119
public void Does_execute_CustomSql_after_table_created ( )
124
120
{
125
- using ( var db = OpenDbConnection ( ) )
126
- {
127
- db . DropAndCreateTable < ModelWithSeedDataSql > ( ) ;
121
+ using var db = OpenDbConnection ( ) ;
122
+ db . DropAndCreateTable < ModelWithSeedDataSql > ( ) ;
128
123
129
- var seedDataNames = db . Select < ModelWithSeedDataSql > ( ) . ConvertAll ( x => x . Name ) ;
124
+ var seedDataNames = db . Select < ModelWithSeedDataSql > ( ) . ConvertAll ( x => x . Name ) ;
130
125
131
- Assert . That ( seedDataNames , Is . EquivalentTo ( new [ ] { "Foo" , "Bar" } ) ) ;
132
- }
126
+ Assert . That ( seedDataNames , Is . EquivalentTo ( new [ ] { "Foo" , "Bar" } ) ) ;
133
127
}
134
128
135
129
[ Test ]
@@ -141,49 +135,43 @@ public void Does_execute_CustomSql_after_table_created_using_dynamic_attribute()
141
135
"INSERT INTO {0} (Name) VALUES ('Foo');" . Fmt ( "DynamicAttributeSeedData" . SqlTable ( DialectProvider ) ) +
142
136
"INSERT INTO {0} (Name) VALUES ('Bar');" . Fmt ( "DynamicAttributeSeedData" . SqlTable ( DialectProvider ) ) ) ) ;
143
137
144
- using ( var db = OpenDbConnection ( ) )
145
- {
146
- db . DropAndCreateTable < DynamicAttributeSeedData > ( ) ;
138
+ using var db = OpenDbConnection ( ) ;
139
+ db . DropAndCreateTable < DynamicAttributeSeedData > ( ) ;
147
140
148
- var seedDataNames = db . Select < DynamicAttributeSeedData > ( ) . ConvertAll ( x => x . Name ) ;
141
+ var seedDataNames = db . Select < DynamicAttributeSeedData > ( ) . ConvertAll ( x => x . Name ) ;
149
142
150
- Assert . That ( seedDataNames , Is . EquivalentTo ( new [ ] { "Foo" , "Bar" } ) ) ;
151
- }
143
+ Assert . That ( seedDataNames , Is . EquivalentTo ( new [ ] { "Foo" , "Bar" } ) ) ;
152
144
}
153
145
154
146
[ Test ]
155
147
public void Does_execute_CustomSql_before_table_dropped ( )
156
148
{
157
- using ( var db = OpenDbConnection ( ) )
149
+ using var db = OpenDbConnection ( ) ;
150
+ db . CreateTable < ModelWithPreDropSql > ( ) ;
151
+ try
158
152
{
159
- db . CreateTable < ModelWithPreDropSql > ( ) ;
160
- try
161
- {
162
- db . DropTable < ModelWithPreDropSql > ( ) ;
163
- Assert . Fail ( "Should throw" ) ;
164
- }
165
- catch ( Exception )
166
- {
167
- Assert . That ( db . TableExists ( "ModelWithPreDropSql" . SqlTableRaw ( DialectProvider ) ) ) ;
168
- }
153
+ db . DropTable < ModelWithPreDropSql > ( ) ;
154
+ Assert . Fail ( "Should throw" ) ;
155
+ }
156
+ catch ( Exception )
157
+ {
158
+ Assert . That ( db . TableExists ( "ModelWithPreDropSql" . SqlTableRaw ( DialectProvider ) ) ) ;
169
159
}
170
160
}
171
161
172
162
[ Test ]
173
163
public void Does_execute_CustomSql_after_table_dropped ( )
174
164
{
175
- using ( var db = OpenDbConnection ( ) )
165
+ using var db = OpenDbConnection ( ) ;
166
+ db . CreateTable < ModelWithPostDropSql > ( ) ;
167
+ try
176
168
{
177
- db . CreateTable < ModelWithPostDropSql > ( ) ;
178
- try
179
- {
180
- db . DropTable < ModelWithPostDropSql > ( ) ;
181
- Assert . Fail ( "Should throw" ) ;
182
- }
183
- catch ( Exception )
184
- {
185
- Assert . That ( ! db . TableExists ( "ModelWithPostDropSql" ) ) ;
186
- }
169
+ db . DropTable < ModelWithPostDropSql > ( ) ;
170
+ Assert . Fail ( "Should throw" ) ;
171
+ }
172
+ catch ( Exception )
173
+ {
174
+ Assert . That ( ! db . TableExists ( "ModelWithPostDropSql" ) ) ;
187
175
}
188
176
}
189
177
@@ -200,69 +188,62 @@ public class CustomSelectTest
200
188
[ Test ]
201
189
public void Can_select_custom_field_expressions ( )
202
190
{
203
- using ( var db = OpenDbConnection ( ) )
204
- {
205
- db . DropAndCreateTable < CustomSelectTest > ( ) ;
191
+ using var db = OpenDbConnection ( ) ;
192
+ db . DropAndCreateTable < CustomSelectTest > ( ) ;
206
193
207
- db . Insert ( new CustomSelectTest { Id = 1 , Width = 10 , Height = 5 } ) ;
194
+ db . Insert ( new CustomSelectTest { Id = 1 , Width = 10 , Height = 5 } ) ;
208
195
209
- var row = db . SingleById < CustomSelectTest > ( 1 ) ;
196
+ var row = db . SingleById < CustomSelectTest > ( 1 ) ;
210
197
211
- Assert . That ( row . Area , Is . EqualTo ( 10 * 5 ) ) ;
212
- }
198
+ Assert . That ( row . Area , Is . EqualTo ( 10 * 5 ) ) ;
213
199
}
214
200
215
201
[ Test ]
216
202
public void Can_Count_Distinct ( )
217
203
{
218
- using ( var db = OpenDbConnection ( ) )
219
- {
220
- db . DropAndCreateTable < LetterFrequency > ( ) ;
204
+ using var db = OpenDbConnection ( ) ;
205
+ db . DropAndCreateTable < LetterFrequency > ( ) ;
221
206
222
- var rows = "A,B,B,C,C,C,D,D,E" . Split ( ',' ) . Map ( x => new LetterFrequency { Letter = x } ) ;
207
+ var rows = "A,B,B,C,C,C,D,D,E" . Split ( ',' ) . Map ( x => new LetterFrequency { Letter = x } ) ;
223
208
224
- db . InsertAll ( rows ) ;
209
+ db . InsertAll ( rows ) ;
225
210
226
- var count = db . Count ( db . From < LetterFrequency > ( ) . Select ( x => x . Letter ) ) ;
227
- Assert . That ( count , Is . EqualTo ( rows . Count ) ) ;
211
+ var count = db . Count ( db . From < LetterFrequency > ( ) . Select ( x => x . Letter ) ) ;
212
+ Assert . That ( count , Is . EqualTo ( rows . Count ) ) ;
228
213
229
- count = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( x => Sql . Count ( x . Letter ) ) ) ;
230
- Assert . That ( count , Is . EqualTo ( rows . Count ) ) ;
214
+ count = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( x => Sql . Count ( x . Letter ) ) ) ;
215
+ Assert . That ( count , Is . EqualTo ( rows . Count ) ) ;
231
216
232
- var distinctCount = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( x => Sql . CountDistinct ( x . Letter ) ) ) ;
233
- Assert . That ( distinctCount , Is . EqualTo ( rows . Map ( x => x . Letter ) . Distinct ( ) . Count ( ) ) ) ;
217
+ var distinctCount = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( x => Sql . CountDistinct ( x . Letter ) ) ) ;
218
+ Assert . That ( distinctCount , Is . EqualTo ( rows . Map ( x => x . Letter ) . Distinct ( ) . Count ( ) ) ) ;
234
219
235
- distinctCount = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( "COUNT(DISTINCT Letter)" ) ) ;
236
- Assert . That ( distinctCount , Is . EqualTo ( rows . Map ( x => x . Letter ) . Distinct ( ) . Count ( ) ) ) ;
237
- }
220
+ distinctCount = db . Scalar < long > ( db . From < LetterFrequency > ( ) . Select ( "COUNT(DISTINCT Letter)" ) ) ;
221
+ Assert . That ( distinctCount , Is . EqualTo ( rows . Map ( x => x . Letter ) . Distinct ( ) . Count ( ) ) ) ;
238
222
}
239
223
240
224
[ Test ]
241
225
public void Does_select_aliases_on_constant_expressions ( )
242
226
{
243
- using ( var db = OpenDbConnection ( ) )
244
- {
245
- db . DropAndCreateTable < LetterFrequency > ( ) ;
246
- db . Insert ( new LetterFrequency { Letter = "A" } ) ;
247
-
248
- var q = db . From < LetterFrequency > ( )
249
- . Select ( x => new
250
- {
251
- param = 1 ,
252
- descr = x . Letter ,
253
- str = "hi" ,
254
- date = DateTime . UtcNow
255
- } ) ;
256
-
257
- var results = db . Select < Dictionary < string , object > > ( q ) [ 0 ] ;
227
+ using var db = OpenDbConnection ( ) ;
228
+ db . DropAndCreateTable < LetterFrequency > ( ) ;
229
+ db . Insert ( new LetterFrequency { Letter = "A" } ) ;
230
+
231
+ var q = db . From < LetterFrequency > ( )
232
+ . Select ( x => new
233
+ {
234
+ param = 1 ,
235
+ descr = x . Letter ,
236
+ str = "hi" ,
237
+ date = DateTime . UtcNow
238
+ } ) ;
239
+
240
+ var results = db . Select < Dictionary < string , object > > ( q ) [ 0 ] ;
258
241
259
- Assert . That ( results [ "param" ] , Is . EqualTo ( 1 ) ) ;
260
- Assert . That ( results [ "descr" ] , Is . EqualTo ( "A" ) ) ;
261
- Assert . That ( results [ "str" ] , Is . EqualTo ( "hi" ) ) ;
262
- Assert . That ( results [ "date" ] , Is . Not . Empty ) ;
263
- }
242
+ Assert . That ( results [ "param" ] , Is . EqualTo ( 1 ) ) ;
243
+ Assert . That ( results [ "descr" ] , Is . EqualTo ( "A" ) ) ;
244
+ Assert . That ( results [ "str" ] , Is . EqualTo ( "hi" ) ) ;
245
+ Assert . That ( results [ "date" ] , Is . Not . Empty ) ;
264
246
}
265
247
266
-
267
248
}
268
249
}
0 commit comments