-
Notifications
You must be signed in to change notification settings - Fork 7
/
mongodb_test.go
93 lines (89 loc) · 1.74 KB
/
mongodb_test.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
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
/*
* Copyright (c) 2012 Jason McVetta. This is Free Software, released under the
* terms of the WTFPL v2. It comes without any warranty, express or implied.
* See http://sam.zoy.org/wtfpl/COPYING for more details.
*
*
* Derived from:
*
* jQuery File Upload Plugin GAE Go Example 2.0
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Original software by Tschan licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
package jfu
import (
"bytes"
"github.com/jmcvetta/randutil"
"labix.org/v2/mgo"
// "labix.org/v2/mgo/bson"
// "log"
"io"
"testing"
)
// Initialize a randomly-named testing database
func InitMongo(t *testing.T) *mgo.Database {
randStr, err := randutil.AlphaString(32)
if err != nil {
t.Fatal(err)
}
dbName := "testing_" + randStr
conn, err := mgo.Dial("localhost")
if err != nil {
t.Fatal(err)
}
return conn.DB(dbName)
}
func TestRoundTrip(t *testing.T) {
//
// Initialize mongoStore instance
//
db := InitMongo(t)
defer db.DropDatabase()
data, err := randutil.AlphaString(512)
if err != nil {
t.Fatal(err)
}
ms := mongoStore{gfs: db.GridFS("")}
//
// Create
//
b := bytes.NewBufferString(data)
fi := FileInfo{
Type: "text/plain",
}
err = ms.Create(&fi, b)
if err != nil {
t.Fatal(err)
}
key := fi.Key
t.Log("key:", key)
//
// Get
//
fi, r, err := ms.Get(key)
if err != nil {
t.Error(err)
}
rbuf := new(bytes.Buffer)
io.Copy(rbuf, r)
retr := rbuf.String()
if retr != data {
msg := "Different data retrieved than was saved!\n"
msg += "data: " + data + "\n"
msg += "retr: " + retr + "\n"
t.Error(msg)
}
//
// Delete
//
t.Log(key)
err = ms.Delete(key)
if err != nil {
t.Fatal(err)
}
}