From 4330da601599fc5a8154086b24444deddb267b86 Mon Sep 17 00:00:00 2001 From: Jeff Putz Date: Tue, 6 Feb 2024 14:10:13 -0500 Subject: [PATCH] unit tests, changed approach for PM bug #360 --- .../PrivateMessageStateComposerTests.cs | 50 ++++++++++++++++--- .../Composers/PrivateMessageStateComposer.cs | 4 +- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/PopForums.Test/Composers/PrivateMessageStateComposerTests.cs b/src/PopForums.Test/Composers/PrivateMessageStateComposerTests.cs index 79e62e2f..514f2c07 100644 --- a/src/PopForums.Test/Composers/PrivateMessageStateComposerTests.cs +++ b/src/PopForums.Test/Composers/PrivateMessageStateComposerTests.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; using PopForums.Composers; namespace PopForums.Test.Composers; @@ -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(); 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}; @@ -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 { 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); @@ -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()); + var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers}; var posts = new List(); var post1 = new PrivateMessagePost{ PMID = pm.PMID }; var post2 = new PrivateMessagePost{ PMID = pm.PMID }; @@ -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 { post3 }); + _privateMessageService.GetUsers(pm.PMID).Returns(new List()); var state = await composer.GetState(pm); @@ -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()); + var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers}; _privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List()); _privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List()); + _privateMessageService.GetUsers(pm.PMID).Returns(new List()); var state = await composer.GetState(pm); @@ -86,9 +97,11 @@ 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()); + var pm = new PrivateMessage { LastViewDate = DateTime.UtcNow, PMID = 123, Users = jsonUsers}; _privateMessageService.GetMostRecentPosts(pm.PMID, pm.LastViewDate).Returns(new List()); _privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List()); + _privateMessageService.GetUsers(pm.PMID).Returns(new List()); var state = await composer.GetState(pm); @@ -96,13 +109,38 @@ public async Task PMUsersJsonSet() } [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()); _privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List()); _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()); + _privateMessageService.GetPosts(pm.PMID, pm.LastViewDate).Returns(new List()); + _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); diff --git a/src/PopForums/Composers/PrivateMessageStateComposer.cs b/src/PopForums/Composers/PrivateMessageStateComposer.cs index 56b16b41..1ad52740 100644 --- a/src/PopForums/Composers/PrivateMessageStateComposer.cs +++ b/src/PopForums/Composers/PrivateMessageStateComposer.cs @@ -25,9 +25,9 @@ public async Task 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>(); var pmUsers = await _privateMessageService.GetUsers(pm.PMID); - var isUserNotFound = pmUsers.Count != pmUsersFromPMRecord.Count(); + var isUserNotFound = pmUsers.Count != pmUsersFromPMRecord.Count; state.IsUserNotFound = isUserNotFound; return state; }