using ClosedXML.Excel; using FluentAssertions; using Newtonsoft.Json; using NUnit.Framework; using Strata.Excel.TestUtilities; using System; using System.Collections.Generic; using System.IO; using System.Linq; using static Strata.Excel.Core.ExportUtils; using static Strata.Excel.Core.Test.Unit.ExcelExportTests.TestExcelExport; namespace Strata.Excel.Core.Test.Unit.ExcelLoadTests { [TestFixture] public class TestExcelLoad { [OneTimeSetUp] public void RunBeforeAnyTests() { Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory; // or identically under the hoods Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory); } private static readonly ResourceFileExtractor _dataExtractor = new ResourceFileExtractor(".ExcelExportTests."); [TestCaseSource(nameof(LoadTestCases))] [Ignore("need to look for alternative that can handle large data")] public void ExcelLoadTest(int load, int batchSize) { var jsonData = _dataExtractor.ReadFileFromResource("data.json"); var loadData = JsonConvert.DeserializeObject>(jsonData); var data = new List(); while (data.Count < load) { data.AddRange(loadData); } Action action = () => { var expectedLoad = data.Count + 1; // Add 1 to include the header row var options = new ExportOptions { EmptyMessage = "No Mappings", BatchSize = batchSize }; options.AddColumnOptions("Confidence Score", NumberFormatId.ZeroPercent, XLAlignmentHorizontalValues.Right); options.AddColumnOptions("Date Mapped", NumberFormatId.ShortDateSlash, XLAlignmentHorizontalValues.Right); var wb = CreateExcelWorkbook(data, options); #pragma warning disable S125 // Sections of code should not be commented out //var expected = $@"Excel Load Test {TestContext.CurrentContext.Test.Name}.xlsx"; //wb.SaveAs(Path.Combine(@"C:\Git\excel.core\tests\Strata.Excel.Core.Test.Unit\ExcelLoadTests\", expected)); #pragma warning restore S125 // Sections of code should not be commented // assert wb.Worksheets.Should().HaveCount(1); var ws = wb.Worksheet(1); ws.Should().NotBeNull(); var table = ws.Tables.First(); table.RowCount().Should().Be(expectedLoad); }; action.Should().NotThrow() .And.Subject.ExecutionTime().Should().BeLessThan(TimeSpan.FromMinutes(load / 3000)); } public static IEnumerable LoadTestCases() { var batchSize = 100000; for (var i = 48; i < 50; i += 2) { yield return new TestCaseData(i * 10000, batchSize).SetName($"{i}0000 rows with batches of {batchSize}"); } } } }