github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/account_identity/zz_account_identity_test.go (about)

     1  package account_identity_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	. "github.com/onsi/gomega"
     9  
    10  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
    11  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    13  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    14  	"github.com/machinefi/w3bstream/pkg/enums"
    15  	"github.com/machinefi/w3bstream/pkg/errors/status"
    16  	"github.com/machinefi/w3bstream/pkg/modules/account_identity"
    17  	mock_sqlx "github.com/machinefi/w3bstream/pkg/test/mock_depends_kit_sqlx"
    18  	"github.com/machinefi/w3bstream/pkg/types"
    19  )
    20  
    21  func TestAccountIdentity(t *testing.T) {
    22  	c := gomock.NewController(t)
    23  	defer c.Finish()
    24  
    25  	d := mock_sqlx.NewMockDBExecutor(c)
    26  	idg := confid.MustNewSFIDGenerator()
    27  	ctx := contextx.WithContextCompose(
    28  		types.WithMgrDBExecutorContext(d),
    29  		confid.WithSFIDGeneratorContext(idg),
    30  	)(context.Background())
    31  
    32  	t.Run("GetBySFIDAndType", func(t *testing.T) {
    33  		d.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
    34  
    35  		id, typ := idg.MustGenSFID(), enums.ACCOUNT_IDENTITY_TYPE__USERNAME
    36  
    37  		f := account_identity.GetBySFIDAndType
    38  
    39  		t.Run("#Success", func(t *testing.T) {
    40  			d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil)
    41  
    42  			_, err := f(ctx, id, typ)
    43  			NewWithT(t).Expect(err).To(BeNil())
    44  		})
    45  
    46  		t.Run("#Failed", func(t *testing.T) {
    47  			t.Run("#AccountIdentityNotFound", func(t *testing.T) {
    48  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound)
    49  
    50  				_, err := f(ctx, id, typ)
    51  				NewWithT(t).Expect(err).NotTo(BeNil())
    52  
    53  				se, ok := statusx.IsStatusErr(err)
    54  				NewWithT(t).Expect(ok).To(BeTrue())
    55  				NewWithT(t).Expect(se.Key).To(Equal(status.AccountIdentityNotFound.Key()))
    56  			})
    57  			t.Run("#DatabaseError", func(t *testing.T) {
    58  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrDatabase)
    59  
    60  				_, err := f(ctx, id, typ)
    61  				NewWithT(t).Expect(err).NotTo(BeNil())
    62  
    63  				se, ok := statusx.IsStatusErr(err)
    64  				NewWithT(t).Expect(ok).To(BeTrue())
    65  				NewWithT(t).Expect(se.Key).To(Equal(status.DatabaseError.Key()))
    66  			})
    67  		})
    68  	})
    69  }