github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/pkg/gormutil/callbacks_test.go (about)

     1  package gormutil
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/lovung/GoCleanArchitecture/pkg/jwtutil"
    10  
    11  	"gorm.io/gorm"
    12  )
    13  
    14  func TestDeletedByFromClaim(t *testing.T) {
    15  	db, _ := gorm.Open(nil, nil)
    16  	userID := uint64(1)
    17  	jwtClaims := jwtutil.JWTClaims{
    18  		UserID: userID,
    19  	}
    20  	ctx := context.WithValue(context.Background(), jwtutil.JWTClaimsKey, jwtClaims) // nolint
    21  	db = db.WithContext(ctx)
    22  
    23  	type args struct {
    24  		d *gorm.DB
    25  	}
    26  	tests := []struct {
    27  		name  string
    28  		args  args
    29  		want  map[string]interface{}
    30  		want1 bool
    31  	}{
    32  		{
    33  			name: "Test case1: success",
    34  			args: args{
    35  				d: db,
    36  			},
    37  			want:  map[string]interface{}{"deleted_by": userID},
    38  			want1: true,
    39  		},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			got, got1 := DeletedByFromClaim(tt.args.d)
    44  			if !reflect.DeepEqual(got, tt.want) {
    45  				t.Errorf("DeletedByFromClaim() got = %v, want %v", got, tt.want)
    46  			}
    47  			if got1 != tt.want1 {
    48  				t.Errorf("DeletedByFromClaim() got1 = %v, want %v", got1, tt.want1)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestSoftDeleteClauseFromClaim(t *testing.T) {
    55  	db, _ := gorm.Open(nil, nil)
    56  	userID := uint64(1)
    57  	jwtClaims := jwtutil.JWTClaims{
    58  		UserID: userID,
    59  	}
    60  	ctx := context.WithValue(context.Background(), jwtutil.JWTClaimsKey, jwtClaims) // nolint
    61  	db = db.WithContext(ctx)
    62  
    63  	now := time.Now()
    64  
    65  	type args struct {
    66  		d   *gorm.DB
    67  		now time.Time
    68  	}
    69  	tests := []struct {
    70  		name  string
    71  		args  args
    72  		want  map[string]interface{}
    73  		want1 bool
    74  	}{
    75  		{
    76  			name: "Test case1: success",
    77  			args: args{
    78  				d:   db,
    79  				now: now,
    80  			},
    81  			want: map[string]interface{}{
    82  				"deleted_by": userID,
    83  				"deleted_at": now,
    84  			},
    85  			want1: true,
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			got, got1 := SoftDeleteClauseFromClaim(tt.args.d, tt.args.now)
    91  			if !reflect.DeepEqual(got, tt.want) {
    92  				t.Errorf("SoftDeleteClauseFromClaim() got = %v, want %v", got, tt.want)
    93  			}
    94  			if got1 != tt.want1 {
    95  				t.Errorf("SoftDeleteClauseFromClaim() got1 = %v, want %v", got1, tt.want1)
    96  			}
    97  		})
    98  	}
    99  }