Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mock user package + a minor change to delete. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions users/bolt/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion users/bolt/bolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion users/bolt/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions users/bolt/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion users/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
60 changes: 60 additions & 0 deletions users/mock/db.go
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions users/mock/db_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 9 additions & 0 deletions users/mock/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
8 changes: 8 additions & 0 deletions users/mock/go.sum
Original file line number Diff line number Diff line change
@@ -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=