package model import ( "context" "testing" "quyun/app/service/testx" "quyun/database" "quyun/database/fields" . "github.com/smartystreets/goconvey/convey" "go.ipao.vip/atom/contracts" // . "github.com/go-jet/jet/v2/postgres" "github.com/stretchr/testify/suite" "go.uber.org/dig" ) type PostsInjectParams struct { dig.In Initials []contracts.Initial `group:"initials"` } type PostsTestSuite struct { suite.Suite PostsInjectParams } func Test_Posts(t *testing.T) { providers := testx.Default().With(Provide) testx.Serve(providers, t, func(params PostsInjectParams) { suite.Run(t, &PostsTestSuite{ PostsInjectParams: params, }) }) } func (s *PostsTestSuite) Test_Demo() { Convey("Test_Demo", s.T(), func() { database.Truncate(context.Background(), db, tblPosts.TableName()) }) } // Test_Create func (s *PostsTestSuite) Test_Create() { Convey("Test_Create", s.T(), func() { // database.Truncate(context.Background(), db, tblPosts.TableName()) post := &Posts{ Title: "Test Post", Description: "This is a test post", Content: "Post content goes here", Price: 100, Discount: 10, Status: fields.PostStatusDraft, } err := post.Create(context.Background()) So(err, ShouldBeNil) So(post.ID, ShouldNotBeZeroValue) So(post.CreatedAt, ShouldNotBeZeroValue) So(post.UpdatedAt, ShouldNotBeZeroValue) }) } func (s *PostsTestSuite) Test_IncrView() { Convey("Test_IncrView", s.T(), func() { // database.Truncate(context.Background(), db, tblPosts.TableName()) post, err := PostsModel().GetByID(context.Background(), 1) So(err, ShouldBeNil) oldViews := post.Views So(post, ShouldNotBeNil) err = post.IncrViewCount(context.Background()) So(err, ShouldBeNil) So(post.Views, ShouldBeGreaterThan, oldViews) }) }