Last active
December 19, 2015 07:59
-
-
Save OlsonAndrewD/5922488 to your computer and use it in GitHub Desktop.
Test refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This class helps reduce the amount of code in each test. | |
private class TestHelper | |
{ | |
private DateTime StartTime; | |
private Enums.MetricStorageInterval DataGranularity; | |
private List<ceQryDashboardGadgetDataBO> Data1; | |
private List<ceQryDashboardGadgetDataBO> Data2; | |
private List<ExpectedResult> ExpectedResults; | |
public TestHelper(string startTime, Enums.MetricStorageInterval dataGranularity) | |
{ | |
this.StartTime = DateTime.Parse(startTime); | |
this.DataGranularity = dataGranularity; | |
this.Data1 = new List<ceQryDashboardGadgetDataBO>(); | |
this.Data2 = new List<ceQryDashboardGadgetDataBO>(); | |
this.ExpectedResults = new List<ExpectedResult>(); | |
} | |
public void AddToSeries1(decimal value, double minutesFromStartTime) | |
{ | |
this.Data1.Add(this.CreateData(value, minutesFromStartTime)); | |
} | |
public void AddToSeries2(decimal value, double minutesFromStartTime) | |
{ | |
this.Data2.Add(this.CreateData(value, minutesFromStartTime)); | |
} | |
public void AddExpectedResult(decimal? value1, decimal? value2, double minutesFromStartTime) | |
{ | |
this.ExpectedResults.Add(new ExpectedResult | |
{ | |
Time = this.StartTime.AddMinutes(minutesFromStartTime), | |
Value1 = value1, | |
Value2 = value2 | |
}); | |
} | |
public void RunTest(string time) | |
{ | |
var test = new InteractiveBarChartGadget.PartialHoursProvider( | |
new InteractiveBarChartGadget.DataSeries { Data = this.Data1.ToArray() }, | |
new InteractiveBarChartGadget.DataSeries { Data = this.Data2.ToArray() }, | |
this.DataGranularity, | |
this.StartTime); | |
var results = test.GetDataFor(DateTime.Parse(time)).ToArray(); | |
results.Length.ShouldEqual(this.ExpectedResults.Count, "Wrong number of results"); | |
int index = 0; | |
this.ExpectedResults | |
.Zip(results, (e, a) => new { Expected = e, Actual = a, Index = index++ }) | |
.ForEach(x => this.VerifyResult(x.Expected, x.Actual, x.Index)); | |
} | |
private ceQryDashboardGadgetDataBO CreateData(decimal value, double minutesFromStartTime) | |
{ | |
return new ceQryDashboardGadgetDataBO( | |
value: value, | |
storetime: this.StartTime.AddMinutes(minutesFromStartTime)); | |
} | |
private void VerifyResult(ExpectedResult expected, InteractiveBarChartGadget.PartialHourDataItem actual, int index) | |
{ | |
actual.Time.ShouldEqual(expected.Time, String.Format("Wrong time at index {0}", index)); | |
actual.Value1.ShouldEqual(expected.Value1, String.Format("Wrong value 1 at index {0}", index)); | |
actual.Value2.ShouldEqual(expected.Value2, String.Format("Wrong value 2 at index {0}", index)); | |
} | |
private class ExpectedResult | |
{ | |
public DateTime Time { get; set; } | |
public decimal? Value1 { get; set; } | |
public decimal? Value2 { get; set; } | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class PartialHoursProviderTests | |
{ | |
// This test has a lot of code in it. I don't want to start copying this around | |
// to all the other tests. | |
[TestMethod] | |
public void BarChart_PartialHours_Half_Value1Only() | |
{ | |
var startTime = DateTime.Parse("7/2/2013 15:00"); | |
var dataSeries = new InteractiveBarChartGadget.DataSeries | |
{ | |
Data = new[] | |
{ | |
new ceQryDashboardGadgetDataBO(value: 10, storetime: startTime), | |
new ceQryDashboardGadgetDataBO(value: 20, storetime: startTime.AddMinutes(30)), | |
new ceQryDashboardGadgetDataBO(value: 30, storetime: startTime.AddMinutes(60)), | |
new ceQryDashboardGadgetDataBO(value: 40, storetime: startTime.AddMinutes(90)) | |
} | |
}; | |
var test = new InteractiveBarChartGadget.PartialHoursProvider( | |
dataSeries, | |
null, | |
Enums.MetricStorageInterval.HalfHourly, | |
startTime); | |
var results = test.GetDataFor(startTime).ToArray(); | |
results.Length.ShouldEqual(2, "Wrong number of results"); | |
this.VerifyResult(0, startTime, 10, null, results); | |
this.VerifyResult(1, startTime.AddMinutes(30), 20, null, results); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class PartialHoursProviderTests | |
{ | |
// This test uses TestHelper and therefore contains less code. | |
// I could have omitted all the parameter names, but I think it's more understandable this way. | |
[TestMethod] | |
public void BarChart_PartialHours_Half_Value1Only() | |
{ | |
var helper = new TestHelper("7/2/2013 15:00", Enums.MetricStorageInterval.HalfHourly); | |
helper.AddToSeries1(value: 10, minutesFromStartTime: 0); | |
helper.AddToSeries1(value: 20, minutesFromStartTime: 30); | |
helper.AddToSeries1(value: 30, minutesFromStartTime: 60); | |
helper.AddToSeries1(value: 40, minutesFromStartTime: 90); | |
helper.AddExpectedResult(value1: 10, value2: null, minutesFromStartTime: 0); | |
helper.AddExpectedResult(value1: 20, value2: null, minutesFromStartTime: 30); | |
helper.RunTest("7/2/2013 15:00"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment