2021-01-28 14:43:00 +01:00
|
|
|
package mstore_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-13 09:47:06 +02:00
|
|
|
"git.ewintr.nl/go-kit/test"
|
|
|
|
"git.ewintr.nl/gte/pkg/mstore"
|
2021-01-28 14:43:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewMemory(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
folders []string
|
|
|
|
exp error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
folders: []string{},
|
|
|
|
exp: mstore.ErrInvalidFolderSet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil",
|
|
|
|
exp: mstore.ErrInvalidFolderSet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty string",
|
|
|
|
folders: []string{""},
|
|
|
|
exp: mstore.ErrInvalidFolderName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
folders: []string{"one"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := mstore.NewMemory(tc.folders)
|
|
|
|
test.Equals(t, tc.exp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryFolders(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
folders []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
folders: []string{"one"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "many",
|
|
|
|
folders: []string{"one", "two", "three"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mem, err := mstore.NewMemory(tc.folders)
|
|
|
|
test.OK(t, err)
|
|
|
|
actFolders, err := mem.Folders()
|
|
|
|
test.OK(t, err)
|
|
|
|
expFolders := tc.folders
|
|
|
|
sort.Strings(expFolders)
|
|
|
|
test.Equals(t, expFolders, actFolders)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryAdd(t *testing.T) {
|
|
|
|
folder := "folder"
|
2021-01-30 11:20:12 +01:00
|
|
|
subject := "subject"
|
2021-01-28 14:43:00 +01:00
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2021-01-30 11:20:12 +01:00
|
|
|
folder string
|
2021-01-28 14:43:00 +01:00
|
|
|
subject string
|
2021-01-30 11:20:12 +01:00
|
|
|
expMsgs []*mstore.Message
|
|
|
|
expErr error
|
2021-01-28 14:43:00 +01:00
|
|
|
}{
|
|
|
|
{
|
2021-01-30 11:20:12 +01:00
|
|
|
name: "empty",
|
|
|
|
folder: folder,
|
|
|
|
expErr: mstore.ErrInvalidMessage,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid folder",
|
|
|
|
folder: "not there",
|
|
|
|
subject: subject,
|
|
|
|
expErr: mstore.ErrFolderDoesNotExist,
|
2021-01-28 14:43:00 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid",
|
2021-01-30 11:20:12 +01:00
|
|
|
folder: folder,
|
|
|
|
subject: subject,
|
|
|
|
expMsgs: []*mstore.Message{
|
|
|
|
{Uid: 1, Folder: folder, Subject: subject},
|
|
|
|
},
|
2021-01-28 14:43:00 +01:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mem, err := mstore.NewMemory([]string{folder})
|
|
|
|
test.OK(t, err)
|
2021-01-30 11:20:12 +01:00
|
|
|
test.Equals(t, tc.expErr, mem.Add(tc.folder, tc.subject, ""))
|
2021-01-28 14:43:00 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryMessages(t *testing.T) {
|
|
|
|
folderA := "folderA"
|
|
|
|
folderB := "folderB"
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
folder string
|
|
|
|
amount int
|
|
|
|
expErr error
|
|
|
|
}{
|
2021-01-30 11:20:12 +01:00
|
|
|
{
|
|
|
|
name: "unknown folder",
|
|
|
|
folder: "not there",
|
|
|
|
expErr: mstore.ErrFolderDoesNotExist,
|
|
|
|
},
|
2021-01-28 14:43:00 +01:00
|
|
|
{
|
|
|
|
name: "empty folder",
|
|
|
|
folder: folderB,
|
|
|
|
amount: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
folder: folderA,
|
|
|
|
amount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "many",
|
|
|
|
folder: folderA,
|
|
|
|
amount: 3,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mem, err := mstore.NewMemory([]string{folderA, folderB})
|
|
|
|
test.OK(t, err)
|
|
|
|
|
|
|
|
expMessages := []*mstore.Message{}
|
|
|
|
for i := 1; i <= tc.amount; i++ {
|
|
|
|
m := &mstore.Message{
|
|
|
|
Uid: uint32(i),
|
2021-01-30 11:20:12 +01:00
|
|
|
Folder: folderA,
|
2021-01-28 14:43:00 +01:00
|
|
|
Subject: fmt.Sprintf("subject-%d", i),
|
|
|
|
Body: fmt.Sprintf("body-%d", i),
|
|
|
|
}
|
|
|
|
if tc.folder == folderA {
|
|
|
|
expMessages = append(expMessages, m)
|
|
|
|
}
|
2021-01-29 12:29:23 +01:00
|
|
|
test.OK(t, mem.Add(folderA, m.Subject, m.Body))
|
2021-01-28 14:43:00 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 11:20:12 +01:00
|
|
|
actMessages, err := mem.Messages(tc.folder)
|
2021-01-28 14:43:00 +01:00
|
|
|
test.Equals(t, tc.expErr, err)
|
|
|
|
test.Equals(t, expMessages, actMessages)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryRemove(t *testing.T) {
|
|
|
|
folderA, folderB := "folderA", "folderB"
|
2021-01-30 11:20:12 +01:00
|
|
|
subject := "subject"
|
2021-01-28 14:43:00 +01:00
|
|
|
|
|
|
|
mem, err := mstore.NewMemory([]string{folderA, folderB})
|
|
|
|
test.OK(t, err)
|
|
|
|
for i := 1; i <= 3; i++ {
|
2021-01-29 12:29:23 +01:00
|
|
|
test.OK(t, mem.Add(folderA, fmt.Sprintf("subject-%d", i), ""))
|
2021-01-28 14:43:00 +01:00
|
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2021-01-30 11:20:12 +01:00
|
|
|
msg *mstore.Message
|
2021-01-28 14:43:00 +01:00
|
|
|
expUids []uint32
|
|
|
|
expErr error
|
|
|
|
}{
|
|
|
|
{
|
2021-01-30 11:20:12 +01:00
|
|
|
name: "empty",
|
|
|
|
msg: &mstore.Message{
|
|
|
|
Uid: 1,
|
|
|
|
Folder: folderB,
|
|
|
|
Subject: subject,
|
|
|
|
},
|
|
|
|
expErr: mstore.ErrMessageDoesNotExist,
|
2021-01-28 14:43:00 +01:00
|
|
|
},
|
|
|
|
{
|
2021-01-30 11:20:12 +01:00
|
|
|
name: "nil message",
|
|
|
|
expErr: mstore.ErrInvalidMessage,
|
2021-01-28 14:43:00 +01:00
|
|
|
},
|
|
|
|
{
|
2021-01-30 11:20:12 +01:00
|
|
|
name: "unknown folder",
|
|
|
|
msg: &mstore.Message{
|
|
|
|
Uid: 1,
|
|
|
|
Folder: "unknown",
|
|
|
|
Subject: subject,
|
|
|
|
},
|
|
|
|
expErr: mstore.ErrFolderDoesNotExist,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid",
|
|
|
|
msg: &mstore.Message{
|
|
|
|
Uid: 2,
|
|
|
|
Folder: folderA,
|
|
|
|
Subject: subject,
|
|
|
|
},
|
2021-01-28 14:43:00 +01:00
|
|
|
expUids: []uint32{1, 3},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-01-30 11:20:12 +01:00
|
|
|
test.Equals(t, tc.expErr, mem.Remove(tc.msg))
|
2021-01-28 14:43:00 +01:00
|
|
|
if tc.expErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
actUids := []uint32{}
|
2021-01-30 11:20:12 +01:00
|
|
|
actMsgs, err := mem.Messages(tc.msg.Folder)
|
2021-01-28 14:43:00 +01:00
|
|
|
test.OK(t, err)
|
|
|
|
for _, m := range actMsgs {
|
|
|
|
actUids = append(actUids, m.Uid)
|
|
|
|
}
|
|
|
|
test.Equals(t, tc.expUids, actUids)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|