diff --git a/users/bolt/bolt.go b/users/bolt/bolt.go index 915bffb..aa925fc 100644 --- a/users/bolt/bolt.go +++ b/users/bolt/bolt.go @@ -70,16 +70,17 @@ func (db *DB) UpdateUser(ctx context.Context, u users.User) (users.User, error) } // DeleteUser deletes a user record from the database. -func (db *DB) DeleteUser(ctx context.Context, id string) (users.User, error) { +func (db *DB) DeleteUser(ctx context.Context, id string) error { prev, err := internal.GetSnap([]byte(id), db._db) if err != nil { - return nil, err + return err } e, err := internal.NewDeleteEvent(prev.User, prev.Version) if err != nil { - return nil, errors.Wrap(err, "NewDeleteEvent") + return errors.Wrap(err, "NewDeleteEvent") } - return e.Apply(ctx, db._db) + _, err = e.Apply(ctx, db._db) + return err } diff --git a/users/bolt/bolt_test.go b/users/bolt/bolt_test.go index 073dc7c..6d39640 100644 --- a/users/bolt/bolt_test.go +++ b/users/bolt/bolt_test.go @@ -53,7 +53,7 @@ func TestCRUD(t *testing.T) { test.Assert(t, u.GetID() == _u.GetID(), "ids should be equal") test.Assert(t, u.GetName() == _u.GetName(), "names should be equal") // delete - _, err = db.DeleteUser(ctx, u.GetID()) + err = db.DeleteUser(ctx, u.GetID()) test.OK(t, err) _u, err = db.GetUser(ctx, u.GetID()) test.OK(t, err) diff --git a/users/bolt/go.mod b/users/bolt/go.mod index b690880..f1dfa52 100644 --- a/users/bolt/go.mod +++ b/users/bolt/go.mod @@ -6,7 +6,7 @@ require ( github.com/gogo/protobuf v1.1.1 // indirect github.com/justanotherorganization/justanotherbotkit/internal v0.0.1 github.com/justanotherorganization/justanotherbotkit/proto v0.0.1 - github.com/justanotherorganization/justanotherbotkit/users v0.0.1 + github.com/justanotherorganization/justanotherbotkit/users v0.0.2rc1 github.com/pkg/errors v0.8.0 go.etcd.io/bbolt v1.3.0 golang.org/x/sys v0.0.0-20181022134430-8a28ead16f52 // indirect diff --git a/users/bolt/go.sum b/users/bolt/go.sum index 614f507..a63147c 100644 --- a/users/bolt/go.sum +++ b/users/bolt/go.sum @@ -8,8 +8,6 @@ github.com/justanotherorganization/justanotherbotkit/internal v0.0.1 h1:2odRvxW1 github.com/justanotherorganization/justanotherbotkit/internal v0.0.1/go.mod h1:Ky6Plt9xEtoSkl64huLSkq0lq1xYKBxI58Wb45mucV8= github.com/justanotherorganization/justanotherbotkit/proto v0.0.1 h1:N2hHF03EYoOfKXFlU4Sx97laA1fGu5K7K0ZjgbaGjF8= github.com/justanotherorganization/justanotherbotkit/proto v0.0.1/go.mod h1:r2hwUKNIK21pZ+e1KuoNWCRDJIpotmDrwS02cMKZu6c= -github.com/justanotherorganization/justanotherbotkit/users v0.0.1 h1:g96VVlYIYV5kRYfG2eLBQa5Cptg1WaKu2Xv8eftOz6M= -github.com/justanotherorganization/justanotherbotkit/users v0.0.1/go.mod h1:pSY/4vMnSHd4qjLQzLFe3WyGhpVYhNibLQuZTjkIpE4= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= go.etcd.io/bbolt v1.3.0 h1:oY10fI923Q5pVCVt1GBTZMn8LHo5M+RCInFpeMnV4QI= diff --git a/users/db.go b/users/db.go index 49dd7c1..6f94bef 100644 --- a/users/db.go +++ b/users/db.go @@ -12,6 +12,6 @@ type ( // UpdateUser updates a user record in the database. UpdateUser(ctx context.Context, u User) (User, error) // DeleteUser deletes a user record from the database. - DeleteUser(ctx context.Context, id string) (User, error) + DeleteUser(ctx context.Context, id string) error } ) diff --git a/users/mock/db.go b/users/mock/db.go new file mode 100644 index 0000000..7356359 --- /dev/null +++ b/users/mock/db.go @@ -0,0 +1,60 @@ +package mock // import "github.com/justanotherorganization/justanotherbotkit/users/mock" + +import ( + "context" + "sync" + + "github.com/justanotherorganization/justanotherbotkit/users" +) + +// DB is a mock database. +type DB struct { + sync.RWMutex + m map[string]users.User +} + +// Static type checking +var _ users.DB = &DB{} + +// New returns a new in-memory user database. +func New() (db *DB) { + db = new(DB) + db.m = make(map[string]users.User) + return db +} + +// CreateUser creates a new user and saves it in memory. +func (db *DB) CreateUser(_ context.Context, u users.User) (users.User, error) { + db.Lock() + defer db.Unlock() + db.m[u.GetID()] = u + return u, nil +} + +// GetUser gets a user from the database using the ID. +func (db *DB) GetUser(_ context.Context, id string) (users.User, error) { + db.RLock() + defer db.RUnlock() + u := db.m[id] + return u, nil +} + +// UpdateUser udpates a user record in memory. +func (db *DB) UpdateUser(_ context.Context, u users.User) (users.User, error) { + db.Lock() + defer db.Unlock() + db.m[u.GetID()] = u + return u, nil +} + +// DeleteUser deletes a user record from memory. +func (db *DB) DeleteUser(_ context.Context, id string) error { + db.Lock() + defer db.Unlock() + _, ok := db.m[id] + if ok { + delete(db.m, id) + } + + return nil +} diff --git a/users/mock/db_test.go b/users/mock/db_test.go new file mode 100644 index 0000000..c581238 --- /dev/null +++ b/users/mock/db_test.go @@ -0,0 +1,50 @@ +package mock_test + +import ( + "context" + "testing" + + "github.com/gofrs/uuid" + "github.com/justanotherorganization/justanotherbotkit/internal/test" + "github.com/justanotherorganization/justanotherbotkit/proto" + . "github.com/justanotherorganization/justanotherbotkit/users/mock" +) + +func newTestUser(tb testing.TB) *pb.BaseUser { + uid, err := uuid.NewV4() + test.OK(tb, err) + id := uid.String() + + return &pb.BaseUser{ + ID: id, + Name: id, + } +} + +func TestCRUD(t *testing.T) { + ctx := context.Background() + db := New() + // create + u := newTestUser(t) + _u, err := db.CreateUser(ctx, u) + test.OK(t, err) + test.Assert(t, u.GetID() == _u.GetID(), "ids should be equal") + test.Assert(t, u.GetName() == _u.GetName(), "names should be equal") + // retrieve + _u, err = db.GetUser(ctx, u.GetID()) + test.OK(t, err) + test.Assert(t, u.GetID() == _u.GetID(), "ids should be equal") + test.Assert(t, u.GetName() == _u.GetName(), "names should be equal") + // update + u.Name = "test" + _u, err = db.UpdateUser(ctx, u) + test.OK(t, err) + test.Assert(t, u.GetID() == _u.GetID(), "ids should be equal") + test.Assert(t, u.GetName() == _u.GetName(), "names should be equal") + // delete + err = db.DeleteUser(ctx, u.GetID()) + test.OK(t, err) + _u, err = db.GetUser(ctx, u.GetID()) + test.OK(t, err) + test.Assert(t, _u == nil, "user should not exist") +} diff --git a/users/mock/go.mod b/users/mock/go.mod new file mode 100644 index 0000000..9d71678 --- /dev/null +++ b/users/mock/go.mod @@ -0,0 +1,9 @@ +module github.com/justanotherorganization/justanotherbotkit/users/mock + +require ( + github.com/gofrs/uuid v3.1.0+incompatible + github.com/gogo/protobuf v1.1.1 // indirect + github.com/justanotherorganization/justanotherbotkit/internal v0.0.1 + github.com/justanotherorganization/justanotherbotkit/proto v0.0.1 + github.com/justanotherorganization/justanotherbotkit/users v0.0.2rc1 +) diff --git a/users/mock/go.sum b/users/mock/go.sum new file mode 100644 index 0000000..e5ba9e9 --- /dev/null +++ b/users/mock/go.sum @@ -0,0 +1,8 @@ +github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA= +github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/justanotherorganization/justanotherbotkit/internal v0.0.1 h1:2odRvxW138OANcE7Oj1kBK9FiJorwKfV4nFsGTcQAB8= +github.com/justanotherorganization/justanotherbotkit/internal v0.0.1/go.mod h1:Ky6Plt9xEtoSkl64huLSkq0lq1xYKBxI58Wb45mucV8= +github.com/justanotherorganization/justanotherbotkit/proto v0.0.1 h1:N2hHF03EYoOfKXFlU4Sx97laA1fGu5K7K0ZjgbaGjF8= +github.com/justanotherorganization/justanotherbotkit/proto v0.0.1/go.mod h1:r2hwUKNIK21pZ+e1KuoNWCRDJIpotmDrwS02cMKZu6c=