diff --git a/README.md b/README.md index 552e07c..b113294 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ MongoDB Template -[![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) diff --git a/_example/insert/main.go b/_example/insert/main.go index f96c4c9..200c673 100644 --- a/_example/insert/main.go +++ b/_example/insert/main.go @@ -25,8 +25,8 @@ type test struct { func main() { insertOne() - insertMany() - insertOneManualCloseSession() + //insertMany() + //insertOneManualCloseSession() } func insertOne() { diff --git a/go.mod b/go.mod index a4da031..6bcf165 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b01bdab..8742508 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/mongo/template.go b/mongo/template.go index 4f54ca3..b1cb5ad 100644 --- a/mongo/template.go +++ b/mongo/template.go @@ -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 @@ -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)) } } } diff --git a/mongo/template_test.go b/mongo/template_test.go index f696ed3..1297919 100644 --- a/mongo/template_test.go +++ b/mongo/template_test.go @@ -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) } }) @@ -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)) }) } } @@ -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)) }) } } @@ -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)) }) } } @@ -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) } }) @@ -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) } }) @@ -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) @@ -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) @@ -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) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) } }) @@ -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) @@ -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) @@ -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) +}