-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathScriptGeneratorTests.cs
More file actions
585 lines (502 loc) · 21.7 KB
/
ScriptGeneratorTests.cs
File metadata and controls
585 lines (502 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//------------------------------------------------------------------------------
// <copyright file="ScriptGeneratorTests.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.IO;
using Microsoft.SqlServer.TransactSql.ScriptDom;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SqlStudio.Tests.AssemblyTools.TestCategory;
namespace SqlStudio.Tests.UTSqlScriptDom
{
// These tests ensure that we get the correct SqlVersion for each type of SqlScriptGenerator's Options
[TestClass]
public class SqlScriptGeneratorTests
{
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql100ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql100ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql100, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql110ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql110ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql110, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql120ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql120ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql120, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql130ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql130ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql130, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql140ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql140ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql140, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql150ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql150ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql150, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql160ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql160ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql160, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql80ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql80ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql80, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSql90ScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new Sql90ScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql90, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSqlServerlessScriptGenerator()
{
var options = new SqlScriptGeneratorOptions();
var scriptGenerator = new SqlServerlessScriptGenerator(options);
Assert.AreEqual(SqlVersion.Sql160, scriptGenerator.Options.SqlVersion);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewlinesBetweenStatementsGeneratorOption() {
var tableName = new SchemaObjectName();
tableName.Identifiers.Add(new Identifier { Value = "TableName" });
var tableStatement = new CreateTableStatement
{
SchemaObjectName = tableName
};
var tableStatementString = "CREATE TABLE TableName;";
var statements = new StatementList();
statements.Statements.Add(tableStatement);
statements.Statements.Add(tableStatement);
var generatorOptions = new SqlScriptGeneratorOptions {
KeywordCasing = KeywordCasing.Uppercase,
IncludeSemicolons = true,
NumNewlinesAfterStatement = 0
};
var generator = new Sql80ScriptGenerator(generatorOptions);
generator.GenerateScript(statements, out var sql);
Assert.AreEqual(tableStatementString + tableStatementString, sql);
generatorOptions.NumNewlinesAfterStatement = 1;
generator = new Sql80ScriptGenerator(generatorOptions);
generator.GenerateScript(statements, out sql);
Assert.AreEqual(tableStatementString + Environment.NewLine + tableStatementString, sql);
generatorOptions.NumNewlinesAfterStatement = 2;
generator = new Sql80ScriptGenerator(generatorOptions);
generator.GenerateScript(statements, out sql);
Assert.AreEqual(tableStatementString + Environment.NewLine + Environment.NewLine + tableStatementString, sql);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewLineFormattedIndexDefinitionDefault() {
Assert.AreEqual(false, new SqlScriptGeneratorOptions().NewLineFormattedIndexDefinition);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewlineFormattedCheckConstraintDefault() {
Assert.AreEqual(false, new SqlScriptGeneratorOptions().NewlineFormattedCheckConstraint);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenDataTypeAndParametersDefault() {
Assert.AreEqual(true, new SqlScriptGeneratorOptions().SpaceBetweenDataTypeAndParameters);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenParametersInDataTypeDefault() {
Assert.AreEqual(true, new SqlScriptGeneratorOptions().SpaceBetweenParametersInDataType);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenDataTypeAndParametersWhenFalse() {
var expectedSqlText = @"CREATE TABLE DummyTable (
ColumnName VARCHAR(50)
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
SpaceBetweenDataTypeAndParameters = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenDataTypeAndParametersWhenTrue() {
var expectedSqlText = @"CREATE TABLE DummyTable (
ColumnName VARCHAR (50)
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
SpaceBetweenDataTypeAndParameters = true
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenParametersInDataTypeWhenFalse() {
var expectedSqlText = @"CREATE TABLE DummyTable (
ColumnName DECIMAL (5,2)
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
SpaceBetweenParametersInDataType = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestSpaceBetweenParametersInDataTypeWhenTrue() {
var expectedSqlText = @"CREATE TABLE DummyTable (
ColumnName DECIMAL (5, 2)
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
SpaceBetweenParametersInDataType = true
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewlineFormattedCheckConstraintWhenFalse() {
var expectedSqlText = @"CREATE TABLE DummyTable (
CONSTRAINT ComplicatedConstraint CHECK ((Col1 IS NULL
AND (Col2 <> ''
OR Col3 = 0))
OR (Col1 IS NOT NULL
AND ((Col2 = ''
AND Col3 <> 0)
OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ')
AND Col3 < 0
AND (Col5 <> ''
OR Col6 <> '')))))
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
NewlineFormattedCheckConstraint = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewlineFormattedCheckConstraintWhenTrue() {
var expectedSqlText = @"CREATE TABLE DummyTable (
CONSTRAINT ComplicatedConstraint
CHECK ((Col1 IS NULL
AND (Col2 <> ''
OR Col3 = 0))
OR (Col1 IS NOT NULL
AND ((Col2 = ''
AND Col3 <> 0)
OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ')
AND Col3 < 0
AND (Col5 <> ''
OR Col6 <> '')))))
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
NewlineFormattedCheckConstraint = true
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewLineFormattedIndexDefinitionWhenFalse() {
var expectedSqlText = @"CREATE TABLE DummyTable (
INDEX ComplicatedIndex UNIQUE (Col1, Col2, Col3) INCLUDE (Col4, Col5, Col6, Col7, Col8) WHERE Col4 = 'AR'
AND Col3 IN ('ABC', 'XYZ')
AND Col5 = 0
AND Col6 = 1
AND Col7 = 0
AND Col8 IS NOT NULL
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
NewLineFormattedIndexDefinition = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestNewLineFormattedIndexDefinitionWhenTrue() {
var expectedSqlText = @"CREATE TABLE DummyTable (
INDEX ComplicatedIndex
UNIQUE (Col1, Col2, Col3)
INCLUDE (Col4, Col5, Col6, Col7, Col8)
WHERE Col4 = 'AR'
AND Col3 IN ('ABC', 'XYZ')
AND Col5 = 0
AND Col6 = 1
AND Col7 = 0
AND Col8 IS NOT NULL
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
NewLineFormattedIndexDefinition = true
});
}
void ParseAndAssertEquality(string sqlText, SqlScriptGeneratorOptions generatorOptions) {
var parser = new TSql160Parser(true);
var fragment = parser.ParseStatementList(new StringReader(sqlText), out var errors);
Assert.AreEqual(0, errors.Count);
var generator = new Sql160ScriptGenerator(generatorOptions);
generator.GenerateScript(fragment, out var generatedSqlText);
Assert.AreEqual(sqlText, generatedSqlText);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingDefault()
{
Assert.AreEqual(IdentifierCasing.PreserveOriginal, new SqlScriptGeneratorOptions().IdentifierCasing);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingPreserveOriginal()
{
var expectedSqlText = @"CREATE TABLE MyTableName (
MyColumnName VARCHAR (50),
anotherColumn INT,
MixedCaseColumn DECIMAL (10, 2)
);";
ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.PreserveOriginal,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingLowercase()
{
var inputSqlText = @"CREATE TABLE MyTableName (
MyColumnName VARCHAR (50),
AnotherColumn INT,
MixedCaseColumn DECIMAL (10, 2)
);";
var expectedSqlText = @"CREATE TABLE mytablename (
mycolumnname VARCHAR (50),
anothercolumn INT,
mixedcasecolumn DECIMAL (10, 2)
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.Lowercase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingUppercase()
{
var inputSqlText = @"CREATE TABLE MyTableName (
MyColumnName VARCHAR (50),
anotherColumn INT,
MixedCaseColumn DECIMAL (10, 2)
);";
var expectedSqlText = @"CREATE TABLE MYTABLENAME (
MYCOLUMNNAME VARCHAR (50),
ANOTHERCOLUMN INT,
MIXEDCASECOLUMN DECIMAL (10, 2)
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.Uppercase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingPascalCase()
{
var inputSqlText = @"CREATE TABLE MyTableName (
MyColumnName VARCHAR (50),
anotherColumn INT,
MIXEDCASECOLUMN DECIMAL (10, 2)
);";
var expectedSqlText = @"CREATE TABLE Mytablename (
Mycolumnname VARCHAR (50),
Anothercolumn INT,
Mixedcasecolumn DECIMAL (10, 2)
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.PascalCase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingWithAccentedCharactersLowercase()
{
var inputSqlText = @"CREATE TABLE [Příliš Žluťoučký Kůň] (
[Úpěl Ďábelské] VARCHAR (50),
[Ódy] INT
);";
var expectedSqlText = @"CREATE TABLE [příliš žluťoučký kůň] (
[úpěl ďábelské] VARCHAR (50),
[ódy] INT
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.Lowercase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingWithAccentedCharactersUppercase()
{
var inputSqlText = @"CREATE TABLE [Příliš Žluťoučký Kůň] (
[Úpěl Ďábelské] VARCHAR (50),
[Ódy] INT
);";
var expectedSqlText = @"CREATE TABLE [PŘÍLIŠ ŽLUŤOUČKÝ KŮŇ] (
[ÚPĚL ĎÁBELSKÉ] VARCHAR (50),
[ÓDY] INT
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.Uppercase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingWithAccentedCharactersPascalCase()
{
var inputSqlText = @"CREATE TABLE [PŘÍLIŠ ŽLUŤOUČKÝ KŮŇ] (
[ÚPĚL ĎÁBELSKÉ ÓDY] VARCHAR (50),
[ódy] INT
);";
// Note: PascalCase with quoted identifiers containing spaces produces lowercase after first character
// because the GetPascalCase method only capitalizes the first character of the entire string
var expectedSqlText = @"CREATE TABLE [příliš žluťoučký kůň] (
[úpěl ďábelské ódy] VARCHAR (50),
[ódy] INT
);";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.PascalCase,
AlignColumnDefinitionFields = false
});
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingDoesNotAffectKeywords()
{
var inputSqlText = @"Create Table MyTable (
MyColumn VarChar (50) not null,
MyId Int Primary Key
);";
var expectedSqlTextLowercaseKeywords = @"create table MYTABLE (
MYCOLUMN varchar (50) not null,
MYID int primary key
);";
var expectedSqlTextUppercaseKeywords = @"CREATE TABLE mytable (
mycolumn VARCHAR (50) NOT NULL,
myid INT PRIMARY KEY
);";
var parser = new TSql160Parser(true);
var fragment = parser.ParseStatementList(new StringReader(inputSqlText), out var errors);
Assert.AreEqual(0, errors.Count);
// The setting trigger both keyword and identifier casing, set opposite to each other,
// and hence it's clear which part is affected by which setting.
// Test with lowercase keywords and uppercase identifiers
var generatorLowerKeywords = new Sql160ScriptGenerator(new SqlScriptGeneratorOptions {
KeywordCasing = KeywordCasing.Lowercase,
IdentifierCasing = IdentifierCasing.Uppercase,
AlignColumnDefinitionFields = false
});
generatorLowerKeywords.GenerateScript(fragment, out var generatedSqlTextLower);
Assert.AreEqual(expectedSqlTextLowercaseKeywords, generatedSqlTextLower);
// Test with uppercase keywords and lowercase identifiers
var generatorUpperKeywords = new Sql160ScriptGenerator(new SqlScriptGeneratorOptions {
KeywordCasing = KeywordCasing.Uppercase,
IdentifierCasing = IdentifierCasing.Lowercase,
AlignColumnDefinitionFields = false
});
generatorUpperKeywords.GenerateScript(fragment, out var generatedSqlTextUpper);
Assert.AreEqual(expectedSqlTextUppercaseKeywords, generatedSqlTextUpper);
}
[TestMethod]
[Priority(0)]
[SqlStudioTestCategory(Category.UnitTest)]
public void TestIdentifierCasingWithComplexStatement()
{
var inputSqlText = @"SELECT T1.MyColumn,
T2.AnotherColumn
FROM MySchema.MyTable AS T1
INNER JOIN AnotherSchema.AnotherTable AS T2 ON T1.Id = T2.ForeignId
WHERE T1.StatusCode = 'ACTIVE';";
var expectedSqlText = @"SELECT t1.mycolumn,
t2.anothercolumn
FROM myschema.mytable AS t1
INNER JOIN
anotherschema.anothertable AS t2
ON t1.id = t2.foreignid
WHERE t1.statuscode = 'ACTIVE';";
ParseTransformAndAssertEquality(inputSqlText, expectedSqlText, new SqlScriptGeneratorOptions {
IdentifierCasing = IdentifierCasing.Lowercase,
AlignClauseBodies = true,
NewLineBeforeFromClause = true,
NewLineBeforeWhereClause = true,
NewLineBeforeJoinClause = true
});
}
void ParseTransformAndAssertEquality(string inputSqlText, string expectedSqlText, SqlScriptGeneratorOptions generatorOptions)
{
var parser = new TSql160Parser(true);
var fragment = parser.ParseStatementList(new StringReader(inputSqlText), out var errors);
Assert.AreEqual(0, errors.Count);
var generator = new Sql160ScriptGenerator(generatorOptions);
generator.GenerateScript(fragment, out var generatedSqlText);
Assert.AreEqual(expectedSqlText, generatedSqlText);
}
}
}