Skip to content

Commit 372a1e4

Browse files
author
Justin
committed
Handling more edge cases.
Added more unit tests.
1 parent decddb2 commit 372a1e4

9 files changed

Lines changed: 262 additions & 54 deletions

File tree

Lines changed: 189 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
using System;
2-
using Microsoft.VisualStudio.TestTools.UnitTesting;
3-
using SteamWebAPI2.Interfaces;
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
42
using Moq;
5-
using SteamWebAPI2.Utilities;
3+
using SteamWebAPI2.Interfaces;
4+
using SteamWebAPI2.Models.SteamCommunity;
65
using SteamWebAPI2.Models.SteamPlayer;
6+
using SteamWebAPI2.Utilities;
7+
using System;
8+
using System.Collections.Generic;
79
using System.Threading.Tasks;
8-
using SteamWebAPI2.Models.SteamCommunity;
910

1011
namespace SteamWebAPI2.Tests
1112
{
1213
[TestClass]
1314
public class PlayerServiceTests
1415
{
16+
[TestInitialize]
17+
public void Initialize()
18+
{
19+
AutoMapperConfiguration.Initialize();
20+
}
21+
1522
[TestMethod]
1623
[ExpectedException(typeof(ArgumentNullException))]
1724
public void PlayerService_Throw_Exception_If_Steam_API_Key_Is_Null_Or_Empty()
@@ -20,66 +27,222 @@ public void PlayerService_Throw_Exception_If_Steam_API_Key_Is_Null_Or_Empty()
2027
}
2128

2229
[TestMethod]
23-
public async Task PlayerService_IsPlayingSharedGameAsync_Return_Empty_String_If_Web_Interface_Returns_Null()
30+
public async Task PlayerService_IsPlayingSharedGameAsync_Return_Null_If_Web_Interface_Returns_Null()
2431
{
2532
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
2633

27-
mockSteamWebRequest.Setup(x => x.GetAsync<PlayingSharedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), null))
34+
mockSteamWebRequest.Setup(x => x.GetAsync<PlayingSharedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
2835
.ReturnsAsync((PlayingSharedGameResultContainer)null);
2936

3037
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
31-
var result = await service.IsPlayingSharedGameAsync(0, 0);
38+
var result = await service.IsPlayingSharedGameAsync(It.IsAny<ulong>(), It.IsAny<uint>());
3239

33-
Assert.IsNotNull(result);
34-
Assert.AreEqual(result, String.Empty);
40+
Assert.IsTrue(!result.HasValue);
3541
}
3642

3743
[TestMethod]
38-
public async Task PlayerService_IsPlayingSharedGameAsync_Return_Empty_String_If_Web_Interface_Result_Is_Null()
44+
public async Task PlayerService_IsPlayingSharedGameAsync_Return_Null_If_Web_Interface_Result_Is_Null()
3945
{
4046
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
4147

42-
mockSteamWebRequest.Setup(x => x.GetAsync<PlayingSharedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), null))
48+
mockSteamWebRequest.Setup(x => x.GetAsync<PlayingSharedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
4349
.ReturnsAsync(new PlayingSharedGameResultContainer()
4450
{
4551
Result = null
4652
});
4753

4854
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
49-
var result = await service.IsPlayingSharedGameAsync(0, 0);
55+
var result = await service.IsPlayingSharedGameAsync(It.IsAny<ulong>(), It.IsAny<uint>());
5056

51-
Assert.IsNotNull(result);
52-
Assert.AreEqual(result, String.Empty);
57+
Assert.IsTrue(!result.HasValue);
5358
}
5459

5560
[TestMethod]
56-
public async Task PlayerService_GetBadgesAsync_Return_New_If_Web_Request_Is_Null()
61+
public async Task PlayerService_GetBadgesAsync_Return_Null_If_Web_Interface_Returns_Null()
5762
{
5863
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
5964

60-
mockSteamWebRequest.Setup(x => x.GetAsync<BadgesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), null))
65+
mockSteamWebRequest.Setup(x => x.GetAsync<BadgesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
6166
.ReturnsAsync((BadgesResultContainer)null);
6267

6368
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
64-
var result = await service.GetBadgesAsync(0);
69+
var result = await service.GetBadgesAsync(It.IsAny<ulong>());
6570

66-
Assert.IsNotNull(result);
67-
Assert.IsNull(result.Badges);
71+
Assert.IsNull(result);
6872
}
6973

