github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/vdutil/common_rules_test.go (about)

     1  package vdutil
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestGreaterThanZero(t *testing.T) {
    13  	var vdErr *ValidationError
    14  
    15  	_, err := Validate(context.Background(),
    16  		GreaterThanZero("testVar", 100, false))
    17  	require.Nil(t, err)
    18  
    19  	_, err = Validate(context.Background(),
    20  		GreaterThanZero("testVar", -100, false))
    21  	require.NotNil(t, err)
    22  	assert.True(t, errors.As(err, &vdErr))
    23  	assert.Contains(t, err.Error(), "testVar: value -100 <= 0")
    24  
    25  	got1, err := Validate(context.Background(),
    26  		GreaterThanZero("testVar", int64(100), true))
    27  	require.Nil(t, err)
    28  	assert.Equal(t, 100, got1.Data.GetInt("testVar"))
    29  
    30  	got2, err := Validate(context.Background(),
    31  		GreaterThanZero("testVar", "100", true))
    32  	require.Nil(t, err)
    33  	assert.Equal(t, int32(100), got2.Data.GetInt32("testVar"))
    34  
    35  	got3, err := Validate(context.Background(),
    36  		GreaterThanZero("testVar", "0", true))
    37  	require.NotNil(t, err)
    38  	require.NotNil(t, got3)
    39  	assert.True(t, errors.As(err, &vdErr))
    40  	assert.Contains(t, err.Error(), "testVar: value 0 <= 0")
    41  
    42  	got4, err := Validate(context.Background(),
    43  		GreaterThanZero("testVar", "xyz", true))
    44  	require.NotNil(t, err)
    45  	require.NotNil(t, got4)
    46  	assert.True(t, errors.As(err, &vdErr))
    47  	assert.Contains(t, err.Error(), "testVar: value xyz is not integer")
    48  }
    49  
    50  func TestAllElementsGreaterThanZero(t *testing.T) {
    51  	t.Run("not integer", func(t *testing.T) {
    52  		slice := []string{"1", "xyz", "2"}
    53  		got, err := Validate(context.Background(),
    54  			AllElementsGreaterThanZero("testVar", slice, true))
    55  		require.NotNil(t, err)
    56  		require.NotNil(t, got)
    57  		assert.Contains(t, err.Error(), "slice has non-integer element")
    58  	})
    59  
    60  	t.Run("valid", func(t *testing.T) {
    61  		slice := []int{1, 2, 3}
    62  		got, err := Validate(context.Background(),
    63  			AllElementsGreaterThanZero("testVar", slice, true))
    64  		require.Nil(t, err)
    65  		assert.Equal(t, []int{1, 2, 3}, got.Data.MustGet("testVar"))
    66  	})
    67  
    68  	t.Run("invalid", func(t *testing.T) {
    69  		slice := []uint8{1, 2, 3, 0}
    70  		got, err := Validate(context.Background(),
    71  			AllElementsGreaterThanZero("testVar", slice, true))
    72  		require.NotNil(t, err)
    73  		require.NotNil(t, got)
    74  		assert.Contains(t, err.Error(), "slice element 0 <= 0")
    75  	})
    76  }
    77  
    78  func TestAllElementsNotZero(t *testing.T) {
    79  	t.Run("valid", func(t *testing.T) {
    80  		slice := []int32{1, 2}
    81  		_, err := Validate(context.Background(),
    82  			AllElementsNotZero("testVar", slice))
    83  		require.Nil(t, err)
    84  	})
    85  
    86  	t.Run("invalid", func(t *testing.T) {
    87  		slice := []string{"abc", ""}
    88  		_, err := Validate(context.Background(),
    89  			AllElementsNotZero("testVar", slice))
    90  		require.NotNil(t, err)
    91  		assert.Contains(t, err.Error(), "slice has zero element")
    92  	})
    93  }
    94  
    95  func TestLessThanOrEqual(t *testing.T) {
    96  	var vdErr *ValidationError
    97  
    98  	_, err := Validate(context.Background(),
    99  		LessThanOrEqual("testVar", 20, 20))
   100  	require.Nil(t, err)
   101  
   102  	_, err = Validate(context.Background(),
   103  		LessThanOrEqual("testVar", 20, 25))
   104  	require.NotNil(t, err)
   105  	assert.True(t, errors.As(err, &vdErr))
   106  	assert.Contains(t, err.Error(), "testVar: value 25 > 20")
   107  }
   108  
   109  func TestInRange(t *testing.T) {
   110  	got1, err := Validate(context.Background(),
   111  		InRange("count", 1, 20, 15))
   112  	require.Nil(t, err)
   113  	_ = got1
   114  
   115  	got2, err := Validate(context.Background(),
   116  		InRange("count", 1, 20, 100))
   117  	require.NotNil(t, err)
   118  	assert.Contains(t, err.Error(), "count: value 100 is not in range [1, 20]")
   119  	_ = got2
   120  }
   121  
   122  func TestInRangeMode(t *testing.T) {
   123  	testData := []struct {
   124  		Name     string
   125  		Mode     RangeMode
   126  		Min      int
   127  		Max      int
   128  		Value    int
   129  		ErrIsNil bool
   130  		ErrMsg   string
   131  	}{
   132  		{"testVar", GtAndLte, 1, 20, 15, true, ""},
   133  		{"testVar", GtAndLte, 1, 20, 1, false, "testVar: value 1 is not in range (1, 20]"},
   134  		{"testVar", GtAndLte, 1, 20, 20, true, ""},
   135  
   136  		{"testVar", GtAndLt, 1, 20, 15, true, ""},
   137  		{"testVar", GtAndLt, 1, 20, 1, false, "testVar: value 1 is not in range (1, 20)"},
   138  		{"testVar", GtAndLt, 1, 20, 20, false, "testVar: value 20 is not in range (1, 20)"},
   139  
   140  		{"testVar", GteAndLte, 1, 20, 15, true, ""},
   141  		{"testVar", GteAndLte, 1, 20, 1, true, ""},
   142  		{"testVar", GteAndLte, 1, 20, 20, true, ""},
   143  
   144  		{"testVar", GteAndLt, 1, 20, 15, true, ""},
   145  		{"testVar", GteAndLt, 1, 20, 1, true, ""},
   146  		{"testVar", GteAndLt, 1, 20, 20, false, "testVar: value 20 is not in range [1, 20)"},
   147  	}
   148  
   149  	var vdErr *ValidationError
   150  	for _, c := range testData {
   151  		_, err := Validate(context.Background(),
   152  			InRangeMode(c.Name, c.Mode, c.Min, c.Max, c.Value))
   153  		if c.ErrIsNil {
   154  			assert.Nil(t, err)
   155  		} else {
   156  			assert.NotNil(t, err)
   157  			assert.True(t, errors.As(err, &vdErr))
   158  			assert.Contains(t, err.Error(), c.ErrMsg)
   159  		}
   160  	}
   161  }
   162  
   163  func TestParseStringSliceToInt64Slice(t *testing.T) {
   164  	got1, err := Validate(context.Background(),
   165  		ParseStrsToInt64Slice("entityIDs", []string{"1", "2", "3"}))
   166  	assert.Nil(t, err)
   167  	assert.Equal(t, []int64{1, 2, 3}, got1.Data.GetInt64s("entityIDs"))
   168  }
   169  
   170  func TestParseStringSliceToInt64Map(t *testing.T) {
   171  	got1, err := Validate(context.Background(),
   172  		ParseStrsToInt64Map("entityIDs", []string{"1", "2", "3"}))
   173  	assert.Nil(t, err)
   174  	assert.Equal(t, map[int64]bool{1: true, 2: true, 3: true}, got1.Data.MustGet("entityIDs").(map[int64]bool))
   175  }
   176  
   177  func TestNotNil(t *testing.T) {
   178  	notNilValues := []any{
   179  		1,
   180  		map[int]int{},
   181  		[]int{},
   182  		&Result{},
   183  		GreaterThanZero("", 1234, false),
   184  		Rule(GreaterThanZero("", 1234, false)),
   185  	}
   186  	for _, x := range notNilValues {
   187  		_, err := Validate(context.Background(), NotNil("testVar", x))
   188  		assert.Nil(t, err)
   189  	}
   190  
   191  	var vdErr *ValidationError
   192  	nilValues := []any{
   193  		nil,
   194  		(*int)(nil),
   195  		(map[int]int)(nil),
   196  		([]int)(nil),
   197  		(*Result)(nil),
   198  		RuleFunc(nil),
   199  		Rule(nil),
   200  	}
   201  	for _, x := range nilValues {
   202  		_, err := Validate(context.Background(), NotNil("testVar", x))
   203  		assert.NotNil(t, err)
   204  		assert.True(t, errors.As(err, &vdErr))
   205  		assert.Contains(t, err.Error(), "value is nil")
   206  	}
   207  }