Skip to content

Commit

Permalink
Merge pull request #22 from wcodesoft/clean/using-common-db-connection
Browse files Browse the repository at this point in the history
Using common db connection
  • Loading branch information
walterjgsp authored Aug 12, 2023
2 parents 2a80112 + 99a842c commit abdf196
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 106 deletions.
23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ module github.com/wcodesoft/mosha-author-service
go 1.20

require (
github.com/charmbracelet/log v0.2.2
github.com/go-chi/chi/v5 v5.0.8
github.com/brianvoe/gofakeit/v6 v6.23.1
github.com/charmbracelet/log v0.2.3
github.com/go-chi/chi/v5 v5.0.10
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5
github.com/smartystreets/goconvey v1.8.0
github.com/wcodesoft/mosha-service-common v0.0.1
go.mongodb.org/mongo-driver v1.12.0
google.golang.org/grpc v1.56.1
github.com/smartystreets/goconvey v1.8.1
github.com/wcodesoft/mosha-service-common v0.0.3
go.mongodb.org/mongo-driver v1.12.1
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/brianvoe/gofakeit/v6 v6.23.0 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
Expand All @@ -30,17 +29,17 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/smartystreets/assertions v1.13.1 // indirect
github.com/smarty/assertions v1.15.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
)
45 changes: 25 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/brianvoe/gofakeit/v6 v6.23.0 h1:pgVhyWpYq4e0GEVCh2gdZnS/nBX+8SnyTBliHg5xjks=
github.com/brianvoe/gofakeit/v6 v6.23.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/brianvoe/gofakeit/v6 v6.23.1 h1:k2gX0hQpJStvixDbbw8oJOvPBg0XmHJWbSOF5JkiUHw=
github.com/brianvoe/gofakeit/v6 v6.23.1/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/log v0.2.2 h1:CaXgos+ikGn5tcws5Cw3paQuk9e/8bIwuYGhnkqQFjo=
github.com/charmbracelet/log v0.2.2/go.mod h1:Zs11hKpb8l+UyX4y1srwZIGW+MPCXJHIty3MB9l/sno=
github.com/charmbracelet/log v0.2.3 h1:YVmBhJtpGL7nW/nlf5u+SEloU8XYljxozGzZpgwIvhs=
github.com/charmbracelet/log v0.2.3/go.mod h1:ZApwwzDbbETVTIRTk7724yQRJAXIktt98yGVMMaa3y8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down Expand Up @@ -41,20 +41,24 @@ github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6f
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU=
github.com/smartystreets/assertions v1.13.1/go.mod h1:cXr/IwVfSo/RbCSPhoAPv73p3hlSdrBH/b3SdnW/LMY=
github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w=
github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/wcodesoft/mosha-service-common v0.0.1 h1:WLKQXCuW+E48plSlbF+0Nu0MZANnukhs10pZFXw1G34=
github.com/wcodesoft/mosha-service-common v0.0.1/go.mod h1:jYlrEAg+EN/AJB+GwjKoR0LOrOxrFApmyhJmhD2Fawk=
github.com/wcodesoft/mosha-service-common v0.0.2 h1:4BR/u1TcngKQ2odP9DrXsd8QI9uguXKJgFPmr24fAxA=
github.com/wcodesoft/mosha-service-common v0.0.2/go.mod h1:t52mtX1tRaPz2DcBTZY27Yla7KwHjeV8U3wA/VuFU94=
github.com/wcodesoft/mosha-service-common v0.0.3 h1:Isx0BDXnW/1XfcL+SimUcjUp15MJ/Buj5Oimu8vI7f0=
github.com/wcodesoft/mosha-service-common v0.0.3/go.mod h1:nDU5zVJuyqwOdggJJp5qBT0QA83/nszE4+uTk6N32no=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand All @@ -64,8 +68,8 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE=
go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0=
go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE=
go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
Expand All @@ -78,8 +82,9 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -104,10 +109,10 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
google.golang.org/grpc v1.56.1 h1:z0dNfjIl0VpaZ9iSVjA6daGatAYwPGstTjt5vkRMFkQ=
google.golang.org/grpc v1.56.1/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/charmbracelet/log"
"github.com/wcodesoft/mosha-author-service/repository"
"github.com/wcodesoft/mosha-author-service/service"
mdb "github.com/wcodesoft/mosha-service-common/database"
"os"
"sync"
)
Expand All @@ -29,11 +30,12 @@ func main() {
mongoHost := getEnv("MONGO_DB_HOST", defaultMongoHost)
grpcPort := getEnv("GRPC_PORT", defaultGrpcPort)

mongoClient, err := repository.NewMongoClient(mongoHost)
mongoClient, err := mdb.NewMongoClient(mongoHost)
if err != nil {
log.Fatal(err)
}
database := repository.NewMongoDatabase(mongoClient, defaultDatabase)
connection := mdb.NewMongoConnection(mongoClient, defaultDatabase, "authors")
database := repository.NewMongoDatabase(connection)
repo := repository.New(database)
s := service.New(repo)

Expand Down
35 changes: 12 additions & 23 deletions repository/mongo_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package repository
import (
"context"
"fmt"
"github.com/charmbracelet/log"
"github.com/wcodesoft/mosha-author-service/data"
mdb "github.com/wcodesoft/mosha-service-common/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type mongoDatabase struct {
client *mongo.Client
coll *mongo.Collection
connection *mdb.MongoConnection
coll *mongo.Collection
}

// AddAuthor adds an author to the mongo database.
func (m mongoDatabase) AddAuthor(author data.Author) (string, error) {
func (m *mongoDatabase) AddAuthor(author data.Author) (string, error) {
result, err := m.coll.InsertOne(context.Background(), fromAuthor(author))
if err != nil {
return "", err
Expand All @@ -26,7 +26,7 @@ func (m mongoDatabase) AddAuthor(author data.Author) (string, error) {
}

// ListAll returns all authors in the mongo database.
func (m mongoDatabase) ListAll() []data.Author {
func (m *mongoDatabase) ListAll() []data.Author {
opts := options.Find().SetSort(bson.D{{Key: "name", Value: 1}})
cursor, err := m.coll.Find(context.Background(), bson.D{}, opts)
if err != nil {
Expand All @@ -44,7 +44,7 @@ func (m mongoDatabase) ListAll() []data.Author {
}

// UpdateAuthor updates an author in the mongo database.
func (m mongoDatabase) UpdateAuthor(author data.Author) (data.Author, error) {
func (m *mongoDatabase) UpdateAuthor(author data.Author) (data.Author, error) {
filter := bson.D{{Key: "_id", Value: author.ID}}
opts := options.Update().SetHint(bson.D{{Key: "_id", Value: 1}})
update := bson.D{{Key: "$set", Value: fromAuthor(author)}}
Expand All @@ -56,7 +56,7 @@ func (m mongoDatabase) UpdateAuthor(author data.Author) (data.Author, error) {
}

// DeleteAuthor deletes an author from the mongo database.
func (m mongoDatabase) DeleteAuthor(id string) error {
func (m *mongoDatabase) DeleteAuthor(id string) error {
filter := bson.D{{Key: "_id", Value: id}}
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
result, err := m.coll.DeleteOne(context.Background(), filter, opts)
Expand All @@ -70,7 +70,7 @@ func (m mongoDatabase) DeleteAuthor(id string) error {
}

// GetAuthor returns an author from the mongo database.
func (m mongoDatabase) GetAuthor(id string) (data.Author, error) {
func (m *mongoDatabase) GetAuthor(id string) (data.Author, error) {
filter := bson.D{{Key: "_id", Value: id}}
opts := options.FindOne().SetHint(bson.D{{Key: "_id", Value: 1}})
var result authorDB
Expand All @@ -81,21 +81,10 @@ func (m mongoDatabase) GetAuthor(id string) (data.Author, error) {
return toAuthor(result), nil
}

func NewMongoClient(mongoURI string) (*mongo.Client, error) {
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(mongoURI).SetServerAPIOptions(serverAPI)
client, err := mongo.Connect(context.Background(), opts)
if err != nil {
return nil, err
}
log.Infof("Connected to MongoDB: %s", mongoURI)
return client, nil
}

func NewMongoDatabase(client *mongo.Client, database string) Database {
coll := client.Database(database).Collection("authors")
// NewMongoDatabase creates a new mongo database.
func NewMongoDatabase(connection *mdb.MongoConnection) Database {
return &mongoDatabase{
client: client,
coll: coll,
connection: connection,
coll: connection.Collection,
}
}
22 changes: 11 additions & 11 deletions repository/mongo_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
faker "github.com/brianvoe/gofakeit/v6"
. "github.com/smartystreets/goconvey/convey"
"github.com/wcodesoft/mosha-author-service/data"
mdb "github.com/wcodesoft/mosha-service-common/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)
Expand All @@ -30,9 +31,10 @@ func TestMongoDB(t *testing.T) {
defer mt.Close()

mt.Run("Test AddAuthor", func(mt *mtest.T) {
conn := mdb.NewMongoConnection(mt.Client, databaseName, "author")
db := NewMongoDatabase(conn)
mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "_id", Value: id}})
Convey("Test AddAuthor correctly", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
author := data.Author{ID: id, Name: name, PicURL: picUrl}
id, err := db.AddAuthor(author)
So(err, ShouldBeNil)
Expand All @@ -41,7 +43,6 @@ func TestMongoDB(t *testing.T) {

mt.AddMockResponses(bson.D{{Key: "ok", Value: 0}, {Key: "_id", Value: id}})
Convey("Test AddAuthor with error", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
author := data.Author{ID: id, Name: name, PicURL: picUrl}
id, err := db.AddAuthor(author)
So(err, ShouldNotBeNil)
Expand All @@ -50,7 +51,8 @@ func TestMongoDB(t *testing.T) {
})

mt.Run("Test GetAuthor", func(mt *mtest.T) {

conn := mdb.NewMongoConnection(mt.Client, databaseName, "author")
db := NewMongoDatabase(conn)
mockFind := mtest.CreateCursorResponse(
1,
"mosha.authors",
Expand All @@ -60,7 +62,6 @@ func TestMongoDB(t *testing.T) {
killCursors := mtest.CreateCursorResponse(0, "mosha.authors", mtest.NextBatch)
mt.AddMockResponses(mockFind, killCursors)
Convey("Test GetAuthor correctly", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
author, err := db.GetAuthor(id)
So(err, ShouldBeNil)
So(author.ID, ShouldEqual, id)
Expand All @@ -69,7 +70,6 @@ func TestMongoDB(t *testing.T) {
})

Convey("Test GetAuthor with error", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
author, err := db.GetAuthor(id)
So(err, ShouldNotBeNil)
So(author.ID, ShouldEqual, "")
Expand All @@ -79,28 +79,29 @@ func TestMongoDB(t *testing.T) {
})

mt.Run("Test DeleteAuthor", func(mt *mtest.T) {
conn := mdb.NewMongoConnection(mt.Client, databaseName, "author")
db := NewMongoDatabase(conn)
Convey("Test DeleteAuthor correctly", mt, func() {
mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "acknowledged", Value: true}, {Key: "n", Value: 1}})
db := NewMongoDatabase(mt.Client, databaseName)
err := db.DeleteAuthor(id)
So(err, ShouldBeNil)
})

Convey("Test DeleteAuthor with error", mt, func() {
mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}, {Key: "acknowledged", Value: true}, {Key: "n", Value: 0}})
db := NewMongoDatabase(mt.Client, databaseName)
err := db.DeleteAuthor("InvalidID")
So(err, ShouldNotBeNil)
})
})

mt.Run("Test UpdateAuthor", func(mt *mtest.T) {
conn := mdb.NewMongoConnection(mt.Client, databaseName, "author")
db := NewMongoDatabase(conn)
mt.AddMockResponses(bson.D{
{Key: "ok", Value: 1},
{Key: "value", Value: createMockedAuthor(id, name, picUrl)}})

Convey("Test UpdateAuthor correctly", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
newName := faker.Name()
author := data.Author{ID: id, Name: newName, PicURL: picUrl}
newAuthor, err := db.UpdateAuthor(author)
Expand All @@ -112,7 +113,6 @@ func TestMongoDB(t *testing.T) {
})

Convey("Test UpdateAuthor with error", mt, func() {
db := NewMongoDatabase(mt.Client, databaseName)
newName := faker.Name()
author := data.Author{ID: "InvallidID", Name: newName, PicURL: picUrl}
newAuthor, err := db.UpdateAuthor(author)
Expand All @@ -124,6 +124,8 @@ func TestMongoDB(t *testing.T) {
})

mt.Run("Test ListAuthors", func(mt *mtest.T) {
conn := mdb.NewMongoConnection(mt.Client, databaseName, "author")
db := NewMongoDatabase(conn)
Convey("Test ListAuthors correctly", mt, func() {
otherName := faker.Name()
otherId := faker.UUID()
Expand All @@ -142,14 +144,12 @@ func TestMongoDB(t *testing.T) {
killCursors := mtest.CreateCursorResponse(0, "mosha.authors", mtest.NextBatch)
mt.AddMockResponses(first, second, killCursors)

db := NewMongoDatabase(mt.Client, databaseName)
authors := db.ListAll()
So(len(authors), ShouldEqual, 2)
})

Convey("Test ListAuthors with error", mt, func() {
mt.AddMockResponses(bson.D{{Key: "ok", Value: 0}})
db := NewMongoDatabase(mt.Client, databaseName)
authors := db.ListAll()
So(len(authors), ShouldNotEqual, 3)
})
Expand Down
Loading

0 comments on commit abdf196

Please sign in to comment.