finskayMagazin/TestTovarV2/EditTovar.cs
2025-06-05 11:22:50 +03:00

155 lines
5.2 KiB
C#

using Avalonia;
using Avalonia.Headless;
using Avalonia.Controls;
using TovarV2;
using Avalonia.Media.Imaging;
using System.IO;
namespace TestTovarV2
{
[TestFixture]
public class EditTovarTests
{
private EditTovar _editTovar;
[OneTimeSetUp]
public void OneTimeSetup()
{
AppBuilder.Configure<App>()
.UseHeadless(new AvaloniaHeadlessPlatformOptions())
.SetupWithoutStarting();
}
[SetUp]
public void Setup()
{
ListPr.ListProd = new List<Product>
{
new Product
{
Id = 0,
nameProd = "Test Product",
priceProd = 100,
quantityProd = 10,
bitmapProd = new Bitmap(Path.Combine("Assets", "test_image.png"))
}
};
ListPr.productForEdit = 0;
_editTovar = new EditTovar();
_editTovar.Show();
}
[TearDown]
public void TearDown()
{
_editTovar.Close();
ListPr.ListProd.Clear();
}
[Test]
public void EditOkClick_ShouldUpdateProductInfo_WhenFieldsAreValid()
{
_editTovar.GetNameTov.Text = "Updated Product";
_editTovar.GetPriceTov.Text = "200";
_editTovar.GetQuantityTov.Text = "20";
var testBitmap = new Bitmap(Path.Combine("Assets", "test_image_updated.png"));
_editTovar.bitmapToBind = testBitmap;
_editTovar.EditOk_Click(null, null);
Assert.That(ListPr.ListProd[0].nameProd, Is.EqualTo("Updated Product"));
Assert.That(ListPr.ListProd[0].priceProd, Is.EqualTo(200));
Assert.That(ListPr.ListProd[0].quantityProd, Is.EqualTo(20));
Assert.That(ListPr.ListProd[0].bitmapProd, Is.EqualTo(testBitmap));
Assert.That(ProductEdit.IsOpen, Is.True);
}
[Test]
public void EditOkClick_ShouldNotCrash_WhenImageNotChanged()
{
_editTovar.GetNameTov.Text = "Updated Product";
_editTovar.GetPriceTov.Text = "200";
_editTovar.GetQuantityTov.Text = "20";
_editTovar.bitmapToBind = null;
_editTovar.EditOk_Click(null, null);
Assert.That(ListPr.ListProd[0].bitmapProd, Is.Not.Null);
}
[Test]
public void AddTovarImgClick_ShouldUpdateImage_WhenFileSelected()
{
var testBitmap = new Bitmap(Path.Combine("Assets", "test_image_updated.png"));
_editTovar.bitmapToBind = testBitmap;
_editTovar.AddTovarImg_Click(null, null);
Assert.That(_editTovar.GetImagePath.Source, Is.EqualTo(testBitmap));
}
[Test]
public void EditOk_Click_ShouldUpdateOnlyName_WhenOneFieldIsFilled()
{
var originalPrice = ListPr.ListProd[0].priceProd;
_editTovar.GetNameTov.Text = "New Name Only";
_editTovar.EditOk_Click(null, null);
Assert.That(ListPr.ListProd[0].nameProd, Is.EqualTo("New Name Only"));
Assert.That(ListPr.ListProd[0].priceProd, Is.EqualTo(originalPrice));
}
[Test]
public void EditOkClick_ShouldCorrectlyUpdateMultipleProducts_WhenAddSecondProduct()
{
ListPr.ListProd.Add(new Product
{
Id = 1,
nameProd = "Second Product",
priceProd = 200,
quantityProd = 20,
bitmapProd = new Bitmap(Path.Combine("Assets", "test_image2.png"))
});
ListPr.productForEdit = 1;
var secondEdit = new EditTovar();
secondEdit.GetNameTov.Text = "Updated Second";
secondEdit.GetPriceTov.Text = "300";
secondEdit.GetQuantityTov.Text = "30";
secondEdit.EditOk_Click(null, null);
Assert.That(ListPr.ListProd[1].nameProd, Is.EqualTo("Updated Second"));
Assert.That(ListPr.ListProd[0].nameProd, Is.EqualTo("Test Product"));
}
[Test]
public void EditOkClick_ShouldHandleNegativePrice_WhenPriceIsNegative()
{
_editTovar.GetPriceTov.Text = "-100";
_editTovar.EditOk_Click(null, null);
Assert.That(ListPr.ListProd[0].priceProd, Is.EqualTo(-100));
}
[Test]
public void EditOkClick_ShouldHandleEmptyFields_WhenFieldsIsEmpty()
{
_editTovar.GetNameTov.Text = "";
_editTovar.GetPriceTov.Text = "";
_editTovar.GetQuantityTov.Text = "";
Assert.DoesNotThrow(() => _editTovar.EditOk_Click(null, null));
Assert.That(ListPr.ListProd[0].nameProd, Is.Not.EqualTo(""));
Assert.That(ListPr.ListProd[0].priceProd, Is.Not.EqualTo(0));
Assert.That(ListPr.ListProd[0].quantityProd, Is.Not.EqualTo(0));
}
}
}