From 7611bedef774ff8dc7e61acc36edaa6175a55a00 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Wed, 19 Jul 2023 10:56:49 -0600 Subject: [PATCH] conversation: adds Equal() --- model/conversation.go | 38 +++++++++++++++++++++++++++++++++++++- web/server_test.go | 23 +++-------------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/model/conversation.go b/model/conversation.go index f99f301..d376581 100644 --- a/model/conversation.go +++ b/model/conversation.go @@ -24,7 +24,7 @@ func (c *Conversation) GetDisplayName() string { return *c.DisplayName } -func (c Conversation) MarshalZerologObject(e *zerolog.Event) { +func (c *Conversation) MarshalZerologObject(e *zerolog.Event) { e.Str("guid", c.Guid) e.Time("date", c.Date) e.Int("unreadCount", c.UnreadCount) @@ -34,3 +34,39 @@ func (c Conversation) MarshalZerologObject(e *zerolog.Event) { e.Str("displayName", *c.DisplayName) } } + +func (c *Conversation) Equal(o *Conversation) bool { + if c.Guid != o.Guid { + return false + } + + if !c.Date.Equal(o.Date) { + return false + } + + if c.UnreadCount != o.UnreadCount { + return false + } + + if c.LastMessagePreview != o.LastMessagePreview { + return false + } + + if len(c.Participants) != len(o.Participants) { + return false + } + + for i, p := range c.Participants { + if p != o.Participants[i] { + return false + } + } + + if c.DisplayName != nil && o.DisplayName != nil { + if *c.DisplayName != *o.DisplayName { + return false + } + } + + return true +} diff --git a/web/server_test.go b/web/server_test.go index 9f8899a..173ad0e 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -84,26 +84,9 @@ func TestConversations(t *testing.T) { t.Fatalf("Unexpected number of conversations: %d (expected %d)", len(convos), 1) } - if convos[0].Guid != conversation.Guid { - t.Fatalf("Unexpected conversation GUID: %s (expected %s)", convos[0].Guid, conversation.Guid) - } - - if convos[0].LastMessagePreview != conversation.LastMessagePreview { - t.Fatalf("Unexpected conversation LastMessagePreview: %s (expected %s)", convos[0].LastMessagePreview, conversation.LastMessagePreview) - } - - if convos[0].UnreadCount != conversation.UnreadCount { - t.Fatalf("Unexpected conversation UnreadCount: %d (expected %d)", convos[0].UnreadCount, conversation.UnreadCount) - } - - for i, p := range convos[0].Participants { - if p != conversation.Participants[i] { - t.Fatalf("Unexpected conversation Participants: %s (expected %s)", p, conversation.Participants[i]) - } - } - - if !convos[0].Date.Equal(conversation.Date) { - t.Fatalf("Unexpected conversation Date: %s (expected %s)", convos[0].Date, conversation.Date) + testConversation := &convos[0] + if testConversation.Equal(&conversation) != true { + t.Fatalf("Unexpected conversation: %v (expected %v)", convos[0], conversation) } }