7074
[TestMethod]
71-
public async Task PlayerService_GetCommunityBadgeProgressAsync_Return_Empty_If_Web_Request_Is_Null()
75+
public async Task PlayerService_GetBadgesAsync_Return_Null_If_Web_Interface_Result_Is_Null()
7276
{
7377
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
7478

75-
mockSteamWebRequest.Setup(x => x.GetAsync<CommunityBadgeProgressResultContainer>(It.IsAny<string>(), It.IsAny<int>(), null))
79+
mockSteamWebRequest.Setup(x => x.GetAsync<BadgesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
80+
.ReturnsAsync(new BadgesResultContainer()
81+
{
82+
Result = null
83+
});
84+
85+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
86+
var result = await service.GetBadgesAsync(It.IsAny<ulong>());
87+
88+
Assert.IsNull(result);
89+
}
90+
91+
[TestMethod]
92+
public async Task PlayerService_GetCommunityBadgeProgressAsync_Return_Null_If_Web_Interface_Returns_Null()
93+
{
94+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
95+
96+
mockSteamWebRequest.Setup(x => x.GetAsync<CommunityBadgeProgressResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
7697
.ReturnsAsync((CommunityBadgeProgressResultContainer)null);
7798

7899
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
79-
var result = await service.GetCommunityBadgeProgressAsync(0, 0);
100+
var result = await service.GetCommunityBadgeProgressAsync(It.IsAny<uint>(), It.IsAny<uint?>());
101+
102+
Assert.IsNull(result);
103+
}
104+
105+
[TestMethod]
106+
public async Task PlayerService_GetCommunityBadgeProgressAsync_Return_Null_If_Web_Interface_Result_Is_Null()
107+
{
108+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
109+
110+
mockSteamWebRequest.Setup(x => x.GetAsync<CommunityBadgeProgressResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
111+
.ReturnsAsync(new CommunityBadgeProgressResultContainer()
112+
{
113+
Result = null
114+
});
115+
116+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
117+
var result = await service.GetCommunityBadgeProgressAsync(It.IsAny<uint>(), It.IsAny<uint?>());
118+
119+
Assert.IsNull(result);
120+
}
121+
122+
[TestMethod]
123+
public async Task PlayerService_GetSteamLevelAsync_Return_Null_If_Web_Interface_Returns_Null()
124+
{
125+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
126+
127+
mockSteamWebRequest.Setup(x => x.GetAsync<SteamLevelResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
128+
.ReturnsAsync((SteamLevelResultContainer)null);
129+
130+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
131+
var result = await service.GetSteamLevelAsync(It.IsAny<uint>());
132+
133+
Assert.IsNull(result);
134+
}
135+
136+
[TestMethod]
137+
public async Task PlayerService_GetSteamLevelAsync_Return_Null_If_Web_Interface_Result_Is_Null()
138+
{
139+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
140+
141+
mockSteamWebRequest.Setup(x => x.GetAsync<SteamLevelResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
142+
.ReturnsAsync(new SteamLevelResultContainer()
143+
{
144+
Result = null
145+
});
146+
147+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
148+
var result = await service.GetSteamLevelAsync(It.IsAny<uint>());
149+
150+
Assert.IsNull(result);
151+
}
152+
153+
[TestMethod]
154+
public async Task PlayerService_GetOwnedGamesAsync_Return_Null_If_Web_Interface_Returns_Null()
155+
{
156+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
157+
158+
mockSteamWebRequest.Setup(x => x.GetAsync<OwnedGamesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
159+
.ReturnsAsync((OwnedGamesResultContainer)null);
160+
161+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
162+
var result = await service.GetOwnedGamesAsync(It.IsAny<ulong>(), It.IsAny<bool?>(), It.IsAny<bool?>(), It.IsAny<IReadOnlyCollection<uint>>());
163+
164+
Assert.IsNull(result);
165+
}
166+
167+
[TestMethod]
168+
public async Task PlayerService_GetOwnedGamesAsync_Return_Null_If_Web_Interface_Result_Is_Null()
169+
{
170+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
171+
172+
mockSteamWebRequest.Setup(x => x.GetAsync<OwnedGamesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
173+
.ReturnsAsync(new OwnedGamesResultContainer()
174+
{
175+
Result = null
176+
});
177+
178+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
179+
var result = await service.GetOwnedGamesAsync(It.IsAny<ulong>(), It.IsAny<bool?>(), It.IsAny<bool?>(), It.IsAny<IReadOnlyCollection<uint>>());
180+
181+
Assert.IsNull(result);
182+
}
183+
184+
[TestMethod]
185+
public async Task PlayerService_GetOwnedGamesAsync_Trim_Starting_And_Ending_Spaces_From_Owned_Game_Names()
186+
{
187+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
188+
189+
mockSteamWebRequest.Setup(x => x.GetAsync<OwnedGamesResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
190+
.ReturnsAsync(new OwnedGamesResultContainer()
191+
{
192+
Result = new OwnedGamesResult()
193+
{
194+
GameCount = 10,
195+
OwnedGames = new List<OwnedGame>()
196+
{
197+
new OwnedGame() { Name = " Test Game 1 " },
198+
new OwnedGame() { Name = " Test Game 2 " },
199+
new OwnedGame() { Name = " Test Game 3 " },
200+
new OwnedGame() { Name = " Test Game 4 " },
201+
new OwnedGame() { Name = " Test Game 5 " },
202+
}
203+
}
204+
});
205+
206+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
207+
var result = await service.GetOwnedGamesAsync(It.IsAny<ulong>(), It.IsAny<bool?>(), It.IsAny<bool?>(), It.IsAny<IReadOnlyCollection<uint>>());
208+
209+
// Make sure all game names have been trimmed on the edges
210+
foreach(var game in result.OwnedGames)
211+
{
212+
Assert.IsTrue(!game.Name.StartsWith(" "));
213+
Assert.IsTrue(!game.Name.EndsWith(" "));
214+
}
215+
}
216+
217+
[TestMethod]
218+
public async Task PlayerService_GetRecentlyPlayedGamesAsync_Return_Null_If_Web_Interface_Returns_Null()
219+
{
220+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
221+
222+
mockSteamWebRequest.Setup(x => x.GetAsync<RecentlyPlayedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
223+
.ReturnsAsync((RecentlyPlayedGameResultContainer)null);
224+
225+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
226+
var result = await service.GetRecentlyPlayedGamesAsync(It.IsAny<ulong>());
227+
228+
Assert.IsNull(result);
229+
}
230+
231+
[TestMethod]
232+
public async Task PlayerService_GetRecentlyPlayedGamesAsync_Return_Null_If_Web_Interface_Result_Is_Null()
233+
{
234+
var mockSteamWebRequest = new Mock<ISteamWebInterface>();
235+
236+
mockSteamWebRequest.Setup(x => x.GetAsync<RecentlyPlayedGameResultContainer>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<IList<SteamWebRequestParameter>>()))
237+
.ReturnsAsync(new RecentlyPlayedGameResultContainer()
238+
{
239+
Result = null
240+
});
241+
242+
var service = new PlayerService(String.Empty, mockSteamWebRequest.Object);
243+
var result = await service.GetRecentlyPlayedGamesAsync(It.IsAny<ulong>());
80244

81-
Assert.IsNotNull(result);
82-
Assert.AreEqual(result.Count, 0);
245+
Assert.IsNull(result);
83246
}
84247
}
85-
}
248+
}

SteamWebAPI2/Interfaces/IPlayerService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public interface IPlayerService
1414

1515
Task<RecentlyPlayedGamesResultModel> GetRecentlyPlayedGamesAsync(ulong steamId);
1616

17-
Task<int> GetSteamLevelAsync(ulong steamId);
17+
Task<uint?> GetSteamLevelAsync(ulong steamId);
1818

19-
Task<string> IsPlayingSharedGameAsync(ulong steamId, uint appId);
19+
Task<ulong?> IsPlayingSharedGameAsync(ulong steamId, uint appId);
2020
}
2121
}

SteamWebAPI2/Interfaces/PlayerService.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.ObjectModel;
77
using System.Threading.Tasks;
88
using SteamWebAPI2.Utilities;
9+
using SteamWebAPI2.Exceptions;
910

1011
namespace SteamWebAPI2.Interfaces
1112
{
@@ -30,7 +31,7 @@ public PlayerService(string steamWebApiKey, ISteamWebInterface steamWebInterface
3031
/// <param name="steamId"></param>
3132
/// <param name="appId"></param>
3233
/// <returns></returns>
33-
public async Task<string> IsPlayingSharedGameAsync(ulong steamId, uint appId)
34+
public async Task<ulong?> IsPlayingSharedGameAsync(ulong steamId, uint appId)
3435
{
3536
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
3637

@@ -41,7 +42,7 @@ public async Task<string> IsPlayingSharedGameAsync(ulong steamId, uint appId)
4142

4243
if (playingSharedGameResult == null || playingSharedGameResult.Result == null)
4344
{
44-
return String.Empty;
45+
return null;
4546
}
4647

4748
return playingSharedGameResult.Result.LenderSteamId;
@@ -62,7 +63,7 @@ public async Task<IReadOnlyCollection<BadgeQuestModel>> GetCommunityBadgeProgres
6263

6364
if(badgeProgressResult == null || badgeProgressResult.Result == null)
6465
{
65-
return new List<BadgeQuestModel>().AsReadOnly();
66+
return null;
6667
}
6768

6869
var badgeProgressModels = AutoMapperConfiguration.Mapper.Map<IList<BadgeQuest>, IList<BadgeQuestModel>>(badgeProgressResult.Result.Quests);
@@ -81,9 +82,9 @@ public async Task<BadgesResultModel> GetBadgesAsync(ulong steamId)
8182
parameters.AddIfHasValue(steamId, "steamid");
8283
var badgesResult = await steamWebInterface.GetAsync<BadgesResultContainer>("GetBadges", 1, parameters);
8384

84-
if(badgesResult == null)
85+
if(badgesResult == null || badgesResult.Result == null)
8586
{
86-
return new BadgesResultModel();
87+
return null;
8788
}
8889

8990
var badgesResultModel = AutoMapperConfiguration.Mapper.Map<BadgesResult, BadgesResultModel>(badgesResult.Result);
@@ -96,11 +97,17 @@ public async Task<BadgesResultModel> GetBadgesAsync(ulong steamId)
9697
/// </summary>
9798
/// <param name="steamId"></param>
9899
/// <returns></returns>
99-
public async Task<int> GetSteamLevelAsync(ulong steamId)
100+
public async Task<uint?> GetSteamLevelAsync(ulong steamId)
100101
{
101102
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
102103
parameters.AddIfHasValue(steamId, "steamid");
103104
var steamLevelResult = await steamWebInterface.GetAsync<SteamLevelResultContainer>("GetSteamLevel", 1, parameters);
105+
106+
if (steamLevelResult == null || steamLevelResult.Result == null)
107+
{
108+
return null;
109+
}
110+
104111
return steamLevelResult.Result.PlayerLevel;
105112
}
106113

@@ -133,12 +140,17 @@ public async Task<OwnedGamesResultModel> GetOwnedGamesAsync(ulong steamId, bool?
133140

134141
var ownedGamesResult = await steamWebInterface.GetAsync<OwnedGamesResultContainer>("GetOwnedGames", 1, parameters);
135142

143+
if(ownedGamesResult == null || ownedGamesResult.Result == null)
144+
{
145+
return null;
146+
}
147+
136148
// for some reason, some games have trailing spaces in the result
137-
if (ownedGamesResult.Result != null && ownedGamesResult.Result.OwnedGames != null)
149+
if (ownedGamesResult.Result.OwnedGames != null)
138150
{
139151
foreach (var ownedGame in ownedGamesResult.Result.OwnedGames)
140152
{
141-
if (!String.IsNullOrEmpty(ownedGame.Name))
153+
if (!String.IsNullOrWhiteSpace(ownedGame.Name))
142154
{
143155
ownedGame.Name = ownedGame.Name.Trim();
144156
}
@@ -161,6 +173,11 @@ public async Task<RecentlyPlayedGamesResultModel> GetRecentlyPlayedGamesAsync(ul
161173
parameters.AddIfHasValue(steamId, "steamid");
162174
var recentlyPlayedGamesResult = await steamWebInterface.GetAsync<RecentlyPlayedGameResultContainer>("GetRecentlyPlayedGames", 1, parameters);
163175

176+
if (recentlyPlayedGamesResult == null || recentlyPlayedGamesResult.Result == null)
177+
{
178+
return null;
179+
}
180+
164181
var recentlyPlayedGamesResultModel = AutoMapperConfiguration.Mapper.Map<RecentlyPlayedGameResult, RecentlyPlayedGamesResultModel>(recentlyPlayedGamesResult.Result);
165182

166183
return recentlyPlayedGamesResultModel;

SteamWebAPI2/Models/SteamCommunity/SteamLevelResultContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SteamWebAPI2.Models.SteamCommunity
55
internal class SteamLevelResult
66
{
77
[JsonProperty("player_level")]
8-
public int PlayerLevel { get; set; }
8+
public uint PlayerLevel { get; set; }
99
}
1010

1111
internal class SteamLevelResultContainer

0 commit comments

Comments
 (0)