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: 3 additions & 1 deletion aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ 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.AggregateOptions) ([]*T, error)
AggregateWithParse(ctx context.Context, result any, opts ...*options.AggregateOptions) error
}

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

type Aggregator[T any] struct {
collection *mongo.Collection
pipeline any
Expand Down
20 changes: 10 additions & 10 deletions aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,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 @@ -98,8 +98,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 @@ -137,16 +137,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 @@ -156,8 +156,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: 3 additions & 1 deletion creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ 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.InsertOneOptions) (*mongo.InsertOneResult, error)
InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
}

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

type Creator[T any] struct {
collection *mongo.Collection
modelHook any
Expand Down
20 changes: 10 additions & 10 deletions creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,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 @@ -79,8 +79,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 @@ -109,7 +109,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 @@ -118,8 +118,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 @@ -132,8 +132,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: 3 additions & 1 deletion deleter/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
}
Expand All @@ -34,6 +34,8 @@ func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
return &Deleter[T]{collection: collection, filter: nil}
}

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

type Deleter[T any] struct {
collection *mongo.Collection
filter any
Expand Down
28 changes: 14 additions & 14 deletions deleter/deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,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 @@ -80,8 +80,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 @@ -93,8 +93,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 @@ -125,16 +125,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 @@ -145,8 +145,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 @@ -158,8 +158,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
5 changes: 3 additions & 2 deletions finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ 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.FindOneOptions) (*T, error)
Find(ctx context.Context, opts ...*options.FindOptions) ([]*T, error)
Count(ctx context.Context, opts ...*options.CountOptions) (int64, error)
Distinct(ctx context.Context, fieldName string, opts ...*options.DistinctOptions) ([]any, error)
DistinctWithParse(ctx context.Context, fieldName string, result any, opts ...*options.DistinctOptions) error
FindOneAndUpdate(ctx context.Context, opts ...*options.FindOneAndUpdateOptions) (*T, error)
}

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
Loading
Loading