Skip to content

Commit

Permalink
Updated dependencies, and implementation go-helper, go-errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Jan 28, 2024
1 parent 11dddbf commit e0fc62d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MongoDB Template
<!--suppress ALL -->
<img align="right" src="gopher-mongo.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.1.2-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.1.2)
[![Project status](https://img.shields.io/badge/version-v1.1.3-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.1.3)
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-mongo-template)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-mongo-template)
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-mongo-template/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-mongo?branch=main)
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-mongo-template/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-mongo)
Expand Down
4 changes: 2 additions & 2 deletions _example/insert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type test struct {

func main() {
insertOne()
insertMany()
insertOneManualCloseSession()
//insertMany()
//insertOneManualCloseSession()
}

func insertOne() {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/GabrielHCataldo/go-mongo-template
go 1.21.3

require (
github.com/GabrielHCataldo/go-errors v1.0.8
github.com/GabrielHCataldo/go-helper v1.3.1
github.com/GabrielHCataldo/go-logger v1.1.6
github.com/GabrielHCataldo/go-errors v1.1.1
github.com/GabrielHCataldo/go-helper v1.3.4
github.com/GabrielHCataldo/go-logger v1.2.0
go.mongodb.org/mongo-driver v1.13.1
)

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/GabrielHCataldo/go-errors v1.0.8 h1:28ryfzyU0fYDNgg+37zZauwnRRMyFSujWpntmuuCx00=
github.com/GabrielHCataldo/go-errors v1.0.8/go.mod h1:ka5NCxa3Ry4TqvJXJMSCl5Vj3QWrdLMNAUXQNSjD9kg=
github.com/GabrielHCataldo/go-helper v1.3.1 h1:TPk087CQv7BcEeHoC47zakaZwJ+L5bSFLLnHAAGQfj0=
github.com/GabrielHCataldo/go-helper v1.3.1/go.mod h1:+whI36aUg1BfvLpZWJ8j81+d5iPuxl58ApOJqTIliCU=
github.com/GabrielHCataldo/go-logger v1.1.6 h1:D1fFmDy+94QtPyuhOz1F6H7jFxnFd5/+rF1iLyt2J0E=
github.com/GabrielHCataldo/go-logger v1.1.6/go.mod h1:Z+bJSxDzm0vLzV93qn+FOPbiQ3XX2JIaSABA0++Z9r0=
github.com/GabrielHCataldo/go-errors v1.1.1 h1:cbEJIfWRk44843wE8eVpyvr9F7zOn/AHD8xozF/RUkM=
github.com/GabrielHCataldo/go-errors v1.1.1/go.mod h1:rrwIP14EEGJ0N8BUpxuwHNM8auO5ZsVj9jGEj442ibo=
github.com/GabrielHCataldo/go-helper v1.3.4 h1:Rcf7gRbk6oI/YrZMIcieOotA2nldAoIjeiky+hHVPv8=
github.com/GabrielHCataldo/go-helper v1.3.4/go.mod h1:+whI36aUg1BfvLpZWJ8j81+d5iPuxl58ApOJqTIliCU=
github.com/GabrielHCataldo/go-logger v1.2.0 h1:fGdlkx+ihU7nhNYTvi/KOlLb2WEHmJrCuIFU9/zGQ1g=
github.com/GabrielHCataldo/go-logger v1.2.0/go.mod h1:Q7+M9m9gu1MtgGiFkoYOHtobLLM/kJCPZVhszIJtsJ0=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
14 changes: 4 additions & 10 deletions mongo/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,18 +927,12 @@ func (t *Template) CloseSession(ctx context.Context, abort bool) error {

// CommitTransaction commit all transactions on session
func (t *Template) CommitTransaction(ctx context.Context) error {
if helper.IsNil(t.session) {
return errNoOpenSession(2)
}
return errors.NewSkipCaller(2, t.session.CommitTransaction(ctx))
return t.commitTransaction(ctx, 2)
}

// AbortTransaction abort all transactions on session
func (t *Template) AbortTransaction(ctx context.Context) error {
if helper.IsNil(t.session) {
return errNoOpenSession(2)
}
return errors.NewSkipCaller(2, t.session.AbortTransaction(ctx))
return t.abortTransaction(ctx, 2)
}

// Disconnect closes the mongodb connection client with return error
Expand Down Expand Up @@ -1008,8 +1002,8 @@ func (t *Template) insertMany(sc mongo.SessionContext, a any, opt *option.Insert
Comment: opt.Comment,
})
if helper.IsNotNil(err) {
_, _, _, message := errors.GetErrorDetails(err)
errs = append(errs, helper.Sprintln(message, "index:", i))
errMessage := errors.Details(err).GetMessage()
errs = append(errs, helper.Sprintln(errMessage, "index:", i))
}
}
}
Expand Down
120 changes: 71 additions & 49 deletions mongo/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ func TestTemplateFindOne(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.FindOne(ctx, tt.filter, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindOne() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -240,12 +240,12 @@ func TestTemplateFindOneAndDelete(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.FindOneAndDelete(ctx, tt.filter, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindOneAndDelete() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
_ = mongoTemplate.CloseSession(ctx, err != nil)
_ = mongoTemplate.CloseSession(ctx, helper.IsNotNil(err))
})
}
}
Expand All @@ -257,12 +257,12 @@ func TestTemplateFindOneAndReplace(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.FindOneAndReplace(ctx, tt.filter, tt.replacement, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindOneAndReplace() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
_ = mongoTemplate.CloseSession(ctx, err != nil)
_ = mongoTemplate.CloseSession(ctx, helper.IsNotNil(err))
})
}
}
Expand All @@ -274,12 +274,12 @@ func TestTemplateFindOneAndUpdate(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.FindOneAndUpdate(ctx, tt.filter, tt.update, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindOneAndUpdate() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
_ = mongoTemplate.CloseSession(ctx, err != nil)
_ = mongoTemplate.CloseSession(ctx, helper.IsNotNil(err))
})
}
}
Expand All @@ -291,9 +291,9 @@ func TestTemplateFind(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.Find(ctx, tt.filter, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("Find() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -307,9 +307,9 @@ func TestTemplateFindAll(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.FindAll(ctx, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindAll() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -323,9 +323,9 @@ func TestTemplateFindPageable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
v, err := mongoTemplate.FindPageable(ctx, tt.filter, tt.pageInput, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("FindPageable() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result pageable:", v)
Expand All @@ -341,9 +341,9 @@ func TestTemplateExists(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
v, err := mongoTemplate.Exists(ctx, tt.filter, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("Exists() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result pageable:", v)
Expand All @@ -359,9 +359,9 @@ func TestTemplateExistsById(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
v, err := mongoTemplate.ExistsById(ctx, tt.id, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("ExistsById() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result pageable:", v)
Expand All @@ -377,9 +377,9 @@ func TestTemplateAggregate(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.Aggregate(ctx, tt.pipeline, tt.dest, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("Aggregate() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -393,9 +393,9 @@ func TestTemplateCountDocuments(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
_, err := mongoTemplate.CountDocuments(ctx, tt.filter, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("CountDocuments() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -409,9 +409,9 @@ func TestTemplateEstimatedDocumentCount(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
_, err := mongoTemplate.EstimatedDocumentCount(ctx, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("EstimatedDocumentCount() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -425,9 +425,9 @@ func TestTemplateDistinct(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.Distinct(ctx, tt.fieldName, tt.filter, tt.dest, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("Distinct() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -441,9 +441,9 @@ func TestTemplateWatch(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
_, err := mongoTemplate.Watch(ctx, tt.pipeline, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("Watch() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -461,9 +461,9 @@ func TestTemplateWatchWithHandler(t *testing.T) {
initDocument()
}()
err := mongoTemplate.WatchWithHandler(ctx, tt.pipeline, tt.handler, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("WatchWithHandler() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -478,9 +478,9 @@ func TestTemplateDropCollection(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.DropCollection(ctx, tt.ref)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("DropCollection() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -495,9 +495,9 @@ func TestTemplateDropDatabase(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.DropDatabase(ctx, tt.ref)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("DropDatabase() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -513,9 +513,9 @@ func TestTemplateCreateOneIndex(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
_, err := mongoTemplate.CreateOneIndex(ctx, tt.input)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("CreateOneIndex() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -531,9 +531,9 @@ func TestTemplateCreateManyIndex(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
_, err := mongoTemplate.CreateManyIndex(ctx, tt.inputs)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("CreateManyIndex() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -547,9 +547,9 @@ func TestTemplateDropOneIndex(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.DropOneIndex(ctx, tt.nameIndex, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("DropOneIndex() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -563,9 +563,9 @@ func TestTemplateDropAllIndexes(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
err := mongoTemplate.DropAllIndexes(ctx, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("DropAllIndexes() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
}
})
Expand All @@ -579,9 +579,9 @@ func TestTemplateListIndexes(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
result, err := mongoTemplate.ListIndexes(ctx, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("ListIndexes() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result list indexes:", result)
Expand All @@ -597,9 +597,9 @@ func TestTemplateListIndexSpecifications(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), tt.durationTimeout)
defer cancel()
result, err := mongoTemplate.ListIndexSpecifications(ctx, tt.ref, tt.option, nil)
if (err != nil) != tt.wantErr {
if helper.IsNotEqualTo(helper.IsNotNil(err), tt.wantErr) {
t.Errorf("ListIndexSpecifications() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result list indexes:", result)
Expand All @@ -612,3 +612,25 @@ func TestTemplateGetClient(t *testing.T) {
initMongoTemplate()
logger.Info("result:", mongoTemplate.GetClient())
}

func TestTemplateCommitTransaction(t *testing.T) {
initMongoTemplate()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err := mongoTemplate.CommitTransaction(ctx)
logger.Info("result err:", err)
_ = mongoTemplate.StartSession(ctx)
err = mongoTemplate.CommitTransaction(ctx)
logger.Info("result err:", err)
}

func TestTemplateAbortTransaction(t *testing.T) {
initMongoTemplate()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err := mongoTemplate.AbortTransaction(ctx)
logger.Info("result err:", err)
_ = mongoTemplate.StartSession(ctx)
err = mongoTemplate.AbortTransaction(ctx)
logger.Info("result err:", err)
}

0 comments on commit e0fc62d

Please sign in to comment.