|
5 | 5 | //------------------------------------------------------------------------------ |
6 | 6 |
|
7 | 7 | using System; |
| 8 | +using System.IO; |
8 | 9 | using Microsoft.SqlServer.TransactSql.ScriptDom; |
9 | 10 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
10 | 11 | using SqlStudio.Tests.AssemblyTools.TestCategory; |
@@ -157,5 +158,181 @@ public void TestNewlinesBetweenStatementsGeneratorOption() { |
157 | 158 | generator.GenerateScript(statements, out sql); |
158 | 159 | Assert.AreEqual(tableStatementString + Environment.NewLine + Environment.NewLine + tableStatementString, sql); |
159 | 160 | } |
| 161 | + |
| 162 | + [TestMethod] |
| 163 | + [Priority(0)] |
| 164 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 165 | + public void TestNewLineFormattedIndexDefinitionDefault() { |
| 166 | + Assert.AreEqual(false, new SqlScriptGeneratorOptions().NewLineFormattedIndexDefinition); |
| 167 | + } |
| 168 | + |
| 169 | + [TestMethod] |
| 170 | + [Priority(0)] |
| 171 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 172 | + public void TestNewlineFormattedCheckConstraintDefault() { |
| 173 | + Assert.AreEqual(false, new SqlScriptGeneratorOptions().NewlineFormattedCheckConstraint); |
| 174 | + } |
| 175 | + |
| 176 | + [TestMethod] |
| 177 | + [Priority(0)] |
| 178 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 179 | + public void TestSpaceBetweenDataTypeAndParametersDefault() { |
| 180 | + Assert.AreEqual(true, new SqlScriptGeneratorOptions().SpaceBetweenDataTypeAndParameters); |
| 181 | + } |
| 182 | + |
| 183 | + [TestMethod] |
| 184 | + [Priority(0)] |
| 185 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 186 | + public void TestSpaceBetweenParametersInDataTypeDefault() { |
| 187 | + Assert.AreEqual(true, new SqlScriptGeneratorOptions().SpaceBetweenParametersInDataType); |
| 188 | + } |
| 189 | + |
| 190 | + [TestMethod] |
| 191 | + [Priority(0)] |
| 192 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 193 | + public void TestSpaceBetweenDataTypeAndParametersWhenFalse() { |
| 194 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 195 | + ColumnName VARCHAR(50) |
| 196 | +);"; |
| 197 | + |
| 198 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 199 | + SpaceBetweenDataTypeAndParameters = false |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + [TestMethod] |
| 204 | + [Priority(0)] |
| 205 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 206 | + public void TestSpaceBetweenDataTypeAndParametersWhenTrue() { |
| 207 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 208 | + ColumnName VARCHAR (50) |
| 209 | +);"; |
| 210 | + |
| 211 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 212 | + SpaceBetweenDataTypeAndParameters = true |
| 213 | + }); |
| 214 | + } |
| 215 | + |
| 216 | + [TestMethod] |
| 217 | + [Priority(0)] |
| 218 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 219 | + public void TestSpaceBetweenParametersInDataTypeWhenFalse() { |
| 220 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 221 | + ColumnName DECIMAL (5,2) |
| 222 | +);"; |
| 223 | + |
| 224 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 225 | + SpaceBetweenParametersInDataType = false |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + [TestMethod] |
| 230 | + [Priority(0)] |
| 231 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 232 | + public void TestSpaceBetweenParametersInDataTypeWhenTrue() { |
| 233 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 234 | + ColumnName DECIMAL (5, 2) |
| 235 | +);"; |
| 236 | + |
| 237 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 238 | + SpaceBetweenParametersInDataType = true |
| 239 | + }); |
| 240 | + } |
| 241 | + |
| 242 | + [TestMethod] |
| 243 | + [Priority(0)] |
| 244 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 245 | + public void TestNewlineFormattedCheckConstraintWhenFalse() { |
| 246 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 247 | + CONSTRAINT ComplicatedConstraint CHECK ((Col1 IS NULL |
| 248 | + AND (Col2 <> '' |
| 249 | + OR Col3 = 0)) |
| 250 | + OR (Col1 IS NOT NULL |
| 251 | + AND ((Col2 = '' |
| 252 | + AND Col3 <> 0) |
| 253 | + OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ') |
| 254 | + AND Col3 < 0 |
| 255 | + AND (Col5 <> '' |
| 256 | + OR Col6 <> ''))))) |
| 257 | +);"; |
| 258 | + |
| 259 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 260 | + NewlineFormattedCheckConstraint = false |
| 261 | + }); |
| 262 | + } |
| 263 | + |
| 264 | + [TestMethod] |
| 265 | + [Priority(0)] |
| 266 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 267 | + public void TestNewlineFormattedCheckConstraintWhenTrue() { |
| 268 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 269 | + CONSTRAINT ComplicatedConstraint |
| 270 | + CHECK ((Col1 IS NULL |
| 271 | + AND (Col2 <> '' |
| 272 | + OR Col3 = 0)) |
| 273 | + OR (Col1 IS NOT NULL |
| 274 | + AND ((Col2 = '' |
| 275 | + AND Col3 <> 0) |
| 276 | + OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ') |
| 277 | + AND Col3 < 0 |
| 278 | + AND (Col5 <> '' |
| 279 | + OR Col6 <> ''))))) |
| 280 | +);"; |
| 281 | + |
| 282 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 283 | + NewlineFormattedCheckConstraint = true |
| 284 | + }); |
| 285 | + } |
| 286 | + |
| 287 | + [TestMethod] |
| 288 | + [Priority(0)] |
| 289 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 290 | + public void TestNewLineFormattedIndexDefinitionWhenFalse() { |
| 291 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 292 | + INDEX ComplicatedIndex UNIQUE (Col1, Col2, Col3) INCLUDE (Col4, Col5, Col6, Col7, Col8) WHERE Col4 = 'AR' |
| 293 | + AND Col3 IN ('ABC', 'XYZ') |
| 294 | + AND Col5 = 0 |
| 295 | + AND Col6 = 1 |
| 296 | + AND Col7 = 0 |
| 297 | + AND Col8 IS NOT NULL |
| 298 | +);"; |
| 299 | + |
| 300 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 301 | + NewLineFormattedIndexDefinition = false |
| 302 | + }); |
| 303 | + } |
| 304 | + |
| 305 | + [TestMethod] |
| 306 | + [Priority(0)] |
| 307 | + [SqlStudioTestCategory(Category.UnitTest)] |
| 308 | + public void TestNewLineFormattedIndexDefinitionWhenTrue() { |
| 309 | + var expectedSqlText = @"CREATE TABLE DummyTable ( |
| 310 | + INDEX ComplicatedIndex |
| 311 | + UNIQUE (Col1, Col2, Col3) |
| 312 | + INCLUDE (Col4, Col5, Col6, Col7, Col8) |
| 313 | + WHERE Col4 = 'AR' |
| 314 | + AND Col3 IN ('ABC', 'XYZ') |
| 315 | + AND Col5 = 0 |
| 316 | + AND Col6 = 1 |
| 317 | + AND Col7 = 0 |
| 318 | + AND Col8 IS NOT NULL |
| 319 | +);"; |
| 320 | + |
| 321 | + ParseAndAssertEquality(expectedSqlText, new SqlScriptGeneratorOptions { |
| 322 | + NewLineFormattedIndexDefinition = true |
| 323 | + }); |
| 324 | + } |
| 325 | + |
| 326 | + void ParseAndAssertEquality(string sqlText, SqlScriptGeneratorOptions generatorOptions) { |
| 327 | + var parser = new TSql160Parser(true); |
| 328 | + var fragment = parser.ParseStatementList(new StringReader(sqlText), out var errors); |
| 329 | + |
| 330 | + Assert.AreEqual(0, errors.Count); |
| 331 | + |
| 332 | + var generator = new Sql160ScriptGenerator(generatorOptions); |
| 333 | + generator.GenerateScript(fragment, out var generatedSqlText); |
| 334 | + |
| 335 | + Assert.AreEqual(sqlText, generatedSqlText); |
| 336 | + } |
160 | 337 | } |
161 | 338 | } |
0 commit comments