-
Notifications
You must be signed in to change notification settings - Fork 311
Use UTF8 instance that doesn't emit BOM #3399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93531ad
Use UTF8 instance that doesn't emit BOM
apoorvdeshmukh 135adff
Merge remote-tracking branch 'origin/main' into dev/ad/3397
apoorvdeshmukh 415c025
Address review comments
apoorvdeshmukh dff3f0a
Hardcode expected bytes
apoorvdeshmukh 03413c8
Parameterize the test for MARS and BulkCopy streaming behavior
apoorvdeshmukh 41cea80
Move common project to test dir
apoorvdeshmukh 5d7a524
Merge remote-tracking branch 'origin/main' into dev/ad/3397
apoorvdeshmukh f32f2bf
Test improvements for readability
apoorvdeshmukh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/TestBulkCopyWithUTF8.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Data; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
{ | ||
public class TestBulkCopyWithUtf8 | ||
{ | ||
private readonly string _connectionString; | ||
|
||
public TestBulkCopyWithUtf8() | ||
{ | ||
_connectionString = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString){MultipleActiveResultSets = true}.ConnectionString; | ||
} | ||
|
||
private void SetupTables(SqlConnection connection, string sourceTable, string destinationTable, string insertQuery) | ||
{ | ||
string columnDefinition = "(str_col varchar(max) COLLATE Latin1_General_100_CS_AS_KS_WS_SC_UTF8)"; | ||
DataTestUtility.CreateTable(connection, sourceTable, columnDefinition); | ||
DataTestUtility.CreateTable(connection, destinationTable, columnDefinition); | ||
|
||
using SqlCommand insertCommand = connection.CreateCommand(); | ||
insertCommand.CommandText = insertQuery; | ||
Helpers.TryExecute(insertCommand, insertQuery); | ||
} | ||
|
||
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse))] | ||
public void BulkCopy_Utf8Data_ShouldMatchSource() | ||
{ | ||
string sourceTable = DataTestUtility.GetUniqueName("SrcUtf8DataTable"); | ||
string destinationTable = DataTestUtility.GetUniqueName("DstUtf8DataTable"); | ||
string insertQuery = $"INSERT INTO {sourceTable} VALUES('test')"; | ||
|
||
using SqlConnection sourceConnection = new SqlConnection(_connectionString); | ||
sourceConnection.Open(); | ||
SetupTables(sourceConnection, sourceTable, destinationTable, insertQuery); | ||
apoorvdeshmukh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using SqlCommand countCommand = new SqlCommand($"SELECT COUNT(*) FROM {destinationTable}", sourceConnection); | ||
apoorvdeshmukh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
long initialCount = Convert.ToInt64(countCommand.ExecuteScalar()); | ||
|
||
using SqlCommand sourceDataCommand = new SqlCommand($"SELECT str_col FROM {sourceTable}", sourceConnection); | ||
using SqlDataReader reader = sourceDataCommand.ExecuteReader(CommandBehavior.SequentialAccess); | ||
|
||
using SqlConnection destinationConnection = new SqlConnection(_connectionString); | ||
destinationConnection.Open(); | ||
|
||
using SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection) | ||
{ | ||
EnableStreaming = true, | ||
DestinationTableName = destinationTable | ||
}; | ||
|
||
try | ||
{ | ||
bulkCopy.WriteToServer(reader); | ||
apoorvdeshmukh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Bulk copy failed: {ex.Message}"); | ||
} | ||
|
||
long finalCount = Convert.ToInt64(countCommand.ExecuteScalar()); | ||
Assert.Equal(1, finalCount - initialCount); | ||
apoorvdeshmukh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using SqlCommand verifyCommand = new SqlCommand($"SELECT cast(str_col as varbinary) FROM {destinationTable}", destinationConnection); | ||
using SqlDataReader verifyReader = verifyCommand.ExecuteReader(CommandBehavior.SequentialAccess); | ||
|
||
byte[] expectedBytes = new byte[] { 0x74, 0x65, 0x73, 0x74 }; | ||
|
||
Assert.True(verifyReader.Read(), "No data found in destination table after bulk copy."); | ||
|
||
byte[] actualBytes = verifyReader.GetSqlBinary(0).Value; | ||
Assert.Equal(expectedBytes.Length, actualBytes.Length); | ||
Assert.Equal(expectedBytes, actualBytes); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.