aboutsummaryrefslogtreecommitdiff
path: root/storage_test.go
blob: 5356596d540bc9a73acebda295a5545cfb034468 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

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

const testCollection = "characters"

var storage Storage
var testContext context.Context

func testInsert(t *testing.T) {
	// insert a document in an invalid connection
	insertedId, err := storage.InsertOne(testContext, "invalid_collection",
		OrderedDocument{{"key", "invalid"}})
	if insertedId != nil || err == nil {
		t.Fatal("inserting documents in invalid collections must fail")
	}

	// insert ordered document
	beatriceId, err := storage.InsertOne(testContext, testCollection,
		OrderedDocument{{"name", "Beatrice"}, {"description", "blablabla"}})
	if err != nil {
		t.Fatal(err)
	}
	if beatriceId == nil {
		t.Fatal("failed to insert an ordered document")
	}

	// insert unordered document
	virgilioId, err := storage.InsertOne(testContext, testCollection,
		UnorderedDocument{"name": "Virgilio", "description": "blablabla"})
	if err != nil {
		t.Fatal(err)
	}
	if virgilioId == nil {
		t.Fatal("failed to insert an unordered document")
	}

	// insert document with custom id
	danteId := "000000"
	insertedId, err = storage.InsertOne(testContext, testCollection,
		UnorderedDocument{"_id": danteId, "name": "Dante Alighieri", "description": "blablabla"})
	if err != nil {
		t.Fatal(err)
	}
	if insertedId != danteId {
		t.Fatal("returned id doesn't match")
	}

	// insert duplicate document
	insertedId, err = storage.InsertOne(testContext, testCollection,
		UnorderedDocument{"_id": danteId, "name": "Dante Alighieri", "description": "blablabla"})
	if insertedId != nil || err == nil {
		t.Fatal("inserting duplicate id must fail")
	}
}

func testFindOne(t *testing.T) {
	// find a document in an invalid connection
	result, err := storage.FindOne(testContext, "invalid_collection",
		OrderedDocument{{"key", "invalid"}})
	if result != nil || err == nil {
		t.Fatal("find a document in an invalid collections must fail")
	}

	// find an existing document
	result, err = storage.FindOne(testContext, testCollection, OrderedDocument{{"_id", "000000"}})
	if err != nil {
		t.Fatal(err)
	}
	if result == nil {
		t.Fatal("FindOne cannot find the valid document")
	}
	name, ok := result["name"]
	if !ok || name != "Dante Alighieri" {
		t.Fatal("document retrieved with FindOne is invalid")
	}

	// find an existing document
	result, err = storage.FindOne(testContext, testCollection, OrderedDocument{{"_id", "invalid_id"}})
	if err != nil {
		t.Fatal(err)
	}
	if result != nil {
		t.Fatal("FindOne cannot find an invalid document")
	}
}



func TestBasicOperations(t *testing.T) {
	t.Run("testInsert", testInsert)
	t.Run("testFindOne", testFindOne)
}

func TestMain(m *testing.M) {
	uniqueDatabaseName := sha256.Sum256([]byte(time.Now().String()))
	client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		panic("failed to create mongo client")
	}

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

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

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

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