RhSolutions-AddIn/RhSolutions.Tests/CanWriteProducts.cs

116 lines
5.7 KiB
C#
Raw Normal View History

2023-05-18 07:15:52 +03:00
using Microsoft.Extensions.DependencyInjection;
using RhSolutions.AddIn;
using System.IO;
namespace RhSolutions.Tests;
[ExcelTestSettings(OutOfProcess = true)]
public class CanWriteProducts : IDisposable
{
private RhSolutionsAddIn _addIn;
private IReader _reader;
public CanWriteProducts()
{
_addIn = new();
_addIn.AutoOpen();
2023-05-23 07:01:33 +03:00
_reader = new ExcelReader(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-18 07:15:52 +03:00
}
2023-05-21 10:24:16 +03:00
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecification.xlsx")]
2023-05-18 07:23:59 +03:00
public void CanWriteSingle()
2023-05-18 07:15:52 +03:00
{
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
2023-05-18 07:23:59 +03:00
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx"));
2023-05-18 07:15:52 +03:00
var products = _reader.ReadProducts(new[] { sourceSheet });
2023-06-20 07:25:44 +03:00
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-18 07:15:52 +03:00
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
2023-05-18 07:23:59 +03:00
Assert.Equal("TestSpecification", products.First().Item1);
Assert.Equal("TargetSpecification", targetProducts.First().Item1);
2023-05-18 07:15:52 +03:00
Assert.Equal(products.First().Item2.Count(), targetProducts.First().Item2.Count());
2023-05-18 07:23:59 +03:00
Assert.Equal(products.First().Item2.Values.Sum(), targetProducts.First().Item2.Values.Sum());
2023-05-18 07:15:52 +03:00
}
2023-05-21 10:24:16 +03:00
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationMultipleProductLines.xlsx")]
public void CanWriteMultipleProductLines()
{
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationMultipleProductLines.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
2023-06-20 07:25:44 +03:00
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-21 10:24:16 +03:00
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
Assert.Equal("TestSpecificationMultipleProductLines", products.First().Item1);
Assert.Equal("TargetSpecificationMultipleProductLines", targetProducts.First().Item1);
2023-05-21 10:26:02 +03:00
Assert.Equal(2, targetProducts.First().Item2.Count());
2023-05-21 10:24:16 +03:00
Assert.True(Enumerable.SequenceEqual(products.First().Item2, targetProducts.First().Item2));
}
2023-05-21 15:13:17 +03:00
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationNotFound.xlsx")]
public void CanWriteNotFound()
{
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNotFound.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
2023-06-20 07:25:44 +03:00
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-21 15:13:17 +03:00
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
Assert.Equal("???", targetSheet.Range["B4"].Value2);
Assert.Contains("Молот Тора", targetSheet.Range["C4"].Value2);
Assert.Contains("15555551555", targetSheet.Range["C4"].Value2);
}
2023-05-21 15:17:49 +03:00
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationReplaced.xlsx")]
public void CanWriteReplaced()
{
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationReplaced.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
2023-06-20 07:25:44 +03:00
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-21 15:17:49 +03:00
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
2023-05-21 15:13:17 +03:00
2023-05-21 15:17:49 +03:00
Assert.Equal("TestSpecificationReplaced", products.First().Item1);
Assert.Equal("TargetSpecificationReplaced", targetProducts.First().Item1);
2023-05-27 07:37:36 +03:00
var result = targetProducts.First().Item2.ToArray();
Assert.Contains("Молот Тора", result[0].Key.Name);
Assert.Contains("15555551555", result[0].Key.Name);
Assert.Equal(1, result[0].Value);
Assert.Contains("Нога Вирта", result[1].Key.Name);
Assert.Contains("17777771777", result[1].Key.Name);
Assert.Equal(1, result[1].Value);
2023-05-21 15:17:49 +03:00
}
2023-05-21 15:13:17 +03:00
2023-05-21 15:21:20 +03:00
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationNewVariant.xlsx")]
public void CanWriteNewVariant()
{
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNewVariant.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
2023-06-20 07:25:44 +03:00
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
2023-05-21 15:21:20 +03:00
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
Assert.Equal("TestSpecificationNewVariant", products.First().Item1);
Assert.Equal("TargetSpecificationNewVariant", targetProducts.First().Item1);
2023-05-27 07:37:36 +03:00
var result = targetProducts.First().Item2.ToArray();
Assert.Contains("Молот Тора", result[0].Key.Name);
2023-05-27 08:17:36 +03:00
Assert.Contains("11201111555", result[0].Key.Name);
2023-05-27 07:37:36 +03:00
Assert.Equal(1, result[0].Value);
2023-05-21 15:21:20 +03:00
}
2023-05-18 07:15:52 +03:00
public void Dispose()
{
_addIn.AutoClose();
Util.Application.ActiveWindow.Close(SaveChanges: false);
}
}