github.com/epsagon/epsagon-go@v1.39.0/wrappers/mongo/mongo_test.go (about)

     1  package epsagonmongo
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/benweissmann/memongo"
     9  	"github.com/epsagon/epsagon-go/epsagon"
    10  	"github.com/epsagon/epsagon-go/protocol"
    11  	"github.com/epsagon/epsagon-go/tracer"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"go.mongodb.org/mongo-driver/bson"
    15  	"go.mongodb.org/mongo-driver/mongo"
    16  	"go.mongodb.org/mongo-driver/mongo/options"
    17  )
    18  
    19  func TestMongoWrapper(t *testing.T) {
    20  	RegisterFailHandler(Fail)
    21  	RunSpecs(t, "Mongo Driver Test Suite")
    22  }
    23  
    24  var _ = Describe("mongo_wrapper", func() {
    25  	Describe("CollectionWrapper", func() {
    26  		var (
    27  			mongoServer        *memongo.Server
    28  			mongoOptions       *memongo.Options
    29  			started            chan bool
    30  			testConf           *epsagon.Config
    31  			events             []*protocol.Event
    32  			exceptions         []*protocol.Exception
    33  			wrapper            *MongoCollectionWrapper
    34  			testContext        context.Context
    35  			testDatabaseName   string
    36  			testCollectionName string
    37  			cancel             func()
    38  		)
    39  		BeforeEach(func() {
    40  			started = make(chan bool)
    41  			// start server goroutine, runs in background until block
    42  			go func() {
    43  				mongoOptions = &memongo.Options{
    44  					MongoVersion:   "4.2.0",
    45  					StartupTimeout: 5 * time.Second,
    46  				}
    47  				mongoServer, _ = memongo.StartWithOptions(mongoOptions)
    48  				started <- true
    49  			}()
    50  
    51  			testConf = &epsagon.Config{Config: tracer.Config{
    52  				Disable:  true,
    53  				TestMode: true,
    54  			}}
    55  			events = make([]*protocol.Event, 0)
    56  			exceptions = make([]*protocol.Exception, 0)
    57  			tracer.GlobalTracer = &tracer.MockedEpsagonTracer{
    58  				Events:     &events,
    59  				Exceptions: &exceptions,
    60  				Labels:     make(map[string]interface{}),
    61  				Config:     &testConf.Config,
    62  			}
    63  
    64  			testCollectionName = "collectionName"
    65  			testDatabaseName = "databaseName"
    66  			testContext, cancel = context.WithTimeout(context.Background(), 2*time.Second)
    67  
    68  			// blocking await until server is started
    69  			select {
    70  			case <-started:
    71  				break
    72  			}
    73  			client, _ := mongo.Connect(testContext, options.Client().ApplyURI(mongoServer.URI()))
    74  			wrapper = &MongoCollectionWrapper{
    75  				collection: client.Database(testDatabaseName).Collection(testCollectionName),
    76  				tracer:     tracer.GlobalTracer,
    77  			}
    78  		})
    79  		AfterEach(func() {
    80  			mongoServer.Stop()
    81  			cancel()
    82  		})
    83  		Context("Writing DB", func() {
    84  			It("calls InsertOne", func() {
    85  				_, err := wrapper.InsertOne(context.Background(), struct {
    86  					Name string
    87  				}{"TestName"})
    88  				Expect(err).To(BeNil())
    89  			})
    90  			It("calls InsertMany", func() {
    91  				_, err := wrapper.InsertMany(context.Background(), []interface{}{
    92  					bson.D{
    93  						{Key: "name", Value: "hello"},
    94  						{Key: "age", Value: "33"},
    95  					},
    96  					bson.D{
    97  						{Key: "name", Value: "world"},
    98  						{Key: "age", Value: "44"},
    99  					},
   100  				})
   101  				Expect(err).To(BeNil())
   102  			})
   103  		})
   104  		Context("Reading DB", func() {
   105  			It("calls InsertOne and FindOne", func() {
   106  				type doc struct {
   107  					Name string
   108  				}
   109  				reqDoc := doc{Name: "TestName"}
   110  				resDoc := doc{}
   111  
   112  				_, err := wrapper.InsertOne(context.Background(), reqDoc)
   113  				Expect(err).To(BeNil())
   114  
   115  				response := wrapper.FindOne(
   116  					context.Background(),
   117  					bson.D{{Key: "name", Value: "TestName"}},
   118  				)
   119  
   120  				response.Decode(&resDoc)
   121  				Expect(reqDoc).To(Equal(resDoc))
   122  				Expect(response.Err()).To(BeNil())
   123  
   124  			})
   125  			It("calls InsertMany and Find", func() {
   126  				type doc struct {
   127  					Name string
   128  				}
   129  				docs := []interface{}{
   130  					bson.D{
   131  						{Key: "name", Value: "hello"},
   132  						{Key: "age", Value: "33"},
   133  					},
   134  					bson.D{
   135  						{Key: "name", Value: "world"},
   136  						{Key: "age", Value: "44"},
   137  					},
   138  				}
   139  
   140  				_, err := wrapper.InsertMany(context.Background(), docs)
   141  				Expect(err).To(BeNil())
   142  
   143  				cur, err := wrapper.Find(
   144  					context.Background(),
   145  					bson.M{},
   146  				)
   147  				Expect(err).To(BeNil())
   148  
   149  
   150  				readCursor(cur)
   151  			})
   152  			It("calls CountDocuments", func() {
   153  				docs := []interface{}{
   154  					bson.D{
   155  						{Key: "name", Value: "hello"},
   156  						{Key: "age", Value: "33"},
   157  					},
   158  					bson.D{
   159  						{Key: "name", Value: "world"},
   160  						{Key: "age", Value: "44"},
   161  					},
   162  				}
   163  
   164  				wrapper.InsertMany(context.Background(), docs)
   165  				res, err := wrapper.CountDocuments(
   166  					context.Background(),
   167  					bson.D{{}},
   168  				)
   169  				Expect(err).To(BeNil())
   170  				Expect(res).To(Equal(int64(2)))
   171  			})
   172  		})
   173  	})
   174  })