Skip to content

Commit

Permalink
Merge branch 'hotfix/v0.8.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
ddspog committed Apr 15, 2018
2 parents 56808e0 + 8d89969 commit 43d6241
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 253 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.o
*.a
*.so
*.out

# Folders
_obj
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mongo [![GoDoc](https://godoc.org/github.com/ddspog/mongo?status.svg)](https://godoc.org/github.com/ddspog/mongo) [![Go Report Card](https://goreportcard.com/badge/github.com/ddspog/mongo)](https://goreportcard.com/report/github.com/ddspog/mongo) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![Travis CI](https://travis-ci.org/ddspog/mongo.svg?branch=master)](https://travis-ci.org/ddspog/mongo)

by [ddspog](http://hithub.com/ddspog)
by [ddspog](http://github.com/ddspog)

Package **mongo** helps you mask the connection to MongoDB using mgo package.

Expand Down Expand Up @@ -66,11 +66,28 @@ if err := mongo.Connect(); err != nil {
}
```

## Testing

This package contains a nice coverage with the unit tests, within the
objetives of the project.

The elements, embedded and mocks sub-packages have low coverage because
they fulfill a need to mock mgo elements. These packages only embedded
mgo objects to mock, and by this a lot of unused functions were created
to fulfill interface requisites.

On the other hand, model, handler and mongo package have full coverage,
being the focus of this project.

The project also contains a set of acceptance tests. I've have set the
test-acceptance task with the commands to run it. These tests requires
a mongo test database to be available. It creates, search and remove
elements from it, being reusable without broking the database.

## Contribution

This package has some objectives from now:

* Being incorporate on mgo package (possible fork) on possible future.
* Creating real tests with MongoDB connections.

Any interest in help is much appreciated.
17 changes: 12 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ test-model:
- go test {{.REPO_PATH}}/model -v --cover
silent: true

test-example:
desc: Run a test with an mongo instance running.
test-acceptance:
desc: Run acceptance tests with a real mongo instance running.
cmds:
- echo "Calling tests example execution ..."
- go test {{.REPO_PATH}}/example -v --cover
- echo "Calling acceptance tests execution ..."
- go test {{.REPO_PATH}} -v --cover -tags=acceptance
silent: true

test-mongo:
Expand All @@ -26,8 +26,15 @@ test-mongo:
- go test {{.REPO_PATH}} -v --cover
silent: true

cover:
desc: Check cover of all unit tests.
cmds:
- echo "Checking coverage for all unit tests ..."
- goverage ./...
- go tool cover -html=coverage.out

test:
deps: [test-handler, test-model, test-mongo, test-example]
deps: [test-handler, test-model, test-mongo]
desc: Run all tests.

gen-mock-collection:
Expand Down
252 changes: 125 additions & 127 deletions example/real_test.go → acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package example
// +build acceptance

package mongo

import (
"fmt"
"os"
"testing"

"github.com/ddspog/mongo/model"

"github.com/ddspog/mongo"

"github.com/ddspog/mspec/bdd"

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

Expand All @@ -22,126 +20,6 @@ const (
testid04 = "000000007465737469643034"
)

// Feature Real connection to MongoDB
// - As a developer,
// - I want to be able to connect to MongoDB with this package,
// - So that I could use the Handle methods to to various operations on a real DB.
func Test_Real_connection_to_MongoDB(t *testing.T) {
given, _, _ := bdd.Sentences()

given(t, "a local database with a products collection with 3 documents", func(when bdd.When) {
when("connecting with its url", func(it bdd.It) {
os.Setenv("MONGODB_URL", "mongodb://localhost:27017/test")
err := mongo.Connect()
defer mongo.Disconnect()

it("should connect with no problems", func(assert bdd.Assert) {
assert.NoError(err)
})

conn := NewDBSocket()
defer conn.Close()

db := conn.DB()

it("should open a socket containing valid DB", func(assert bdd.Assert) {
assert.NotNil(db)
})

p, err := NewProductHandle().Link(db)

it("should link correctly with products collection", func(assert bdd.Assert) {
assert.NoError(err)
})

n, err := p.Count()

it("should count 3 documents on products collection", func(assert bdd.Assert) {
assert.NoError(err)
assert.Equal(3, n)
})
})
})
}

// Feature Read data on MongoDB
// - As a developer,
// - I want to be able to connect and retrieve data from MongoDB,
// - So I can use these functionalities on real applications.
func Test_Read_data_on_MongoDB(t *testing.T) {
given, like, s := bdd.Sentences()

given(t, fmt.Sprintf("a local database with a products collection that contains documents with ids: '%[1]s', '%[2]s', '%[3]s'", testid01, testid02, testid03), func(when bdd.When) {
os.Setenv("MONGODB_URL", "mongodb://localhost:27017/test")
_ = mongo.Connect()
defer mongo.Disconnect()

conn := NewDBSocket()
defer conn.Close()
db := conn.DB()

p := NewProductHandle()
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))
product, errFind := p.Find()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFind)
})

it("should return a product with id '%[1]v'", func(assert bdd.Assert) {
assert.Equal(args[0].(string), product.ID().Hex())
})
}, like(
s(testid01), s(testid02), s(testid03),
))

when("using p.FindAll() with a empty Document", func(it bdd.It) {
p.DocumentV = NewProduct()
products, errFindAll := p.FindAll()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFindAll)
})

it("should return 3 documents", func(assert bdd.Assert) {
assert.Equal(3, len(products))
})

if len(products) == 3 {
it("should return the %[1]vth document with id '%[2]v'", func(assert bdd.Assert, args ...interface{}) {
assert.Equal(args[1].(string), products[args[0].(int)-1].ID().Hex())
}, like(
s(1, testid01), s(2, testid02), s(3, testid03),
))
}
})

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))
products, errFindAll := p.FindAll()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFindAll)
})

it("should return 1 documents", func(assert bdd.Assert) {
assert.Equal(1, len(products))
})

if len(products) == 1 {
it("should return the 1th document with id '%[1]v'", func(assert bdd.Assert) {
assert.Equal(args[0].(string), products[0].ID().Hex())
})
}
}, like(
s(testid01), s(testid02), s(testid03),
))
})
}

// Feature Manipulate data on MongoDB
// - As a developer,
// - I want to be able to connect and manipulate data from MongoDB,
Expand All @@ -151,8 +29,8 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) {

given(t, fmt.Sprintf("a local database with a products collection that contains documents with ids: '%[1]s', '%[2]s', '%[3]s'", testid01, testid02, testid03), func(when bdd.When) {
os.Setenv("MONGODB_URL", "mongodb://localhost:27017/test")
_ = mongo.Connect()
defer mongo.Disconnect()
_ = Connect()
defer Disconnect()

conn := NewDBSocket()
defer conn.Close()
Expand Down Expand Up @@ -314,3 +192,123 @@ func Test_Manipulate_data_on_MongoDB(t *testing.T) {
})
})
}

// Feature Real connection to MongoDB
// - As a developer,
// - I want to be able to connect to MongoDB with this package,
// - So that I could use the Handle methods to to various operations on a real DB.
func Test_Real_connection_to_MongoDB(t *testing.T) {
given, _, _ := bdd.Sentences()

given(t, "a local database with a products collection with 3 documents", func(when bdd.When) {
when("connecting with its url", func(it bdd.It) {
os.Setenv("MONGODB_URL", "mongodb://localhost:27017/test")
err := Connect()
defer Disconnect()

it("should connect with no problems", func(assert bdd.Assert) {
assert.NoError(err)
})

conn := NewDBSocket()
defer conn.Close()

db := conn.DB()

it("should open a socket containing valid DB", func(assert bdd.Assert) {
assert.NotNil(db)
})

p, err := NewProductHandle().Link(db)

it("should link correctly with products collection", func(assert bdd.Assert) {
assert.NoError(err)
})

n, err := p.Count()

it("should count 3 documents on products collection", func(assert bdd.Assert) {
assert.NoError(err)
assert.Equal(3, n)
})
})
})
}

// Feature Read data on MongoDB
// - As a developer,
// - I want to be able to connect and retrieve data from MongoDB,
// - So I can use these functionalities on real applications.
func Test_Read_data_on_MongoDB(t *testing.T) {
given, like, s := bdd.Sentences()

given(t, fmt.Sprintf("a local database with a products collection that contains documents with ids: '%[1]s', '%[2]s', '%[3]s'", testid01, testid02, testid03), func(when bdd.When) {
os.Setenv("MONGODB_URL", "mongodb://localhost:27017/test")
_ = Connect()
defer Disconnect()

conn := NewDBSocket()
defer conn.Close()
db := conn.DB()

p := NewProductHandle()
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))
product, errFind := p.Find()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFind)
})

it("should return a product with id '%[1]v'", func(assert bdd.Assert) {
assert.Equal(args[0].(string), product.ID().Hex())
})
}, like(
s(testid01), s(testid02), s(testid03),
))

when("using p.FindAll() with a empty Document", func(it bdd.It) {
p.DocumentV = NewProduct()
products, errFindAll := p.FindAll()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFindAll)
})

it("should return 3 documents", func(assert bdd.Assert) {
assert.Equal(3, len(products))
})

if len(products) == 3 {
it("should return the %[1]vth document with id '%[2]v'", func(assert bdd.Assert, args ...interface{}) {
assert.Equal(args[1].(string), products[args[0].(int)-1].ID().Hex())
}, like(
s(1, testid01), s(2, testid02), s(3, testid03),
))
}
})

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))
products, errFindAll := p.FindAll()

it("should run without errors", func(assert bdd.Assert) {
assert.NoError(errFindAll)
})

it("should return 1 documents", func(assert bdd.Assert) {
assert.Equal(1, len(products))
})

if len(products) == 1 {
it("should return the 1th document with id '%[1]v'", func(assert bdd.Assert) {
assert.Equal(args[0].(string), products[0].ID().Hex())
})
}
}, like(
s(testid01), s(testid02), s(testid03),
))
})
}
Loading

0 comments on commit 43d6241

Please sign in to comment.