Unit testing for UTCDate

This commit is contained in:
2019-07-17 14:42:28 +01:00
parent 83865a3911
commit 325e9908a7
2 changed files with 135 additions and 8 deletions
@@ -44,7 +44,8 @@ namespace Meade.net.Telescope.UnitTests
_sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_43EG) .Returns(() => "43Eg"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_43EG) .Returns(() => "43Eg");
_sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny<Action>())).Callback<Action>(action => { action(); }); _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny<Action>())).Callback<Action>(action => { action(); });
_sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny<Func<ASCOM.Meade.net.Telescope.TelescopeDateDetails>>())).Returns<Func<ASCOM.Meade.net.Telescope.TelescopeDateDetails>>( (func) => func());
_sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties); _sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties);
_astroMathsMock = new Mock<IAstroMaths>(); _astroMathsMock = new Mock<IAstroMaths>();
@@ -1693,5 +1694,131 @@ namespace Meade.net.Telescope.UnitTests
Assert.That(result, Is.EqualTo(rate)); Assert.That(result, Is.EqualTo(rate));
} }
[Test]
public void TrackingRates_Get_ReturnsExpectedType()
{
var result = _telescope.TrackingRates;
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.AssignableTo<TrackingRates>());
}
[Test]
public void UTCDate_Get_WhenNotConnected_ThenThrowsException()
{
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
var exception = Assert.Throws<NotConnectedException>(() => { var result = _telescope.UTCDate; });
Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: UTCDate Get"));
}
[TestCase("10/15/20", "20:15:10", "-1.0", 2020, 10, 15, 19, 15, 10)]
[TestCase("12/03/15", "21:30:45", "+0.0", 2015, 12, 3, 21, 30, 45)]
public void UTCDate_Get_WhenConnected_ThenReturnsUTCDateTime(string telescopeDate, string telescopeTime,
string telescopeUtcCorrection, int year, int month, int day, int hour, int min, int second)
{
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
_telescope.Connected = true;
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GC#")).Returns(telescopeDate);
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GL#")).Returns(telescopeTime);
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns(telescopeUtcCorrection);
var result = _telescope.UTCDate;
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.AssignableTo<DateTime>());
Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Utc));
Assert.That(result.Year, Is.EqualTo(year));
Assert.That(result.Month, Is.EqualTo(month));
Assert.That(result.Day, Is.EqualTo(day));
Assert.That(result.Hour, Is.EqualTo(hour));
Assert.That(result.Minute, Is.EqualTo(min));
Assert.That(result.Second, Is.EqualTo(second));
}
[Test]
public void UTCDate_Set_WhenNotConnected_ThenThrowsException()
{
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
var exception = Assert.Throws<NotConnectedException>(() => { _telescope.UTCDate = new DateTime(2010,10,15,16,42,32, DateTimeKind.Utc); });
Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: UTCDate Set"));
}
[TestCase("10/15/20", "20:15:10", "-1.0", 2020, 10, 15, 19, 15, 10)]
[TestCase("12/03/15", "21:30:45", "+0.0", 2015, 12, 3, 21, 30, 45)]
public void UTCDate_Set_WhenFailsToSetTelescopeTime_ThenThrowsException(string telescopeDate, string telescopeTime, string telescopeUtcCorrection, int year, int month, int day, int hour, int min, int second)
{
double utcOffsetHours = double.Parse(telescopeUtcCorrection);
TimeSpan utcCorrection = TimeSpan.FromHours(utcOffsetHours);
var newDate = new DateTime(year, month, day, hour, min, second, DateTimeKind.Local) + utcCorrection;
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
_telescope.Connected = true;
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns(telescopeUtcCorrection);
_sharedResourcesWrapperMock.Setup(x => x.SendChar($":SL{telescopeTime}#")).Returns("0");
var exception = Assert.Throws<InvalidOperationException>(() => { _telescope.UTCDate = newDate; } );
Assert.That(exception.Message, Is.EqualTo("Failed to set local time"));
}
[TestCase("10/15/20", "20:15:10", "-1.0", 2020, 10, 15, 20, 15, 10)]
[TestCase("12/03/15", "21:30:45", "+0.0", 2015, 12, 3, 21, 30, 45)]
public void UTCDate_Set_WhenFailsToSetTelescopeDate_ThenThrowsException(string telescopeDate, string telescopeTime, string telescopeUtcCorrection, int year, int month, int day, int hour, int min, int second)
{
double utcOffsetHours = double.Parse(telescopeUtcCorrection);
TimeSpan utcCorrection = TimeSpan.FromHours(utcOffsetHours);
var newDate = new DateTime(year, month, day, hour, min, second, DateTimeKind.Local) + utcCorrection;
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
_telescope.Connected = true;
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns(telescopeUtcCorrection);
_sharedResourcesWrapperMock.Setup(x => x.SendChar($":SL{telescopeTime}#")).Returns("1");
_sharedResourcesWrapperMock.Setup(x => x.SendChar($":SC{newDate:MM/dd/yy}#")).Returns("0");
var exception = Assert.Throws<InvalidOperationException>(() => { _telescope.UTCDate = newDate; });
Assert.That(exception.Message, Is.EqualTo("Failed to set local date"));
}
[TestCase("10/15/20", "20:15:10", "-1.0", 2020, 10, 15, 20, 15, 10)]
[TestCase("12/03/15", "21:30:45", "+0.0", 2015, 12, 3, 21, 30, 45)]
public void UTCDate_Set_WhenSucceeds_ThenReadsTwoStringsFromTelescope(string telescopeDate,
string telescopeTime, string telescopeUtcCorrection, int year, int month, int day, int hour, int min,
int second)
{
double utcOffsetHours = double.Parse(telescopeUtcCorrection);
TimeSpan utcCorrection = TimeSpan.FromHours(utcOffsetHours);
var newDate = new DateTime(year, month, day, hour, min, second, DateTimeKind.Local) + utcCorrection;
_sharedResourcesWrapperMock.Setup(x => x.ProductName)
.Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion)
.Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
_telescope.Connected = true;
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns(telescopeUtcCorrection);
_sharedResourcesWrapperMock.Setup(x => x.SendChar($":SL{telescopeTime}#")).Returns("1");
_sharedResourcesWrapperMock.Setup(x => x.SendChar($":SC{telescopeDate}#")).Returns("1");
_telescope.UTCDate = newDate;
_sharedResourcesWrapperMock.Verify(x => x.ReadTerminated(), Times.Exactly(2));
}
} }
} }
+7 -7
View File
@@ -1798,8 +1798,6 @@ namespace ASCOM.Meade.net
private TimeSpan GetUtcCorrection() private TimeSpan GetUtcCorrection()
{ {
CheckConnected("GetUtcCorrection");
string utcOffSet = _sharedResourcesWrapper.SendString(":GG#"); string utcOffSet = _sharedResourcesWrapper.SendString(":GG#");
//:GG# Get UTC offset time //:GG# Get UTC offset time
//Returns: sHH# or sHH.H# //Returns: sHH# or sHH.H#
@@ -1810,7 +1808,7 @@ namespace ASCOM.Meade.net
return utcCorrection; return utcCorrection;
} }
private class TelescopeDateDetails public class TelescopeDateDetails
{ {
public string telescopeDate { get; set; } public string telescopeDate { get; set; }
public string telescopeTime { get; set; } public string telescopeTime { get; set; }
@@ -1830,11 +1828,11 @@ namespace ASCOM.Meade.net
TelescopeDateDetails tdd = new TelescopeDateDetails(); TelescopeDateDetails tdd = new TelescopeDateDetails();
tdd.telescopeDate = _sharedResourcesWrapper.SendString(":GC#"); tdd.telescopeDate = _sharedResourcesWrapper.SendString(":GC#");
//:GC# Get current date. //:GC# Get current date.
//Returns: MM / DD / YY# //Returns: MM/DD/YY#
//The current local calendar date for the telescope. //The current local calendar date for the telescope.
tdd.telescopeTime = _sharedResourcesWrapper.SendString(":GL#"); tdd.telescopeTime = _sharedResourcesWrapper.SendString(":GL#");
//:GL# Get Local Time in 24 hour format //:GL# Get Local Time in 24 hour format
//Returns: HH: MM: SS# //Returns: HH:MM:SS#
//The Local Time in 24 - hour Format //The Local Time in 24 - hour Format
tdd.utcCorrection = GetUtcCorrection(); tdd.utcCorrection = GetUtcCorrection();
@@ -1872,7 +1870,8 @@ namespace ASCOM.Meade.net
var utcCorrection = GetUtcCorrection(); var utcCorrection = GetUtcCorrection();
var localDateTime = value - utcCorrection; var localDateTime = value - utcCorrection;
var timeResult = _sharedResourcesWrapper.SendChar($":SL{localDateTime:HH:mm:ss}#"); string localStingCommand = $":SL{localDateTime:HH:mm:ss}#";
var timeResult = _sharedResourcesWrapper.SendChar(localStingCommand);
//:SLHH:MM:SS# //:SLHH:MM:SS#
//Set the local Time //Set the local Time
//Returns: //Returns:
@@ -1883,7 +1882,8 @@ namespace ASCOM.Meade.net
throw new InvalidOperationException("Failed to set local time"); throw new InvalidOperationException("Failed to set local time");
} }
var dateResult = _sharedResourcesWrapper.SendChar($":SC{localDateTime:MM/dd/yy}#"); string localDateCommand = $":SC{localDateTime:MM/dd/yy}#";
var dateResult = _sharedResourcesWrapper.SendChar(localDateCommand);
//:SCMM/DD/YY# //:SCMM/DD/YY#
//Change Handbox Date to MM/DD/YY //Change Handbox Date to MM/DD/YY
//Returns: <D><string> //Returns: <D><string>