mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
The goal of this make sure that test code is correctly organized to ensure that test suites aren't dependent on each other.
* UnitTests get their own project now (renaming Microsoft.SqlTools.ServiceLayer.Test to Microsoft.SqlTools.ServiceLayer.UnitTests) which is about 90% of the changes to the files.
* IntegrationTests no longer depends on UnitTests, only Test.Common
* Any shared components from TestObjects that spins up a "live" connection has been moved to IntegrationTests Utility/LiveConnectionHelper.cs
* The dictionary-based mock file stream factory has been moved to Test.Common since it is used by UnitTests and IntegrationTests
* Added a overload that doesn't take a dictionary for when we don't care about monitoring the storage (about 90% of the time)
* The RunIf* wrapper methods have been moved to Test.Common
* OwnerUri and StandardQuery constants have been moved to Test.Common Constants file
* Updating to latest SDK version available at https://www.microsoft.com/net/core#windowscmd
* Moving unit tests to unit test folder
* Changing namespaces to UnitTests
* Moving some constants and shared functionality into common project, making the UnitTests reference it
* Unit tests are working!
* Integration tests are working
* Updating automated test runs
* Fixing one last broken unit test
* Exposing internals for other projects
* Moving edit data tests to UnitTest project
* Applying refactor fixes to unit tests
* Fixing flaky test that wasn't awaiting completion
217 lines
8.5 KiB
C#
217 lines
8.5 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
|
|
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
|
|
{
|
|
public class SaveAsCsvFileStreamWriterTests
|
|
{
|
|
[Theory]
|
|
[InlineData("Something\rElse")]
|
|
[InlineData("Something\nElse")]
|
|
[InlineData("Something\"Else")]
|
|
[InlineData("Something,Else")]
|
|
[InlineData("\tSomething")]
|
|
[InlineData("Something\t")]
|
|
[InlineData(" Something")]
|
|
[InlineData("Something ")]
|
|
[InlineData(" \t\r\n\",\r\n\"\r ")]
|
|
public void EncodeCsvFieldShouldWrap(string field)
|
|
{
|
|
// If: I CSV encode a field that has forbidden characters in it
|
|
string output = SaveAsCsvFileStreamWriter.EncodeCsvField(field);
|
|
|
|
// Then: It should wrap it in quotes
|
|
Assert.True(Regex.IsMatch(output, "^\".*")
|
|
&& Regex.IsMatch(output, ".*\"$"));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Something")]
|
|
[InlineData("Something valid.")]
|
|
[InlineData("Something\tvalid")]
|
|
public void EncodeCsvFieldShouldNotWrap(string field)
|
|
{
|
|
// If: I CSV encode a field that does not have forbidden characters in it
|
|
string output = SaveAsCsvFileStreamWriter.EncodeCsvField(field);
|
|
|
|
// Then: It should not wrap it in quotes
|
|
Assert.False(Regex.IsMatch(output, "^\".*\"$"));
|
|
}
|
|
|
|
[Fact]
|
|
public void EncodeCsvFieldReplace()
|
|
{
|
|
// If: I CSV encode a field that has a double quote in it,
|
|
string output = SaveAsCsvFileStreamWriter.EncodeCsvField("Some\"thing");
|
|
|
|
// Then: It should be replaced with double double quotes
|
|
Assert.Equal("\"Some\"\"thing\"", output);
|
|
}
|
|
|
|
[Fact]
|
|
public void EncodeCsvFieldNull()
|
|
{
|
|
// If: I CSV encode a null
|
|
string output = SaveAsCsvFileStreamWriter.EncodeCsvField(null);
|
|
|
|
// Then: there should be a string version of null returned
|
|
Assert.Equal("NULL", output);
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteRowWithoutColumnSelectionOrHeader()
|
|
{
|
|
// Setup:
|
|
// ... Create a request params that has no selection made
|
|
// ... Create a set of data to write
|
|
// ... Create a memory location to store the data
|
|
var requestParams = new SaveResultsAsCsvRequestParams();
|
|
List<DbCellValue> data = new List<DbCellValue>
|
|
{
|
|
new DbCellValue { DisplayValue = "item1" },
|
|
new DbCellValue { DisplayValue = "item2" }
|
|
};
|
|
List<DbColumnWrapper> columns = new List<DbColumnWrapper>
|
|
{
|
|
new DbColumnWrapper(new TestDbColumn("column1")),
|
|
new DbColumnWrapper(new TestDbColumn("column2"))
|
|
};
|
|
byte[] output = new byte[8192];
|
|
|
|
// If: I write a row
|
|
SaveAsCsvFileStreamWriter writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams);
|
|
using (writer)
|
|
{
|
|
writer.WriteRow(data, columns);
|
|
}
|
|
|
|
// Then: It should write one line with 2 items, comma delimited
|
|
string outputString = Encoding.UTF8.GetString(output).TrimEnd('\0', '\r', '\n');
|
|
string[] lines = outputString.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
|
|
Assert.Equal(1, lines.Length);
|
|
string[] values = lines[0].Split(',');
|
|
Assert.Equal(2, values.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteRowWithHeader()
|
|
{
|
|
// Setup:
|
|
// ... Create a request params that has no selection made, headers should be printed
|
|
// ... Create a set of data to write
|
|
// ... Create a memory location to store the data
|
|
var requestParams = new SaveResultsAsCsvRequestParams
|
|
{
|
|
IncludeHeaders = true
|
|
};
|
|
List<DbCellValue> data = new List<DbCellValue>
|
|
{
|
|
new DbCellValue { DisplayValue = "item1" },
|
|
new DbCellValue { DisplayValue = "item2" }
|
|
};
|
|
List<DbColumnWrapper> columns = new List<DbColumnWrapper>
|
|
{
|
|
new DbColumnWrapper(new TestDbColumn("column1")),
|
|
new DbColumnWrapper(new TestDbColumn("column2"))
|
|
};
|
|
byte[] output = new byte[8192];
|
|
|
|
// If: I write a row
|
|
SaveAsCsvFileStreamWriter writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams);
|
|
using (writer)
|
|
{
|
|
writer.WriteRow(data, columns);
|
|
}
|
|
|
|
// Then:
|
|
// ... It should have written two lines
|
|
string outputString = Encoding.UTF8.GetString(output).TrimEnd('\0', '\r', '\n');
|
|
string[] lines = outputString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
|
|
Assert.Equal(2, lines.Length);
|
|
|
|
// ... It should have written a header line with two, comma separated names
|
|
string[] headerValues = lines[0].Split(',');
|
|
Assert.Equal(2, headerValues.Length);
|
|
for (int i = 0; i < columns.Count; i++)
|
|
{
|
|
Assert.Equal(columns[i].ColumnName, headerValues[i]);
|
|
}
|
|
|
|
// Note: No need to check values, it is done as part of the previous test
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteRowWithColumnSelection()
|
|
{
|
|
// Setup:
|
|
// ... Create a request params that selects n-1 columns from the front and back
|
|
// ... Create a set of data to write
|
|
// ... Create a memory location to store the data
|
|
var requestParams = new SaveResultsAsCsvRequestParams
|
|
{
|
|
ColumnStartIndex = 1,
|
|
ColumnEndIndex = 2,
|
|
RowStartIndex = 0, // Including b/c it is required to be a "save selection"
|
|
RowEndIndex = 10,
|
|
IncludeHeaders = true // Including headers to test both column selection logic
|
|
};
|
|
List<DbCellValue> data = new List<DbCellValue>
|
|
{
|
|
new DbCellValue { DisplayValue = "item1" },
|
|
new DbCellValue { DisplayValue = "item2" },
|
|
new DbCellValue { DisplayValue = "item3" },
|
|
new DbCellValue { DisplayValue = "item4" }
|
|
};
|
|
List<DbColumnWrapper> columns = new List<DbColumnWrapper>
|
|
{
|
|
new DbColumnWrapper(new TestDbColumn("column1")),
|
|
new DbColumnWrapper(new TestDbColumn("column2")),
|
|
new DbColumnWrapper(new TestDbColumn("column3")),
|
|
new DbColumnWrapper(new TestDbColumn("column4"))
|
|
};
|
|
byte[] output = new byte[8192];
|
|
|
|
// If: I write a row
|
|
SaveAsCsvFileStreamWriter writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams);
|
|
using (writer)
|
|
{
|
|
writer.WriteRow(data, columns);
|
|
}
|
|
|
|
// Then:
|
|
// ... It should have written two lines
|
|
string outputString = Encoding.UTF8.GetString(output).TrimEnd('\0', '\r', '\n');
|
|
string[] lines = outputString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
|
|
Assert.Equal(2, lines.Length);
|
|
|
|
// ... It should have written a header line with two, comma separated names
|
|
string[] headerValues = lines[0].Split(',');
|
|
Assert.Equal(2, headerValues.Length);
|
|
for (int i = 1; i <= 2; i++)
|
|
{
|
|
Assert.Equal(columns[i].ColumnName, headerValues[i-1]);
|
|
}
|
|
|
|
// ... The second line should have two, comma separated values
|
|
string[] dataValues = lines[1].Split(',');
|
|
Assert.Equal(2, dataValues.Length);
|
|
for (int i = 1; i <= 2; i++)
|
|
{
|
|
Assert.Equal(data[i].DisplayValue, dataValues[i-1]);
|
|
}
|
|
}
|
|
}
|
|
}
|