Skip to content

Commit

Permalink
refactor(mongo): Re-import bson types to mongo 🎨
Browse files Browse the repository at this point in the history
I've re-imported M and ObjectId types to mongo package, to avoid using
mgo packages on outer projects.
  • Loading branch information
ddspog committed Apr 19, 2018
1 parent a1f319f commit d94af85
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 97 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ The Documenter can be used like this:
```go
// Create a type representing the Document type
type Product struct {
IDV bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
IDV ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"`
UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"`
NameV string `json:"name" form:"name" binding:"required" bson:"name"`
PriceV float32 `json:"price" form:"price" binding:"required" bson:"price"`
}

// Implement the Documenter interface.
func (p *Product) ID() (id bson.ObjectId) {
func (p *Product) ID() (id ObjectId) {
id = p.IDV
return
}
Expand All @@ -111,12 +111,12 @@ func (p *Product) New() (doc mongo.Documenter) {

// On these methods, you can use the functions implemented mongo
// package.
func (p *Product) Map() (out bson.M, err error) {
func (p *Product) Map() (out M, err error) {
out, err = mongo.MapDocumenter(p)
return
}

func (p *Product) Init(in bson.M) (err error) {
func (p *Product) Init(in M) (err error) {
var doc mongo.Documenter = p
err = mongo.InitDocumenter(in, &doc)
return
Expand All @@ -140,14 +140,14 @@ p.CalculateCreatedOn()
t := p.CreatedOn()
```

You can also mock some other functions of this package, by mocking some called functions time.Now and bson.NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like:
You can also mock some other functions of this package, by mocking some called functions time.Now and NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like:

```go
create, _ := mongo.NewMockModelSetup(t)
defer create.Finish()

create.Now().Returns(time.Parse("02-01-2006", "22/12/2006"))
create.NewID().Returns(bson.ObjectIdHex("anyID"))
create.NewID().Returns(ObjectIdHex("anyID"))

var d mongo.Documenter
// Call any needed methods ...
Expand Down Expand Up @@ -227,7 +227,7 @@ func (p *ProductHandle) Clean() {
The Update function uses an id as an argument:

```go
func (p *ProductHandle) Update(id bson.ObjectId) (err error) {
func (p *ProductHandle) Update(id ObjectId) (err error) {
err = p.Handle.Update(id, p.Document())
return
}
Expand Down
19 changes: 9 additions & 10 deletions acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ddspog/mongo/elements"
"github.com/ddspog/mspec/bdd"
"github.com/globalsign/mgo/bson"
)

const (
Expand Down Expand Up @@ -98,19 +97,19 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) {
})
})

var newId bson.ObjectId
var newId ObjectId

when(fmt.Sprintf("using p.Insert() to add documents with ids: '%[1]s', '%[2]s', '%[3]s', and a new one", testid01, testid02, testid03), func(it bdd.It) {
p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid01)
p.DocumentV.IDV = ObjectIdHex(testid01)
errInsertDoc01 := p.Insert()

p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid02)
p.DocumentV.IDV = ObjectIdHex(testid02)
errInsertDoc02 := p.Insert()

p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid03)
p.DocumentV.IDV = ObjectIdHex(testid03)
errInsertDoc03 := p.Insert()

p.DocumentV = newProduct()
Expand All @@ -134,21 +133,21 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) {

it("should have p.Find() return all documents", func(assert bdd.Assert) {
p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid01)
p.DocumentV.IDV = ObjectIdHex(testid01)
product01, errFindDoc01 := p.Find()

assert.NoError(errFindDoc01)
assert.Equal(testid01, product01.ID().Hex())

p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid02)
p.DocumentV.IDV = ObjectIdHex(testid02)
product02, errFindDoc02 := p.Find()

assert.NoError(errFindDoc02)
assert.Equal(testid02, product02.ID().Hex())

p.DocumentV = newProduct()
p.DocumentV.IDV = bson.ObjectIdHex(testid03)
p.DocumentV.IDV = ObjectIdHex(testid03)
product03, errFindDoc03 := p.Find()

assert.NoError(errFindDoc03)
Expand Down Expand Up @@ -287,7 +286,7 @@ func Test_Read_data_on_MongoDB(t *testing.T) {
p.Link(db)

when("using p.Find() with '%[1]v' as document id'", func(it bdd.It, args ...interface{}) {
p.DocumentV.IDV = bson.ObjectIdHex(args[0].(string))
p.DocumentV.IDV = ObjectIdHex(args[0].(string))
product, errFind := p.Find()

it("should run without errors", func(assert bdd.Assert) {
Expand Down Expand Up @@ -323,7 +322,7 @@ func Test_Read_data_on_MongoDB(t *testing.T) {
})

when("using p.FindAll() on a Document with id '%[1]v'", func(it bdd.It, args ...interface{}) {
p.DocumentV.IDV = bson.ObjectIdHex(args[0].(string))
p.DocumentV.IDV = ObjectIdHex(args[0].(string))
products, errFindAll := p.FindAll()

it("should run without errors", func(assert bdd.Assert) {
Expand Down
14 changes: 7 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ The Documenter can be used like this:
// Create a type representing the Document type
type Product struct {
IDV bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
IDV ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
CreatedOnV int64 `json:"created_on,omitempty" bson:"created_on,omitempty"`
UpdatedOnV int64 `json:"updated_on,omitempty" bson:"updated_on,omitempty"`
NameV string `json:"name" form:"name" binding:"required" bson:"name"`
PriceV float32 `json:"price" form:"price" binding:"required" bson:"price"`
}
// Implement the Documenter interface.
func (p *Product) ID() (id bson.ObjectId) {
func (p *Product) ID() (id ObjectId) {
id = p.IDV
return
}
Expand All @@ -98,12 +98,12 @@ The Documenter can be used like this:
// On these methods, you can use the functions implemented mongo
// package.
func (p *Product) Map() (out bson.M, err error) {
func (p *Product) Map() (out M, err error) {
out, err = mongo.MapDocumenter(p)
return
}
func (p *Product) Init(in bson.M) (err error) {
func (p *Product) Init(in M) (err error) {
var doc mongo.Documenter = p
err = mongo.InitDocumenter(in, &doc)
return
Expand All @@ -127,14 +127,14 @@ The Documenter can be used like this:
t := p.CreatedOn()
You can also mock some other functions of this package, by mocking some
called functions time.Now and bson.NewObjectId. Use the MockModelSetup
called functions time.Now and NewObjectId. Use the MockModelSetup
presented on this package (only in test environment), like:
create, _ := mongo.NewMockModelSetup(t)
defer create.Finish()
create.Now().Returns(time.Parse("02-01-2006", "22/12/2006"))
create.NewID().Returns(bson.ObjectIdHex("anyID"))
create.NewID().Returns(ObjectIdHex("anyID"))
var d mongo.Documenter
// Call any needed methods ...
Expand Down Expand Up @@ -210,7 +210,7 @@ The Clean function is simple and helps a lot:
The Update function uses an id as an argument:
func (p *ProductHandle) Update(id bson.ObjectId) (err error) {
func (p *ProductHandle) Update(id ObjectId) (err error) {
err = p.Handle.Update(id, p.Document())
return
}
Expand Down
8 changes: 3 additions & 5 deletions documenter.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package mongo

import "github.com/globalsign/mgo/bson"

// Documenter it's an interface that could be common to any documents
// types used to store values on a MongoDB. It contains getters and
// generates to important documents values: _id, created_on and
// updated_on
type Documenter interface {
New() Documenter
Map() (bson.M, error)
Init(bson.M) error
ID() bson.ObjectId
Map() (M, error)
Init(M) error
ID() ObjectId
CreatedOn() int64
UpdatedOn() int64
GenerateID()
Expand Down
24 changes: 11 additions & 13 deletions documenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"time"

"github.com/ddspog/mspec/bdd"

"github.com/globalsign/mgo/bson"
)

// Feature Enable embedding with Document
Expand All @@ -18,7 +16,7 @@ func Test_Enable_embedding_with_Document(t *testing.T) {

given(t, "a new embedded Product p with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProduct()
p.IDV = bson.ObjectIdHex(args[0].(string))
p.IDV = ObjectIdHex(args[0].(string))

when("casting to Documenter interface d", func(it bdd.It) {
var d Documenter = p
Expand All @@ -40,7 +38,7 @@ func Test_Create_Document_with_functional_Getters(t *testing.T) {

given(t, "a Product p with ID '%[1]v', CreatedOn = %[2]v, UpdatedOn = %[3]v", func(when bdd.When, args ...interface{}) {
p := newProduct()
p.IDV = bson.ObjectIdHex(args[0].(string))
p.IDV = ObjectIdHex(args[0].(string))
p.CreatedOnV = args[1].(int64)
p.UpdatedOnV = args[2].(int64)

Expand Down Expand Up @@ -76,8 +74,8 @@ func Test_Create_Document_with_functional_Setters(t *testing.T) {
given(t, "a Product p with ID '%[1]v', CreatedOn = %[2]v, UpdatedOn = %[3]v", func(when bdd.When, args ...interface{}) {
p := newProduct()

when("p.IDV = bson.ObjectIdHex(%[1]v)", func(it bdd.It) {
p.IDV = bson.ObjectIdHex(args[0].(string))
when("p.IDV = ObjectIdHex(%[1]v)", func(it bdd.It) {
p.IDV = ObjectIdHex(args[0].(string))
it("p.ID().Hex() should return '%[1]v'", func(assert bdd.Assert) {
assert.Equal(p.ID().Hex(), args[0].(string))
})
Expand Down Expand Up @@ -146,7 +144,7 @@ func Test_Generate_ID_of_Document(t *testing.T) {
p := newProduct()

when("p.GenerateID() is called", func(it bdd.It) {
create.NewID().Returns(bson.ObjectIdHex(args[0].(string)))
create.NewID().Returns(ObjectIdHex(args[0].(string)))
p.GenerateID()
it("p.ID().Hex() should return %[1]v", func(assert bdd.Assert) {
assert.Equal(p.ID().Hex(), args[0].(string))
Expand All @@ -159,14 +157,14 @@ func Test_Generate_ID_of_Document(t *testing.T) {

// Feature Encoding to map object.
// - As a developer,
// - I want that Documenter to be able to convert to bson.M object,
// - I want that Documenter to be able to convert to M object,
// - So that I can use to ease call on mgo methods.
func Test_Encoding_to_map_object(t *testing.T) {
given, like, s := bdd.Sentences()

given(t, "a Product p with id '%[1]s'", func(when bdd.When, args ...interface{}) {
p := newProduct()
p.IDV = bson.ObjectIdHex(args[0].(string))
p.IDV = ObjectIdHex(args[0].(string))

when("out, errMap := p.Map() is called", func(it bdd.It) {
out, errMap := p.Map()
Expand All @@ -175,8 +173,8 @@ func Test_Encoding_to_map_object(t *testing.T) {
assert.NoError(errMap)
})

it("out['_id'] should be bson.ObjectId equal to '%[1]s'", func(assert bdd.Assert) {
id, ok := out["_id"].(bson.ObjectId)
it("out['_id'] should be ObjectId equal to '%[1]s'", func(assert bdd.Assert) {
id, ok := out["_id"].(ObjectId)
assert.True(ok)
assert.Equal(id.Hex(), args[0].(string))
})
Expand All @@ -186,8 +184,8 @@ func Test_Encoding_to_map_object(t *testing.T) {
))

given(t, "a map m with m['_id'] equal to id '%[1]s' and p an empty Product", func(when bdd.When, args ...interface{}) {
m := bson.M{
"_id": bson.ObjectIdHex(args[0].(string)),
m := M{
"_id": ObjectIdHex(args[0].(string)),
}

p := newProduct()
Expand Down
Loading

0 comments on commit d94af85

Please sign in to comment.