github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/minver/minver_test.go (about)

     1  // Copyright 2024 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package minver
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/utils/structwalk"
    23  )
    24  
    25  type SubStruct struct {
    26  	SubStructPtrStringNoTag  *string `yaml:"sub_string_no_tag,omitempty"`
    27  	SubStructPtrStringTagGtr *string `yaml:"sub_string_tag_gt,omitempty" minver:"0.0.3"`
    28  	SubStructPtrStringTagEq  *string `yaml:"sub_string_tag_eq,omitempty" minver:"0.0.2"`
    29  	SubStructPtrStringTagLt  *string `yaml:"sub_string_tag_lt,omitempty" minver:"0.0.1"`
    30  	SubStructPtrStringTagTBD *string `yaml:"sub_string_tag_tbd,omitempty" minver:"TBD"`
    31  }
    32  
    33  type MinVerTestStruct struct {
    34  	StringPtrWithTag    *string `yaml:"string_ptr_no_tag,omitempty"`
    35  	StringPtrWithTagGtr *string `yaml:"string_ptr_tag_gt,omitempty" minver:"0.0.3"`
    36  	StringPtrWithTagEq  *string `yaml:"string_ptr_tag_eq,omitempty" minver:"0.0.2"`
    37  	StringPtrWithTagLt  *string `yaml:"string_ptr_tag_lt,omitempty" minver:"0.0.1"`
    38  	StringPtrWithTagTBD *string `yaml:"string_ptr_tag_lt,omitempty" minver:"TBD"`
    39  
    40  	SSPtrNoTag  *SubStruct `yaml:"sub_struct_ptr_no_tag"`
    41  	SSPtrTagGtr *SubStruct `yaml:"sub_struct_ptr_tag_gt,omitempty" minver:"0.0.3"`
    42  	SSPtrTagEq  *SubStruct `yaml:"sub_struct_ptr_tag_eq,omitempty" minver:"0.0.2"`
    43  	SSPtrTagLt  *SubStruct `yaml:"sub_struct_ptr_tag_lt,omitempty" minver:"0.0.1"`
    44  	SSPtrTagTBD *SubStruct `yaml:"sub_struct_ptr_tag_lt,omitempty" minver:"TBD"`
    45  
    46  	SlSSNoTag  []SubStruct `yaml:"sub_struct_slice_no_tag"`
    47  	SlSSTagGtr []SubStruct `yaml:"sub_struct_slice_tag_gt,omitempty" minver:"0.0.3"`
    48  	SlSSTagEq  []SubStruct `yaml:"sub_struct_slice_tag_eq,omitempty" minver:"0.0.2"`
    49  	SlSSTagLt  []SubStruct `yaml:"sub_struct_slice_tag_lt,omitempty" minver:"0.0.1"`
    50  	SlSSTagTBD []SubStruct `yaml:"sub_struct_slice_tag_lt,omitempty" minver:"TBD"`
    51  
    52  	SlSSPtrNoTag  []*SubStruct `yaml:"sub_struct_ptr_slice_no_tag"`
    53  	SlSSPtrTagGtr []*SubStruct `yaml:"sub_struct_ptr_slice_tag_gt,omitempty" minver:"0.0.3"`
    54  	SlSSPtrTagEq  []*SubStruct `yaml:"sub_struct_ptr_slice_tag_eq,omitempty" minver:"0.0.2"`
    55  	SlSSPtrTagLt  []*SubStruct `yaml:"sub_struct_ptr_slice_tag_lt,omitempty" minver:"0.0.1"`
    56  	SlSSPtrTagTBD []*SubStruct `yaml:"sub_struct_ptr_slice_tag_lt,omitempty" minver:"TBD"`
    57  }
    58  
    59  func ptr[T any](t T) *T {
    60  	return &t
    61  }
    62  
    63  func newSubSt() SubStruct {
    64  	return SubStruct{
    65  		SubStructPtrStringNoTag:  ptr("sub_string_no_tag"),
    66  		SubStructPtrStringTagGtr: ptr("sub_string_tag_gt"),
    67  		SubStructPtrStringTagEq:  ptr("sub_string_tag_eq"),
    68  		SubStructPtrStringTagLt:  ptr("sub_string_tag_lt"),
    69  		SubStructPtrStringTagTBD: ptr("sub_string_tag_tbd"),
    70  	}
    71  }
    72  
    73  func requireNullGtAndTBDFields(t *testing.T, st *SubStruct) {
    74  	require.NotNil(t, st.SubStructPtrStringNoTag)
    75  	require.NotNil(t, st.SubStructPtrStringTagLt)
    76  	require.NotNil(t, st.SubStructPtrStringTagEq)
    77  	require.Nil(t, st.SubStructPtrStringTagGtr)
    78  	require.Nil(t, st.SubStructPtrStringTagTBD)
    79  }
    80  
    81  func TestNullUnsupportedFields(t *testing.T) {
    82  	st := MinVerTestStruct{
    83  		StringPtrWithTag:    ptr("string_ptr_no_tag"),
    84  		StringPtrWithTagGtr: ptr("string_ptr_tag_gt"),
    85  		StringPtrWithTagEq:  ptr("string_ptr_tag_eq"),
    86  		StringPtrWithTagLt:  ptr("string_ptr_tag_lt"),
    87  		StringPtrWithTagTBD: ptr("string_ptr_tag_tbd"),
    88  
    89  		SSPtrNoTag:  ptr(newSubSt()),
    90  		SSPtrTagGtr: ptr(newSubSt()),
    91  		SSPtrTagEq:  ptr(newSubSt()),
    92  		SSPtrTagLt:  ptr(newSubSt()),
    93  		SSPtrTagTBD: ptr(newSubSt()),
    94  
    95  		SlSSNoTag:  []SubStruct{newSubSt(), newSubSt()},
    96  		SlSSTagGtr: []SubStruct{newSubSt(), newSubSt()},
    97  		SlSSTagEq:  []SubStruct{newSubSt(), newSubSt()},
    98  		SlSSTagLt:  []SubStruct{newSubSt(), newSubSt()},
    99  		SlSSTagTBD: []SubStruct{newSubSt(), newSubSt()},
   100  
   101  		SlSSPtrNoTag:  []*SubStruct{ptr(newSubSt()), ptr(newSubSt())},
   102  		SlSSPtrTagGtr: []*SubStruct{ptr(newSubSt()), ptr(newSubSt())},
   103  		SlSSPtrTagEq:  []*SubStruct{ptr(newSubSt()), ptr(newSubSt())},
   104  		SlSSPtrTagLt:  []*SubStruct{ptr(newSubSt()), ptr(newSubSt())},
   105  		SlSSPtrTagTBD: []*SubStruct{ptr(newSubSt()), ptr(newSubSt())},
   106  	}
   107  
   108  	err := NullUnsupported(2, &st)
   109  	require.NoError(t, err)
   110  
   111  	require.Equal(t, *st.StringPtrWithTag, "string_ptr_no_tag")
   112  	require.Equal(t, *st.StringPtrWithTagLt, "string_ptr_tag_lt")
   113  	require.Equal(t, *st.StringPtrWithTagEq, "string_ptr_tag_eq")
   114  
   115  	require.Nil(t, st.StringPtrWithTagGtr)
   116  	require.Nil(t, st.SSPtrTagGtr)
   117  	require.Nil(t, st.SlSSTagGtr)
   118  	require.Nil(t, st.SlSSPtrTagGtr)
   119  	require.Nil(t, st.SlSSPtrTagTBD)
   120  
   121  	requireNullGtAndTBDFields(t, st.SSPtrNoTag)
   122  	requireNullGtAndTBDFields(t, st.SSPtrTagLt)
   123  	requireNullGtAndTBDFields(t, st.SSPtrTagEq)
   124  
   125  	requireNullGtAndTBDFields(t, &st.SlSSNoTag[0])
   126  	requireNullGtAndTBDFields(t, &st.SlSSNoTag[1])
   127  	requireNullGtAndTBDFields(t, &st.SlSSTagLt[0])
   128  	requireNullGtAndTBDFields(t, &st.SlSSTagLt[1])
   129  	requireNullGtAndTBDFields(t, &st.SlSSTagEq[0])
   130  	requireNullGtAndTBDFields(t, &st.SlSSTagEq[1])
   131  
   132  	requireNullGtAndTBDFields(t, st.SlSSPtrNoTag[0])
   133  	requireNullGtAndTBDFields(t, st.SlSSPtrNoTag[1])
   134  	requireNullGtAndTBDFields(t, st.SlSSPtrTagLt[0])
   135  	requireNullGtAndTBDFields(t, st.SlSSPtrTagLt[1])
   136  	requireNullGtAndTBDFields(t, st.SlSSPtrTagEq[0])
   137  	requireNullGtAndTBDFields(t, st.SlSSPtrTagEq[1])
   138  }
   139  
   140  func TestMinVer(t *testing.T) {
   141  	// validates the test function is doing what's expected
   142  	type notNullableWithMinVer struct {
   143  		notNullable string `minver:"1.0.0"`
   144  	}
   145  
   146  	err := structwalk.Walk(&notNullableWithMinVer{}, ValidateMinVerFunc)
   147  	require.Error(t, err)
   148  
   149  	type nullableWithoutOmitEmpty struct {
   150  		nullable *string `minver:"1.0.0" yaml:"nullable"`
   151  	}
   152  
   153  	err = structwalk.Walk(&nullableWithoutOmitEmpty{}, ValidateMinVerFunc)
   154  	require.Error(t, err)
   155  
   156  	type nullableWithOmitEmpty struct {
   157  		nullable *string `minver:"1.0.0" yaml:"nullable,omitempty"`
   158  	}
   159  
   160  	err = structwalk.Walk(&nullableWithOmitEmpty{}, ValidateMinVerFunc)
   161  	require.NoError(t, err)
   162  }