Skip to content

Commit

Permalink
Unit test for OutOfMemoryException #6
Browse files Browse the repository at this point in the history
  • Loading branch information
swidz committed Oct 31, 2023
1 parent 337dc1f commit d3c9bb3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Sql2Xls.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{818D0665-BB42-4419-8F4B-0AA990D6F7FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sql2Xls.Integration.Tests", "test\Sql2Xls.Integration.Tests\Sql2Xls.Integration.Tests.csproj", "{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,10 +24,17 @@ Global
{2325A3AD-C686-4841-86A5-3A280B8C5D40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2325A3AD-C686-4841-86A5-3A280B8C5D40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2325A3AD-C686-4841-86A5-3A280B8C5D40}.Release|Any CPU.Build.0 = Release|Any CPU
{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D54CD1D0-1E75-4272-BFD7-CCFAE7A4CC8B} = {818D0665-BB42-4419-8F4B-0AA990D6F7FF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {980973F3-FC04-4190-9F38-08919B3F51DB}
EndGlobalSection
Expand Down
10 changes: 8 additions & 2 deletions src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ protected SharedStringTablePart CreateSharedStringTablePart(SpreadsheetDocument
protected virtual SharedStringTable CreateSharedStringTable(
SpreadsheetDocument document, SharedStringTablePart sharedStringPart, Dictionary<string, SharedStringCacheItem> dict, int count)
{
SharedStringTable sharedStringTable = new SharedStringTable
var sharedStringTable = new SharedStringTable
{
UniqueCount = UInt32Value.FromUInt32((uint)dict.Count),
Count = UInt32Value.FromUInt32((uint)count)
Expand Down Expand Up @@ -700,7 +700,13 @@ protected virtual SharedStringTable CreateSharedStringTable(
var columnInfo = WorksheetColumns[colIndex];
if (!sharedStringsCache.ContainsKey(columnInfo.ColumnName))
{
sharedStringsCache.Add(columnInfo.ColumnName, new SharedStringCacheItem { Position = uniqueCount, Value = columnInfo.ColumnName });
sharedStringsCache.Add(columnInfo.ColumnName,
new SharedStringCacheItem
{
Position = uniqueCount,
Value = columnInfo.ColumnName
});

uniqueCount++;

var sharedStringItem = new SharedStringItem(new Text(columnInfo.ColumnName));
Expand Down
2 changes: 1 addition & 1 deletion src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private void CreateSheetDataSAX(OpenXmlWriter openXmlWriter, DataTable dataTable
{
CreateCellFromDataTypeSAX(openXmlWriter, colIndex, rowIndex, dsrow[colIndex]);
}
openXmlWriter.WriteEndElement();
openXmlWriter.WriteEndElement();
rowIndex++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql2Xls/Sql2Xls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0-beta0002" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0-beta0003" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
Expand Down
1 change: 1 addition & 0 deletions test/Sql2Xls.Integration.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
53 changes: 53 additions & 0 deletions test/Sql2Xls.Integration.Tests/Issue_0006_OutOfMemoryException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.Extensions.Logging.Abstractions;
using Sql2Xls.Excel;
using Sql2Xls.Excel.Adapters;
using System.Data;

namespace Sql2Xls.Integration.Tests;

[TestClass]
public class Issue_0006_OutOfMemoryException
{
private const string _CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

private string GetRandomString(Random rand, int len)
{
return new string(Enumerable.Repeat(_CHARS, len)
.Select(s => s[rand.Next(s.Length)]).ToArray());
}

[TestMethod]
[DataRow(154, 250_000, 20)]
public void GenerateLargeExcel(int numberOfColumns, int numberOfRows, int fieldlen)
{
Random rand = new Random();

DataTable dt = new DataTable("MyTable");
for (int j = 0; j < numberOfColumns; j++)
{
dt.Columns.Add($"Column{j}", typeof(string));
}

for (int i = 0; i < numberOfRows; i++)
{
var row = dt.NewRow();

for (int j = 0; j < numberOfColumns; j++)
{
row[$"Column{j}"] = GetRandomString(rand, fieldlen);
}

dt.Rows.Add(row);
}

var context = new ExcelExportContext()
{
FileName = "c:\\datamigration\\excel\\test.xlsx",
SheetName = "MyTable"
};

using var excelAdapter = new ExcelExportSAXAdapter(NullLogger<ExcelExportSAXAdapter>.Instance);
excelAdapter.Context = context;
excelAdapter.LoadFromDataTable(dt);
}
}
25 changes: 25 additions & 0 deletions test/Sql2Xls.Integration.Tests/Sql2Xls.Integration.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sql2Xls\Sql2Xls.csproj" />
</ItemGroup>

</Project>

0 comments on commit d3c9bb3

Please sign in to comment.