Skip to content

Commit

Permalink
unit tests, changed approach for PM bug #360
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffputz committed Feb 6, 2024
1 parent 7495fe2 commit 4330da6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
50 changes: 44 additions & 6 deletions src/PopForums.Test/Composers/PrivateMessageStateComposerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using PopForums.Composers;

namespace PopForums.Test.Composers;
Expand All @@ -19,7 +20,8 @@ public class GetState : PrivateMessageStateComposerTests
public async Task MessagesMappedWithBuffer()
{
var composer = GetComposer();
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123 };
var jsonUsers = JsonSerializer.SerializeToElement(new[] {new {UserID = 2, Name = "Jeff"}, new {UserID = 3, Name = "Diana"}, new {UserID = 4, Name = "Simon"}});
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
var posts = new List<PrivateMessagePost>();
var post1 = new PrivateMessagePost{ PMID = pm.PMID, UserID = 2, Name = "Jeff", PostTime = new DateTime(2020,1,1), FullText = "post1", PMPostID = 1};
var post2 = new PrivateMessagePost{ PMID = pm.PMID, UserID = 3, Name = "Diana", PostTime = new DateTime(2021,1,1), FullText = "post2", PMPostID = 2};
Expand All @@ -28,6 +30,11 @@ public async Task MessagesMappedWithBuffer()
posts.Add(post2);
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(posts);
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost> { post3 });
_privateMessageService.GetUsers(pm.PMID).Returns([
new PrivateMessageUser { PMID = pm.PMID, UserID = 2 },
new PrivateMessageUser { PMID = pm.PMID, UserID = 3 },
new PrivateMessageUser { PMID = pm.PMID, UserID = 4 }
]);

var state = await composer.GetState(pm);

Expand All @@ -54,7 +61,8 @@ public async Task MessagesMappedWithBuffer()
public async Task NewestPostIDSet()
{
var composer = GetComposer();
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123 };
var jsonUsers = JsonSerializer.SerializeToElement(Array.Empty<object>());
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
var posts = new List<PrivateMessagePost>();
var post1 = new PrivateMessagePost{ PMID = pm.PMID };
var post2 = new PrivateMessagePost{ PMID = pm.PMID };
Expand All @@ -63,6 +71,7 @@ public async Task NewestPostIDSet()
posts.Add(post2);
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(posts);
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost> { post3 });
_privateMessageService.GetUsers(pm.PMID).Returns(new List<PrivateMessageUser>());

var state = await composer.GetState(pm);

Expand All @@ -73,9 +82,11 @@ public async Task NewestPostIDSet()
public async Task PMIDSet()
{
var composer = GetComposer();
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123 };
var jsonUsers = JsonSerializer.SerializeToElement(Array.Empty<object>());
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetUsers(pm.PMID).Returns(new List<PrivateMessageUser>());

var state = await composer.GetState(pm);

Expand All @@ -86,23 +97,50 @@ public async Task PMIDSet()
public async Task PMUsersJsonSet()
{
var composer = GetComposer();
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = new JsonElement()};
var jsonUsers = JsonSerializer.SerializeToElement(Array.Empty<object>());
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetUsers(pm.PMID).Returns(new List<PrivateMessageUser>());

var state = await composer.GetState(pm);

Assert.Equal(pm.Users, state.Users);
}

[Fact]
public async Task IsUserNotFoundSet()
public async Task IsUserNotFoundSetToFalse()
{
var composer = GetComposer();
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = new JsonElement()};
var jsonUsers = JsonSerializer.SerializeToElement(new[] {new {UserID = 2, Name = "Jeff"}, new {UserID = 3, Name = "Diana"}, new {UserID = 4, Name = "Simon"}});
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.IsUserNotFound(pm.PMID).Returns(true);
_privateMessageService.GetUsers(pm.PMID).Returns([
new PrivateMessageUser { PMID = pm.PMID, UserID = 2 },
new PrivateMessageUser { PMID = pm.PMID, UserID = 3 },
new PrivateMessageUser { PMID = pm.PMID, UserID = 4 }
]);

var state = await composer.GetState(pm);

Assert.False(state.IsUserNotFound);
}

[Fact]
public async Task IsUserNotFoundSetToTrue()
{
var composer = GetComposer();
var jsonUsers = JsonSerializer.SerializeToElement(new[] {new {UserID = 2, Name = "Jeff"}, new {UserID = 3, Name = "Diana"}, new {UserID = 4, Name = "Simon"}});
var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers};
_privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List<PrivateMessagePost>());
_privateMessageService.IsUserNotFound(pm.PMID).Returns(true);
_privateMessageService.GetUsers(pm.PMID).Returns([
new PrivateMessageUser { PMID = pm.PMID, UserID = 2 },
new PrivateMessageUser { PMID = pm.PMID, UserID = 4 }
]);

var state = await composer.GetState(pm);

Expand Down
4 changes: 2 additions & 2 deletions src/PopForums/Composers/PrivateMessageStateComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public async Task<PrivateMessageState> GetState(PrivateMessage pm)
var clientMessages = ClientPrivateMessagePost.MapForClient(messages);
state.Messages = clientMessages;
state.Users = pm.Users;
var pmUsersFromPMRecord = pm.Users.EnumerateArray();
var pmUsersFromPMRecord = pm.Users.Deserialize<List<object>>();
var pmUsers = await _privateMessageService.GetUsers(pm.PMID);
var isUserNotFound = pmUsers.Count != pmUsersFromPMRecord.Count();
var isUserNotFound = pmUsers.Count != pmUsersFromPMRecord.Count;
state.IsUserNotFound = isUserNotFound;
return state;
}
Expand Down

0 comments on commit 4330da6

Please sign in to comment.