aboutsummaryrefslogtreecommitdiff
path: root/caronte_test.go
blob: cbf867d64232a8a68f621b96de098294263454a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"context"
	"crypto/sha256"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"os"
	"testing"
	"time"
)

var storage Storage
var testContext context.Context

func TestMain(m *testing.M) {
	mongoHost, ok := os.LookupEnv("MONGO_HOST")
	if !ok {
		mongoHost = "localhost"
	}
	mongoPort, ok := os.LookupEnv("MONGO_PORT")
	if !ok {
		mongoPort = "27017"
	}

	uniqueDatabaseName := sha256.Sum256([]byte(time.Now().String()))

	client, err := mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%v", mongoHost, mongoPort)))
	if err != nil {
		panic("failed to create mongo client")
	}

	db := client.Database(fmt.Sprintf("%x", uniqueDatabaseName[:31]))
	mongoStorage := MongoStorage{
		client:      client,
		collections: map[string]*mongo.Collection{testCollection: db.Collection(testCollection)},
	}

	testContext, _ = context.WithTimeout(context.Background(), 10 * time.Second)

	err = mongoStorage.Connect(nil)
	if err != nil {
		panic(err)
	}
	storage = &mongoStorage

	exitCode := m.Run()
	os.Exit(exitCode)
}