func TestInsertArticle(t *testing.T) { Convey("Test insert a article", t, func() { db := new(LevelDBStore) e := db.Connect("db.test") defer db.db.Close() Convey("The error should be nil", func() { So(e, ShouldBeNil) }) p := new(postmi.Post) p.Title = "ololo" e = db.Save(p) So(e, ShouldBeNil) So(p.Id, ShouldNotEqual, 0) }) }
func (ls *LevelDBStore) Save(p *postmi.Post) error { if p.Id == 0 { e := ls.Inc() if e != nil { return e } p.Id = ls.currentAutoIncrement } e := ls.db.Put(idKey(p.Id), p.MustJSON(), nil) if e != nil { return e } return nil }
func TestGetSliceArticle(t *testing.T) { Convey("test get slice of articles", t, func() { db := new(LevelDBStore) e := db.Connect("db.test") Convey("The error should be nil", func() { So(e, ShouldBeNil) }) defer db.db.Close() p := new(postmi.Post) p.Title = "ololo" e = db.Save(p) p = new(postmi.Post) p.Title = "alolo" e = db.Save(p) posts, e := db.GetSlice(10, 0) So(e, ShouldBeNil) So(len(posts), ShouldEqual, 3) So(posts[0].Id, ShouldEqual, 3) So(posts[2].Id, ShouldEqual, 1) }) }