flamingo.me/flamingo-commerce/v3@v3.11.0/cart/domain/validation/restrictionService_test.go (about)

     1  package validation_test
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"flamingo.me/flamingo-commerce/v3/cart/domain/validation"
    10  	"flamingo.me/flamingo/v3/framework/web"
    11  
    12  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
    13  	"flamingo.me/flamingo-commerce/v3/product/domain"
    14  )
    15  
    16  var _ validation.MaxQuantityRestrictor = (*MockRestrictor)(nil)
    17  
    18  type MockRestrictor struct {
    19  	IsRestricted  bool
    20  	MaxQty        int
    21  	DifferenceQty int
    22  }
    23  
    24  func (r *MockRestrictor) Name() string {
    25  	return "MockRestrictor"
    26  }
    27  
    28  func (r *MockRestrictor) Restrict(ctx context.Context, session *web.Session, product domain.BasicProduct, currentCart *cart.Cart, deliveryCode string) *validation.RestrictionResult {
    29  	return &validation.RestrictionResult{
    30  		IsRestricted:        r.IsRestricted,
    31  		MaxAllowed:          r.MaxQty,
    32  		RemainingDifference: r.DifferenceQty,
    33  	}
    34  }
    35  
    36  func TestRestrictionService_RestrictQty(t *testing.T) {
    37  	type fields struct {
    38  		qtyRestrictors []validation.MaxQuantityRestrictor
    39  	}
    40  	type args struct {
    41  		ctx          context.Context
    42  		product      domain.BasicProduct
    43  		cart         *cart.Cart
    44  		deliveryCode string
    45  	}
    46  	tests := []struct {
    47  		name                      string
    48  		fields                    fields
    49  		args                      args
    50  		expectedRestrictionResult *validation.RestrictionResult
    51  	}{
    52  		{
    53  			name: "no restrictors",
    54  			fields: fields{
    55  				qtyRestrictors: nil,
    56  			},
    57  			args: args{
    58  				ctx:     context.Background(),
    59  				product: nil,
    60  				cart:    nil,
    61  			},
    62  			expectedRestrictionResult: &validation.RestrictionResult{
    63  				IsRestricted:        false,
    64  				MaxAllowed:          math.MaxInt32,
    65  				RemainingDifference: math.MaxInt32,
    66  			},
    67  		},
    68  		{
    69  			name: "no restricting restrictors",
    70  			fields: fields{
    71  				qtyRestrictors: []validation.MaxQuantityRestrictor{&MockRestrictor{IsRestricted: false}, &MockRestrictor{IsRestricted: false}},
    72  			},
    73  			args: args{
    74  				ctx:     context.Background(),
    75  				product: nil,
    76  				cart:    nil,
    77  			},
    78  			expectedRestrictionResult: &validation.RestrictionResult{
    79  				IsRestricted:        false,
    80  				MaxAllowed:          math.MaxInt32,
    81  				RemainingDifference: math.MaxInt32,
    82  			},
    83  		},
    84  		{
    85  			name: "restrict to 5",
    86  			fields: fields{
    87  				qtyRestrictors: []validation.MaxQuantityRestrictor{&MockRestrictor{IsRestricted: true, MaxQty: 5, DifferenceQty: 5}},
    88  			},
    89  			args: args{
    90  				ctx:     context.Background(),
    91  				product: nil,
    92  				cart:    nil,
    93  			},
    94  			expectedRestrictionResult: &validation.RestrictionResult{
    95  				IsRestricted:        true,
    96  				MaxAllowed:          5,
    97  				RemainingDifference: 5,
    98  			},
    99  		},
   100  		{
   101  			name: "multiple restrictors to 17 / -7",
   102  			fields: fields{
   103  				qtyRestrictors: []validation.MaxQuantityRestrictor{
   104  					&MockRestrictor{IsRestricted: true, MaxQty: 19, DifferenceQty: 19},
   105  					&MockRestrictor{IsRestricted: true, MaxQty: 21, DifferenceQty: 5},
   106  					&MockRestrictor{IsRestricted: false, MaxQty: -42, DifferenceQty: -42},
   107  					&MockRestrictor{IsRestricted: true, MaxQty: 17, DifferenceQty: 6},
   108  					&MockRestrictor{IsRestricted: true, MaxQty: 500, DifferenceQty: -7},
   109  					&MockRestrictor{IsRestricted: true, MaxQty: math.MaxInt32, DifferenceQty: math.MaxInt32},
   110  				},
   111  			},
   112  			args: args{
   113  				ctx:     context.Background(),
   114  				product: nil,
   115  				cart:    nil,
   116  			},
   117  			expectedRestrictionResult: &validation.RestrictionResult{
   118  				IsRestricted:        true,
   119  				MaxAllowed:          17,
   120  				RemainingDifference: -7,
   121  			},
   122  		},
   123  	}
   124  	for _, tt := range tests {
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			rs := &validation.RestrictionService{}
   127  			rs.Inject(tt.fields.qtyRestrictors)
   128  			got := rs.RestrictQty(tt.args.ctx, web.EmptySession(), tt.args.product, tt.args.cart, tt.args.deliveryCode)
   129  			if !reflect.DeepEqual(got, tt.expectedRestrictionResult) {
   130  				t.Errorf("RestrictionService.RestrictQty() got = %v, expected = %v", got, tt.expectedRestrictionResult)
   131  			}
   132  		})
   133  	}
   134  }