Address warnings and (some) nullables (#2013)

This commit is contained in:
Cheena Malhotra
2023-04-18 20:57:13 -07:00
committed by GitHub
parent d56f2309da
commit 648d7dbd3c
83 changed files with 674 additions and 588 deletions

View File

@@ -17,7 +17,7 @@ using NUnit.Framework;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
public class SaveAsCsvFileStreamWriterTests
public partial class SaveAsCsvFileStreamWriterTests
{
[Test]
public void Constructor_NullStream()
@@ -148,7 +148,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
string output = writer.EncodeCsvField(field);
// Then: It should wrap it in quotes
Assert.True(Regex.IsMatch(output, "^\".*\"$", RegexOptions.Singleline));
Assert.True(GetCsvRegexSingleLine().IsMatch(output));
}
[TestCase("Something\rElse")] // Contains carriage return [TODO: Don't support this]
@@ -165,7 +165,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
string output = writer.EncodeCsvField(field);
// Then: It should wrap it in quotes
Assert.True(Regex.IsMatch(output, @"^\[.*\[$", RegexOptions.Singleline));
Assert.True(GetCsvBracketRegex().IsMatch(output));
}
[TestCase("\tSomething")] // Starts with tab
@@ -186,7 +186,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
string output = writer.EncodeCsvField(field);
// Then: It should wrap it in quotes
Assert.True(Regex.IsMatch(output, "^\".*\"$", RegexOptions.Singleline));
Assert.True(GetCsvRegexSingleLine().IsMatch(output));
}
[TestCase("Something")]
@@ -201,7 +201,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
string output = writer.EncodeCsvField(field);
// Then: It should not wrap it in quotes
Assert.False(Regex.IsMatch(output, "^\".*\"$"));
Assert.False(GetCsvRegex().IsMatch(output));
}
[TestCase(null, "Some\"thing", "\"Some\"\"thing\"")] // Default identifier
@@ -277,7 +277,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
byte[] output = new byte[8192];
// If: I write a row
SaveAsCsvFileStreamWriter writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams, columns);
var writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams, columns);
using (writer)
{
writer.WriteRow(data, columns);
@@ -425,5 +425,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
Assert.IsEmpty(lines[lines.Length - 1]);
return lines.Take(lines.Length - 1).ToArray();
}
[GeneratedRegex("^\\[.*\\[$", RegexOptions.Singleline)]
private static partial Regex GetCsvBracketRegex();
[GeneratedRegex("^\".*\"$")]
private static partial Regex GetCsvRegex();
[GeneratedRegex("^\".*\"$", RegexOptions.Singleline)]
private static partial Regex GetCsvRegexSingleLine();
}
}

View File

