2021-06-25 09:14:27 +02:00
|
|
|
package storage_test
|
2021-01-30 11:20:12 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 11:59:26 +02:00
|
|
|
"ewintr.nl/go-kit/test"
|
|
|
|
"ewintr.nl/gte/internal/storage"
|
|
|
|
"ewintr.nl/gte/internal/task"
|
|
|
|
"ewintr.nl/gte/pkg/mstore"
|
2021-01-30 11:20:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoFindAll(t *testing.T) {
|
|
|
|
folderA := "folderA"
|
|
|
|
folderB := "folderB"
|
|
|
|
|
|
|
|
type msgs struct {
|
|
|
|
Folder string
|
|
|
|
Subject string
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
tasks []msgs
|
|
|
|
folder string
|
|
|
|
expTasks int
|
|
|
|
expErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
folder: folderA,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown folder",
|
|
|
|
folder: "unknown",
|
2021-06-25 09:14:27 +02:00
|
|
|
expErr: storage.ErrMStoreError,
|
2021-01-30 11:20:12 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not empty",
|
|
|
|
folder: folderA,
|
|
|
|
tasks: []msgs{
|
|
|
|
{Folder: folderA, Subject: "sub-1"},
|
|
|
|
{Folder: folderA, Subject: "sub-2"},
|
|
|
|
{Folder: folderB, Subject: "sub-3"},
|
|
|
|
{Folder: folderA, Subject: "sub-4"},
|
|
|
|
},
|
|
|
|
expTasks: 3,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
store, err := mstore.NewMemory([]string{folderA, folderB})
|
|
|
|
test.OK(t, err)
|
|
|
|
for _, task := range tc.tasks {
|
|
|
|
test.OK(t, store.Add(task.Folder, task.Subject, "body"))
|
|
|
|
}
|
2021-06-25 09:14:27 +02:00
|
|
|
repo := storage.NewRemoteRepository(store)
|
2021-01-30 11:20:12 +01:00
|
|
|
actTasks, err := repo.FindAll(tc.folder)
|
|
|
|
test.Equals(t, true, errors.Is(err, tc.expErr))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test.Equals(t, tc.expTasks, len(actTasks))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoUpdate(t *testing.T) {
|
|
|
|
id := "id"
|
2021-06-04 10:45:56 +02:00
|
|
|
oldFolder := task.FOLDER_INBOX
|
|
|
|
folder := task.FOLDER_NEW
|
2021-01-30 11:20:12 +01:00
|
|
|
action := "action"
|
|
|
|
|
|
|
|
oldMsg := &mstore.Message{
|
|
|
|
Uid: 1,
|
|
|
|
Folder: oldFolder,
|
|
|
|
Subject: "old subject",
|
|
|
|
Body: "old body",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
task *task.Task
|
|
|
|
expErr error
|
|
|
|
expMsgs []*mstore.Message
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil task",
|
2021-06-25 09:14:27 +02:00
|
|
|
expErr: storage.ErrInvalidTask,
|
2021-01-30 11:20:12 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "task without message",
|
|
|
|
task: &task.Task{
|
|
|
|
Id: id,
|
|
|
|
Folder: folder,
|
|
|
|
Action: action,
|
|
|
|
},
|
2021-06-25 09:14:27 +02:00
|
|
|
expErr: storage.ErrMStoreError,
|
2021-01-30 11:20:12 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "changed task",
|
|
|
|
task: &task.Task{
|
|
|
|
Id: id,
|
|
|
|
Folder: folder,
|
|
|
|
Action: action,
|
|
|
|
Message: oldMsg,
|
|
|
|
},
|
|
|
|
expMsgs: []*mstore.Message{
|
|
|
|
{Uid: 2, Folder: folder, Subject: action},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mem, err := mstore.NewMemory([]string{folder, oldFolder})
|
|
|
|
test.OK(t, err)
|
|
|
|
test.OK(t, mem.Add(oldMsg.Folder, oldMsg.Subject, oldMsg.Body))
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
repo := storage.NewRemoteRepository(mem)
|
2021-01-30 11:20:12 +01:00
|
|
|
|
|
|
|
actErr := repo.Update(tc.task)
|
|
|
|
test.Equals(t, true, errors.Is(actErr, tc.expErr))
|
|
|
|
if tc.expErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actMsgs, err := mem.Messages(folder)
|
|
|
|
test.OK(t, err)
|
|
|
|
for i, _ := range actMsgs {
|
|
|
|
actMsgs[i].Body = ""
|
|
|
|
}
|
|
|
|
test.Equals(t, tc.expMsgs, actMsgs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCleanUp(t *testing.T) {
|
|
|
|
id := "id"
|
|
|
|
subject := "subject"
|
|
|
|
|
2021-07-10 11:29:48 +02:00
|
|
|
mem, err := mstore.NewMemory(task.KnownFolders)
|
2021-01-30 11:20:12 +01:00
|
|
|
test.OK(t, err)
|
|
|
|
|
|
|
|
for v := 1; v <= 3; v++ {
|
|
|
|
body := fmt.Sprintf(`
|
|
|
|
id: %s
|
|
|
|
version: %d
|
|
|
|
`, id, v)
|
2021-07-10 11:29:48 +02:00
|
|
|
folder := task.FOLDER_NEW
|
2021-01-30 11:20:12 +01:00
|
|
|
if v%2 == 1 {
|
2021-07-10 11:29:48 +02:00
|
|
|
folder = task.FOLDER_PLANNED
|
2021-01-30 11:20:12 +01:00
|
|
|
}
|
|
|
|
test.OK(t, mem.Add(folder, subject, body))
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
repo := storage.NewRemoteRepository(mem)
|
2021-01-30 11:20:12 +01:00
|
|
|
test.OK(t, repo.CleanUp())
|
|
|
|
|
|
|
|
expNew := []*mstore.Message{}
|
2021-07-10 11:29:48 +02:00
|
|
|
actNew, err := mem.Messages(task.FOLDER_NEW)
|
2021-01-30 11:20:12 +01:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Equals(t, expNew, actNew)
|
|
|
|
expPlanned := []*mstore.Message{{
|
|
|
|
Uid: 3,
|
2021-07-10 11:29:48 +02:00
|
|
|
Folder: task.FOLDER_PLANNED,
|
2021-01-30 11:20:12 +01:00
|
|
|
Subject: subject,
|
|
|
|
Body: `
|
|
|
|
id: id
|
|
|
|
version: 3
|
|
|
|
`,
|
|
|
|
}}
|
2021-07-10 11:29:48 +02:00
|
|
|
actPlanned, err := mem.Messages(task.FOLDER_PLANNED)
|
2021-01-30 11:20:12 +01:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Equals(t, expPlanned, actPlanned)
|
|
|
|
}
|
2021-07-10 11:29:48 +02:00
|
|
|
|
|
|
|
func TestRepoRemove(t *testing.T) {
|
|
|
|
mem, err := mstore.NewMemory(task.KnownFolders)
|
|
|
|
test.OK(t, err)
|
|
|
|
|
|
|
|
for id := 1; id <= 3; id++ {
|
|
|
|
test.OK(t, mem.Add(task.FOLDER_PLANNED, "action", fmt.Sprintf("id: id-%d\n", id)))
|
|
|
|
}
|
|
|
|
remote := storage.NewRemoteRepository(mem)
|
|
|
|
tasks := []*task.Task{
|
|
|
|
{Id: "id-1"},
|
|
|
|
{Id: "id-3"},
|
|
|
|
}
|
|
|
|
test.OK(t, remote.Remove(tasks))
|
|
|
|
actMsgs, err := mem.Messages(task.FOLDER_PLANNED)
|
|
|
|
expMsgs := []*mstore.Message{{
|
|
|
|
Uid: 2,
|
|
|
|
Folder: task.FOLDER_PLANNED,
|
|
|
|
Subject: "action",
|
|
|
|
Body: "id: id-2\n",
|
|
|
|
}}
|
|
|
|
test.Equals(t, expMsgs, actMsgs)
|
|
|
|
|
|
|
|
}
|