Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramioh committed Oct 15, 2024
1 parent b9130eb commit 01b41c8
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/MiniExcel/ExcelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration co
switch (excelType)
{
case ExcelType.XLSX:
return new ExcelOpenXmlTemplate(stream, configuration);
var valueExtractor = new InputValueExtractor();
return new ExcelOpenXmlTemplate(stream, configuration, valueExtractor);
default:
throw new NotSupportedException($"Please Issue for me");
}
Expand Down
8 changes: 5 additions & 3 deletions src/MiniExcel/SaveByTemplate/ExcelOpenXmlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ static ExcelOpenXmlTemplate()

private readonly Stream _stream;
private readonly OpenXmlConfiguration _configuration;
private readonly IInputValueExtractor _inputValueExtractor;
private readonly StringBuilder _calcChainContent = new StringBuilder();

public ExcelOpenXmlTemplate(Stream stream, IConfiguration configuration)
public ExcelOpenXmlTemplate(Stream stream, IConfiguration configuration, InputValueExtractor inputValueExtractor)
{
_stream = stream;
_configuration = (OpenXmlConfiguration)configuration ?? OpenXmlConfiguration.DefaultConfig;
_inputValueExtractor = inputValueExtractor;
}

public void SaveAsByTemplate(string templatePath, object value)
Expand Down Expand Up @@ -77,11 +79,11 @@ public void SaveAsByTemplateImpl(Stream templateStream, object value)
var sheetStream = sheet.Open();
var fullName = sheet.FullName;

var inputValues = _inputValueExtractor.ToValueDictionary(value);
ZipArchiveEntry entry = _archive.zipFile.CreateEntry(fullName);
using (var zipStream = entry.Open())
{
GenerateSheetXmlImpl(sheet, zipStream, sheetStream,
InputValueExtractor.ToValueDictionary(value), sharedStrings, false);
GenerateSheetXmlImpl(sheet, zipStream, sheetStream, inputValues, sharedStrings, false);
//doc.Save(zipStream); //don't do it because : ![image](https://user-images.githubusercontent.com/12729184/114361127-61a5d100-9ba8-11eb-9bb9-34f076ee28a2.png)
}

Expand Down
9 changes: 9 additions & 0 deletions src/MiniExcel/SaveByTemplate/IInputValueExtractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace MiniExcelLibs.OpenXml.SaveByTemplate
{
public interface IInputValueExtractor
{
IDictionary<string, object> ToValueDictionary(object valueObject);
}
}
4 changes: 2 additions & 2 deletions src/MiniExcel/SaveByTemplate/InputValueExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace MiniExcelLibs.OpenXml.SaveByTemplate
{
public static class InputValueExtractor
public class InputValueExtractor : IInputValueExtractor
{
public static IDictionary<string, object> ToValueDictionary(object valueObject)
public IDictionary<string, object> ToValueDictionary(object valueObject)
=> valueObject is Dictionary<string, object> valueDictionary
? GetValuesFromDictionary(valueDictionary)
: GetValuesFromObject(valueObject);
Expand Down
1 change: 1 addition & 0 deletions tests/MiniExcelTests/MiniExcelTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="EPPlus" Version="4.5.3.3" />
<PackageReference Include="ExcelDataReader" Version="3.6.0" />
<PackageReference Include="ExcelDataReader.DataSet" Version="3.6.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NPOI" Version="2.7.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
Expand Down
104 changes: 104 additions & 0 deletions tests/MiniExcelTests/SaveByTemplate/InputValueExtractorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.Data;
using FluentAssertions;
using MiniExcelLibs.OpenXml.SaveByTemplate;
using Xunit;

namespace MiniExcelTests.SaveByTemplate;

public class InputValueExtractorTests
{
[Fact]
public void ToValueDictionary_Given_InputIsDictionaryWithoutDataReader_Then_Output_IsAnEquivalentDictionary()
{
var valueDictionary = new Dictionary<string, object>
{
["Name"] = "John",
["Age"] = 18,
["Fruits"] = new List<string> { "Apples, Oranges" },
};

var sut = new InputValueExtractor();
var result = sut.ToValueDictionary(valueDictionary);

result.Should().BeEquivalentTo(valueDictionary);
}

[Fact]
public void ToValueDictionary_Given_InputIsDictionaryWithDataReader_Then_DataReaderIsConvertedToListOfDictionaries()
{
var dataTable = new DataTable();

dataTable.Columns.Add("id", typeof(int));
dataTable.Columns.Add("name", typeof(string));
dataTable.Rows.Add(1, "Jack");
dataTable.Rows.Add(2, "Mike");

var expectedOutput = new List<Dictionary<string, object>>
{
new() { ["id"] = 1, ["name"] = "Jack" },
new() { ["id"] = 2, ["name"] = "Mike" }
};

var valueDictionary = new Dictionary<string, object>
{
["DataReader"] = dataTable.CreateDataReader()
};

var sut = new InputValueExtractor();
var result = sut.ToValueDictionary(valueDictionary);

result["DataReader"].Should().BeEquivalentTo(expectedOutput);
}

[Fact]
public void ToValueDictionary_Given_InputIsPocoRecord_Then_Output_IsAnEquivalentDictionary()
{
var valueObject = new PocoRecord("John", 18, new List<string> { "Apples, Oranges" });

var expectedOutput = new Dictionary<string, object>()
{
["Name"] = "John",
["Age"] = 18,
["Fruits"] = new List<string> { "Apples, Oranges" }
};

var sut = new InputValueExtractor();
var result = sut.ToValueDictionary(valueObject);

result.Should().BeEquivalentTo(expectedOutput);
}

[Fact]
public void ToValueDictionary_Given_InputIsPocoClass_Then_Output_IsAnEquivalentDictionary()
{
var valueObject = new PocoClass
{
Name = "John",
Age = 18,
Fruits = new List<string> { "Apples, Oranges" }
};

var expectedOutput = new Dictionary<string, object>()
{
["Name"] = "John",
["Age"] = 18,
["Fruits"] = new List<string> { "Apples, Oranges" }
};

var sut = new InputValueExtractor();
var result = sut.ToValueDictionary(valueObject);

result.Should().BeEquivalentTo(expectedOutput);
}


private record PocoRecord(string Name, int Age, IEnumerable<string> Fruits);

private class PocoClass
{
public string Name { get; set; }
public int Age { get; set; }
public IEnumerable<string> Fruits; // Field
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Threading.Tasks;
using Xunit;

namespace MiniExcelTests
namespace MiniExcelTests.SaveByTemplate
{
public class MiniExcelTemplateAsyncTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading.Tasks;
using Xunit;

namespace MiniExcelTests
namespace MiniExcelTests.SaveByTemplate
{
public class MiniExcelTemplateTests
{
Expand Down

0 comments on commit 01b41c8

Please sign in to comment.