Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
)

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

var _ iAggregator[any] = (*Aggregator[any])(nil)
var _ IAggregator[any] = (*Aggregator[any])(nil)

type Aggregator[T any] struct {
collection *mongo.Collection
Expand Down
20 changes: 10 additions & 10 deletions aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func TestAggregator_New(t *testing.T) {
func TestAggregator_Aggregation(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser]
ctx context.Context
want []*TestUser
wantErr assert.ErrorAssertionFunc
}{
{
name: "got error",
mock: func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser] {
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser] {
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
aggregator.EXPECT().Aggregate(ctx).Return(nil, errors.New("can only marshal slices and arrays into aggregation pipelines, but got invalid")).Times(1)
return aggregator
},
Expand All @@ -97,8 +97,8 @@ func TestAggregator_Aggregation(t *testing.T) {
},
{
name: "got result",
mock: func(ctx context.Context, ctl *gomock.Controller) iAggregator[TestUser] {
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IAggregator[TestUser] {
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
aggregator.EXPECT().Aggregate(ctx).Return([]*TestUser{
{Name: "chenmingyong", Age: 24},
{Name: "gopher", Age: 25},
Expand Down Expand Up @@ -136,16 +136,16 @@ func TestAggregator_AggregateWithParse(t *testing.T) {
}
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser]
ctx context.Context
result []*User
want []*User
wantErr assert.ErrorAssertionFunc
}{
{
name: "got error",
mock: func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser] {
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser] {
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
aggregator.EXPECT().AggregateWithParse(ctx, result).Return(errors.New("can only marshal slices and arrays into aggregation pipelines, but got invalid")).Times(1)
return aggregator
},
Expand All @@ -155,8 +155,8 @@ func TestAggregator_AggregateWithParse(t *testing.T) {
},
{
name: "got result",
mock: func(ctx context.Context, ctl *gomock.Controller, result any) iAggregator[TestUser] {
aggregator := mocks.NewMockiAggregator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, result any) IAggregator[TestUser] {
aggregator := mocks.NewMockIAggregator[TestUser](ctl)
aggregator.EXPECT().AggregateWithParse(ctx, result).Return(nil).Times(1)
return aggregator
},
Expand Down
4 changes: 2 additions & 2 deletions creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
)

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

var _ iCreator[any] = (*Creator[any])(nil)
var _ ICreator[any] = (*Creator[any])(nil)

type Creator[T any] struct {
collection *mongo.Collection
Expand Down
20 changes: 10 additions & 10 deletions creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ func TestNewCreator(t *testing.T) {
func TestCreator_One(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser]
ctx context.Context
doc *TestUser

wantErr error
}{
{
name: "nil doc",
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser] {
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
mockCollection := mocks.NewMockICreator[TestUser](ctl)
mockCollection.EXPECT().InsertOne(ctx, doc).Return(nil, errors.New("nil filter")).Times(1)
return mockCollection
},
Expand All @@ -77,8 +77,8 @@ func TestCreator_One(t *testing.T) {
},
{
name: "success",
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) iCreator[TestUser] {
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
mockCollection := mocks.NewMockICreator[TestUser](ctl)
mockCollection.EXPECT().InsertOne(ctx, doc).Return(&mongo.InsertOneResult{InsertedID: "?"}, nil).Times(1)
return mockCollection
},
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestCreator_One(t *testing.T) {
func TestCreator_Many(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser]
ctx context.Context
docs []*TestUser

Expand All @@ -116,8 +116,8 @@ func TestCreator_Many(t *testing.T) {
}{
{
name: "nil docs",
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser] {
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
mockCollection := mocks.NewMockICreator[TestUser](ctl)
mockCollection.EXPECT().InsertMany(ctx, docs).Return(nil, errors.New("nil docs")).Times(1)
return mockCollection
},
Expand All @@ -130,8 +130,8 @@ func TestCreator_Many(t *testing.T) {
},
{
name: "success",
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) iCreator[TestUser] {
mockCollection := mocks.NewMockiCreator[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
mockCollection := mocks.NewMockICreator[TestUser](ctl)
mockCollection.EXPECT().InsertMany(ctx, docs).Return(&mongo.InsertManyResult{InsertedIDs: make([]interface{}, 2)}, nil).Times(1)
return mockCollection
},
Expand Down
4 changes: 2 additions & 2 deletions deleter/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
)

//go:generate mockgen -source=deleter.go -destination=../mock/deleter.mock.go -package=mocks
type iDeleter[T any] interface {
type IDeleter[T any] interface {
DeleteOne(ctx context.Context, opts ...options.Lister[options.DeleteOptions]) (*mongo.DeleteResult, error)
DeleteMany(ctx context.Context, opts ...options.Lister[options.DeleteOptions]) (*mongo.DeleteResult, error)
}

var _ iDeleter[any] = (*Deleter[any])(nil)
var _ IDeleter[any] = (*Deleter[any])(nil)

func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
return &Deleter[T]{collection: collection, filter: nil}
Expand Down
28 changes: 14 additions & 14 deletions deleter/deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ func TestDeleter_DeleteOne(t *testing.T) {
testCases := []struct {
name string

mock func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser]
ctx context.Context

want *mongo.DeleteResult
wantErr assert.ErrorAssertionFunc
}{
{
name: "error: nil filter",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteOne(ctx).Return(nil, errors.New("nil filter")).Times(1)
return mockCollection
},
Expand All @@ -79,8 +79,8 @@ func TestDeleter_DeleteOne(t *testing.T) {
},
{
name: "deleted count: 0",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteOne(ctx).Return(&mongo.DeleteResult{DeletedCount: 0}, nil).Times(1)
return mockCollection
},
Expand All @@ -92,8 +92,8 @@ func TestDeleter_DeleteOne(t *testing.T) {
},
{
name: "delete success",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteOne(ctx).Return(&mongo.DeleteResult{DeletedCount: 1}, nil).Times(1)
return mockCollection
},
Expand Down Expand Up @@ -124,16 +124,16 @@ func TestDeleter_DeleteMany(t *testing.T) {
testCases := []struct {
name string

mock func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser]
ctx context.Context

want *mongo.DeleteResult
wantErr assert.ErrorAssertionFunc
}{
{
name: "error: nil filter",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteMany(ctx).Return(nil, errors.New("nil filter")).Times(1)
return mockCollection
},
Expand All @@ -144,8 +144,8 @@ func TestDeleter_DeleteMany(t *testing.T) {
},
{
name: "deleted count: 0",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteMany(ctx).Return(&mongo.DeleteResult{DeletedCount: 0}, nil).Times(1)
return mockCollection
},
Expand All @@ -157,8 +157,8 @@ func TestDeleter_DeleteMany(t *testing.T) {
},
{
name: "delete success",
mock: func(ctx context.Context, ctl *gomock.Controller) iDeleter[TestUser] {
mockCollection := mocks.NewMockiDeleter[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IDeleter[TestUser] {
mockCollection := mocks.NewMockIDeleter[TestUser](ctl)
mockCollection.EXPECT().DeleteMany(ctx).Return(&mongo.DeleteResult{DeletedCount: 2}, nil).Times(1)
return mockCollection
},
Expand Down
4 changes: 2 additions & 2 deletions finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

//go:generate mockgen -source=finder.go -destination=../mock/finder.mock.go -package=mocks
type iFinder[T any] interface {
type IFinder[T any] interface {
FindOne(ctx context.Context, opts ...options.Lister[options.FindOneOptions]) (*T, error)
Find(ctx context.Context, opts ...options.Lister[options.FindOptions]) ([]*T, error)
Count(ctx context.Context, opts ...options.Lister[options.CountOptions]) (int64, error)
Expand All @@ -36,7 +36,7 @@ func NewFinder[T any](collection *mongo.Collection) *Finder[T] {
return &Finder[T]{collection: collection, filter: bson.D{}}
}

var _ iFinder[any] = (*Finder[any])(nil)
var _ IFinder[any] = (*Finder[any])(nil)

type Finder[T any] struct {
collection *mongo.Collection
Expand Down
34 changes: 17 additions & 17 deletions finder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ func TestFinder_New(t *testing.T) {
func TestFinder_One(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser]
ctx context.Context

want *TestUser
wantErr error
}{
{
name: "error",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().FindOne(gomock.Any()).Return(nil, mongo.ErrNoDocuments).Times(1)
return mockCollection
},
wantErr: mongo.ErrNoDocuments,
},
{
name: "match the first one",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().FindOne(gomock.Any()).Return(&TestUser{
Name: "chenmingyong",
Age: 24,
Expand Down Expand Up @@ -84,25 +84,25 @@ func TestFinder_One(t *testing.T) {
func TestFinder_All(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser]
ctx context.Context

want []*TestUser
wantErr error
}{
{
name: "empty documents",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().Find(ctx).Return([]*TestUser{}, nil).Times(1)
return mockCollection
},
want: []*TestUser{},
},
{
name: "matched",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().Find(ctx).Return([]*TestUser{
{
Name: "chenmingyong",
Expand Down Expand Up @@ -143,16 +143,16 @@ func TestFinder_All(t *testing.T) {
func TestFinder_Count(t *testing.T) {
testCases := []struct {
name string
mock func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser]
mock func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser]
ctx context.Context

want int64
wantErr error
}{
{
name: "error",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().Count(ctx).Return(int64(0), errors.New("nil filter error")).Times(1)
return mockCollection
},
Expand All @@ -161,17 +161,17 @@ func TestFinder_Count(t *testing.T) {
},
{
name: "matched 0",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().Count(ctx).Return(int64(0), nil).Times(1)
return mockCollection
},
want: 0,
},
{
name: "matched 1",
mock: func(ctx context.Context, ctl *gomock.Controller) iFinder[TestUser] {
mockCollection := mocks.NewMockiFinder[TestUser](ctl)
mock: func(ctx context.Context, ctl *gomock.Controller) IFinder[TestUser] {
mockCollection := mocks.NewMockIFinder[TestUser](ctl)
mockCollection.EXPECT().Count(ctx).Return(int64(1), nil).Times(1)
return mockCollection
},
Expand Down
Loading
Loading