2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/common"
|
|
|
|
. "github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
g.Describe("Users", func() {
|
|
|
|
var db *DB // temporary database
|
|
|
|
|
|
|
|
// create a new database before each unit
|
|
|
|
// test and destroy afterwards.
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
db = Must("/tmp/drone.test.db")
|
|
|
|
})
|
|
|
|
g.AfterEach(func() {
|
|
|
|
os.Remove(db.Path())
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should find", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
user, err := db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(user.Login).Equal("octocat")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should insert", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
err := db.SetUserNotExists(&common.User{Login: "octocat"})
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
user, err := db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(user.Login).Equal("octocat")
|
|
|
|
g.Assert(user.Created != 0).IsTrue()
|
|
|
|
g.Assert(user.Updated != 0).IsTrue()
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should not insert if exists", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUser(&common.User{Login: "octocat"})
|
|
|
|
err := db.SetUserNotExists(&common.User{Login: "octocat"})
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(ErrKeyExists)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should update", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
user, err := db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
user.Email = "octocat@github.com"
|
2015-04-15 05:04:38 +00:00
|
|
|
err = db.SetUser(user)
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
user_, err := db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(user_.Login).Equal(user.Login)
|
|
|
|
g.Assert(user_.Email).Equal(user.Email)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should delete", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
user, err := db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
err = db.DelUser(user)
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
_, err = db.User("octocat")
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(ErrKeyNotFound)
|
|
|
|
})
|
|
|
|
|
2015-04-11 05:22:55 +00:00
|
|
|
g.It("Should list", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "bert"})
|
|
|
|
db.SetUserNotExists(&common.User{Login: "ernie"})
|
|
|
|
users, err := db.UserList()
|
2015-04-11 05:22:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(len(users)).Equal(2)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
g.It("Should count", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "bert"})
|
|
|
|
db.SetUserNotExists(&common.User{Login: "ernie"})
|
|
|
|
count, err := db.UserCount()
|
2015-04-07 08:20:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(count).Equal(2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|