github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/featureflag/feature_flags_sync_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package featureflag
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/masterhung0112/hk_server/v5/model"
    12  )
    13  
    14  func TestGetStructFields(t *testing.T) {
    15  	type testStruct struct {
    16  		FieldOne       string
    17  		SecondField    bool
    18  		SomeOtherField int
    19  	}
    20  
    21  	fields := getStructFields(testStruct{})
    22  	require.Equal(t,
    23  		[]string{
    24  			"FieldOne",
    25  			"SecondField",
    26  			"SomeOtherField",
    27  		},
    28  		fields,
    29  	)
    30  
    31  	featureFlagsFields := getStructFields(model.FeatureFlags{})
    32  	require.Contains(t, featureFlagsFields, "TestFeature")
    33  }
    34  
    35  func TestFeatureFlagsFromMap(t *testing.T) {
    36  	for name, tc := range map[string]struct {
    37  		FeatureMap        map[string]string
    38  		Base              model.FeatureFlags
    39  		ExpectedTestValue string
    40  	}{
    41  		"empty": {
    42  			FeatureMap:        map[string]string{},
    43  			Base:              model.FeatureFlags{},
    44  			ExpectedTestValue: "",
    45  		},
    46  		"no base value": {
    47  			FeatureMap:        map[string]string{"TestFeature": "expectedvalue"},
    48  			Base:              model.FeatureFlags{},
    49  			ExpectedTestValue: "expectedvalue",
    50  		},
    51  		"only base value": {
    52  			FeatureMap:        map[string]string{},
    53  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    54  			ExpectedTestValue: "somebasevalue",
    55  		},
    56  		"override base value": {
    57  			FeatureMap:        map[string]string{"TestFeature": "overridevalue"},
    58  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    59  			ExpectedTestValue: "overridevalue",
    60  		},
    61  		"override base value with extras": {
    62  			FeatureMap:        map[string]string{"TestFeature": "overridevalue", "SomeOldFlag": "oldvalue"},
    63  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    64  			ExpectedTestValue: "overridevalue",
    65  		},
    66  		"all values do not exist": {
    67  			FeatureMap:        map[string]string{"SomeOldFlag": "oldvalue"},
    68  			Base:              model.FeatureFlags{},
    69  			ExpectedTestValue: "",
    70  		},
    71  		"bool on": {
    72  			FeatureMap:        map[string]string{"TestBoolFeature": "on"},
    73  			Base:              model.FeatureFlags{TestBoolFeature: true},
    74  			ExpectedTestValue: "",
    75  		},
    76  		"bool true": {
    77  			FeatureMap:        map[string]string{"TestBoolFeature": "true"},
    78  			Base:              model.FeatureFlags{TestBoolFeature: true},
    79  			ExpectedTestValue: "",
    80  		},
    81  		"bool True": {
    82  			FeatureMap:        map[string]string{"TestBoolFeature": "True"},
    83  			Base:              model.FeatureFlags{TestBoolFeature: true},
    84  			ExpectedTestValue: "",
    85  		},
    86  		"bool 1": {
    87  			FeatureMap:        map[string]string{"TestBoolFeature": "1"},
    88  			Base:              model.FeatureFlags{TestBoolFeature: true},
    89  			ExpectedTestValue: "",
    90  		},
    91  		"bool off": {
    92  			FeatureMap:        map[string]string{"TestBoolFeature": "off"},
    93  			Base:              model.FeatureFlags{},
    94  			ExpectedTestValue: "",
    95  		},
    96  		"bool false": {
    97  			FeatureMap:        map[string]string{"TestBoolFeature": "false"},
    98  			Base:              model.FeatureFlags{},
    99  			ExpectedTestValue: "",
   100  		},
   101  		"bool other value": {
   102  			FeatureMap:        map[string]string{"TestBoolFeature": "someotherbadvalue"},
   103  			Base:              model.FeatureFlags{},
   104  			ExpectedTestValue: "",
   105  		},
   106  	} {
   107  		t.Run(name, func(t *testing.T) {
   108  			require.Equal(t, tc.ExpectedTestValue, featureFlagsFromMap(tc.FeatureMap, tc.Base).TestFeature)
   109  		})
   110  	}
   111  }