ProductStorageTests.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using ProductStore = qdr.app.studiou.orders2printpack.ProductStorage.ProductStorage;
  2. namespace qdr.app.studiou.orders2printpack.Test
  3. {
  4. /// <summary>
  5. /// Tests around the variant price behaviour when products are imported.
  6. /// Import creates product variants with Price = 0; the predefined variant
  7. /// prices (loaded from settings) must survive that, otherwise the user has
  8. /// to re-enter every price by hand after each import.
  9. /// </summary>
  10. [TestFixture]
  11. public class ProductStorageTests
  12. {
  13. // Mirrors how ImportProducts adds a variant: a parent product plus a
  14. // variation row whose VariantName matches a predefined variant, Price = 0.
  15. private static void SimulateImportedVariant(ProductStore storage, string parentSku, string variantName, decimal price = 0m)
  16. {
  17. var parent = storage.AddNewProduct();
  18. parent.Sku = parentSku;
  19. var variant = storage.AddNewProductVariant(parentSku);
  20. variant.VariantName = variantName;
  21. variant.Price = price;
  22. }
  23. [Test]
  24. public void Import_DoesNotResetPredefinedVariantPrice()
  25. {
  26. // Arrange: predefined variants with prices, as restored from settings.
  27. var storage = new ProductStore();
  28. storage.AppendVariantsFromString("Foto 10x15cm=150;Foto 15x23cm=120");
  29. // Act: import brings in a matching variant priced at 0.
  30. SimulateImportedVariant(storage, "P1", "Foto 10x15cm", price: 0m);
  31. var variants = storage.Variants.ToList();
  32. // Assert: the configured price is kept, not overwritten with 0.
  33. var imported = variants.Single(x => x.Name == "Foto 10x15cm");
  34. Assert.That(imported.Price, Is.EqualTo(150m));
  35. // The untouched predefined variant is unaffected too.
  36. var other = variants.Single(x => x.Name == "Foto 15x23cm");
  37. Assert.That(other.Price, Is.EqualTo(120m));
  38. }
  39. [Test]
  40. public void Import_PersistsPredefinedPriceAfterRoundTrip()
  41. {
  42. // Arrange
  43. var storage = new ProductStore();
  44. storage.AppendVariantsFromString("Foto 10x15cm=150");
  45. // Act: import (price 0) then serialise as settings would on form close.
  46. SimulateImportedVariant(storage, "P1", "Foto 10x15cm", price: 0m);
  47. _ = storage.Variants.ToList();
  48. var persisted = storage.GetVariantsAsString();
  49. // Assert: the price written back to settings is still 150, not 0.
  50. Assert.That(persisted, Does.Contain("Foto 10x15cm=150"));
  51. }
  52. [Test]
  53. public void Variants_StillSyncsRealNonZeroProductPrice()
  54. {
  55. // Guard against over-correcting: a genuine non-zero product price
  56. // must still update the variant, as it did before the fix.
  57. var storage = new ProductStore();
  58. storage.AppendVariantsFromString("Foto 10x15cm=150");
  59. SimulateImportedVariant(storage, "P1", "Foto 10x15cm", price: 99m);
  60. var imported = storage.Variants.Single(x => x.Name == "Foto 10x15cm");
  61. Assert.That(imported.Price, Is.EqualTo(99m));
  62. }
  63. [Test]
  64. public void Variants_AddsBrandNewVariantFromImport()
  65. {
  66. // A variant name that isn't predefined should still appear in the list.
  67. var storage = new ProductStore();
  68. storage.AppendVariantsFromString("Foto 10x15cm=150");
  69. SimulateImportedVariant(storage, "P1", "Plakát A2", price: 0m);
  70. var variants = storage.Variants.ToList();
  71. Assert.That(variants.Select(x => x.Name), Does.Contain("Plakát A2"));
  72. }
  73. }
  74. }