-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_test.go
55 lines (43 loc) · 1.33 KB
/
document_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package modm
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestUser struct {
DefaultField `bson:",inline"`
Name string `bson:"name,omitempty" json:"name"`
Age uint `bson:"age,omitempty" json:"age"`
Bio string `bson:"-" json:"bio"`
}
func (u *TestUser) AfterFind(ctx context.Context) {
u.Bio = fmt.Sprintf("%s is %d years old.", u.Name, u.Age)
}
var AfterInsertOK = false
func (u *TestUser) AfterInsert(ctx context.Context) {
AfterInsertOK = true
}
var AfterUpdateOK = false
func (u *TestUser) AfterUpdate(ctx context.Context) {
AfterUpdateOK = true
}
func TestDocument(t *testing.T) {
db, cleanup := setupTestDatabase(t)
defer cleanup()
repo := NewRepo[*TestUser](db.Collection(testColl))
ctx := context.TODO()
user, err := repo.InsertOne(ctx, &TestUser{Name: "gooooo", Age: 6})
require.NoError(t, err)
require.NotNil(t, user.CreatedAt)
require.True(t, AfterInsertOK)
modifiedCount, err := repo.UpdateOne(ctx, &TestUser{Age: 6}, &TestUser{Name: "goooooo", Age: 7})
require.NoError(t, err)
require.NotZero(t, modifiedCount)
require.True(t, AfterUpdateOK)
u, err := repo.FindOne(ctx, &TestUser{Age: 7})
require.NoError(t, err)
assert.NotEqual(t, u.CreatedAt, u.UpdatedAt)
assert.Equal(t, "goooooo is 7 years old.", u.Bio)
}