From 91bbeb99ed7ba1706c305baf1afb179b24f7d5e8 Mon Sep 17 00:00:00 2001 From: yqrashawn Date: Fri, 28 Oct 2022 17:10:14 +0800 Subject: [PATCH] test: TestLatestMessageByChatID --- protocol/persistence_test.go | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/protocol/persistence_test.go b/protocol/persistence_test.go index 4598794a6..fc3c92d22 100644 --- a/protocol/persistence_test.go +++ b/protocol/persistence_test.go @@ -247,6 +247,34 @@ func TestMessageByChatID(t *testing.T) { ) } +func TestLatestMessageByChatID(t *testing.T) { + db, err := openTestDB() + require.NoError(t, err) + p := newSQLitePersistence(db) + + var ids []string + for i := 0; i < 10; i++ { + id := strconv.Itoa(i) + err := insertMinimalMessage(p, id) + require.NoError(t, err) + ids = append(ids, id) + } + + id := strconv.Itoa(10) + err = insertMinimalDeletedMessage(p, id) + require.NoError(t, err) + ids = append(ids, id) + + id = strconv.Itoa(11) + err = insertMinimalDeletedForMeMessage(p, id) + require.NoError(t, err) + ids = append(ids, id) + + m, err := p.LatestMessageByChatID(testPublicChatID) + require.NoError(t, err) + require.Equal(t, m[0].ID, ids[9]) +} + func TestOldestMessageWhisperTimestampByChatID(t *testing.T) { db, err := openTestDB() require.NoError(t, err) @@ -781,6 +809,26 @@ func insertMinimalMessage(p *sqlitePersistence, id string) error { }}) } +func insertMinimalDeletedMessage(p *sqlitePersistence, id string) error { + return p.SaveMessages([]*common.Message{{ + ID: id, + Deleted: true, + LocalChatID: testPublicChatID, + ChatMessage: protobuf.ChatMessage{Text: "some-text"}, + From: "me", + }}) +} + +func insertMinimalDeletedForMeMessage(p *sqlitePersistence, id string) error { + return p.SaveMessages([]*common.Message{{ + ID: id, + DeletedForMe: true, + LocalChatID: testPublicChatID, + ChatMessage: protobuf.ChatMessage{Text: "some-text"}, + From: "me", + }}) +} + func insertDiscordMessageWithAttachments(p *sqlitePersistence, id string, discordMessageID string) error { err := insertMinimalDiscordMessage(p, id, discordMessageID) if err != nil {