/
test_helpers.go
46 lines (41 loc) · 988 Bytes
/
test_helpers.go
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
package api
import (
"context"
"log"
"os"
"testing"
"github.com/joho/godotenv"
"github.com/joshdstockdale/go-booking/db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type testdb struct {
client *mongo.Client
*db.Store
}
func (tdb *testdb) teardown(t *testing.T) {
dbName := os.Getenv(db.MongoDBEnvName)
if err := tdb.client.Database(dbName).Drop(context.TODO()); err != nil {
t.Fatal(err)
}
}
func setup(t *testing.T) *testdb {
if err := godotenv.Load("../.env"); err != nil {
log.Fatal(err)
}
dburi := os.Getenv("MONGO_DB_URL_TEST")
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(dburi))
if err != nil {
log.Fatal(err)
}
hotelStore := db.NewMongoHotelStore(client)
return &testdb{
client: client,
Store: &db.Store{
Hotel: hotelStore,
User: db.NewMongoUserStore(client),
Room: db.NewMongoRoomStore(client, hotelStore),
Booking: db.NewMongoBookingStore(client),
},
}
}