2019-07-31 07:50:08 +00:00
|
|
|
package statusproto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/status-im/status-protocol-go/sqlite"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTableUserMessagesAllFieldsCount(t *testing.T) {
|
|
|
|
db := sqlitePersistence{}
|
|
|
|
expected := len(strings.Split(db.tableUserMessagesLegacyAllFields(), ","))
|
|
|
|
require.Equal(t, expected, db.tableUserMessagesLegacyAllFieldsCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveMessages(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
err := insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, id, m.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageByID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, id, m.ID)
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
func TestMessagesExist(t *testing.T) {
|
2019-07-31 07:50:08 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = insertMinimalMessage(p, "1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result, err := p.MessagesExist([]string{"1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, result["1"])
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, "2")
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
result, err = p.MessagesExist([]string{"1", "2", "3"})
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-13 13:09:46 +00:00
|
|
|
|
|
|
|
require.True(t, result["1"])
|
|
|
|
require.True(t, result["2"])
|
|
|
|
require.False(t, result["3"])
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageByChatID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
chatID := "super-chat"
|
|
|
|
count := 1000
|
|
|
|
pageSize := 50
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
var messages []*Message
|
2019-07-31 07:50:08 +00:00
|
|
|
for i := 0; i < count; i++ {
|
2019-08-13 13:09:46 +00:00
|
|
|
messages = append(messages, &Message{
|
|
|
|
ID: strconv.Itoa(i),
|
|
|
|
ChatID: chatID,
|
|
|
|
From: "me",
|
|
|
|
ClockValue: int64(i),
|
2019-07-31 07:50:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Add some other chats.
|
|
|
|
if count%5 == 0 {
|
2019-08-13 13:09:46 +00:00
|
|
|
messages = append(messages, &Message{
|
|
|
|
ID: strconv.Itoa(count + i),
|
|
|
|
ChatID: "other-chat",
|
|
|
|
From: "me",
|
|
|
|
ClockValue: int64(i),
|
2019-07-31 07:50:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some out-of-order message. Add more than page size.
|
|
|
|
outOfOrderCount := pageSize + 1
|
|
|
|
allCount := count + outOfOrderCount
|
|
|
|
for i := 0; i < pageSize+1; i++ {
|
2019-08-13 13:09:46 +00:00
|
|
|
messages = append(messages, &Message{
|
|
|
|
ID: strconv.Itoa(count*2 + i),
|
|
|
|
ChatID: chatID,
|
|
|
|
From: "me",
|
|
|
|
ClockValue: int64(i), // use very old clock values
|
2019-07-31 07:50:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-31 07:50:08 +00:00
|
|
|
var (
|
|
|
|
result []*Message
|
|
|
|
cursor string
|
|
|
|
iter int
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
items []*Message
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
items, cursor, err = p.MessageByChatID(chatID, cursor, pageSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
result = append(result, items...)
|
|
|
|
|
|
|
|
iter++
|
|
|
|
if len(cursor) == 0 || iter > count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "", cursor) // for loop should exit because of cursor being empty
|
|
|
|
require.EqualValues(t, math.Ceil(float64(allCount)/float64(pageSize)), iter)
|
|
|
|
require.Equal(t, len(result), allCount)
|
|
|
|
require.True(
|
|
|
|
t,
|
|
|
|
// Verify descending order.
|
|
|
|
sort.SliceIsSorted(result, func(i, j int) bool {
|
|
|
|
return result[i].ClockValue > result[j].ClockValue
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
func TestMessageReplies(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
chatID := "super-chat"
|
|
|
|
message1 := &Message{
|
|
|
|
ID: "id-1",
|
|
|
|
ChatID: chatID,
|
|
|
|
Content: "content-1",
|
|
|
|
From: "1",
|
|
|
|
ClockValue: int64(1),
|
|
|
|
}
|
|
|
|
message2 := &Message{
|
|
|
|
ID: "id-2",
|
|
|
|
ChatID: chatID,
|
|
|
|
Content: "content-2",
|
|
|
|
From: "2",
|
|
|
|
ClockValue: int64(2),
|
|
|
|
ReplyTo: "id-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
message3 := &Message{
|
|
|
|
ID: "id-3",
|
|
|
|
ChatID: chatID,
|
|
|
|
Content: "content-3",
|
|
|
|
From: "3",
|
|
|
|
ClockValue: int64(3),
|
|
|
|
ReplyTo: "non-existing",
|
|
|
|
}
|
|
|
|
|
|
|
|
messages := []*Message{message1, message2, message3}
|
|
|
|
|
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedMessages, _, err := p.MessageByChatID(chatID, "", 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "non-existing", retrievedMessages[0].ReplyTo)
|
|
|
|
require.Nil(t, retrievedMessages[0].QuotedMessage)
|
|
|
|
|
|
|
|
require.Equal(t, "id-1", retrievedMessages[1].ReplyTo)
|
|
|
|
require.Equal(t, &QuotedMessage{From: "1", Content: "content-1"}, retrievedMessages[1].QuotedMessage)
|
|
|
|
|
|
|
|
require.Equal(t, "", retrievedMessages[2].ReplyTo)
|
|
|
|
require.Nil(t, retrievedMessages[2].QuotedMessage)
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:50:08 +00:00
|
|
|
func TestMessageByChatIDWithTheSameClockValues(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
chatID := "super-chat"
|
|
|
|
clockValues := []int64{10, 10, 9, 9, 9, 11, 12, 11, 100000, 6, 4, 5, 5, 5, 5}
|
|
|
|
count := len(clockValues)
|
|
|
|
pageSize := 2
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
var messages []*Message
|
|
|
|
|
2019-07-31 07:50:08 +00:00
|
|
|
for i, clock := range clockValues {
|
2019-08-13 13:09:46 +00:00
|
|
|
messages = append(messages, &Message{
|
|
|
|
ID: strconv.Itoa(i),
|
|
|
|
ChatID: chatID,
|
|
|
|
From: "me",
|
|
|
|
ClockValue: clock,
|
2019-07-31 07:50:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-31 07:50:08 +00:00
|
|
|
var (
|
|
|
|
result []*Message
|
|
|
|
cursor string
|
|
|
|
iter int
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
items []*Message
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
items, cursor, err = p.MessageByChatID(chatID, cursor, pageSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
result = append(result, items...)
|
|
|
|
|
|
|
|
iter++
|
|
|
|
if cursor == "" || iter > count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Empty(t, cursor) // for loop should exit because of cursor being empty
|
|
|
|
require.Len(t, result, count)
|
|
|
|
// Verify the order.
|
|
|
|
expectedClockValues := make([]int64, len(clockValues))
|
|
|
|
copy(expectedClockValues, clockValues)
|
|
|
|
sort.Slice(expectedClockValues, func(i, j int) bool {
|
|
|
|
return expectedClockValues[i] > expectedClockValues[j]
|
|
|
|
})
|
|
|
|
resultClockValues := make([]int64, 0, len(clockValues))
|
|
|
|
for _, m := range result {
|
|
|
|
resultClockValues = append(resultClockValues, m.ClockValue)
|
|
|
|
}
|
|
|
|
require.EqualValues(t, expectedClockValues, resultClockValues)
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
func TestDeleteMessageByID(t *testing.T) {
|
2019-07-31 07:50:08 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
2019-08-13 13:09:46 +00:00
|
|
|
id := "1"
|
2019-07-31 07:50:08 +00:00
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
2019-07-31 07:50:08 +00:00
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
m, err := p.MessageByID(id)
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-13 13:09:46 +00:00
|
|
|
require.Equal(t, id, m.ID)
|
|
|
|
|
|
|
|
err = p.DeleteMessage(m.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = p.MessageByID(id)
|
|
|
|
require.EqualError(t, err, "record not found")
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
func TestDeleteMessagesByChatID(t *testing.T) {
|
2019-07-31 07:50:08 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = insertMinimalMessage(p, "1")
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = insertMinimalMessage(p, "2")
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
m, _, err := p.MessageByChatID("chat-id", "", 10)
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-13 13:09:46 +00:00
|
|
|
require.Equal(t, 2, len(m))
|
2019-07-31 07:50:08 +00:00
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
err = p.DeleteMessagesByChatID("chat-id")
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
m, _, err = p.MessageByChatID("chat-id", "", 10)
|
2019-07-31 07:50:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-13 13:09:46 +00:00
|
|
|
require.Equal(t, 0, len(m))
|
2019-07-31 07:50:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarkMessageSeen(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, m.Seen)
|
|
|
|
|
|
|
|
err = p.MarkMessagesSeen(m.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err = p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, m.Seen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateMessageOutgoingStatus(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = p.UpdateMessageOutgoingStatus(id, "new-status")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "new-status", m.OutgoingStatus)
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:05:58 +00:00
|
|
|
func TestSetContactGeneratedData(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
existingContact := Contact{
|
|
|
|
ID: "contact-one",
|
|
|
|
Address: "contact-address",
|
|
|
|
Name: "contact-name",
|
|
|
|
Photo: "contact-photo",
|
|
|
|
LastUpdated: 20,
|
|
|
|
SystemTags: []string{"1", "2"},
|
|
|
|
DeviceInfo: []ContactDeviceInfo{
|
|
|
|
ContactDeviceInfo{
|
|
|
|
InstallationID: "1",
|
|
|
|
Timestamp: 2,
|
|
|
|
FCMToken: "token",
|
|
|
|
},
|
|
|
|
ContactDeviceInfo{
|
|
|
|
InstallationID: "2",
|
|
|
|
Timestamp: 3,
|
|
|
|
FCMToken: "token-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TributeToTalk: "talk",
|
|
|
|
}
|
|
|
|
|
|
|
|
existingContactUpdate := Contact{
|
|
|
|
ID: "contact-one",
|
|
|
|
Address: "contact-address",
|
|
|
|
Alias: "generated-name-one",
|
|
|
|
}
|
|
|
|
|
|
|
|
nonExistingContactUpdate := Contact{
|
|
|
|
ID: "contact-two",
|
|
|
|
Address: "contact-address",
|
|
|
|
Alias: "generated-name-two",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.SaveContact(existingContact, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-08 12:09:37 +00:00
|
|
|
err = p.SetContactsGeneratedData([]Contact{existingContactUpdate, nonExistingContactUpdate}, nil)
|
2019-09-16 15:05:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
allContacts, err := p.Contacts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 2, len(allContacts))
|
|
|
|
|
|
|
|
// Make sure it has not been modified
|
|
|
|
require.Equal(t, int64(20), allContacts[0].LastUpdated)
|
|
|
|
|
|
|
|
// Ensure new contact has been saved
|
|
|
|
require.Equal(t, "contact-two", allContacts[1].ID)
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:50:08 +00:00
|
|
|
func openTestDB() (*sql.DB, error) {
|
|
|
|
dbPath, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-21 08:31:20 +00:00
|
|
|
return sqlite.Open(dbPath.Name(), "")
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertMinimalMessage(p sqlitePersistence, id string) error {
|
2019-08-13 13:09:46 +00:00
|
|
|
return p.SaveMessagesLegacy([]*Message{&Message{
|
|
|
|
ID: id,
|
|
|
|
ChatID: "chat-id",
|
|
|
|
From: "me",
|
|
|
|
}})
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|