init
This commit is contained in:
45
backend/app/services/provider.gen.go
Executable file
45
backend/app/services/provider.gen.go
Executable file
@@ -0,0 +1,45 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
db *gorm.DB,
|
||||
test *test,
|
||||
user *user,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &services{
|
||||
db: db,
|
||||
test: test,
|
||||
user: user,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*test, error) {
|
||||
obj := &test{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*user, error) {
|
||||
obj := &user{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
31
backend/app/services/services.gen.go
Normal file
31
backend/app/services/services.gen.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _db *gorm.DB
|
||||
|
||||
// exported CamelCase Services
|
||||
var (
|
||||
Test *test
|
||||
User *user
|
||||
)
|
||||
|
||||
// @provider(model)
|
||||
type services struct {
|
||||
db *gorm.DB
|
||||
// define Services
|
||||
test *test
|
||||
user *user
|
||||
}
|
||||
|
||||
func (svc *services) Prepare() error {
|
||||
_db = svc.db
|
||||
|
||||
// set exported Services here
|
||||
Test = svc.test
|
||||
User = svc.user
|
||||
|
||||
return nil
|
||||
}
|
||||
10
backend/app/services/test.go
Normal file
10
backend/app/services/test.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package services
|
||||
|
||||
import "context"
|
||||
|
||||
// @provider
|
||||
type test struct{}
|
||||
|
||||
func (t *test) Test(ctx context.Context) (string, error) {
|
||||
return "Test", nil
|
||||
}
|
||||
44
backend/app/services/test_test.go
Normal file
44
backend/app/services/test_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
//go:build legacytests
|
||||
// +build legacytests
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type TestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type TestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
TestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_Test(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p TestSuiteInjectParams) {
|
||||
suite.Run(t, &TestSuite{TestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TestSuite) Test_Test() {
|
||||
Convey("test_work", t.T(), func() {
|
||||
t.T().Log("start test at", time.Now())
|
||||
})
|
||||
}
|
||||
33
backend/app/services/user.go
Normal file
33
backend/app/services/user.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type user struct{}
|
||||
|
||||
func (t *user) FindByUsername(ctx context.Context, username string) (*models.User, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
model, err := query.Where(tbl.Username.Eq(username)).First()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FindByusername failed, %s", username)
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
func (t *user) Create(ctx context.Context, user *models.User) (*models.User, error) {
|
||||
if err := user.EncryptPassword(ctx); err != nil {
|
||||
return nil, errors.Wrap(err, "encrypt user password failed")
|
||||
}
|
||||
|
||||
if err := user.Create(ctx); err != nil {
|
||||
return nil, errors.Wrapf(err, "Create user failed, %s", user.Username)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
96
backend/app/services/user_test.go
Normal file
96
backend/app/services/user_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/gen/types"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type UserTestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *sql.DB
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type UserTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
UserTestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_User(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p UserTestSuiteInjectParams) {
|
||||
suite.Run(t, &UserTestSuite{UserTestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *UserTestSuite) Test_Create() {
|
||||
Convey("test user create", t.T(), func() {
|
||||
database.Truncate(t.T().Context(), t.DB, models.TableNameUser)
|
||||
|
||||
m := &models.User{
|
||||
Username: "test-user",
|
||||
Password: "test-password",
|
||||
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||||
Status: consts.UserStatusPendingVerify,
|
||||
}
|
||||
|
||||
err := m.Create(t.T().Context())
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(m.ID, ShouldBeGreaterThan, 0)
|
||||
|
||||
same := m.ComparePassword(t.T().Context(), "test-password")
|
||||
So(same, ShouldBeTrue)
|
||||
|
||||
same = m.ComparePassword(t.T().Context(), "test-password1")
|
||||
So(same, ShouldBeFalse)
|
||||
})
|
||||
}
|
||||
|
||||
// FindByUsername
|
||||
func (t *UserTestSuite) Test_FindByUsername() {
|
||||
Convey("test user FindByUsername", t.T(), func() {
|
||||
database.Truncate(t.T().Context(), t.DB, models.TableNameUser)
|
||||
|
||||
Convey("user table is empty", func() {
|
||||
m, err := User.FindByUsername(t.T().Context(), "test-user")
|
||||
So(err, ShouldNotBeNil)
|
||||
So(m, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("insert one record", func() {
|
||||
username := "test-user"
|
||||
m := &models.User{
|
||||
Username: username,
|
||||
Password: "test-password",
|
||||
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||||
Status: consts.UserStatusPendingVerify,
|
||||
}
|
||||
|
||||
err := m.Create(t.T().Context())
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("user table is not empty", func() {
|
||||
m, err := User.FindByUsername(t.T().Context(), username)
|
||||
So(err, ShouldBeNil)
|
||||
So(m, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user