Skip to content

Commit

Permalink
Throws an exception when a column isn't of the expected type
Browse files Browse the repository at this point in the history
  • Loading branch information
tdwright committed Aug 21, 2018
1 parent e559d98 commit 2ed2e15
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 25 deletions.
138 changes: 113 additions & 25 deletions ConTabs.Tests/GeneratedColumnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Collections.Generic;
using ConTabs.Exceptions;

namespace ConTabs.Tests
{
Expand Down Expand Up @@ -53,31 +54,55 @@ public void ColumnGeneratedUsingLocalFunction()
circOfMerc.ShouldBe(7664);
}

[Test]
public void ColumnGeneratedUsingComplexTypes()
{
// Arrange
var data = new[]
{
new { Start = new DateTime(2017, 01, 01), End = new DateTime(2018, 01, 01) },
new { Start = new DateTime(1996, 10, 15), End = DateTime.Now.Date },
new { Start = new DateTime(1970, 01, 01), End = new DateTime(2038, 01, 19) },
};

var table = Table.Create(data);

// Act
table.Columns.AddGeneratedColumn<DateTime, double>(
(start, end) => (end - start).TotalDays,
"Total Days",
table.Columns[0], table.Columns["End"]);
[Test]
public void ColumnGeneratedUsingComplexTypes()
{
// Arrange
var data = new[]
{
new { Start = new DateTime(2017, 01, 01), End = new DateTime(2018, 01, 01) },
new { Start = new DateTime(1996, 10, 15), End = DateTime.Now.Date },
new { Start = new DateTime(1970, 01, 01), End = new DateTime(2038, 01, 19) },
};

var table = Table.Create(data);

// Act
table.Columns.AddGeneratedColumn<DateTime, double>(
(start, end) => (end - start).TotalDays,
"Total Days",
table.Columns[0], table.Columns["End"]);

// Assert
table.Columns.Count.ShouldBe(3);
table.Columns["Total Days"].Values[0].ShouldBe(365);
}

// Assert
table.Columns.Count.ShouldBe(3);
table.Columns[2].Values[0].ShouldBe(365);
}
[Test]
public void ColumnGeneratedUsingComplexDifferentTypes()
{
// Arrange
var data = new[]
{
new { Start = new DateTime(2017, 01, 01), Duration = 3 },
new { Start = new DateTime(1996, 10, 15), Duration = 100 },
new { Start = new DateTime(1970, 01, 01), Duration = 50 },
};

var table = Table.Create(data);

// Act
table.Columns.AddGeneratedColumn<DateTime, int, DateTime>(
(start, days) => start.AddDays(days),
"End",
table.Columns[0], table.Columns["Duration"]);

// Assert
table.Columns.Count.ShouldBe(3);
table.Columns["End"].Values[0].ShouldBe(new DateTime(2017,1,4));
}

[Test]
[Test]
public void ColumnGeneratedUsingRangeEntireTable()
{
// Arrange
Expand Down Expand Up @@ -137,6 +162,69 @@ int ComputeValues(List<object> numbers)
// Assert
table.Columns.Count.ShouldBe(5);
table.Columns["Sum"].Values[0].ShouldBe(16);
}
}
}

[Test]
public void SingleInputColumn_WrongType_ShouldThrow()
{
// Arrange
var data = DemoDataProvider.ListOfDemoData();
var table = Table<Planet>.Create(data);

// Act
TestDelegate testDelegate = () => table.Columns.AddGeneratedColumn<string, double>(
(x) => 12d,
"Twelve",
table.Columns["Diameter"]);

// Assert
Assert.Throws<TypeMismatchException>(testDelegate).Message.ShouldBe("Computed column expected type 'String', but column 'Diameter' is of type 'Int32'.");
}

[Test]
public void TwoInputColumns_FirstIsWrongType_ShouldThrow()
{
// Arrange
var data = new[]
{
new { Start = new DateTime(2017, 01, 01), Duration = 3 },
new { Start = new DateTime(1996, 10, 15), Duration = 100 },
new { Start = new DateTime(1970, 01, 01), Duration = 50 },
};

var table = Table.Create(data);

// Act
TestDelegate testDelegate = () => table.Columns.AddGeneratedColumn<string, int, DateTime>(
(start, days) => DateTime.Parse(start).AddDays(days),
"End",
table.Columns[0], table.Columns["Duration"]);

// Assert
Assert.Throws<TypeMismatchException>(testDelegate).Message.ShouldBe("Computed column expected type 'String', but column 'Start' is of type 'DateTime'.");
}

[Test]
public void TwoInputColumns_SecondIsWrongType_ShouldThrow()
{
// Arrange
var data = new[]
{
new { Start = new DateTime(2017, 01, 01), Duration = 3 },
new { Start = new DateTime(1996, 10, 15), Duration = 100 },
new { Start = new DateTime(1970, 01, 01), Duration = 50 },
};

var table = Table.Create(data);

// Act
TestDelegate testDelegate = () => table.Columns.AddGeneratedColumn<DateTime, string, DateTime>(
(start, days) => start.AddDays(int.Parse(days)),
"End",
table.Columns[0], table.Columns["Duration"]);

// Assert
Assert.Throws<TypeMismatchException>(testDelegate).Message.ShouldBe("Computed column expected type 'String', but column 'Duration' is of type 'Int32'.");
}
}
}
8 changes: 8 additions & 0 deletions ConTabs/Columns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ private Column FindColByName(string name)
/// <param name="column">The parameter to operate on</param>
public void AddGeneratedColumn<TInput, TOutput>(Func<TInput, TOutput> expression, string columnName, Column column)
{
if (column.SourceType != typeof(TInput))
throw new TypeMismatchException(column.ColumnName, typeof(TInput), column.SourceType);

var results = new List<object>();

for (int i = 0; i < column.Values.Count; i++)
Expand Down Expand Up @@ -132,6 +135,11 @@ private Column FindColByName(string name)
/// <param name="column2">The second operand within the given expression</param>
public void AddGeneratedColumn<TInput1, TInput2, TOutput>(Func<TInput1, TInput2, TOutput> expression, string columnName, Column column1, Column column2)
{
if (column1.SourceType != typeof(TInput1))
throw new TypeMismatchException(column1.ColumnName, typeof(TInput1), column1.SourceType);
if (column2.SourceType != typeof(TInput2))
throw new TypeMismatchException(column2.ColumnName, typeof(TInput2), column2.SourceType);

var results = new List<object>();

for (int i = 0; i < column1.Values.Count; i++)
Expand Down

0 comments on commit 2ed2e15

Please sign in to comment.