using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using SyncEngine.MySql; namespace SyncEngine.Tests { [TestClass] public class ProductFieldMapperTests { private const int SampleProductId = 12345; [TestMethod] public void MakeSlug_FormatsCodeAndDescription() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "100482", "Red Flapper Dress"); Assert.AreEqual("12345-100482-red-flapper-dress", slug); } [TestMethod] public void MakeSlug_HandlesEmptyDescription() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "100482", ""); Assert.AreEqual("12345-100482-", slug); } [TestMethod] public void MakeSlug_HandlesEmptyCode_KeepsDoubleHyphen() { var slug = ProductFieldMapper.MakeSlug(100482, "", "Red Flapper Dress"); Assert.AreEqual("100482--red-flapper-dress", slug); } [TestMethod] public void MakeSlug_LowercasesCodePrefix() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "ABC123", "Dress"); Assert.AreEqual("12345-abc123-dress", slug); } [TestMethod] public void UrlTitle_StripsPunctuationAndLowercases() { var title = ProductFieldMapper.UrlTitle("Hello, World! 2024"); Assert.AreEqual("hello-world-2024", title); } [TestMethod] public void UrlTitle_TransliteratesAccents() { var title = ProductFieldMapper.UrlTitle("Café & Co. (Special!)"); Assert.AreEqual("cafe-co-special", title); } [TestMethod] public void MakeSlug_SlugifiesSlashesInCode() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "100/482", "Red Dress"); Assert.AreEqual("12345-100482-red-dress", slug); } [TestMethod] public void MakeSlug_SlugifiesComplexCodePrefix() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "A/FIT-O/TIME WK-HR1A", "Wardrobe Fitting"); Assert.AreEqual("12345-afit-otime-wk-hr1a-wardrobe-fitting", slug); } [TestMethod] public void MakeSlug_StripsAsterisksFromCode() { var slug = ProductFieldMapper.MakeSlug(SampleProductId, "***_tmp_100000", "Pink Bandanas"); Assert.AreEqual("12345-_tmp_100000-pink-bandanas", slug); } [TestMethod] public void ToPrice_DoubleFloatDrift_RoundsToTwoDecimals() { var price = ProductFieldMapper.ToPrice(12.499999999999d); Assert.IsNotNull(price); Assert.AreEqual(12.50m, price.Value); } [TestMethod] public void ToPrice_FloatValue_RoundsToTwoDecimals() { var price = ProductFieldMapper.ToPrice(19.995f); Assert.IsNotNull(price); Assert.AreEqual(20.00m, price.Value); } [TestMethod] public void ToPrice_NullOrDbNull_ReturnsNull() { Assert.IsNull(ProductFieldMapper.ToPrice(null)); Assert.IsNull(ProductFieldMapper.ToPrice(DBNull.Value)); } [TestMethod] public void ToPrice_UnroundedDecimal_MatchesMysqlDecimalColumn() { var unrounded = ProductFieldMapper.ToPrice(12.5d); var fromMysql = ProductFieldMapper.ToPrice(12.50m); Assert.AreEqual(unrounded, fromMysql); } [TestMethod] public void NormalizeOptionalString_WhitespaceBecomesNull() { Assert.IsNull(ProductFieldMapper.NormalizeOptionalString(null)); Assert.IsNull(ProductFieldMapper.NormalizeOptionalString("")); Assert.IsNull(ProductFieldMapper.NormalizeOptionalString(" ")); } [TestMethod] public void NormalizeOptionalString_TrimsValue() { Assert.AreEqual("Dress", ProductFieldMapper.NormalizeOptionalString(" Dress ")); } [TestMethod] public void TrimRequiredString_TrimsButKeepsEmpty() { Assert.AreEqual(string.Empty, ProductFieldMapper.TrimRequiredString(" ")); Assert.AreEqual("Red Dress", ProductFieldMapper.TrimRequiredString(" Red Dress ")); } [TestMethod] public void ToBool_NullOrDbNull_ReturnsFalse() { Assert.IsFalse(ProductFieldMapper.ToBool(null)); Assert.IsFalse(ProductFieldMapper.ToBool(DBNull.Value)); } [TestMethod] public void ToBool_ZeroAndFalse_ReturnsFalse() { Assert.IsFalse(ProductFieldMapper.ToBool(false)); Assert.IsFalse(ProductFieldMapper.ToBool(0)); Assert.IsFalse(ProductFieldMapper.ToBool(0L)); Assert.IsFalse(ProductFieldMapper.ToBool((byte)0)); Assert.IsFalse(ProductFieldMapper.ToBool("0")); Assert.IsFalse(ProductFieldMapper.ToBool("false")); } [TestMethod] public void ToBool_TruthyIncludingAccessYes_ReturnsTrue() { Assert.IsTrue(ProductFieldMapper.ToBool(true)); Assert.IsTrue(ProductFieldMapper.ToBool(1)); Assert.IsTrue(ProductFieldMapper.ToBool(-1)); Assert.IsTrue(ProductFieldMapper.ToBool((byte)1)); Assert.IsTrue(ProductFieldMapper.ToBool("1")); Assert.IsTrue(ProductFieldMapper.ToBool("true")); } } }