I have an application that runs hourly, and I want to use it to also send an email at the start of the day of the first Monday of the month. I searched and found information to help me code what I needed, and all my tests pass, but I want to make sure that I am testing for all edge cases and that I am also being a good little programmer and writing good code.
I have the application being called from a task scheduler on the hour, sometimes some of the code doesn't get called right on the hour.
My Code for checking the date:
public bool CheckSendNewEvent()
{
var now = DateTime.Now;
const int SendTime = 8;
var firstMonday = GetFirstMondayofMonth(now);
var timeToSendOnFirstMonday = new DateTime(firstMonday.Year, firstMonday.Month, firstMonday.Day, SendTime, 0, 0);
return now > timeToSendOnFirstMonday && now < (timeToSendOnFirstMonday.AddHours(1));
}
public static bool CheckSendNewEvent(DateTime now)
{
const int SendTime = 8;
var firstMonday = GetFirstMondayofMonth(now);
var timeToSendOnFirstMonday = new DateTime(firstMonday.Year, firstMonday.Month, firstMonday.Day, SendTime, 0, 0);
return now > timeToSendOnFirstMonday && now < (timeToSendOnFirstMonday.AddHours(1));
}
/// From http://stackoverflow.com/a/3284486/1214743
///<summary>Gets the first week day following a date.</summary>
///<param name="date">The date.</param>
///<param name="dayOfWeek">The day of week to return.</param>
///<returns>The first dayOfWeek day following date, or date if it is on dayOfWeek.</returns>
public static DateTime Next(DateTime date, DayOfWeek dayOfWeek)
{
return date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
}
public static DateTime GetFirstMondayofMonth(DateTime now)
{
var monday = new DateTime(now.Year, now.Month, 1);
monday = Next(monday, DayOfWeek.Monday);
return monday;
}
I hate to admit this, but I am pretty much a beginner when it comes to Unit Tests, and I think that my life would be much easier with them, so I want to make sure that I am starting out with good habits.
Some of these tests were written before and after reading some articles about naming tests, hence the reason for the bad naming consistency
So here are my tests:
[TestMethod]
public void CheckSendNewEvent_Test()
{
var program = new Program();
var checker = program.CheckSendNewEvent();
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_MinDate()
{
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(DateTime.MinValue);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_MaxDate()
{
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(DateTime.MaxValue);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_201603010800()
{
DateTime march1st2016 = new DateTime(2016, 3, 1, 8, 0, 0);
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(march1st2016);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_201603070800()
{
DateTime march1st2016 = new DateTime(2016, 3, 7, 8, 0, 0);
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(march1st2016);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_20160301_0800()
{
DateTime march1st2016_8AM = new DateTime(2016, 3, 1, 8, 0, 0);
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(march1st2016_8AM);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_20160301_080005()
{
DateTime march1st2016_80005AM = new DateTime(2016, 3, 1, 8, 0, 5);
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(march1st2016_80005AM);
Assert.AreEqual(false, checker);
}
[TestMethod]
public void CheckSendNewEvent_20160307_080005()
{
DateTime march7th2016_80005AM = new DateTime(2016, 3, 7, 8, 0, 5);
var checker = AttorneyEventEmailer.Program.CheckSendNewEvent(march7th2016_80005AM);
Assert.AreEqual(true, checker);
}
[TestMethod]
public void GetFirstMondayOfMonth_20150406_20150406()
{
var date_20150406 = new DateTime(2015, 4, 6);
var firstMonday = new DateTime(2015, 4, 6);
var testResult = Program.GetFirstMondayofMonth(date_20150406);
Assert.AreEqual(testResult, firstMonday);
}
[TestMethod]
public void GetFirstMondayOfMonth_20150401_20150406()
{
var date_20150401 = new DateTime(2015, 4, 1);
var firstMonday = new DateTime(2015, 4, 6);
var testResult = Program.GetFirstMondayofMonth(date_20150401);
Assert.AreEqual(testResult, firstMonday);
}
[TestMethod]
public void GetFirstMondayOfMonth_20150415_20150406()
{
var date_20150415 = new DateTime(2015, 4, 15);
var firstMonday = new DateTime(2015, 4, 6);
var testResult = Program.GetFirstMondayofMonth(date_20150415);
Assert.AreEqual(testResult, firstMonday);
}
now > timeToSendOnFirstMonday
-- is there a reason you want it to return false at exactly 8am, or should that be>=
? \$\endgroup\$CheckSendNewEvent_201603070800
in particular. \$\endgroup\$