Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jensteichert committed Nov 27, 2024
1 parent 26a5a2c commit 1f21d09
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 24 deletions.
4 changes: 0 additions & 4 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)


type Collection[T Document] struct {
collection *mongo.Collection
}
Expand Down Expand Up @@ -52,7 +51,6 @@ func (repo *Collection[T]) UpdateMany(filter interface{}, doc bson.M) error {
return err
}


func (repo *Collection[T]) DeleteById(id string) error {
res, err := repo.collection.DeleteOne(DefaultContext(), bson.M{"_id": id})

Expand Down Expand Up @@ -112,8 +110,6 @@ func (repo *Collection[T]) Drop() error {
return err
}


func (repo *Collection[T]) NewId() primitive.ObjectID {
return primitive.NewObjectID()
}

5 changes: 0 additions & 5 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type testdoc struct {
Title string `bson:"title" json:"title"`
}


type testdocWithCustomID struct {
Doc `bson:",inline"`
Title string `bson:"title" json:"title"`
Expand All @@ -27,7 +26,6 @@ func (pg *testdocWithCustomID) NewID() string {
return "test_" + primitive.NewObjectID().Hex()
}


func TestCollection_Insert(t *testing.T) {
rand.Seed(time.Now().UnixNano())
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
Expand Down Expand Up @@ -252,7 +250,6 @@ func TestCollection_DeleteById(t *testing.T) {
mockDb.Disconnect()
}


func TestCollection_CountDocuments(t *testing.T) {
rand.Seed(time.Now().UnixNano())
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
Expand Down Expand Up @@ -281,8 +278,6 @@ func TestCollection_CountDocuments(t *testing.T) {
mockDb.Disconnect()
}



func TestCollection_Aggregate(t *testing.T) {
rand.Seed(time.Now().UnixNano())
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
Expand Down
2 changes: 1 addition & 1 deletion database.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (db *Database) Connect(connectionString string, dbName string) error {
}

func (db *Database) Disconnect() error {
err := db.client.Disconnect(DefaultContext());
err := db.client.Disconnect(DefaultContext())
db.db = nil
return err
}
Expand Down
10 changes: 5 additions & 5 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Document interface {
}

type Doc struct {
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
}

func (doc *Doc) NewID() string {
Expand All @@ -30,8 +30,8 @@ func (doc *Doc) GetID() string {
}

type DocWithTimestamps struct {
Doc `bson:",inline"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
Doc `bson:",inline"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty" bson:"updated_at,omitempty"`
}

Expand All @@ -41,7 +41,7 @@ func (doc *DocWithTimestamps) BeforeInsert() error {
}

func (doc *DocWithTimestamps) BeforeUpdate() error {
now := time.Now();
now := time.Now()
doc.UpdatedAt = &now
return nil
}
}
2 changes: 1 addition & 1 deletion document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ func TestCDocument_NewID_Custom(t *testing.T) {

assert.NotEmpty(t, doc.NewID())
assert.True(t, strings.HasPrefix(doc.NewID(), "td_"))
}
}
2 changes: 1 addition & 1 deletion hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type BeforeInsertHook interface {

type BeforeUpdateHook interface {
BeforeUpdate() error
}
}
11 changes: 6 additions & 5 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import (
)

type testDoc struct {
mock.Mock `bson:"-"`
mock.Mock `bson:"-"`
DocWithTimestamps `bson:",inline"`
Title string `bson:"title" json:"title"`
Title string `bson:"title" json:"title"`
}

func(t *testDoc) BeforeInsert() error {
func (t *testDoc) BeforeInsert() error {
t.DocWithTimestamps.BeforeInsert()
args := t.Called()
return args.Error(0)
}

func(t *testDoc) BeforeUpdate() error {
func (t *testDoc) BeforeUpdate() error {
t.DocWithTimestamps.BeforeUpdate()
args := t.Called()
return args.Error(0)
}

var mockDb = Database{}

func TestBeforeInsertHook(t *testing.T) {
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
doc := testDoc{Title: "Test"}
Expand All @@ -47,4 +48,4 @@ func TestBeforeUpdateHook(t *testing.T) {
assert.Nil(t, updateErr)

doc.AssertExpectations(t)
}
}
4 changes: 2 additions & 2 deletions indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

func (repo *Collection[T]) CreateIndex(keys bson.D) error {
func (repo *Collection[T]) CreateIndex(keys bson.D) error {
mod := mongo.IndexModel{
Keys: keys,
Keys: keys,
Options: nil,
}
_, err := repo.collection.Indexes().CreateOne(DefaultContext(), mod)
Expand Down

0 comments on commit 1f21d09

Please sign in to comment.