test(sql): Add comprehensive unit tests for breakdown classes and utilities

Introduces new unit tests for Snowflake and SQL Server-specific SQL breakdown
classes (DELETE, INSERT, UPDATE, PROCEDURE) and various general SQL utility
functions. This significantly increases test coverage, ensuring robustness
and correctness across different SQL dialects and helper logic.
This commit is contained in:
Thom Lamb
2026-06-15 08:16:22 -05:00
parent 9f9450dc6d
commit fbedea3941
13 changed files with 2575 additions and 0 deletions
@@ -0,0 +1,324 @@
using System.Collections;
using Strata.SqlTools.SqlBreakdown.Utilities;
namespace Strata.SqlTools.SqlBreakdown.Tests.Utilities;
[TestFixture]
public class ArrayUtilsTests
{
private static readonly Guid GuidA = new("11111111-1111-1111-1111-111111111111");
private static readonly Guid GuidB = new("22222222-2222-2222-2222-222222222222");
private static readonly Guid GuidC = new("33333333-3333-3333-3333-333333333333");
#region NewArray / Coalesce
[Test]
public void NewArray_ReturnsArrayOfValues()
{
// Act
var result = ArrayUtils.NewArray(1, 2, 3);
// Assert
Assert.That(result, Is.EqualTo(new[] { 1, 2, 3 }));
}
[Test]
public void Coalesce_ReturnsFirstNonNull()
{
// Act & Assert
Assert.That(ArrayUtils.Coalesce(null!, null!, "x", "y"), Is.EqualTo("x"));
}
[Test]
public void Coalesce_AllNull_ReturnsNull()
{
// Act & Assert
Assert.That(ArrayUtils.Coalesce(null!, null!), Is.Null);
}
#endregion
#region CSV -> Collection
[Test]
public void GetArrayListFromCsv_TrimsAndSplits()
{
// Act
var result = ArrayUtils.GetArrayListFromCsv("a, b, c");
// Assert
Assert.That(result, Is.EqualTo(new ArrayList { "a", "b", "c" }));
}
[Test]
public void GetArrayListFromCsv_Empty_ReturnsEmptyList()
{
// Act & Assert
Assert.That(ArrayUtils.GetArrayListFromCsv(string.Empty), Is.Empty);
}
[Test]
public void GetGenericListFromCsv_TrimsEntries()
{
// Act
var result = ArrayUtils.GetGenericListFromCsv("a, b ,c");
// Assert
Assert.That(result, Is.EqualTo(new List<string> { "a", "b", "c" }));
}
[Test]
public void GetGenericListFromCsv_Empty_ReturnsEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.GetGenericListFromCsv(string.Empty), Is.Empty);
}
[Test]
public void GetIntegerListFromCsv_SkipsNonIntegers()
{
// Act
var result = ArrayUtils.GetIntegerListFromCsv("1,2,x,3");
// Assert
Assert.That(result, Is.EqualTo(new List<int> { 1, 2, 3 }));
}
[Test]
public void GetGuidListFromCsv_ParsesValidSkipsInvalid()
{
// Act
var result = ArrayUtils.GetGuidListFromCsv($"{GuidA},not-a-guid,{GuidB}");
// Assert
Assert.That(result, Is.EqualTo(new List<Guid> { GuidA, GuidB }));
}
[Test]
public void GetGuidListFromCsv_Empty_ReturnsEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.GetGuidListFromCsv(string.Empty), Is.Empty);
}
[Test]
public void GetGuidArrayFromCsv_ParsesGuids()
{
// Act
var result = ArrayUtils.GetGuidArrayFromCsv($"{GuidA},{GuidB}");
// Assert
Assert.That(result, Is.EqualTo(new[] { GuidA, GuidB }));
}
#endregion
#region Collection -> CSV
[Test]
public void GetCsvFromArrayList_JoinsWithCommas()
{
// Act
var result = ArrayUtils.GetCsvFromArrayList(new ArrayList { "a", "b", "c" });
// Assert
Assert.That(result, Is.EqualTo("a,b,c"));
}
[Test]
public void GetCsvFromGenericList_JoinsWithCommas()
{
// Act & Assert
Assert.That(ArrayUtils.GetCsvFromGenericList(new List<string> { "a", "b" }), Is.EqualTo("a,b"));
}
[Test]
public void GetCsvFromGenericList_SingleQuotes_WrapsEachValue()
{
// Act
var result = ArrayUtils.GetCsvFromGenericList(new List<string> { "a", "b" }, ArrayUtils.QuoteType.Single);
// Assert
Assert.That(result, Is.EqualTo("'a','b'"));
}
[Test]
public void GetCsvFromGenericList_DoubleQuotes_WrapsEachValue()
{
// Act
var result = ArrayUtils.GetCsvFromGenericList(new List<string> { "a", "b" }, ArrayUtils.QuoteType.Double);
// Assert
Assert.That(result, Is.EqualTo("\"a\",\"b\""));
}
[Test]
public void GetCsvFromGenericList_NoneQuoteType_BehavesLikePlainJoin()
{
// Act & Assert
Assert.That(ArrayUtils.GetCsvFromGenericList(new List<string> { "a", "b" }, ArrayUtils.QuoteType.None), Is.EqualTo("a,b"));
}
[Test]
public void GetCsvFromGenericListOfGuids_Null_ReturnsEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.GetCsvFromGenericListOfGuids(null!), Is.EqualTo(string.Empty));
}
[Test]
public void GetCsvFromGenericListOfGuids_NoQuotes_JoinsGuids()
{
// Act
var result = ArrayUtils.GetCsvFromGenericListOfGuids(new List<Guid> { GuidA, GuidB });
// Assert
Assert.That(result, Is.EqualTo($"{GuidA},{GuidB}"));
}
[Test]
public void GetCsvFromGenericListOfGuids_SingleQuotes_WrapsEachGuid()
{
// Act
var result = ArrayUtils.GetCsvFromGenericListOfGuids(new List<Guid> { GuidA }, ArrayUtils.QuoteType.Single);
// Assert
Assert.That(result, Is.EqualTo($"'{GuidA}'"));
}
#endregion
#region Encode
[Test]
public void Encode_NoInternalDelimiters_JoinsWithCommas()
{
// Act & Assert
Assert.That(ArrayUtils.Encode(new List<string> { "x", "y", "z" }), Is.EqualTo("x,y,z"));
}
[Test]
public void Encode_InternalComma_ReplacedWithIllegalCharacter()
{
// Arrange - internal commas are swapped for char(8) so they survive the comma join
const char illegal = (char)8;
// Act
var result = ArrayUtils.Encode(new List<string> { "a,b", "c" });
// Assert
Assert.That(result, Is.EqualTo($"a{illegal}b,c"));
}
#endregion
#region Guid List <-> String
[Test]
public void ConvertGuidStringToList_Empty_ReturnsEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.ConvertGuidStringToList(string.Empty), Is.Empty);
}
[Test]
public void ConvertGuidStringToList_ParsesAll()
{
// Act
var result = ArrayUtils.ConvertGuidStringToList($"{GuidA},{GuidB}");
// Assert
Assert.That(result, Is.EqualTo(new List<Guid> { GuidA, GuidB }));
}
[Test]
public void ConvertToGuidArray_ParsesStrings()
{
// Act
var result = ArrayUtils.ConvertToGuidArray(new[] { GuidA.ToString(), GuidB.ToString() });
// Assert
Assert.That(result, Is.EqualTo(new[] { GuidA, GuidB }));
}
[Test]
public void ConvertToGuidList_ParsesStrings()
{
// Act
var result = ArrayUtils.ConvertToGuidList(new[] { GuidA.ToString() });
// Assert
Assert.That(result, Is.EqualTo(new List<Guid> { GuidA }));
}
[Test]
public void ConvertGuidListToString_Null_ReturnsEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.ConvertGuidListToString(null!), Is.EqualTo(string.Empty));
}
[Test]
public void ConvertGuidListToString_NoSurround_JoinsWithCommas()
{
// Act & Assert
Assert.That(ArrayUtils.ConvertGuidListToString(new[] { GuidA, GuidB }), Is.EqualTo($"{GuidA},{GuidB}"));
}
[Test]
public void ConvertGuidListToString_WithSurroundChar_WrapsEachGuid()
{
// Act
var result = ArrayUtils.ConvertGuidListToString(new[] { GuidA }, "'");
// Assert
Assert.That(result, Is.EqualTo($"'{GuidA}'"));
}
[Test]
public void ConvertGuidListToStringList_ReturnsStringRepresentations()
{
// Act
var result = ArrayUtils.ConvertGuidListToStringList(new[] { GuidA, GuidB });
// Assert
Assert.That(result, Is.EqualTo(new List<string> { GuidA.ToString(), GuidB.ToString() }));
}
#endregion
#region Intersection
[Test]
public void GetGuidListsIntersection_ReturnsCommonGuids()
{
// Act
var result = ArrayUtils.GetGuidListsIntersection(
new List<Guid> { GuidA, GuidB },
new List<Guid> { GuidB, GuidC });
// Assert
Assert.That(result, Is.EqualTo(new List<Guid> { GuidB }));
}
[Test]
public void GetGuidListsIntersection_NoOverlap_ReturnsEmpty()
{
// Act
var result = ArrayUtils.GetGuidListsIntersection(
new List<Guid> { GuidA },
new List<Guid> { GuidB });
// Assert
Assert.That(result, Is.Empty);
}
[Test]
public void GetGuidListsIntersection_NullArguments_ReturnEmpty()
{
// Act & Assert
Assert.That(ArrayUtils.GetGuidListsIntersection(null!, null!), Is.Empty);
}
#endregion
}
@@ -0,0 +1,215 @@
using Strata.SqlTools.SqlBreakdown.Utilities;
namespace Strata.SqlTools.SqlBreakdown.Tests.Utilities;
[TestFixture]
public class GuidUtilsTests
{
private static readonly Guid Sample = new("0123abcd-4567-89ef-0123-456789abcdef");
#region TranslateGuid / UnTranslateGuid
[Test]
public void TranslateGuid_RemovesHyphensAndPrefixesWithG()
{
// Act
var result = GuidUtils.TranslateGuid(Sample);
Assert.Multiple(() =>
{
// Assert
Assert.That(result, Does.StartWith("g"));
Assert.That(result, Has.Length.EqualTo(33));
Assert.That(result, Does.Not.Contain("-"));
});
}
[Test]
public void UnTranslateGuid_RoundTripsTranslateGuid()
{
// Arrange
var translated = GuidUtils.TranslateGuid(Sample);
// Act & Assert
Assert.That(GuidUtils.UnTranslateGuid(translated), Is.EqualTo(Sample));
}
[Test]
public void UnTranslateGuid_AlreadyHyphenated_ParsesDirectly()
{
// Act & Assert
Assert.That(GuidUtils.UnTranslateGuid(Sample.ToString()), Is.EqualTo(Sample));
}
[TestCase("")] // null/empty guard
[TestCase("notprefixed")] // no hyphen, does not start with 'g'
[TestCase("gTOOSHORT")] // starts with 'g' but wrong length (not 33)
public void UnTranslateGuid_InvalidInputs_ReturnEmpty(string value)
{
// Act & Assert
Assert.That(GuidUtils.UnTranslateGuid(value), Is.EqualTo(Guid.Empty));
}
#endregion
#region SQL-safe column GUID
[Test]
public void GetSqlColumnSafeGuid_UppercasesAndReplacesHyphens()
{
// Act
var result = GuidUtils.GetSqlColumnSafeGuid(Sample);
Assert.Multiple(() =>
{
// Assert
Assert.That(result, Does.StartWith("G"));
Assert.That(result, Does.Not.Contain("-"));
Assert.That(result, Does.Contain("_"));
});
}
[Test]
public void GetGuidFromSqlColumnSafeGuid_RoundTrips()
{
// Arrange
var safe = GuidUtils.GetSqlColumnSafeGuid(Sample);
// Act & Assert
Assert.That(GuidUtils.GetGuidFromSqlColumnSafeGuid(safe), Is.EqualTo(Sample));
}
#endregion
#region TranslateTextToGuid
[Test]
public void TranslateTextToGuid_IsDeterministic()
{
// Act & Assert - same input must always yield the same GUID
Assert.That(GuidUtils.TranslateTextToGuid("hello world"), Is.EqualTo(GuidUtils.TranslateTextToGuid("hello world")));
}
[Test]
public void TranslateTextToGuid_EmptyInput_ReturnsEmpty()
{
// Act & Assert
Assert.That(GuidUtils.TranslateTextToGuid(string.Empty), Is.EqualTo(Guid.Empty));
}
[Test]
public void TranslateTextToGuid_DifferentInputs_GenerallyDiffer()
{
// Act & Assert
Assert.That(GuidUtils.TranslateTextToGuid("alpha"), Is.Not.EqualTo(GuidUtils.TranslateTextToGuid("beta")));
}
#endregion
#region Validation
[TestCase("0123abcd-4567-89ef-0123-456789abcdef", true)]
[TestCase("not a guid", false)]
[TestCase("", false)]
public void IsGuid_String_ValidatesCorrectly(string value, bool expected)
{
// Act & Assert
Assert.That(GuidUtils.IsGuid(value), Is.EqualTo(expected));
}
[Test]
public void IsGuid_Object_GuidInstance_ReturnsTrue()
{
// Act & Assert
Assert.That(GuidUtils.IsGuid((object)Sample), Is.True);
}
[Test]
public void IsGuid_Object_GuidString_ReturnsTrue()
{
// Act & Assert
Assert.That(GuidUtils.IsGuid((object)Sample.ToString()), Is.True);
}
[Test]
public void IsGuid_Object_NonGuid_ReturnsFalse()
{
// Act & Assert
Assert.That(GuidUtils.IsGuid((object)12345), Is.False);
}
#endregion
#region FindFirstGuid / GetGuids
[Test]
public void FindFirstGuid_EmbeddedGuid_ReturnsIt()
{
// Act
var result = GuidUtils.FindFirstGuid($"prefix {Sample} suffix");
// Assert
Assert.That(result, Is.EqualTo(Sample));
}
[Test]
public void FindFirstGuid_NoGuid_ReturnsEmpty()
{
// Act & Assert
Assert.That(GuidUtils.FindFirstGuid("nothing here"), Is.EqualTo(Guid.Empty));
}
[Test]
public void GetGuids_ReturnsAllEmbeddedGuids()
{
// Arrange
var second = new Guid("99999999-9999-9999-9999-999999999999");
// Act
var result = GuidUtils.GetGuids($"a {Sample} b {second} c");
// Assert
Assert.That(result, Has.Count.EqualTo(2));
}
#endregion
#region GetGuid
[Test]
public void GetGuid_ValidString_ParsesValue()
{
// Act & Assert
Assert.That(GuidUtils.GetGuid(Sample.ToString()), Is.EqualTo(Sample));
}
[Test]
public void GetGuid_InvalidString_ReturnsEmpty()
{
// Act & Assert
Assert.That(GuidUtils.GetGuid("nope"), Is.EqualTo(Guid.Empty));
}
[Test]
public void GetGuid_InvalidString_ReturnsProvidedDefault()
{
// Act & Assert
Assert.That(GuidUtils.GetGuid("nope", Sample), Is.EqualTo(Sample));
}
[Test]
public void GetGuid_NullObject_ReturnsEmpty()
{
// Act & Assert
Assert.That(GuidUtils.GetGuid((object)null!), Is.EqualTo(Guid.Empty));
}
[Test]
public void GetGuid_Object_ParsesToStringValue()
{
// Act & Assert
Assert.That(GuidUtils.GetGuid((object)Sample.ToString()), Is.EqualTo(Sample));
}
#endregion
}
@@ -0,0 +1,480 @@
using System.Data;
using Strata.SqlTools.SqlBreakdown.Enums.SQL;
using Strata.SqlTools.SqlBreakdown.Utilities;
namespace Strata.SqlTools.SqlBreakdown.Tests.Utilities;
[TestFixture]
public class SqlUtilsTests
{
#region String Manipulation
[Test]
public void StripColumnTableAlias_RemovesTableAliases()
{
// Act & Assert
Assert.That(SqlUtils.StripColumnTableAlias("a.Col1, b.Col2"), Is.EqualTo("Col1,Col2"));
}
[Test]
public void StripColumnTableAlias_NoAlias_LeavesColumnUntouched()
{
// Act & Assert
Assert.That(SqlUtils.StripColumnTableAlias("MyCol"), Is.EqualTo("MyCol"));
}
[TestCase("O'Brien", "O''Brien")]
[TestCase("", "")]
[TestCase("clean", "clean")]
public void EscapeInvalidCharacter_DoublesSingleQuotes(string input, string expected)
{
// Act & Assert
Assert.That(SqlUtils.EscapeInvalidCharacter(input), Is.EqualTo(expected));
}
[TestCase("Col", "[Col]")]
[TestCase("[Col]", "[Col]")]
[TestCase("[Col", "[Col]")]
[TestCase("Col]", "[Col]")]
public void AddBrackets_WrapsWhenMissing(string input, string expected)
{
// Act & Assert
Assert.That(SqlUtils.AddBrackets(input), Is.EqualTo(expected));
}
[Test]
public void RemoveBrackets_StripsSquareBrackets()
{
// Act & Assert
Assert.That(SqlUtils.RemoveBrackets("[Col]"), Is.EqualTo("Col"));
}
[Test]
public void GetSqlFriendlyName_RemovesInvalidCharactersByDefault()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlFriendlyName("a.b c"), Is.EqualTo("abc"));
}
[Test]
public void GetSqlFriendlyName_UsesReplacement()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlFriendlyName("a.b c", "_"), Is.EqualTo("a_b_c"));
}
#endregion
#region Object Name Handling
[Test]
public void GetSqlObjectExpression_BuildsBracketedExpression()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlObjectExpression("dbo", "MyTable"), Is.EqualTo("[dbo].[MyTable]"));
}
[Test]
public void GetSqlObjectExpression_StripsExistingBrackets()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlObjectExpression("[dbo]", "[MyTable]"), Is.EqualTo("[dbo].[MyTable]"));
}
[Test]
public void GetSqlObjectName_ExtractsObjectPortion()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlObjectName("[dbo].[MyTable]"), Is.EqualTo("MyTable"));
}
[Test]
public void GetSqlObjectName_NoDot_ReturnsInput()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlObjectName("MyTable"), Is.EqualTo("MyTable"));
}
[Test]
public void GetSqlObjectName_MultipleDots_Throws()
{
// Act & Assert
Assert.Throws<NotSupportedException>(() => SqlUtils.GetSqlObjectName("a.b.c"));
}
[Test]
public void GetSqlSchemaName_ExtractsSchema()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlSchemaName("data.DimClient"), Is.EqualTo("data"));
}
[Test]
public void GetSqlSchemaName_NoDot_ReturnsDbo()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlSchemaName("DimClient"), Is.EqualTo("dbo"));
}
[Test]
public void GetSqlSchemaName_MultipleDots_Throws()
{
// Act & Assert
Assert.Throws<NotSupportedException>(() => SqlUtils.GetSqlSchemaName("a.b.c"));
}
[TestCase("dbo", true)]
[TestCase("DBO", true)]
[TestCase("data", false)]
public void IsDefaultSchema_ChecksAgainstDbo(string schema, bool expected)
{
// Act & Assert
Assert.That(SqlUtils.IsDefaultSchema(schema), Is.EqualTo(expected));
}
#endregion
#region Data Type Conversions
[Test]
public void GetStringConversionLength_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetStringConversionLength(SqlDataType.Bit), Is.EqualTo(1));
Assert.That(SqlUtils.GetStringConversionLength(SqlDataType.Int), Is.EqualTo(int.MaxValue.ToString().Length));
Assert.That(SqlUtils.GetStringConversionLength(SqlDataType.UniqueIdentifier), Is.EqualTo(Guid.Empty.ToString().Length));
Assert.That(SqlUtils.GetStringConversionLength(SqlDataType.NVarChar), Is.EqualTo(SqlUtils.MAX_NVARCHAR_LENGTH));
});
}
[Test]
public void ConvertGuidToAlias_ProducesPrefixedUppercaseAlias()
{
// Arrange
var guid = new Guid("0123abcd-4567-89ef-0123-456789abcdef");
// Act
var result = SqlUtils.ConvertGuidToAlias(guid, "p");
Assert.Multiple(() =>
{
// Assert
Assert.That(result, Does.StartWith("p"));
Assert.That(result, Does.Not.Contain("-"));
Assert.That(result, Is.EqualTo("p0123ABCD456789EF0123456789ABCDEF"));
});
}
[Test]
public void IsFixedPrecision_DecimalIsNotFixed()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.IsFixedPrecision(SqlDataType.Decimal), Is.False);
Assert.That(SqlUtils.IsFixedPrecision(SqlDataType.Int), Is.True);
// IsFixedScale mirrors IsFixedPrecision
Assert.That(SqlUtils.IsFixedScale(SqlDataType.Decimal), Is.False);
Assert.That(SqlUtils.IsFixedScale(SqlDataType.Int), Is.True);
});
}
[Test]
public void IsFixedMaxLength_VariesByType()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.IsFixedMaxLength(SqlDataType.Int), Is.True);
Assert.That(SqlUtils.IsFixedMaxLength(SqlDataType.UniqueIdentifier), Is.True);
Assert.That(SqlUtils.IsFixedMaxLength(SqlDataType.VarChar), Is.False);
});
}
[Test]
public void GetFixedScale_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetFixedScale(SqlDataType.DateTime), Is.EqualTo(3));
Assert.That(SqlUtils.GetFixedScale(SqlDataType.Money), Is.EqualTo(4));
Assert.That(SqlUtils.GetFixedScale(SqlDataType.Int), Is.EqualTo(0));
});
}
[Test]
public void GetFixedScale_Decimal_Throws()
{
// Act & Assert
Assert.Throws<NotImplementedException>(() => SqlUtils.GetFixedScale(SqlDataType.Decimal));
}
[Test]
public void GetFixedPrecision_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetFixedPrecision(SqlDataType.BigInt), Is.EqualTo(19));
Assert.That(SqlUtils.GetFixedPrecision(SqlDataType.Int), Is.EqualTo(10));
Assert.That(SqlUtils.GetFixedPrecision(SqlDataType.Bit), Is.EqualTo(1));
});
}
[Test]
public void GetFixedMaxLength_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetFixedMaxLength(SqlDataType.Int), Is.EqualTo(4));
Assert.That(SqlUtils.GetFixedMaxLength(SqlDataType.UniqueIdentifier), Is.EqualTo(16));
Assert.That(SqlUtils.GetFixedMaxLength(SqlDataType.TinyInt), Is.EqualTo(1));
});
}
[Test]
public void GetFixedMaxLength_UnsupportedType_Throws()
{
// Act & Assert - VarChar has no fixed length
Assert.Throws<NotImplementedException>(() => SqlUtils.GetFixedMaxLength(SqlDataType.VarChar));
}
#endregion
#region Well-Known SQL Errors
[Test]
public void GetWellKnownSqlError_DuplicateKeyRow_IsUniqueKeyViolation()
{
// Arrange
var ex = new Exception("Cannot insert duplicate key row in object 'dbo.Users'.");
// Act & Assert
Assert.That(SqlUtils.GetWellKnownSqlError(ex), Is.EqualTo(SqlUtils.WellKnownSqlError.UniqueKeyViolation));
}
[Test]
public void GetWellKnownSqlError_ObjectAlreadyExists_IsDetected()
{
// Arrange
var ex = new Exception("There is already an object named 'Users' in the database.");
// Act & Assert
Assert.That(SqlUtils.GetWellKnownSqlError(ex), Is.EqualTo(SqlUtils.WellKnownSqlError.ObjectAlreadyExists));
}
[Test]
public void GetWellKnownSqlError_TruncateForeignKey_IsDetected()
{
// Arrange
var ex = new Exception("Cannot truncate table 'dbo.Orders' because it is being referenced by a FOREIGN KEY constraint.");
// Act & Assert
Assert.That(SqlUtils.GetWellKnownSqlError(ex), Is.EqualTo(SqlUtils.WellKnownSqlError.TruncateTableForeignKeyReferenceError));
}
[Test]
public void GetWellKnownSqlError_UnrecognizedMessage_IsUnknown()
{
// Act & Assert
Assert.That(SqlUtils.GetWellKnownSqlError(new Exception("Something else entirely")), Is.EqualTo(SqlUtils.WellKnownSqlError.Unknown));
}
#endregion
#region Data Type Helpers
[Test]
public void GetClientFriendlyDataTypeName_MapsCategories()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetClientFriendlyDataTypeName(SqlDataType.NVarChar), Is.EqualTo("Text"));
Assert.That(SqlUtils.GetClientFriendlyDataTypeName(SqlDataType.Int), Is.EqualTo("Whole Number"));
Assert.That(SqlUtils.GetClientFriendlyDataTypeName(SqlDataType.Decimal), Is.EqualTo("Decimal"));
Assert.That(SqlUtils.GetClientFriendlyDataTypeName(SqlDataType.DateTime), Is.EqualTo("Date"));
Assert.That(SqlUtils.GetClientFriendlyDataTypeName(SqlDataType.Money), Is.EqualTo("Dollars"));
});
}
[Test]
public void IdentityColumnTypes_ContainsIntegerTypes()
{
// Act
var types = SqlUtils.IdentityColumnTypes().ToList();
// Assert
Assert.That(types, Is.EquivalentTo(new[]
{
SqlDataType.BigInt, SqlDataType.TinyInt, SqlDataType.Int, SqlDataType.SmallInt
}));
}
[Test]
public void GetOrderBySqlDirectionString_MapsDirections()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetOrderBySqlDirectionString(SortDirection.Ascending), Is.EqualTo("ASC"));
Assert.That(SqlUtils.GetOrderBySqlDirectionString(SortDirection.Descending), Is.EqualTo("DESC"));
});
}
[Test]
public void GetSimpleDataType_MapsToCategories()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSimpleDataType(SqlDataType.Int), Is.EqualTo(SimpleDataType.Numeric));
Assert.That(SqlUtils.GetSimpleDataType(SqlDataType.DateTime), Is.EqualTo(SimpleDataType.Date));
Assert.That(SqlUtils.GetSimpleDataType(SqlDataType.Bit), Is.EqualTo(SimpleDataType.Boolean));
Assert.That(SqlUtils.GetSimpleDataType(SqlDataType.UniqueIdentifier), Is.EqualTo(SimpleDataType.GUID));
Assert.That(SqlUtils.GetSimpleDataType(SqlDataType.NVarChar), Is.EqualTo(SimpleDataType.String));
});
}
[TestCase("1", true)]
[TestCase("0", true)]
[TestCase("yes", true)]
[TestCase("no", true)]
[TestCase("true", true)]
[TestCase("maybe", false)]
public void IsStringValueValidForSimpleDataType_Boolean(string value, bool expected)
{
// Act & Assert
Assert.That(SqlUtils.IsStringValueValidForSimpleDataType(SimpleDataType.Boolean, value), Is.EqualTo(expected));
}
[TestCase("3.14", true)]
[TestCase("100", true)]
[TestCase("abc", false)]
public void IsStringValueValidForSimpleDataType_Numeric(string value, bool expected)
{
// Act & Assert
Assert.That(SqlUtils.IsStringValueValidForSimpleDataType(SimpleDataType.Numeric, value), Is.EqualTo(expected));
}
[Test]
public void IsStringValueValidForSimpleDataType_GuidAndString()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.IsStringValueValidForSimpleDataType(SimpleDataType.GUID, Guid.NewGuid().ToString()), Is.True);
Assert.That(SqlUtils.IsStringValueValidForSimpleDataType(SimpleDataType.GUID, "nope"), Is.False);
// String accepts anything
Assert.That(SqlUtils.IsStringValueValidForSimpleDataType(SimpleDataType.String, "anything"), Is.True);
});
}
[Test]
public void GetValueType_MapsToClrTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetValueType(SqlDataType.Bit), Is.EqualTo(typeof(bool)));
Assert.That(SqlUtils.GetValueType(SqlDataType.DateTime), Is.EqualTo(typeof(DateTime)));
Assert.That(SqlUtils.GetValueType(SqlDataType.Int), Is.EqualTo(typeof(double)));
Assert.That(SqlUtils.GetValueType(SqlDataType.UniqueIdentifier), Is.EqualTo(typeof(Guid)));
Assert.That(SqlUtils.GetValueType(SqlDataType.NVarChar), Is.EqualTo(typeof(string)));
});
}
[Test]
public void GetSqlDataType_MapsFromSqlDbType()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSqlDataType(SqlDbType.Int), Is.EqualTo(SqlDataType.Int));
Assert.That(SqlUtils.GetSqlDataType(SqlDbType.DateTime2), Is.EqualTo(SqlDataType.DateTime));
Assert.That(SqlUtils.GetSqlDataType(SqlDbType.UniqueIdentifier), Is.EqualTo(SqlDataType.UniqueIdentifier));
// Unmapped types fall back to NVarChar
Assert.That(SqlUtils.GetSqlDataType(SqlDbType.Variant), Is.EqualTo(SqlDataType.NVarChar));
});
}
#endregion
#region Default Values
[Test]
public void GetSqlDefaultValueDefinition_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSqlDefaultValueDefinition(SqlDataType.Int), Is.EqualTo("(0)"));
Assert.That(SqlUtils.GetSqlDefaultValueDefinition(SqlDataType.VarChar), Is.EqualTo("('')"));
Assert.That(SqlUtils.GetSqlDefaultValueDefinition(SqlDataType.DateTime), Is.EqualTo("(getdate())"));
Assert.That(SqlUtils.GetSqlDefaultValueDefinition(SqlDataType.UniqueIdentifier), Is.EqualTo($"('{Guid.Empty}')"));
});
}
[Test]
public void GetSqlZeroValue_KnownTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSqlZeroValue(SqlDataType.Int), Is.EqualTo(0));
Assert.That(SqlUtils.GetSqlZeroValue(SqlDataType.NVarChar), Is.EqualTo(string.Empty));
Assert.That(SqlUtils.GetSqlZeroValue(SqlDataType.DateTime), Is.EqualTo(new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)));
Assert.That(SqlUtils.GetSqlZeroValue(SqlDataType.UniqueIdentifier), Is.EqualTo(Guid.Empty));
});
}
[Test]
public void GetSqlDefaultValue_NullDefinition_ReturnsNull()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.Int, string.Empty), Is.Null);
}
[Test]
public void GetSqlDefaultValue_ParsesScalarTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.Int, "(0)"), Is.EqualTo(0));
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.BigInt, "(123)"), Is.EqualTo(123L));
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.Bit, "(1)"), Is.EqualTo(true));
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.Bit, "(0)"), Is.EqualTo(false));
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.NVarChar, "('hello')"), Is.EqualTo("hello"));
});
}
[Test]
public void GetSqlDefaultValue_UniqueIdentifierZeroGuid_ReturnsEmpty()
{
// Act & Assert
Assert.That(SqlUtils.GetSqlDefaultValue(SqlDataType.UniqueIdentifier, "(dbo.ZeroGUID())"), Is.EqualTo(Guid.Empty));
}
#endregion
#region Join / Connection Strings
[Test]
public void GetSqlJoinTypeString_MapsJoinTypes()
{
Assert.Multiple(() =>
{
Assert.That(SqlUtils.GetSqlJoinTypeString(SqlJoinType.Left), Is.EqualTo("LEFT"));
Assert.That(SqlUtils.GetSqlJoinTypeString(SqlJoinType.Right), Is.EqualTo("RIGHT"));
Assert.That(SqlUtils.GetSqlJoinTypeString(SqlJoinType.Inner), Is.EqualTo("INNER"));
Assert.That(SqlUtils.GetSqlJoinTypeString(SqlJoinType.Cross), Is.EqualTo("CROSS"));
});
}
[Test]
public void GetSqlConnectionString_BuildsIntegratedSecurityConnection()
{
// Act
var result = SqlUtils.GetSqlConnectionString("MyServer", "MyDb");
Assert.Multiple(() =>
{
// Assert
Assert.That(result, Does.Contain("data source=MyServer;"));
Assert.That(result, Does.Contain("initial catalog=MyDb;"));
Assert.That(result, Does.Contain("Integrated Security=SSPI;"));
});
}
#endregion
}
@@ -0,0 +1,39 @@
using Strata.SqlTools.SqlBreakdown.Utilities;
namespace Strata.SqlTools.SqlBreakdown.Tests.Utilities;
[TestFixture]
public class StringUtilsTests
{
[TestCase('a', -842352705)]
[TestCase('z', -842352730)]
[TestCase('A', -842352673)]
[TestCase('Z', -842352698)]
[TestCase('0', -842352768)]
[TestCase('9', -842352759)]
[TestCase(' ', -842352737)]
[TestCase('-', -842352782)]
public void GetHashCode32Bit_KnownCharacters_ReturnsHardcodedHash(char c, int expected)
{
// Act & Assert
Assert.That(StringUtils.GetHashCode32Bit(c), Is.EqualTo(expected));
}
[Test]
public void GetHashCode32Bit_UnmappedCharacter_UsesConsistentFallback()
{
// Arrange - '!' is not in the hardcoded table
const char c = '!';
var expected = -842352737 - (int)c;
// Act & Assert
Assert.That(StringUtils.GetHashCode32Bit(c), Is.EqualTo(expected));
}
[Test]
public void GetHashCode32Bit_SameCharacter_IsDeterministic()
{
// Act & Assert - critical: TranslateTextToGuid relies on stable hashing
Assert.That(StringUtils.GetHashCode32Bit('q'), Is.EqualTo(StringUtils.GetHashCode32Bit('q')));
}
}