From f01228d94554669146137dea9614d87df22c01f3 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 31 Mar 2023 15:27:31 +0300 Subject: Add Excel Table classes --- RhSolutions.Tests/ExcelTablesTests.cs | 65 +++++++++++++++++++++ RhSolutions.Tests/RhSolutions.Tests.csproj | 3 + RhSolutions.Tests/RhSolutionsCheckTest.cs | 29 --------- .../TestWorkbooks/ExcelTableTest.xlsx | Bin 0 -> 8987 bytes RhSolutions.Tests/Usings.cs | 2 +- RhSolutions.Tests/WorkbookValidationTests.cs | 29 +++++++++ 6 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 RhSolutions.Tests/ExcelTablesTests.cs delete mode 100644 RhSolutions.Tests/RhSolutionsCheckTest.cs create mode 100644 RhSolutions.Tests/TestWorkbooks/ExcelTableTest.xlsx create mode 100644 RhSolutions.Tests/WorkbookValidationTests.cs (limited to 'RhSolutions.Tests') diff --git a/RhSolutions.Tests/ExcelTablesTests.cs b/RhSolutions.Tests/ExcelTablesTests.cs new file mode 100644 index 0000000..3ce71bc --- /dev/null +++ b/RhSolutions.Tests/ExcelTablesTests.cs @@ -0,0 +1,65 @@ +namespace RhSolutions.Tests; + +[ExcelTestSettings(OutOfProcess = true, Workbook = @"TestWorkbooks\ExcelTableTest.xlsx")] +public class ExcelTablesTests : IDisposable +{ + ExcelTable.ExcelTable table; + + public ExcelTablesTests() + { + Util.Application.Workbooks.Add(); + + Worksheet worksheet = Util.Workbook.Sheets[1]; + Range range = worksheet.Range["E7:G9"]; + table = new(range); + } + + [ExcelFact] + public void CanReadExcelTable() + { + Assert.Equal(3, table.Columns.Where(c => c.Header.StartsWith("Столбец")).Count()); + Assert.Equal("Столбец 1", table.Rows.First()[0].Value); + Assert.Equal("Столбец 2", table[0, 1].Value); + Assert.Equal(123d, table[1, 1].Value); + Assert.Null(table[1, 2].Value); + } + + [ExcelFact] + public void CanModifyTableCells() + { + table[2, 1].Value = 1; + table[1, 1].Value = (double)table[1, 1].Value + 123; + Assert.Equal(1d, table[2, 1].Value); + Assert.Equal(246d, table[1, 1].Value); + } + + [ExcelFact] + public void CanFindInTable() + { + table[2, 0].Value = "Find!"; + table[2, 1].Value = "Find this!"; + table[2, 2].Value = "Find that!"; + + + var cells = table.Find("Find"); + Assert.Collection(cells, item => Assert.Equal("Find!", item.Value), + item => Assert.Equal("Find this!", item.Value), + item => Assert.Equal("Find that!", item.Value)); + Assert.Equal(0, table.Find("Значение").First().ParentColumn.Index); + Assert.Equal(3, table.Rows[2].Find("Find").Count()); + Assert.Empty(table.Columns[1].Find("Пусто")); + } + + [ExcelFact] + public void CanAddColumns() + { + int originalCount = table.Columns.Count(); + table.Columns[1].AddLeft(); + Assert.Equal(originalCount + 1, table.Columns.Count()); + } + + public void Dispose() + { + Util.Application.ActiveWindow.Close(SaveChanges: false); + } +} diff --git a/RhSolutions.Tests/RhSolutions.Tests.csproj b/RhSolutions.Tests/RhSolutions.Tests.csproj index aeb9c9e..9410802 100644 --- a/RhSolutions.Tests/RhSolutions.Tests.csproj +++ b/RhSolutions.Tests/RhSolutions.Tests.csproj @@ -24,6 +24,9 @@ PreserveNewest + + PreserveNewest + diff --git a/RhSolutions.Tests/RhSolutionsCheckTest.cs b/RhSolutions.Tests/RhSolutionsCheckTest.cs deleted file mode 100644 index f4d1317..0000000 --- a/RhSolutions.Tests/RhSolutionsCheckTest.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace RhSolutions.Tests; - -[ExcelTestSettings(OutOfProcess = true)] -public class WorkbookCheck : IDisposable -{ - public WorkbookCheck() - { - Util.Application.Workbooks.Add(); - } - - [ExcelFact(Workbook = @"TestWorkbooks\EmptyTestTable.xlsx")] - public void WorksheetIsCorrect() - { - Worksheet worksheet= Util.Workbook.Sheets[1]; - Assert.True(worksheet.IsRehauSource()); - } - - [ExcelFact(Workbook = @"TestWorkbooks\EmptyWorkbook.xlsx")] - public void EmptyWorkbookIsNotCorrect() - { - Worksheet worksheet = Util.Workbook.Sheets[1]; - Assert.False(worksheet.IsRehauSource()); - } - - public void Dispose() - { - Util.Application.ActiveWorkbook.Close(SaveChanges: false); - } -} diff --git a/RhSolutions.Tests/TestWorkbooks/ExcelTableTest.xlsx b/RhSolutions.Tests/TestWorkbooks/ExcelTableTest.xlsx new file mode 100644 index 0000000..da4cf0b Binary files /dev/null and b/RhSolutions.Tests/TestWorkbooks/ExcelTableTest.xlsx differ diff --git a/RhSolutions.Tests/Usings.cs b/RhSolutions.Tests/Usings.cs index 74130ca..4b95659 100644 --- a/RhSolutions.Tests/Usings.cs +++ b/RhSolutions.Tests/Usings.cs @@ -1,5 +1,5 @@ global using Xunit; global using Microsoft.Office.Interop.Excel; global using ExcelDna.Testing; -global using RhSolutions.Models; +global using RhSolutions.ExcelTable; global using RhSolutions.Services; diff --git a/RhSolutions.Tests/WorkbookValidationTests.cs b/RhSolutions.Tests/WorkbookValidationTests.cs new file mode 100644 index 0000000..aec7139 --- /dev/null +++ b/RhSolutions.Tests/WorkbookValidationTests.cs @@ -0,0 +1,29 @@ +namespace RhSolutions.Tests; + +[ExcelTestSettings(OutOfProcess = true)] +public class WorkbookValidationTests : IDisposable +{ + public WorkbookValidationTests() + { + Util.Application.Workbooks.Add(); + } + + [ExcelFact(Workbook = @"TestWorkbooks\EmptyTestTable.xlsx")] + public void WorksheetIsCorrect() + { + Worksheet worksheet = Util.Workbook.Sheets[1]; + Assert.True(worksheet.IsRehauSource()); + } + + [ExcelFact(Workbook = @"TestWorkbooks\EmptyWorkbook.xlsx")] + public void EmptyWorkbookIsNotCorrect() + { + Worksheet worksheet = Util.Workbook.Sheets[1]; + Assert.False(worksheet.IsRehauSource()); + } + + public void Dispose() + { + Util.Application.ActiveWindow.Close(SaveChanges: false); + } +} \ No newline at end of file -- cgit v1.2.3