Skip to content

Commit

Permalink
test: add unit tests and remove unused function
Browse files Browse the repository at this point in the history
  • Loading branch information
miilord committed Nov 13, 2023
1 parent 2f0ff0f commit 482ec25
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
9 changes: 9 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ func TestRepo_Distinct(t *testing.T) {
t.Errorf("Expected no error, but got an error: %v", err)
}
assert.ElementsMatch(t, []interface{}{"go", "goo"}, values)

values2, err := repo.Distinct(ctx, "name", &TestUser{Age: 2})
if len(values2) == 0 {
t.Errorf("Expected non-empty distinct values, but got an empty slice")
}
if err != nil {
t.Errorf("Expected no error, but got an error: %v", err)
}
assert.ElementsMatch(t, []interface{}{"go"}, values2)
}

func TestRepo_Aggregate(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package modm
import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

// TestDefaultFieldHooks verifies that the DefaultField hooks are functioning correctly.
Expand All @@ -22,10 +24,21 @@ func TestDefaultFieldHooks(t *testing.T) {
if df.UpdatedAt.IsZero() {
t.Fatalf("BeforeInsert did not set a default UpdatedAt")
}
df.AfterInsert(ctx)

// Test the BeforeUpdate hook
df.BeforeUpdate(ctx)
if df.UpdatedAt.IsZero() {
t.Fatalf("BeforeUpdate did not set a default UpdatedAt")
}
df.AfterUpdate(ctx)

df.AfterFind(ctx)

uniques := df.Uniques()
require.Zero(t, len(uniques))
require.NotNil(t, uniques)
indexes := df.Indexes()
require.Zero(t, len(indexes))
require.NotNil(t, indexes)
}
17 changes: 0 additions & 17 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modm

import (
"encoding/json"
"strings"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -15,22 +14,6 @@ func GetPointer[T any](value T) *T {
return &value
}

// DeepCopy creates a deep copy of the source document using JSON encoding and decoding.
func DeepCopy[T comparable](src T) T {
srcJSON, err := json.Marshal(src)
if err != nil {
return *new(T)
}

var dest T
err = json.Unmarshal(srcJSON, &dest)
if err != nil {
return *new(T)
}

return dest
}

// StructToBSOND converts structure variables to bson.D.
func StructToBSOND(v interface{}) (doc bson.D, err error) {
data, err := bson.Marshal(v)
Expand Down
39 changes: 26 additions & 13 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ func TestGetPointer(t *testing.T) {
assert.Equal(t, value, *ptr)
}

func TestDeepCopy(t *testing.T) {
type User struct {
Name string
Age int
}
src := User{
Name: "John",
Age: 30,
}
dest := DeepCopy[User](src)
assert.Equal(t, src, dest)
}

func TestStructToBSOND(t *testing.T) {
// Test struct to bson.D conversion
src := struct {
Expand Down Expand Up @@ -107,3 +94,29 @@ func TestIndexesToModel(t *testing.T) {
assert.Equal(t, bs, ebs)
}
}

func TestSplitSortField(t *testing.T) {
t.Run("Empty Field", func(t *testing.T) {
key, sort := SplitSortField("")
assert.Equal(t, "", key)
assert.Equal(t, int32(1), sort)
})

t.Run("Sort Ascending", func(t *testing.T) {
key, sort := SplitSortField("+fieldName")
assert.Equal(t, "fieldName", key)
assert.Equal(t, int32(1), sort)
})

t.Run("Sort Descending", func(t *testing.T) {
key, sort := SplitSortField("-fieldName")
assert.Equal(t, "fieldName", key)
assert.Equal(t, int32(-1), sort)
})

t.Run("Invalid Sort Symbol", func(t *testing.T) {
key, sort := SplitSortField("*fieldName")
assert.Equal(t, "*fieldName", key)
assert.Equal(t, int32(1), sort)
})
}

0 comments on commit 482ec25

Please sign in to comment.