2023-05-22 01:01:10 +04:00
|
|
|
package justguestbook_test
|
2023-01-12 03:27:45 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-22 00:50:11 +04:00
|
|
|
guestbook "git.arav.su/Arav/justguestbook"
|
2023-01-12 03:27:45 +04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testEMsg = "kek"
|
|
|
|
testRMsg = "lol"
|
|
|
|
)
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
func genTestDB() (db guestbook.Guestbook, err error) {
|
2023-05-22 00:50:11 +04:00
|
|
|
db, err = guestbook.NewSQLiteDB(":memory:")
|
2023-01-12 03:27:45 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to init DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := guestbook.NewEntry("Anonymous", testEMsg, "", true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a new entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.NewEntry(e); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to insert a new entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := guestbook.NewReply(e.ID, testRMsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a new reply")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.NewReply(r); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to insert a new reply")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSqliteInsertAndGetAll(t *testing.T) {
|
|
|
|
db, err := genTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
es, err := db.Entries(1, 30)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, err := db.Count(); err != nil || int64(len(es)) != c {
|
|
|
|
t.Errorf("entries count mismatch (%d != %d). Error: %s", len(es), c, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if es[0].Reply.Message != testRMsg {
|
|
|
|
t.Error("reply isn't", testRMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
func TestSqliteEditEntry(t *testing.T) {
|
2023-01-12 03:27:45 +04:00
|
|
|
db, err := genTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
es0, err := db.Entries(1, 30)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to obtain entries. Error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
me := es0[0]
|
|
|
|
|
|
|
|
me.Name = "NotSoAnonymous"
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
err = db.EditEntry(me)
|
2023-01-12 03:27:45 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2023-05-09 23:30:18 +04:00
|
|
|
|
|
|
|
es0, err = db.Entries(1, 30)
|
|
|
|
if es0[0].Name != me.Name {
|
|
|
|
t.Error("name wasn't changed", es0[0].Name, "!=", me.Name, es0[0].ID, me.ID)
|
|
|
|
}
|
2023-01-12 03:27:45 +04:00
|
|
|
}
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
func TestSqliteEditReply(t *testing.T) {
|
2023-01-12 03:27:45 +04:00
|
|
|
db, err := genTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
es0, err := db.Entries(1, 30)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to obtain entries. Error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := es0[0].Reply
|
|
|
|
|
|
|
|
mr.ID = es0[0].ID
|
|
|
|
mr.Message = "bur"
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
err = db.EditReply(mr)
|
2023-01-12 03:27:45 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSqliteDeleteEntry(t *testing.T) {
|
|
|
|
db, err := genTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-06 22:21:23 +04:00
|
|
|
if err = db.DeleteEntry(1); err != nil {
|
|
|
|
t.Error("cannot delete entry", err)
|
2023-01-12 03:27:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|