/
SunsetTDDTest.cs
194 lines (159 loc) · 7.22 KB
/
SunsetTDDTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace HouseControl.Sunset.Test
{
[TestClass]
public class SunsetTDDTest
{
string sampleData = "{\"results\":{\"sunrise\":\"2:35:18 PM\",\"sunset\":\"1:35:58 AM\",\"solar_noon\":\"8:05:38 PM\",\"day_length\":\"11:00:40\",\"civil_twilight_begin\":\"2:09:53 PM\",\"civil_twilight_end\":\"2:01:23 AM\",\"nautical_twilight_begin\":\"1:40:38 PM\",\"nautical_twilight_end\":\"2:30:38 AM\",\"astronomical_twilight_begin\":\"1:11:37 PM\",\"astronomical_twilight_end\":\"2:59:39 AM\"},\"status\":\"OK\"}";
string errorData = "{\"status\":\"ERROR\"}";
[TestMethod]
public void CheckStatus_WithGoodData_ReturnsTrue()
{
bool result = SunsetTDD.CheckStatus(sampleData);
Assert.IsTrue(result);
}
[TestMethod]
public void CheckStatus_WithErrorData_ReturnsFalse()
{
bool result = SunsetTDD.CheckStatus(errorData);
Assert.IsFalse(result);
}
[TestMethod]
public void ParseSunset_WithGoodData_ReturnsString()
{
string result = SunsetTDD.ParseSunset(sampleData);
Assert.IsNotNull(result);
}
[TestMethod]
public void ParseSunset_WithErrorData_ReturnsNull()
{
string result = SunsetTDD.ParseSunset(errorData);
Assert.IsNull(result);
}
[TestMethod]
public void ParseSunset_WithGoodData_ReturnsExpectedString()
{
string expected = "1:35:58 AM";
string result = SunsetTDD.ParseSunset(sampleData);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void GetLocalTime_UTCTimeString_ReturnsLocalTime()
{
string utcTimeString = "1:35:58 AM";
DateTime currentDate = new DateTime(2015, 02, 15);
DateTime expectedLocalTime = new DateTime(2015, 02, 15, 17, 35, 58);
DateTime result = SunsetTDD.GetLocalTime(utcTimeString, currentDate);
Assert.AreEqual(expectedLocalTime, result);
}
[TestMethod]
public void GetLocalTime_NullTimeString_ReturnsDateOnly()
{
string utcTimeString = null;
DateTime currentDate = new DateTime(2015, 02, 15);
DateTime expectedLocalTime = new DateTime(2015, 02, 15, 0, 0, 0);
DateTime result = SunsetTDD.GetLocalTime(utcTimeString, currentDate);
Assert.AreEqual(expectedLocalTime, result);
}
[TestMethod]
public void ParseSunrise_WithGoodData_ReturnsString()
{
string result = SunsetTDD.ParseSunrise(sampleData);
Assert.IsNotNull(result);
}
[TestMethod]
public void ParseSunrise_WithErrorData_ReturnsNull()
{
string result = SunsetTDD.ParseSunrise(errorData);
Assert.IsNull(result);
}
[TestMethod]
public void ParseSunrise_WithGoodData_ReturnsExpectedString()
{
string expected = "2:35:18 PM";
string result = SunsetTDD.ParseSunrise(sampleData);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void GetSunset_WithDate_ReturnsSunsetForDate()
{
DateTime currentDate = new DateTime(2015, 02, 15);
DateTime expected = new DateTime(2015, 02, 15, 17, 35, 58);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result = provider.GetSunset(currentDate);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void GetSunrise_WithDate_ReturnsSunriseForDate()
{
DateTime currentDate = new DateTime(2015, 02, 15);
DateTime expected = new DateTime(2015, 02, 15, 06, 35, 18);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result = provider.GetSunrise(currentDate);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void GetSunset_CalledMultipleTimesWithSameDate_CallsServiceOnce()
{
DateTime currentDate = new DateTime(2015, 02, 15);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result1 = provider.GetSunset(currentDate);
DateTime result2 = provider.GetSunset(currentDate);
serviceMock.Verify(s => s.GetServiceData(currentDate), Times.Once());
}
[TestMethod]
public void GetSunrise_CalledMultipleTimesWithSameDate_CallsServiceOnce()
{
DateTime currentDate = new DateTime(2015, 02, 15);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result1 = provider.GetSunrise(currentDate);
DateTime result2 = provider.GetSunrise(currentDate);
serviceMock.Verify(s => s.GetServiceData(currentDate), Times.Once());
}
[TestMethod]
public void GetSunriseGetSunset_CalledWithSameDate_CallsServiceOnce()
{
DateTime currentDate = new DateTime(2015, 02, 15);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result1 = provider.GetSunrise(currentDate);
DateTime result2 = provider.GetSunset(currentDate);
serviceMock.Verify(s => s.GetServiceData(currentDate), Times.Once());
}
[TestMethod]
public void GetSunset_CalledWithDifferentDates_CallsServiceMoreThanOnce()
{
DateTime date1 = new DateTime(2015, 02, 15);
DateTime date2 = new DateTime(2015, 02, 16);
Mock<ISunsetService> serviceMock = new Mock<ISunsetService>();
serviceMock.Setup(s => s.GetServiceData(It.IsAny<DateTime>()))
.Returns(sampleData);
SunsetTDD provider = new SunsetTDD();
provider.SunsetService = serviceMock.Object;
DateTime result1 = provider.GetSunset(date1);
DateTime result2 = provider.GetSunset(date2);
serviceMock.Verify(s => s.GetServiceData(It.IsAny<DateTime>()), Times.Exactly(2));
}
}
}