Skip to content

Commit 9047663

Browse files
committed
Update CI settings
1 parent 1ca695f commit 9047663

File tree

4 files changed

+221
-3
lines changed

4 files changed

+221
-3
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ before_install:
1414
script:
1515
- go test -v ./...
1616
- go tool vet .
17+
18+
after_success:
19+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
[![GoDoc](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/yeeuu/mongo)
44
[![Build Status](https://travis-ci.org/yeeuu/mongo.svg?branch=master)](https://travis-ci.org/yeeuu/mongo)
5+
[![codecov](https://codecov.io/gh/yeeuu/mongo/branch/master/graph/badge.svg)](https://codecov.io/gh/yeeuu/mongo)
56
[![Go Report Card](https://goreportcard.com/badge/github.com/yeeuu/mongo)](https://goreportcard.com/report/github.com/yeeuu/mongo)
67

78

89
Simple ODM wraps mgo with interface.
910

1011
## TODO
11-
* [ ] 0.1.0 release
12-
* [ ] Test coverage
13-
* [ ] Travis-ci support
12+
- [ ] 0.1.0 release
13+
- [ ] Test coverage
14+
- [x] Travis-ci support
1415

1516
## Limits
1617

storage_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package mongo
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/globalsign/mgo"
9+
)
10+
11+
func TestStorage(t *testing.T) {
12+
sess, err := mgo.DialWithTimeout("127.0.0.1", 2*time.Second)
13+
if err != nil {
14+
panic(err)
15+
}
16+
defer sess.Close()
17+
sess.DB("test").DropDatabase()
18+
store := NewStorage(sess)
19+
err = store.Insert(&userSelector{}, user{Name: "hello"})
20+
if err != nil {
21+
panic(err)
22+
}
23+
var u user
24+
name := "hello"
25+
qs, err := store.Query(&userSelector{
26+
Name: &name,
27+
})
28+
if err != nil {
29+
panic(err)
30+
}
31+
err = qs.One(&u)
32+
if err != nil {
33+
panic(err)
34+
}
35+
qs.Close()
36+
fmt.Println(u)
37+
err = store.Insert(&userSelector{}, user{Name: "world"})
38+
qs, err = store.Query(&userSelector{})
39+
if err != nil {
40+
panic(err)
41+
}
42+
var users []user
43+
err = qs.All(&users)
44+
if err != nil {
45+
panic(err)
46+
}
47+
qs.Close()
48+
fmt.Println(users)
49+
}

util_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package mongo
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/globalsign/mgo"
8+
"github.com/globalsign/mgo/bson"
9+
)
10+
11+
type user struct {
12+
ID bson.ObjectId `bson:"_id,omitempty"`
13+
Name string `bson:"name"`
14+
}
15+
16+
type userSelector struct {
17+
ID *bson.ObjectId `bson:"_id"`
18+
IDs *[]bson.ObjectId `bson:"_id"`
19+
Name *string `bson:"name"`
20+
}
21+
22+
func (us *userSelector) Database() string {
23+
return "test"
24+
}
25+
26+
func (us *userSelector) Collection() string {
27+
return "users"
28+
}
29+
30+
func Test_structToBsonM(t *testing.T) {
31+
type args struct {
32+
s interface{}
33+
}
34+
id := bson.NewObjectId()
35+
ids := []bson.ObjectId{bson.NewObjectId()}
36+
name := "hello"
37+
tests := []struct {
38+
name string
39+
args args
40+
want bson.M
41+
wantErr bool
42+
}{
43+
// TODO: Add test cases.
44+
{
45+
name: "nil",
46+
args: args{
47+
nil,
48+
},
49+
want: bson.M{},
50+
wantErr: true,
51+
},
52+
{
53+
name: "struct",
54+
args: args{
55+
user{},
56+
},
57+
want: bson.M{},
58+
wantErr: true,
59+
},
60+
{
61+
name: "empty",
62+
args: args{
63+
&userSelector{},
64+
},
65+
want: bson.M{},
66+
wantErr: false,
67+
},
68+
{
69+
name: "name",
70+
args: args{
71+
&userSelector{Name: &name},
72+
},
73+
want: bson.M{"name": name},
74+
wantErr: false,
75+
},
76+
{
77+
name: "id",
78+
args: args{
79+
&userSelector{ID: &id},
80+
},
81+
want: bson.M{"_id": id},
82+
wantErr: false,
83+
},
84+
{
85+
name: "ids",
86+
args: args{
87+
&userSelector{IDs: &ids},
88+
},
89+
want: bson.M{"_id": bson.M{"$in": ids}},
90+
wantErr: false,
91+
},
92+
}
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
got, err := structToBsonM(tt.args.s)
96+
if (err != nil) != tt.wantErr {
97+
t.Errorf("structToBsonM() error = %v, wantErr %v", err, tt.wantErr)
98+
return
99+
}
100+
if !reflect.DeepEqual(got, tt.want) {
101+
t.Errorf("structToBsonM() = %v, want %v", got, tt.want)
102+
}
103+
})
104+
}
105+
}
106+
107+
func Test_getCollection(t *testing.T) {
108+
sess, err := mgo.Dial("127.0.0.1")
109+
if err != nil {
110+
t.Skip("Cannot connect to mongodb.")
111+
return
112+
}
113+
defer sess.Close()
114+
type args struct {
115+
sess *mgo.Session
116+
query interface{}
117+
}
118+
tests := []struct {
119+
name string
120+
args args
121+
wantC *mgo.Collection
122+
wantErr bool
123+
}{
124+
// TODO: Add test cases.
125+
{
126+
name: "nil",
127+
args: args{
128+
sess: sess,
129+
query: nil,
130+
},
131+
wantC: nil,
132+
wantErr: true,
133+
},
134+
{
135+
name: "struct",
136+
args: args{
137+
sess: sess,
138+
query: user{},
139+
},
140+
wantC: nil,
141+
wantErr: true,
142+
},
143+
{
144+
name: "struct pointer",
145+
args: args{
146+
sess: sess,
147+
query: &user{},
148+
},
149+
wantC: nil,
150+
wantErr: true,
151+
},
152+
}
153+
for _, tt := range tests {
154+
t.Run(tt.name, func(t *testing.T) {
155+
gotC, err := getCollection(tt.args.sess, tt.args.query)
156+
if (err != nil) != tt.wantErr {
157+
t.Errorf("getCollection() error = %v, wantErr %v", err, tt.wantErr)
158+
return
159+
}
160+
if !reflect.DeepEqual(gotC, tt.wantC) {
161+
t.Errorf("getCollection() = %v, want %v", gotC, tt.wantC)
162+
}
163+
})
164+
}
165+
}

0 commit comments

Comments
 (0)