109 lines
2.4 KiB
Go
109 lines
2.4 KiB
Go
package users
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"backend/app/service/testx"
|
|
"backend/database"
|
|
"backend/database/fields"
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
"backend/database/models/qvyun_v2/public/table"
|
|
|
|
"git.ipao.vip/rogeecn/atom/contracts"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
"github.com/stretchr/testify/suite"
|
|
"go.uber.org/dig"
|
|
)
|
|
|
|
type ServiceInjectParams struct {
|
|
dig.In
|
|
Initials []contracts.Initial `group:"initials"`
|
|
|
|
Svc *Service
|
|
}
|
|
|
|
type ServiceTestSuite struct {
|
|
suite.Suite
|
|
ServiceInjectParams
|
|
}
|
|
|
|
func Test_DiscoverMedias(t *testing.T) {
|
|
providers := testx.Default().With(
|
|
Provide,
|
|
)
|
|
|
|
testx.Serve(providers, t, func(params ServiceInjectParams) {
|
|
suite.Run(t, &ServiceTestSuite{ServiceInjectParams: params})
|
|
})
|
|
}
|
|
|
|
func (s *ServiceTestSuite) Test_GetUserIDByOpenID() {
|
|
Convey("Test GetUserIDByOpenID", s.T(), func() {
|
|
v, err := fields.AuthChannelWeChat.Value()
|
|
So(err, ShouldBeNil)
|
|
So(v, ShouldEqual, 1)
|
|
|
|
Convey("truncate tables", func() {
|
|
err := database.Truncate(context.Background(), s.Svc.db, table.UserOauths.TableName())
|
|
So(err, ShouldBeNil)
|
|
})
|
|
|
|
Convey("insert data", func() {
|
|
m := model.UserOauths{
|
|
Channel: fields.AuthChannelWeChat,
|
|
UserID: 1,
|
|
OpenID: "test_open_id",
|
|
AccessToken: "test_access_token",
|
|
RefreshToken: "test_refresh_token",
|
|
ExpireAt: time.Now().Add(time.Hour),
|
|
}
|
|
|
|
tbl := table.UserOauths
|
|
stmt := tbl.INSERT(
|
|
tbl.MutableColumns.Except(
|
|
tbl.CreatedAt,
|
|
tbl.UpdatedAt,
|
|
tbl.DeletedAt,
|
|
tbl.UnionID,
|
|
tbl.Meta,
|
|
),
|
|
).MODEL(m)
|
|
s.T().Log(stmt.Sql())
|
|
|
|
_, err := stmt.ExecContext(context.Background(), s.Svc.db)
|
|
So(err, ShouldBeNil)
|
|
})
|
|
|
|
Convey("get user id by open id", func() {
|
|
userID, err := s.Svc.GetUserIDByOpenID(context.Background(), fields.AuthChannelWeChat, "test_open_id")
|
|
So(err, ShouldBeNil)
|
|
So(userID, ShouldEqual, 1)
|
|
})
|
|
})
|
|
}
|
|
|
|
// CreateUser
|
|
func (s *ServiceTestSuite) Test_CreateUser() {
|
|
Convey("Test CreateUser", s.T(), func() {
|
|
Convey("truncate tables", func() {
|
|
err := database.Truncate(context.Background(), s.Svc.db, table.Users.TableName())
|
|
So(err, ShouldBeNil)
|
|
})
|
|
|
|
Convey("create user", func() {
|
|
user := &model.Users{
|
|
Email: "test@qq.com",
|
|
Phone: "12345678901",
|
|
Username: "test",
|
|
Password: "test",
|
|
Age: 18,
|
|
}
|
|
|
|
_, err := s.Svc.CreateUser(context.Background(), user)
|
|
So(err, ShouldBeNil)
|
|
})
|
|
})
|
|
}
|