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) }) }) }) }