Created
September 16, 2010 13:41
-
-
Save alexyork/582440 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace RefactoringNeeded | |
{ | |
#region Alert filter | |
public static class AlertFilter | |
{ | |
public static IEnumerable<Alert> FilterByStatus(this IEnumerable<Alert> alerts, | |
int? primaryStatusId, | |
int? secondaryStatusId, | |
bool isNewEntry) | |
{ | |
var matchingAlerts = | |
from alert in alerts | |
where alert.PrimaryStatusId == primaryStatusId && | |
alert.SecondaryStatusId == secondaryStatusId | |
select alert; | |
var newEntries = | |
from alert in alerts | |
where alert.PrimaryStatusId == null && | |
alert.SecondaryStatusId == null && | |
isNewEntry | |
select alert; | |
return matchingAlerts.Union(newEntries).ToList(); | |
} | |
} | |
public class Alert | |
{ | |
public int AlertId { get; set; } | |
public int? PrimaryStatusId { get; set; } | |
public int? SecondaryStatusId { get; set; } | |
} | |
#endregion | |
#region Unit tests | |
[TestClass] | |
public class AlertFilterTests | |
{ | |
private IEnumerable<Alert> _alerts; | |
[TestInitialize] | |
public void SetUp() | |
{ | |
GenerateMockAlerts(); | |
} | |
#region Test methods | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_are_null() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(null, null, true); | |
Assert.AreEqual(1, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_are_null_or_has_correct_value() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(1, null, true); | |
Assert.AreEqual(3, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_are_null_or_has_correct_externalstatusid() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(null, 2, true); | |
Assert.AreEqual(4, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_are_null_or_has_correct_statusid() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(1, 2, true); | |
Assert.AreEqual(2, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_are_null_and_is_not_new() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(null, null, false); | |
Assert.AreEqual(1, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_statuses_has_correct_value() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(1, null, false); | |
Assert.AreEqual(2, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_externalstatusid_has_correct_value() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(null, 2, false); | |
Assert.AreEqual(3, alertsAfterFilter.Count()); | |
} | |
[TestMethod] | |
public void Should_return_all_alerts_where_both_statuses_has_correct_value() | |
{ | |
var alertsAfterFilter = _alerts.FilterByStatus(1, 2, false); | |
Assert.AreEqual(1, alertsAfterFilter.Count()); | |
} | |
#endregion | |
#region Mock | |
private void GenerateMockAlerts() | |
{ | |
_alerts = new[] { | |
new Alert {AlertId = 1, PrimaryStatusId = null, SecondaryStatusId = null }, | |
new Alert {AlertId = 2, PrimaryStatusId = 1, SecondaryStatusId = null}, | |
new Alert {AlertId = 3, PrimaryStatusId = 1, SecondaryStatusId = null}, | |
new Alert {AlertId = 4, PrimaryStatusId = null, SecondaryStatusId = 2}, | |
new Alert {AlertId = 5, PrimaryStatusId = null, SecondaryStatusId = 2}, | |
new Alert {AlertId = 6, PrimaryStatusId = null, SecondaryStatusId = 2}, | |
new Alert {AlertId = 7, PrimaryStatusId = 1, SecondaryStatusId = 2} | |
}; | |
} | |
#endregion | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment