Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public void SqlParameterProperties(string connection)
const string firstColumnName = @"firstColumn";
const string secondColumnName = @"secondColumn";
const string thirdColumnName = @"thirdColumn";
string inputProcedureName = DataTestUtility.GetUniqueName("InputProc").ToString();
string outputProcedureName = DataTestUtility.GetUniqueName("OutputProc").ToString();
string inputProcedureName = DataTestUtility.GetShortName("InputProc").ToString();
string outputProcedureName = DataTestUtility.GetShortName("OutputProc").ToString();
const int charColumnSize = 100;
const int decimalColumnPrecision = 10;
const int decimalColumnScale = 4;
Expand Down Expand Up @@ -722,7 +722,7 @@ public void TestExecuteReader(string connection)
[ClassData(typeof(AEConnectionStringProvider))]
public async Task TestExecuteReaderAsyncWithLargeQuery(string connectionString)
{
string randomName = DataTestUtility.GetUniqueName(Guid.NewGuid().ToString().Replace("-", ""), false);
string randomName = DataTestUtility.GetShortName(Guid.NewGuid().ToString().Replace("-", ""), false);
if (randomName.Length > 50)
{
randomName = randomName.Substring(0, 50);
Expand Down Expand Up @@ -912,8 +912,8 @@ public void TestEnclaveStoredProceduresWithAndWithoutParameters(string connectio
using SqlCommand sqlCommand = new("", sqlConnection, transaction: null,
columnEncryptionSetting: SqlCommandColumnEncryptionSetting.Enabled);

string procWithoutParams = DataTestUtility.GetUniqueName("EnclaveWithoutParams", withBracket: false);
string procWithParam = DataTestUtility.GetUniqueName("EnclaveWithParams", withBracket: false);
string procWithoutParams = DataTestUtility.GetShortName("EnclaveWithoutParams", withBracket: false);
string procWithParam = DataTestUtility.GetShortName("EnclaveWithParams", withBracket: false);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void TestRoundTripWithCspAndCertStoreProvider()
[MemberData(nameof(TestEncryptDecryptWithCsp_Data))]
public void TestEncryptDecryptWithCsp(string connectionString, string providerName, int providerType)
{
string keyIdentifier = DataTestUtility.GetUniqueNameForSqlServer("CSP");
string keyIdentifier = DataTestUtility.GetLongName("CSP");
CspParameters namedCspParameters = new CspParameters(providerType, providerName, keyIdentifier);
using SQLSetupStrategyCspProvider sqlSetupStrategyCsp = new SQLSetupStrategyCspProvider(namedCspParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static bool TcpConnectionStringDoesNotUseAadAuth
{
get
{
SqlConnectionStringBuilder builder = new (TCPConnectionString);
SqlConnectionStringBuilder builder = new(TCPConnectionString);
return builder.Authentication == SqlAuthenticationMethod.SqlPassword || builder.Authentication == SqlAuthenticationMethod.NotSpecified;
}
}
Expand Down Expand Up @@ -556,59 +556,176 @@ public static bool DoesHostAddressContainBothIPv4AndIPv6()
}
}

// Generate a new GUID and return the characters from its 1st and 4th
// parts, as shown here:
//
// 7ff01cb8-88c7-11f0-b433-00155d7e531e
// ^^^^^^^^ ^^^^
//
// These 12 characters are concatenated together without any
// separators. These 2 parts typically comprise a timestamp and clock
// sequence, most likely to be unique for tests that generate names in
// quick succession.
private static string GetGuidParts()
{
var guid = Guid.NewGuid().ToString();
// GOTCHA: The slice operator is inclusive of the start index and
// exclusive of the end index!
return guid.Substring(0, 8) + guid.Substring(19, 4);
}

/// <summary>
/// Generate a unique name to use in Sql Server;
/// some providers does not support names (Oracle supports up to 30).
/// Generate a short unique database object name, whose maximum length
/// is 30 characters, with the format:
///
/// <Prefix>_<GuidParts>
///
/// The Prefix will be truncated to satisfy the overall maximum length.
///
/// The GUID parts will be the characters from the 1st and 4th blocks
/// from a traditional string representation, as shown here:
///
/// 7ff01cb8-88c7-11f0-b433-00155d7e531e
/// ^^^^^^^^ ^^^^
///
/// These 2 parts typically comprise a timestamp and clock sequence,
/// most likely to be unique for tests that generate names in quick
/// succession. The 12 characters are concatenated together without any
/// separators.
/// </summary>
/// <param name="prefix">The name length will be no more then (16 + prefix.Length + escapeLeft.Length + escapeRight.Length).</param>
/// <param name="withBracket">Name without brackets.</param>
/// <returns>Unique name by considering the Sql Server naming rules.</returns>
public static string GetUniqueName(string prefix, bool withBracket = true)
{
string escapeLeft = withBracket ? "[" : string.Empty;
string escapeRight = withBracket ? "]" : string.Empty;
string uniqueName = string.Format("{0}{1}_{2}_{3}{4}",
escapeLeft,
prefix,
DateTime.Now.Ticks.ToString("X", CultureInfo.InvariantCulture), // up to 8 characters
Guid.NewGuid().ToString().Substring(0, 6), // take the first 6 characters only
escapeRight);
return uniqueName;
///
/// <param name="prefix">
/// The prefix to use when generating the unique name, truncated to at
/// most 18 characters when withBracket is false, and 16 characters when
/// withBracket is true.
///
/// This should not contain any characters that cannot be used in
/// database object names. See:
///
/// https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver17#rules-for-regular-identifiers
/// </param>
///
/// <param name="withBracket">
/// When true, the entire generated name will be enclosed in square
/// brackets, for example:
///
/// [MyPrefix_7ff01cb811f0]
/// </param>
///
/// <returns>
/// A unique database object name, no more than 30 characters long.
/// </returns>
public static string GetShortName(string prefix, bool withBracket = true)
{
StringBuilder name = new(30);

if (withBracket)
{
name.Append('[');
}

int maxPrefixLength = withBracket ? 16 : 18;
if (prefix.Length > maxPrefixLength)
{
prefix = prefix.Substring(0, maxPrefixLength);
}

name.Append(prefix);
name.Append('_');
name.Append(GetGuidParts());

if (withBracket)
{
name.Append(']');
}

return name.ToString();
}

/// <summary>
/// Uses environment values `UserName` and `MachineName` in addition to the specified `prefix` and current date
/// to generate a unique name to use in Sql Server;
/// SQL Server supports long names (up to 128 characters), add extra info for troubleshooting.
/// Generate a long unique database object name, whose maximum length is
/// 96 characters, with the format:
///
/// <Prefix>_<GuidParts>_<UserName>_<MachineName>
///
/// The Prefix will be truncated to satisfy the overall maximum length.
///
/// The GUID Parts will be the characters from the 1st and 4th blocks
/// from a traditional string representation, as shown here:
///
/// 7ff01cb8-88c7-11f0-b433-00155d7e531e
/// ^^^^^^^^ ^^^^
///
/// These 2 parts typically comprise a timestamp and clock sequence,
/// most likely to be unique for tests that generate names in quick
/// succession. The 12 characters are concatenated together without any
/// separators.
///
/// The UserName and MachineName are obtained from the Environment,
/// and will be truncated to satisfy the maximum overall length.
/// </summary>
/// <param name="prefix">Add the prefix to the generate string.</param>
/// <param name="withBracket">Database name must be pass with brackets by default.</param>
/// <returns>Unique name by considering the Sql Server naming rules, never longer than 96 characters.</returns>
public static string GetUniqueNameForSqlServer(string prefix, bool withBracket = true)
{
string extendedPrefix = string.Format(
"{0}_{1}_{2}@{3}",
prefix,
Environment.UserName,
Environment.MachineName,
DateTime.Now.ToString("yyyy_MM_dd", CultureInfo.InvariantCulture));
string name = GetUniqueName(extendedPrefix, withBracket);

// Truncate to no more than 96 characters.
const int maxLen = 96;
if (name.Length > maxLen)
{
if (withBracket)
{
name = name.Substring(0, maxLen - 1) + ']';
}
else
{
name = name.Substring(0, maxLen);
}
///
/// <param name="prefix">
/// The prefix to use when generating the unique name, truncated to at
/// most 32 characters.
///
/// This should not contain any characters that cannot be used in
/// database object names. See:
///
/// https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver17#rules-for-regular-identifiers
/// </param>
///
/// <param name="withBracket">
/// When true, the entire generated name will be enclosed in square
/// brackets, for example:
///
/// [MyPrefix_7ff01cb811f0_test_user_ci_agent_machine_name]
/// </param>
///
/// <returns>
/// A unique database object name, no more than 96 characters long.
/// </returns>
public static string GetLongName(string prefix, bool withBracket = true)
{
StringBuilder name = new(96);

if (withBracket)
{
name.Append('[');
}

if (prefix.Length > 32)
{
prefix = prefix.Substring(0, 32);
}

name.Append(prefix);
name.Append('_');
name.Append(GetGuidParts());
name.Append('_');

var suffix =
Environment.UserName + '_' +
Environment.MachineName;

int maxSuffixLength = 96 - name.Length;
if (withBracket)
{
--maxSuffixLength;
}
if (suffix.Length > maxSuffixLength)
{
suffix = suffix.Substring(0, maxSuffixLength);
}

name.Append(suffix);

if (withBracket)
{
name.Append(']');
}

return name;
return name.ToString();
}

public static void CreateTable(SqlConnection sqlConnection, string tableName, string createBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void TestMain()
{
string connectionString = DataTestUtility.TCPConnectionString;

string tempTable = DataTestUtility.GetUniqueNameForSqlServer("table");
string tempTable = DataTestUtility.GetLongName("table");

DbProviderFactory provider = SqlClientFactory.Instance;
try
Expand Down Expand Up @@ -275,7 +275,7 @@ public static void TestMain()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void SqlDataReader_SqlBuffer_GetFieldValue()
{
string tableName = DataTestUtility.GetUniqueNameForSqlServer("SqlBuffer_GetFieldValue");
string tableName = DataTestUtility.GetLongName("SqlBuffer_GetFieldValue");
DateTimeOffset dtoffset = DateTimeOffset.Now;
DateTime dt = DateTime.Now;
//Exclude the millisecond because of rounding at some points by SQL Server.
Expand Down Expand Up @@ -374,7 +374,7 @@ public static void SqlDataReader_SqlBuffer_GetFieldValue()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static async Task SqlDataReader_SqlBuffer_GetFieldValue_Async()
{
string tableName = DataTestUtility.GetUniqueNameForSqlServer("SqlBuffer_GetFieldValue_Async");
string tableName = DataTestUtility.GetLongName("SqlBuffer_GetFieldValue_Async");
DateTimeOffset dtoffset = DateTimeOffset.Now;
DateTime dt = DateTime.Now;
//Exclude the millisecond because of rounding at some points by SQL Server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class AdapterTest
public AdapterTest()
{
// create random name for temp tables
_tempTable = DataTestUtility.GetUniqueName("AdapterTest");
_tempTable = DataTestUtility.GetShortName("AdapterTest");
_tempTable = _tempTable.Replace('-', '_');

_randomGuid = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -555,7 +555,7 @@ public void ParameterTest_AllTypes()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public void ParameterTest_InOut()
{
string procName = DataTestUtility.GetUniqueName("P");
string procName = DataTestUtility.GetShortName("P");
// input, output
string spCreateInOut =
"CREATE PROCEDURE " + procName + " @in int, @inout int OUTPUT, @out nvarchar(8) OUTPUT " +
Expand Down Expand Up @@ -836,13 +836,13 @@ public void BulkUpdateTest()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public void UpdateRefreshTest()
{
string identTableName = DataTestUtility.GetUniqueName("ID_");
string identTableName = DataTestUtility.GetShortName("ID_");
string createIdentTable =
$"CREATE TABLE {identTableName} (id int IDENTITY," +
"LastName nvarchar(50) NULL," +
"Firstname nvarchar(50) NULL)";

string spName = DataTestUtility.GetUniqueName("sp_insert", withBracket: false);
string spName = DataTestUtility.GetShortName("sp_insert", withBracket: false);
string spCreateInsert =
$"CREATE PROCEDURE {spName}" +
"(@FirstName nvarchar(50), @LastName nvarchar(50), @id int OUTPUT) " +
Expand Down Expand Up @@ -1155,7 +1155,7 @@ public void AutoGenUpdateTest()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public void AutoGenErrorTest()
{
string identTableName = DataTestUtility.GetUniqueName("ID_");
string identTableName = DataTestUtility.GetShortName("ID_");
string createIdentTable =
$"CREATE TABLE {identTableName} (id int IDENTITY," +
"LastName nvarchar(50) NULL," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public static async Task ConnectionOpenAsyncDisableRetry()
{
SqlConnectionStringBuilder connectionStringBuilder = new(DataTestUtility.TCPConnectionString)
{
InitialCatalog = DataTestUtility.GetUniqueNameForSqlServer("DoesNotExist", false),
InitialCatalog = DataTestUtility.GetLongName("DoesNotExist", false),
Pooling = false,
ConnectTimeout = 15,
ConnectRetryCount = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class DataClassificationTest
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse), nameof(DataTestUtility.IsSupportedDataClassification))]
public static void TestDataClassificationResultSetRank()
{
s_tableName = DataTestUtility.GetUniqueNameForSqlServer("DC");
s_tableName = DataTestUtility.GetLongName("DC");
using (SqlConnection sqlConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
Expand All @@ -41,7 +41,7 @@ public static void TestDataClassificationResultSetRank()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSupportedDataClassification))]
public static void TestDataClassificationResultSet()
{
s_tableName = DataTestUtility.GetUniqueNameForSqlServer("DC");
s_tableName = DataTestUtility.GetLongName("DC");
using (SqlConnection sqlConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
Expand Down Expand Up @@ -232,7 +232,7 @@ public static void TestDataClassificationBulkCopy()
data.Rows.Add(Guid.NewGuid(), "Company 2", "[email protected]", 1);
data.Rows.Add(Guid.NewGuid(), "Company 3", "[email protected]", 1);

var tableName = DataTestUtility.GetUniqueNameForSqlServer("DC");
var tableName = DataTestUtility.GetLongName("DC");

using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static void CheckSparseColumnBit()
[InlineData("Georgian_Modern_Sort_CI_AS")]
public static void CollatedDataReaderTest(string collation)
{
string dbName = DataTestUtility.GetUniqueName("CollationTest", false);
string dbName = DataTestUtility.GetShortName("CollationTest", false);

SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString)
{
Expand Down
Loading
Loading