2021-02-22 16:55:12 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-31 12:50:44 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/v2/internal/repo"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb"
|
2021-02-22 16:55:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoticesCRUD(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testRepo := filepath.Join("testrun", t.Name())
|
|
|
|
os.RemoveAll(testRepo)
|
|
|
|
|
|
|
|
tr := repo.New(testRepo)
|
|
|
|
|
|
|
|
db, err := Open(tr)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
// boil.DebugWriter = os.Stderr
|
|
|
|
// boil.DebugMode = true
|
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
_, err = db.Notices.GetByID(ctx, 9999)
|
|
|
|
r.Error(err)
|
2021-03-10 15:44:46 +00:00
|
|
|
r.EqualError(err, roomdb.ErrNotFound.Error())
|
2021-02-22 16:55:12 +00:00
|
|
|
|
|
|
|
err = db.Notices.RemoveID(ctx, 9999)
|
|
|
|
r.Error(err)
|
2021-03-10 15:44:46 +00:00
|
|
|
r.EqualError(err, roomdb.ErrNotFound.Error())
|
2021-02-22 16:55:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("new and update", func(t *testing.T) {
|
|
|
|
r := require.New(t)
|
2021-03-10 15:44:46 +00:00
|
|
|
var n roomdb.Notice
|
2021-02-22 16:55:12 +00:00
|
|
|
|
|
|
|
n.Title = fmt.Sprintf("Test notice %d", rand.Int())
|
|
|
|
n.Content = `# This is **not** a test!`
|
|
|
|
n.Language = "en-GB"
|
|
|
|
|
|
|
|
err := db.Notices.Save(ctx, &n)
|
|
|
|
r.NoError(err, "failed to save")
|
|
|
|
r.NotEqual(0, n.ID, "should have a fresh id")
|
|
|
|
|
|
|
|
got, err := db.Notices.GetByID(ctx, n.ID)
|
|
|
|
r.NoError(err, "failed to get saved entry")
|
|
|
|
r.Equal(n.Title, got.Title)
|
|
|
|
r.Equal(n.ID, got.ID)
|
|
|
|
r.Equal(n.Language, got.Language)
|
|
|
|
|
|
|
|
oldID := n.ID
|
|
|
|
n.Title = fmt.Sprintf("Updated test notice %d", rand.Int())
|
|
|
|
err = db.Notices.Save(ctx, &n)
|
|
|
|
r.NoError(err, "failed to save")
|
|
|
|
r.Equal(oldID, n.ID, "should have the same ID")
|
|
|
|
|
|
|
|
// be gone
|
|
|
|
err = db.Notices.RemoveID(ctx, oldID)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
_, err = db.Notices.GetByID(ctx, oldID)
|
|
|
|
r.Error(err)
|
2021-03-10 15:44:46 +00:00
|
|
|
r.EqualError(err, roomdb.ErrNotFound.Error())
|
2021-02-22 16:55:12 +00:00
|
|
|
})
|
|
|
|
}
|
2021-02-23 19:23:50 +00:00
|
|
|
|
|
|
|
func TestPinnedNotices(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testRepo := filepath.Join("testrun", t.Name())
|
|
|
|
os.RemoveAll(testRepo)
|
|
|
|
|
|
|
|
tr := repo.New(testRepo)
|
|
|
|
|
|
|
|
db, err := Open(tr)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
t.Run("defaults", func(t *testing.T) {
|
|
|
|
allTheNotices, err := db.PinnedNotices.List(ctx)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
type expectedNotices struct {
|
2021-03-10 15:44:46 +00:00
|
|
|
Name roomdb.PinnedNoticeName
|
2021-02-23 19:23:50 +00:00
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []expectedNotices{
|
2021-03-10 15:44:46 +00:00
|
|
|
{roomdb.NoticeDescription, 2},
|
|
|
|
{roomdb.NoticeNews, 1},
|
|
|
|
{roomdb.NoticePrivacyPolicy, 2},
|
|
|
|
{roomdb.NoticeCodeOfConduct, 1},
|
2021-02-23 19:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tcase := range cases {
|
2021-03-09 13:27:47 +00:00
|
|
|
notices, has := allTheNotices[tcase.Name]
|
2021-02-23 19:23:50 +00:00
|
|
|
r.True(has, "case %d failed - notice %s not in map", i, tcase.Name)
|
2021-03-09 13:27:47 +00:00
|
|
|
r.Len(notices, tcase.Count, "case %d failed - wrong number of notices for %s", i, tcase.Name)
|
2021-02-23 19:23:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("validity", func(t *testing.T) {
|
2021-03-10 15:44:46 +00:00
|
|
|
var empty roomdb.Notice
|
2021-02-23 19:23:50 +00:00
|
|
|
// no id
|
2021-03-10 15:44:46 +00:00
|
|
|
err = db.PinnedNotices.Set(ctx, roomdb.NoticeNews, empty.ID)
|
2021-02-23 19:23:50 +00:00
|
|
|
r.Error(err)
|
|
|
|
|
|
|
|
// not-null id
|
|
|
|
empty.ID = 999
|
2021-03-10 15:44:46 +00:00
|
|
|
err = db.PinnedNotices.Set(ctx, roomdb.NoticeNews, empty.ID)
|
2021-02-23 19:23:50 +00:00
|
|
|
r.Error(err)
|
|
|
|
|
|
|
|
// invalid notice name
|
|
|
|
err = db.PinnedNotices.Set(ctx, "unknown", empty.ID)
|
|
|
|
r.Error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add new localization", func(t *testing.T) {
|
2021-03-10 15:44:46 +00:00
|
|
|
var notice roomdb.Notice
|
2021-02-23 19:23:50 +00:00
|
|
|
notice.Title = "política de privacidad"
|
|
|
|
notice.Content = "solo una prueba"
|
|
|
|
notice.Language = "es"
|
|
|
|
// save the new notice
|
|
|
|
err = db.Notices.Save(ctx, ¬ice)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
// set it
|
2021-03-10 15:44:46 +00:00
|
|
|
err = db.PinnedNotices.Set(ctx, roomdb.NoticePrivacyPolicy, notice.ID)
|
2021-02-23 19:23:50 +00:00
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
// retreive it
|
2021-03-10 15:44:46 +00:00
|
|
|
ret, err := db.PinnedNotices.Get(ctx, roomdb.NoticePrivacyPolicy, notice.Language)
|
2021-02-23 19:23:50 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(notice, *ret, "notices are not the same")
|
|
|
|
|
|
|
|
// see that it's in the list
|
|
|
|
allTheNotices, err := db.PinnedNotices.List(ctx)
|
|
|
|
r.NoError(err)
|
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
notices, has := allTheNotices[roomdb.NoticePrivacyPolicy]
|
2021-02-23 19:23:50 +00:00
|
|
|
r.True(has)
|
|
|
|
r.Len(notices, 3)
|
|
|
|
|
|
|
|
has = false
|
|
|
|
for _, n := range notices {
|
|
|
|
if n.Title == notice.Title {
|
|
|
|
has = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.True(has, "did not find new notice in list()")
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|