package sqlite_test import ( "testing" "git.arav.top/Arav/justguestbook/database/sqlite" "git.arav.top/Arav/justguestbook/guestbook" "github.com/pkg/errors" ) const ( testEMsg = "kek" testRMsg = "lol" ) func genTestDB() (db *sqlite.SQLiteDatabase, err error) { db, err = sqlite.New(":memory:") 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) } } func TestSqliteUpdateEntry(t *testing.T) { 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.ID = es0[0].ID me.Name = "NotSoAnonymous" ne, err := db.UpdateEntry(me) if err != nil { t.Error(err) } if ne.Name != me.Name { t.Error(ne.Name, "!=", me.Name) } } func TestSqliteUpdateReply(t *testing.T) { 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" nr, err := db.UpdateReply(mr) if err != nil { t.Error(err) } if nr.Message != mr.Message { t.Error(nr.Message, "!=", mr.Message) } } 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) } }() dc, err := db.DeleteEntry(1) if err != nil { t.Error("cannot delete entry", err, dc) } if c, err := db.Count(); err != nil || c != 0 { t.Error(c, err, dc) } }