Skip to content

Commit 2413735

Browse files
export the interfaces
1 parent 7c44763 commit 2413735

File tree

15 files changed

+219
-190
lines changed

15 files changed

+219
-190
lines changed

aggregator/aggregator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import (
2222
)
2323

2424
//go:generate mockgen -source=aggregator.go -destination=../mock/aggregator.mock.go -package=mocks
25-
type iAggregator[T any] interface {
25+
type IAggregator[T any] interface {
2626
Aggregate(ctx context.Context, opts ...*options.AggregateOptions) ([]*T, error)
2727
AggregateWithParse(ctx context.Context, result any, opts ...*options.AggregateOptions) error
2828
}
2929

30+
var _ IAggregator[any] = (*Aggregator[any])(nil)
31+
3032
type Aggregator[T any] struct {
3133
collection *mongo.Collection
3234
pipeline any

aggregator/aggregator_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ func TestAggregator_New(t *testing.T) {
8181
func TestAggregator_Aggregation(t *testing.T) {
8282
testCases := []struct {
8383
name string
84-
mock func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser]
84+
mock func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser]
8585
ctx context.Context
8686
want []*TestUser
8787
wantErr assert.ErrorAssertionFunc
8888
}{
8989
{
9090
name: "got error",
91-
mock: func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser] {
92-
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
91+
mock: func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser] {
92+
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
9393
aggregator.EXPECT().Aggregate(ctx).Return(nil, errors.New("can only marshal slices and arrays into aggregation pipelines, but got invalid")).Times(1)
9494
return aggregator
9595
},
@@ -98,8 +98,8 @@ func TestAggregator_Aggregation(t *testing.T) {
9898
},
9999
{
100100
name: "got result",
101-
mock: func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser] {
102-
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
101+
mock: func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser] {
102+
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
103103
aggregator.EXPECT().Aggregate(ctx).Return([]*TestUser{
104104
{Name: "chenmingyong", Age: 24},
105105
{Name: "gopher", Age: 25},
@@ -137,16 +137,16 @@ func TestAggregator_AggregateWithParse(t *testing.T) {
137137
}
138138
testCases := []struct {
139139
name string
140-
mock func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser]
140+
mock func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser]
141141
ctx context.Context
142142
result []*User
143143
want []*User
144144
wantErr assert.ErrorAssertionFunc
145145
}{
146146
{
147147
name: "got error",
148-
mock: func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser] {
149-
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
148+
mock: func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser] {
149+
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
150150
aggregator.EXPECT().AggregateWithParse(ctx, result).Return(errors.New("can only marshal slices and arrays into aggregation pipelines, but got invalid")).Times(1)
151151
return aggregator
152152
},
@@ -156,8 +156,8 @@ func TestAggregator_AggregateWithParse(t *testing.T) {
156156
},
157157
{
158158
name: "got result",
159-
mock: func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser] {
160-
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
159+
mock: func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser] {
160+
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
161161
aggregator.EXPECT().AggregateWithParse(ctx, result).Return(nil).Times(1)
162162
return aggregator
163163
},

creator/creator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ import (
2727
)
2828

2929
//go:generate mockgen -source=creator.go -destination=../mock/creator.mock.go -package=mocks
30-
type iCreator[T any] interface {
30+
type ICreator[T any] interface {
3131
InsertOne(ctx context.Context, docs *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
3232
InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
3333
}
3434

35+
var _ ICreator[any] = (*Creator[any])(nil)
36+
3537
type Creator[T any] struct {
3638
collection *mongo.Collection
3739
modelHook any

creator/creator_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ func TestNewCreator(t *testing.T) {
6060
func TestCreator_One(t *testing.T) {
6161
testCases := []struct {
6262
name string
63-
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser]
63+
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser]
6464
ctx context.Context
6565
doc *TestUser
6666

6767
wantErr error
6868
}{
6969
{
7070
name: "nil doc",
71-
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser] {
72-
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
71+
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
72+
mockCollection := mocks.NewMockICreator[TestUser](ctl)
7373
mockCollection.EXPECT().InsertOne(ctx, doc).Return(nil, errors.New("nil filter")).Times(1)
7474
return mockCollection
7575
},
@@ -79,8 +79,8 @@ func TestCreator_One(t *testing.T) {
7979
},
8080
{
8181
name: "success",
82-
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser] {
83-
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
82+
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
83+
mockCollection := mocks.NewMockICreator[TestUser](ctl)
8484
mockCollection.EXPECT().InsertOne(ctx, doc).Return(&mongo.InsertOneResult{InsertedID: "?"}, nil).Times(1)
8585
return mockCollection
8686
},
@@ -109,7 +109,7 @@ func TestCreator_One(t *testing.T) {
109109
func TestCreator_Many(t *testing.T) {
110110
testCases := []struct {
111111
name string
112-
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser]
112+
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser]
113113
ctx context.Context
114114
docs []*TestUser
115115

@@ -118,8 +118,8 @@ func TestCreator_Many(t *testing.T) {
118118
}{
119119
{
120120
name: "nil docs",
121-
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser] {
122-
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
121+
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
122+
mockCollection := mocks.NewMockICreator[TestUser](ctl)
123123
mockCollection.EXPECT().InsertMany(ctx, docs).Return(nil, errors.New("nil docs")).Times(1)
124124
return mockCollection
125125
},
@@ -132,8 +132,8 @@ func TestCreator_Many(t *testing.T) {
132132
},
133133
{
134134
name: "success",
135-
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser] {
136-
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
135+
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
136+
mockCollection := mocks.NewMockICreator[TestUser](ctl)
137137
mockCollection.EXPECT().InsertMany(ctx, docs).Return(&mongo.InsertManyResult{InsertedIDs: make([]interface{}, 2)}, nil).Times(1)
138138
return mockCollection
139139
},

deleter/deleter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
//go:generate mockgen -source=deleter.go -destination=../mock/deleter.mock.go -package=mocks
28-
type iDeleter[T any] interface {
28+
type IDeleter[T any] interface {
2929
DeleteOne(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
3030
DeleteMany(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
3131
}
@@ -34,6 +34,8 @@ func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
3434
return &Deleter[T]{collection: collection, filter: nil}
3535
}
3636

37+
var _ IDeleter[any] = (*Deleter[any])(nil)
38+
3739
type Deleter[T any] struct {
3840
collection *mongo.Collection
3941
filter any

deleter/deleter_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ func TestDeleter_DeleteOne(t *testing.T) {
6060
testCases := []struct {
6161
name string
6262

63-
mock func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser]
63+
mock func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser]
6464
ctx context.Context
6565

6666
want *mongo.DeleteResult
6767
wantErr assert.ErrorAssertionFunc
6868
}{
6969
{
7070
name: "error: nil filter",
71-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
72-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
71+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
72+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
7373
mockCollection.EXPECT().DeleteOne(ctx).Return(nil, errors.New("nil filter")).Times(1)
7474
return mockCollection
7575
},
@@ -80,8 +80,8 @@ func TestDeleter_DeleteOne(t *testing.T) {
8080
},
8181
{
8282
name: "deleted count: 0",
83-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
84-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
83+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
84+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
8585
mockCollection.EXPECT().DeleteOne(ctx).Return(&mongo.DeleteResult{DeletedCount: 0}, nil).Times(1)
8686
return mockCollection
8787
},
@@ -93,8 +93,8 @@ func TestDeleter_DeleteOne(t *testing.T) {
9393
},
9494
{
9595
name: "delete success",
96-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
97-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
96+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
97+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
9898
mockCollection.EXPECT().DeleteOne(ctx).Return(&mongo.DeleteResult{DeletedCount: 1}, nil).Times(1)
9999
return mockCollection
100100
},
@@ -125,16 +125,16 @@ func TestDeleter_DeleteMany(t *testing.T) {
125125
testCases := []struct {
126126
name string
127127

128-
mock func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser]
128+
mock func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser]
129129
ctx context.Context
130130

131131
want *mongo.DeleteResult
132132
wantErr assert.ErrorAssertionFunc
133133
}{
134134
{
135135
name: "error: nil filter",
136-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
137-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
136+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
137+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
138138
mockCollection.EXPECT().DeleteMany(ctx).Return(nil, errors.New("nil filter")).Times(1)
139139
return mockCollection
140140
},
@@ -145,8 +145,8 @@ func TestDeleter_DeleteMany(t *testing.T) {
145145
},
146146
{
147147
name: "deleted count: 0",
148-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
149-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
148+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
149+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
150150
mockCollection.EXPECT().DeleteMany(ctx).Return(&mongo.DeleteResult{DeletedCount: 0}, nil).Times(1)
151151
return mockCollection
152152
},
@@ -158,8 +158,8 @@ func TestDeleter_DeleteMany(t *testing.T) {
158158
},
159159
{
160160
name: "delete success",
161-
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
162-
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
161+
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
162+
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
163163
mockCollection.EXPECT().DeleteMany(ctx).Return(&mongo.DeleteResult{DeletedCount: 2}, nil).Times(1)
164164
return mockCollection
165165
},

finder/finder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ import (
2626
)
2727

2828
//go:generate mockgen -source=finder.go -destination=../mock/finder.mock.go -package=mocks
29-
type iFinder[T any] interface {
29+
type IFinder[T any] interface {
3030
FindOne(ctx context.Context, opts ...*options.FindOneOptions) (*T, error)
3131
Find(ctx context.Context, opts ...*options.FindOptions) ([]*T, error)
3232
Count(ctx context.Context, opts ...*options.CountOptions) (int64, error)
3333
Distinct(ctx context.Context, fieldName string, opts ...*options.DistinctOptions) ([]any, error)
3434
DistinctWithParse(ctx context.Context, fieldName string, result any, opts ...*options.DistinctOptions) error
35+
FindOneAndUpdate(ctx context.Context, opts ...*options.FindOneAndUpdateOptions) (*T, error)
3536
}
3637

3738
func NewFinder[T any](collection *mongo.Collection) *Finder[T] {
3839
return &Finder[T]{collection: collection, filter: bson.D{}}
3940
}
4041

41-
var _ iFinder[any] = (*Finder[any])(nil)
42+
var _ IFinder[any] = (*Finder[any])(nil)
4243

4344
type Finder[T any] struct {
4445
collection *mongo.Collection

0 commit comments

Comments
 (0)