aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.Tests/CanWriteProducts.cs
blob: e6676cd0c62125c5635dc2e8167c7351f54cc5e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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()
    {
        Environment.SetEnvironmentVariable("ISTESTING", "true");
        _addIn = new();
        _addIn.AutoOpen();
        _reader = new ExcelReader(Util.Application, RhSolutionsAddIn.Configuration);
    }

    [ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecification.xlsx")]
    public void CanWriteSingle()
    {
        Worksheet sourceSheet = Util.Workbook.Worksheets[1];
        RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx"));
        var products = _reader.ReadProducts(new[] { sourceSheet });
        var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
        _writer.WriteProducts(products);
        Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
        var targetProducts = _reader.ReadProducts(new[] { targetSheet });

        Assert.Equal("TestSpecification", products.First().Item1);
        Assert.Equal("TargetSpecification", targetProducts.First().Item1);
        Assert.Equal(products.First().Item2.Count(), targetProducts.First().Item2.Count());
        Assert.Equal(products.First().Item2.Values.Sum(), targetProducts.First().Item2.Values.Sum());
    }

    [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 });
        var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
        _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);
        Assert.Equal(2, targetProducts.First().Item2.Count());
        Assert.True(Enumerable.SequenceEqual(products.First().Item2, targetProducts.First().Item2));
    }

    [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 });
        var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
        _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);
    }

    [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 });
        var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
        _writer.WriteProducts(products);
        Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
        var targetProducts = _reader.ReadProducts(new[] { targetSheet });

        Assert.Equal("TestSpecificationReplaced", products.First().Item1);
        Assert.Equal("TargetSpecificationReplaced", targetProducts.First().Item1);
        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);
    }

    [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 });
        var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
        _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);
        var result = targetProducts.First().Item2.ToArray();
        Assert.Contains("Молот Тора", result[0].Key.Name);
        Assert.Contains("11201111555", result[0].Key.Name);
        Assert.Equal(1, result[0].Value);
    }

    public void Dispose()
    {
        _addIn.AutoClose();
        Util.Application.ActiveWindow.Close(SaveChanges: false);
    }
}