Files
mp-qvyun/backend/modules/users/service_test.go
2024-12-02 11:29:15 +08:00

68 lines
1.5 KiB
Go

package users
import (
"context"
"testing"
"backend/fixtures"
dbUtil "backend/pkg/db"
"backend/pkg/pg"
. "github.com/smartystreets/goconvey/convey"
)
func TestService_GetOrNew(t *testing.T) {
FocusConvey("Test GetOrNew", t, func() {
// So(dbUtil.TruncateAllTables(context.TODO(), db, "users", "users_tenants"), ShouldBeNil)
db, err := fixtures.GetDB()
So(err, ShouldBeNil)
defer db.Close()
Convey("Test GetOrNew", func() {
svc := &Service{db: db}
So(svc.Prepare(), ShouldBeNil)
user, err := svc.GetByOpenID(context.Background(), "hello")
So(err, ShouldBeNil)
So(user, ShouldNotBeNil)
So(user.OpenID, ShouldEqual, "hello")
})
FocusConvey("Test GetOrNew", func() {
svc := &Service{db: db}
So(svc.Prepare(), ShouldBeNil)
openid := "test_openid"
authInfo := pg.UserOAuth{
AccessToken: "test_access_token",
}
user, err := svc.GetOrNew(context.Background(), 1, openid, authInfo)
So(err, ShouldBeNil)
So(user.OpenID, ShouldEqual, openid)
})
})
}
func TestService_CreateTenantUser(t *testing.T) {
FocusConvey("Test CreateTenantUser", t, func() {
db, err := fixtures.GetDB()
So(err, ShouldBeNil)
defer db.Close()
So(dbUtil.TruncateAllTables(context.TODO(), db, "users", "users_tenants"), ShouldBeNil)
FocusConvey("Test Create", func() {
svc := &Service{db: db}
So(svc.Prepare(), ShouldBeNil)
err := svc.CreateTenantUser(context.Background(), 1, 1)
So(err, ShouldBeNil)
err = svc.CreateTenantUser(context.Background(), 1, 1)
So(err, ShouldBeNil)
})
})
}