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

     1  package resource
     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"
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/mock"
    14  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    15  	"github.com/machinefi/w3bstream/pkg/errors/status"
    16  	"github.com/machinefi/w3bstream/pkg/models"
    17  	"github.com/machinefi/w3bstream/pkg/types"
    18  )
    19  
    20  func TestResource(t *testing.T) {
    21  	ctrl := gomock.NewController(t)
    22  	defer ctrl.Finish()
    23  
    24  	var (
    25  		db  = mock.NewMockDBExecutor(ctrl)
    26  		idg = confid.MustNewSFIDGenerator()
    27  		ctx = contextx.WithContextCompose(
    28  			types.WithMgrDBExecutorContext(db),
    29  			confid.WithSFIDGeneratorContext(idg),
    30  			types.WithAccountContext(&models.Account{
    31  				RelAccount: models.RelAccount{AccountID: idg.MustGenSFID()},
    32  			}),
    33  		)(context.Background())
    34  	)
    35  
    36  	t.Run("Get", func(t *testing.T) {
    37  		t.Run("#Success", func(t *testing.T) {
    38  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).Times(2)
    39  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(2)
    40  
    41  			// GetBySFID
    42  			{
    43  				_, err := GetBySFID(ctx, idg.MustGenSFID())
    44  				NewWithT(t).Expect(err).To(BeNil())
    45  			}
    46  
    47  			// GetByMd5
    48  			{
    49  				_, err := GetByMd5(ctx, "resource_md5")
    50  				NewWithT(t).Expect(err).To(BeNil())
    51  			}
    52  		})
    53  
    54  		t.Run("#ResourceNotFound", func(t *testing.T) {
    55  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).Times(2)
    56  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, "")).Times(2)
    57  
    58  			// GetBySFID
    59  			{
    60  				_, err := GetBySFID(ctx, 1)
    61  				NewWithT(t).Expect(err).To(Equal(status.ResourceNotFound))
    62  			}
    63  
    64  			// GetByMd5
    65  			{
    66  				_, err := GetByMd5(ctx, "resource_md5")
    67  				NewWithT(t).Expect(err).To(Equal(status.ResourceNotFound))
    68  			}
    69  		})
    70  	})
    71  
    72  	t.Run("List", func(t *testing.T) {
    73  		t.Run("Success", func(t *testing.T) {
    74  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).Times(4)
    75  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(2)
    76  
    77  			_, err := List(ctx, &ListReq{})
    78  			NewWithT(t).Expect(err).To(BeNil())
    79  		})
    80  	})
    81  
    82  	t.Run("GetOwnerByAccountAndSFID", func(t *testing.T) {
    83  		t.Run("Success", func(t *testing.T) {
    84  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    85  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil)
    86  
    87  			_, err := GetOwnerByAccountAndSFID(ctx, types.MustAccountFromContext(ctx).AccountID, 1)
    88  			NewWithT(t).Expect(err).To(BeNil())
    89  		})
    90  
    91  		t.Run("#ResourceNotFound", func(t *testing.T) {
    92  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    93  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, ""))
    94  
    95  			_, err := GetOwnerByAccountAndSFID(ctx, types.MustAccountFromContext(ctx).AccountID, 1)
    96  			NewWithT(t).Expect(err).To(Equal(status.ResourcePermNotFound))
    97  		})
    98  	})
    99  
   100  	t.Run("RemoveOwnershipBySFID", func(t *testing.T) {
   101  		t.Run("Success", func(t *testing.T) {
   102  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
   103  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil)
   104  
   105  			err := RemoveOwnershipBySFID(ctx, 1)
   106  			NewWithT(t).Expect(err).To(BeNil())
   107  		})
   108  	})
   109  }