Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecole committed Oct 2, 2016
1 parent 3a9c601 commit 513e81a
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 21 deletions.
61 changes: 42 additions & 19 deletions src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>2ccc31a1-9647-43ec-8179-acc7ab005ff2</ProjectGuid>
<ProjectGuid>{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EPPlus.Extensions.Tests</RootNamespace>
Expand All @@ -30,25 +30,49 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>

<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>


<Reference Include="Microsoft.CSharp"/>

<Reference Include="System.Data"/>

<Reference Include="System.Net.Http"/>

<Reference Include="System.Xml"/>
<Reference Include="EPPlus, Version=3.1.3.0, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
<HintPath>..\packages\EPPlus.3.1.3.3\lib\net35\EPPlus.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Fixie, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Fixie.1.0.2\lib\net45\Fixie.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Shouldly, Version=2.8.2.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
<HintPath>..\packages\Shouldly.2.8.2\lib\net451\Shouldly.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="ExtensionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Marvel.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="States.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EPPlus.Extensions\EPPlus.Extensions.csproj">
<Project>{03756be7-6cc0-4d3e-80cf-3f7db49bc77a}</Project>
<Name>EPPlus.Extensions</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand All @@ -57,5 +81,4 @@
<Target Name="AfterBuild">
</Target>
-->

</Project>
</Project>
138 changes: 138 additions & 0 deletions src/EPPlus.Extensions.Tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.IO;
using System.Reflection;
using OfficeOpenXml;
using Shouldly;

namespace EPPlus.Extensions.Tests
{
public class ExtensionTests
{
public void ToDataSetSimple_ShouldHandleHeaderRows_WhenSpecified()
{
var package = GetMarvelPackage();

var result = package.ToDataSet(true);

result.Tables[0].Rows.Count.ShouldBe(10);
}

public void ToDataSetSimple_ShouldHandleHeaderRows_WhenNotSpecified()
{
var package = GetMarvelPackage();

var result = package.ToDataSet(false);

result.Tables[0].Rows.Count.ShouldBe(11);
}

public void ToDataSet_ShouldThrowException_WhenHeaderRowIsLessThanZero()
{
var package = new ExcelPackage();

var exception = Should.Throw<ArgumentOutOfRangeException>(() => package.ToDataSet(-1));
exception.ParamName.ShouldBe("headerRow");
}

public void ToDataSet_ShouldReturnOneTable_WhenOneSheet()
{
var package = GetStatesPackage();

var result = package.ToDataSet(0);

result.Tables.Count.ShouldBe(1);
}

public void ToDataSet_ShouldReturnTwoTables_WhenTwoSheets()
{
var package = GetMarvelPackage();

var result = package.ToDataSet(0);

result.Tables.Count.ShouldBe(2);
}

public void ToDataSet_ShouldHandleHeaderRows_WhenSetToZero()
{
var package = GetStatesPackage();

var result = package.ToDataSet(0);

result.Tables[0].Rows.Count.ShouldBe(50);
}

public void ToDataSet_ShouldHandleHeaderRows_WhenSetToOne()
{
var package = GetStatesPackage();

var result = package.ToDataSet(1);

result.Tables[0].Rows.Count.ShouldBe(49);
}

public void ToDataSet_ShouldHandleHeaderRows_WhenSetToTen()
{
var package = GetStatesPackage();

var result = package.ToDataSet(10);

result.Tables[0].Rows.Count.ShouldBe(40);
}

public void ToDataSet_ShouldNameColumnsWithHeaderValues_WhenHeaderValuesExist()
{
var package = GetMarvelPackage();

var result = package.ToDataSet(1);

result.Tables[0].Columns[0].ColumnName.ShouldBe("First Name");
result.Tables[0].Columns[1].ColumnName.ShouldBe("Last Name");
result.Tables[0].Columns[2].ColumnName.ShouldBe("Alter Ego");
}

public void ToDataSet_ShouldUseGenericColumnNames_WhenHeaderValuesDoNotExist()
{
var package = GetStatesPackage();

var result = package.ToDataSet(0);

result.Tables[0].Columns[0].ColumnName.ShouldBe("Column 1");
}

public void ToDataSet_ShouldAddColumns_ForEachSourceColumn()
{
var package = GetMarvelPackage();

var result = package.ToDataSet(0);

result.Tables[0].Columns.Count.ShouldBe(3);
}

public void ToDataSet_ShouldAddRows_ForEachSourceRow()
{
var package = GetStatesPackage();

var result = package.ToDataSet(0);

result.Tables[0].Rows.Count.ShouldBe(50);
result.Tables[0].Rows[0][0].ToString().ShouldBe("Alabama");
result.Tables[0].Rows[49][0].ToString().ShouldBe("Wyoming");
}

private static ExcelPackage GetMarvelPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Marvel.xlsx");
var file = new FileInfo(path);
var package = new ExcelPackage(file);
return package;
}

private static ExcelPackage GetStatesPackage()
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"States.xlsx");
var file = new FileInfo(path);
var package = new ExcelPackage(file);
return package;
}
}
}
Binary file added src/EPPlus.Extensions.Tests/Marvel.xlsx
Binary file not shown.
Binary file added src/EPPlus.Extensions.Tests/States.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions src/EPPlus.Extensions.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EPPlus" version="3.1.3.3" targetFramework="net452" />
<package id="Fixie" version="1.0.2" targetFramework="net452" />
<package id="Shouldly" version="2.8.2" targetFramework="net452" />
</packages>
8 changes: 7 additions & 1 deletion src/EPPlus.Extensions.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlus.Extensions", "EPPlus.Extensions\EPPlus.Extensions.csproj", "{03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlus.Extensions.Tests", "EPPlus.Extensions.Tests\EPPlus.Extensions.Tests.csproj", "{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Release|Any CPU.Build.0 = Release|Any CPU
{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Extensions/EPPlusExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static DataSet ToDataSet(this ExcelPackage package, int headerRow = 0)
{
if (headerRow < 0)
{
throw new ArgumentException("headerRow must be 0 or greater.");
throw new ArgumentOutOfRangeException(nameof(headerRow), headerRow, "Must be 0 or greater.");
}

var result = new DataSet();
Expand Down

0 comments on commit 513e81a

Please sign in to comment.