@@ -16,7 +16,7 @@ using NUnit.Framework;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
public class SaveAsExcelFileStreamWriterHelperTests : IDisposable
public partial class SaveAsExcelFileStreamWriterHelperTests : IDisposable
{
private Stream _stream;
public SaveAsExcelFileStreamWriterHelperTests()
@@ -25,7 +25,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
using (var helper = new SaveAsExcelFileStreamWriterHelper(_stream, true))
using (var sheet = helper.AddSheet())
{
DbCellValue value = new DbCellValue();
var value = new DbCellValue();
sheet.AddRow();
value.IsNull = true;
@@ -71,7 +71,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
sheet.AddCell(value);
}
}
Regex contentRemoveLinebreakLeadingSpace = new Regex(@"\r?\n\s*");
[GeneratedRegex("\\r?\\n\\s*")]
private static partial Regex GetContentRemoveLinebreakLeadingSpaceRegex();
private void ContentMatch(string fileName)
{
string referencePath = Path.Combine(RunEnvironmentInfo.GetTestDataLocation(),
@@ -79,11 +82,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
"SaveAsExcelFileStreamWriterHelperTests",
fileName);
string referenceContent = File.ReadAllText(referencePath);
referenceContent = contentRemoveLinebreakLeadingSpace.Replace(referenceContent, "");
referenceContent = GetContentRemoveLinebreakLeadingSpaceRegex().Replace(referenceContent, "");
using (ZipArchive zip = new ZipArchive(_stream, ZipArchiveMode.Read, true))
using (var zip = new ZipArchive(_stream, ZipArchiveMode.Read, true))
{
using (var reader = new StreamReader(zip.GetEntry(fileName).Open()))
using (var reader = new StreamReader(zip?.GetEntry(fileName)?.Open()!))
{
string realContent = reader.ReadToEnd();
Assert.AreEqual(referenceContent, realContent);
@@ -130,7 +133,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
public class SaveAsExcelFileStreamWriterHelperReferenceManagerTests
{
private Mock<XmlWriter> _xmlWriterMock;
private string LastWrittenReference { get; set; }
private string? LastWrittenReference { get; set; }
private int LastWrittenRow { get; set; }
public SaveAsExcelFileStreamWriterHelperReferenceManagerTests()
@@ -145,7 +148,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
_xmlWriterMock.Setup(a => a.WriteEndAttribute());
_xmlWriterMock.Setup(a => a.WriteValue(It.IsAny<int>()))
.Callback<int>(row => LastWrittenRow = row);
}
[Test]
@@ -203,8 +205,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
manager.AssureColumnReference();
manager.WriteAndIncreaseColumnReference();
Assert.AreEqual("XFD1", LastWrittenReference);
var ex = Assert.Throws<InvalidOperationException>(
() => manager.AssureColumnReference());
var ex = Assert.Throws<InvalidOperationException>(manager.AssureColumnReference);
Assert.That(ex.Message, Does.Contain("max column number is 16384"));
}
[Test]
@@ -232,8 +233,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
var xmlWriter = _xmlWriterMock.Object;
var manager = new SaveAsExcelFileStreamWriterHelper.ReferenceManager(xmlWriter);
var ex = Assert.Throws<InvalidOperationException>(
() => manager.AssureColumnReference());
var ex = Assert.Throws<InvalidOperationException>(manager.AssureColumnReference);
Assert.That(ex.Message, Does.Contain("AddRow must be called before AddCell"));
}

View File

@@ -18,10 +18,11 @@ using NUnit.Framework;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
public class SaveAsMarkdownFileStreamWriterTests
public partial class SaveAsMarkdownFileStreamWriterTests
{
// Regex: Matches '|' not preceded by a '\'
private static readonly Regex UnescapedPipe = new Regex(@"(?<!\\)\|", RegexOptions.Compiled);
[GeneratedRegex("(?<!\\\\)\\|", RegexOptions.Compiled)]
private static partial Regex GetUnescapedPipeRegex();
[Test]
public void Constructor_NullStream()
@@ -362,7 +363,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
private static void ValidateLine(string line, IEnumerable<string> expectedCells)
{
string[] cells = UnescapedPipe.Split(line);
string[] cells = GetUnescapedPipeRegex().Split(line);
string[] expectedCellsArray = expectedCells as string[] ?? expectedCells.ToArray();
Assert.That(cells.Length - 2, Is.EqualTo(expectedCellsArray.Length), "Wrong number of cells in output");

View File

@@ -21,7 +21,7 @@ using NUnit.Framework;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
public class ServiceBufferReaderWriterTests
public partial class ServiceBufferReaderWriterTests
{
[Test]
public void ReaderStreamNull()
@@ -49,7 +49,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
invalidStream.SetupGet(s => s.CanSeek).Returns(true);
Assert.Throws<InvalidOperationException>(() =>
{
ServiceBufferFileStreamReader obj = new ServiceBufferFileStreamReader(invalidStream.Object, new QueryExecutionSettings());
var obj = new ServiceBufferFileStreamReader(invalidStream.Object, new QueryExecutionSettings());
obj.Dispose();
});
}
@@ -64,7 +64,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
invalidStream.SetupGet(s => s.CanSeek).Returns(false);
Assert.Throws<InvalidOperationException>(() =>
{
ServiceBufferFileStreamReader obj = new ServiceBufferFileStreamReader(invalidStream.Object, new QueryExecutionSettings());
var obj = new ServiceBufferFileStreamReader(invalidStream.Object, new QueryExecutionSettings());
obj.Dispose();
});
}
@@ -95,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
invalidStream.SetupGet(s => s.CanSeek).Returns(true);
Assert.Throws<InvalidOperationException>(() =>
{
ServiceBufferFileStreamWriter obj = new ServiceBufferFileStreamWriter(invalidStream.Object, new QueryExecutionSettings());
var obj = new ServiceBufferFileStreamWriter(invalidStream.Object, new QueryExecutionSettings());
obj.Dispose();
});
}
@@ -110,7 +110,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
invalidStream.SetupGet(s => s.CanSeek).Returns(false);
Assert.Throws<InvalidOperationException>(() =>
{
ServiceBufferFileStreamWriter obj = new ServiceBufferFileStreamWriter(invalidStream.Object, new QueryExecutionSettings());
var obj = new ServiceBufferFileStreamWriter(invalidStream.Object, new QueryExecutionSettings());
obj.Dispose();
});
}
@@ -128,7 +128,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
// If:
// ... I write a type T to the writer
using (ServiceBufferFileStreamWriter writer = new ServiceBufferFileStreamWriter(new MemoryStream(storage), overrideSettings))
using (var writer = new ServiceBufferFileStreamWriter(new MemoryStream(storage), overrideSettings))
{
int writtenBytes = writeFunc(writer, value);
Assert.AreEqual(valueLength, writtenBytes);
@@ -136,7 +136,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
// ... And read the type T back
FileStreamReadResult outValue;
using (ServiceBufferFileStreamReader reader = new ServiceBufferFileStreamReader(new MemoryStream(storage), overrideSettings))
using (var reader = new ServiceBufferFileStreamReader(new MemoryStream(storage), overrideSettings))
{
outValue = readFunc(reader, rowId);
}
@@ -312,7 +312,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATE column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTe"});
var col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTe"});
foreach (DateTime value in testValues)
{
@@ -320,7 +320,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value does not have a time string
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2}$"));
Assert.True(GetDateRegex().IsMatch(displayValue));
}
}
@@ -335,7 +335,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIME column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe"});
var col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe"});
foreach (DateTime value in testValues)
{
@@ -343,7 +343,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 3 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{3}$"));
Assert.True(GetDateTimeRegex().IsMatch(displayValue));
}
}
@@ -358,7 +358,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIME column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn
var col = new DbColumnWrapper(new TestDbColumn
{
DataTypeName = "DaTeTiMe2",
NumericScale = precision
@@ -370,7 +370,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with variable number of milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}"));
Assert.True(GetTimeRegex().IsMatch(displayValue));
if (precision > 0)
{
Assert.True(Regex.IsMatch(displayValue, $@"\.[\d]{{{precision}}}$"));
@@ -389,7 +389,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIME2 column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe2", NumericScale = 0});
var col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe2", NumericScale = 0});
foreach (DateTime value in testValues)
{
@@ -397,7 +397,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 0 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}$"));
Assert.True(GetTimeRegex().IsMatch(displayValue));
}
}
@@ -412,7 +412,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIME2 column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe2", NumericScale = 255});
var col = new DbColumnWrapper(new TestDbColumn {DataTypeName = "DaTeTiMe2", NumericScale = 255});
foreach (DateTime value in testValues)
{
@@ -420,7 +420,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 7 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{7}$"));
Assert.True(GetDateTime2Regex().IsMatch(displayValue));
}
}
@@ -436,7 +436,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIMEOFFSET column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn { DataTypeName = "datetimeoffset", NumericScale = 6 });
var col = new DbColumnWrapper(new TestDbColumn { DataTypeName = "datetimeoffset", NumericScale = 6 });
foreach (DateTimeOffset value in testValues)
{
@@ -444,7 +444,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTimeOffset(0, rowId, col));
// Make sure the display value has a time string with 6 milliseconds and a time zone
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{6} [+-][01][\d]:[\d]{2}$"));
Assert.True(GetDateTimeOffset6Regex().IsMatch(displayValue));
}
}
@@ -458,7 +458,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
};
// Setup: Create a DATETIMEOFFSET column
DbColumnWrapper col = new DbColumnWrapper(new TestDbColumn { DataTypeName = "datetimeoffset", NumericScale = 0 });
var col = new DbColumnWrapper(new TestDbColumn { DataTypeName = "datetimeoffset", NumericScale = 0 });
foreach (DateTimeOffset value in testValues)
{
@@ -466,7 +466,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadDateTimeOffset(0, rowId, col));
// Make sure the display value has a time string with no millisecond and a time zone
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2} [+-][01][\d]:[\d]{2}$"));
Assert.True(GetDateTimeOffset0Regex().IsMatch(displayValue));
}
}
@@ -490,11 +490,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
public void StringNullTest()
{
// Setup: Create a mock file stream
using (MemoryStream stream = new MemoryStream(new byte[8192]))
using (var stream = new MemoryStream(new byte[8192]))
{
// If:
// ... I write null as a string to the writer
using (ServiceBufferFileStreamWriter writer = new ServiceBufferFileStreamWriter(stream, new QueryExecutionSettings()))
using (var writer = new ServiceBufferFileStreamWriter(stream, new QueryExecutionSettings()))
{
// Then:
// ... I should get an argument null exception
@@ -514,7 +514,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
// Setup:
// ... Generate the test value
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
sb.Append(values[i%values.Length]);
@@ -529,11 +529,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
public void BytesNullTest()
{
// Setup: Create a mock file stream wrapper
using (MemoryStream stream = new MemoryStream(new byte[8192]))
using (var stream = new MemoryStream(new byte[8192]))
{
// If:
// ... I write null as a string to the writer
using (ServiceBufferFileStreamWriter writer = new ServiceBufferFileStreamWriter(stream, new QueryExecutionSettings()))
using (var writer = new ServiceBufferFileStreamWriter(stream, new QueryExecutionSettings()))
{
// Then:
// ... I should get an argument null exception
@@ -554,7 +554,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{
// Setup:
// ... Generate the test value
List<byte> sb = new List<byte>();
var sb = new List<byte>();
for (int i = 0; i < length; i++)
{
sb.Add(values[i % values.Length]);
@@ -602,5 +602,22 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
(reader, rowId) => reader.ReadMoney(0, rowId));
}
}
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2}$")]
private static partial Regex GetDateRegex();
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}")]
private static partial Regex GetTimeRegex();
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}\\.[\\d]{3}$")]
private static partial Regex GetDateTimeRegex();
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}\\.[\\d]{7}$")]
private static partial Regex GetDateTime2Regex();
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}\\.[\\d]{6} [+-][01][\\d]:[\\d]{2}$")]
private static partial Regex GetDateTimeOffset6Regex();
[GeneratedRegex("^[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2} [+-][01][\\d]:[\\d]{2}$")]
private static partial Regex GetDateTimeOffset0Regex();
}
}