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 }