github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/devicetwin/dtcommon/util_test.go (about)

     1  /*
     2  Copyright 2018 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package dtcommon
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  )
    23  
    24  // TestValidateValue is function to test ValidateValue
    25  func TestValidateValue(t *testing.T) {
    26  	cases := []struct {
    27  		// name is name of the testcase
    28  		name string
    29  		// valueType is value type of testcase, first parameter to ValidateValue function
    30  		valueType string
    31  		// value is value in the test case, second parameter to ValidateValue function
    32  		value string
    33  		// wantErr is expected error in the test case, returned by ValidateValue function
    34  		wantErr error
    35  	}{{
    36  		// valuetype nil success
    37  		name:      "ValidateValueNilSuccessCase",
    38  		valueType: "",
    39  		value:     "test",
    40  		wantErr:   nil,
    41  	}, {
    42  		// valuetype nil success
    43  		name:      "ValidateValueStringSuccessCase",
    44  		valueType: "string",
    45  		value:     "test",
    46  		wantErr:   nil,
    47  	}, {
    48  		// int error
    49  		name:      "ValidateValueIntErrorCase",
    50  		valueType: "int",
    51  		value:     "test",
    52  		wantErr:   errors.New("the value is not int"),
    53  	}, {
    54  		// float error
    55  		name:      "ValidateValueFloatErrorCase",
    56  		valueType: "float",
    57  		value:     "test",
    58  		wantErr:   errors.New("the value is not float"),
    59  	}, {
    60  		// bool error
    61  		name:      "ValidateValueBoolErrorCase",
    62  		valueType: "boolean",
    63  		value:     "test",
    64  		wantErr:   errors.New("the bool value must be true or false"),
    65  	}, {
    66  		// deleted
    67  		name:      "ValidateValueDeletedSuccessCase",
    68  		valueType: "deleted",
    69  		value:     "test",
    70  		wantErr:   nil,
    71  	}, {
    72  		// not supported
    73  		name:      "ValidateValueNotSupportedErrorCase",
    74  		valueType: "test",
    75  		value:     "test",
    76  		wantErr:   errors.New("the value type is not allowed"),
    77  	}, {
    78  		// int success
    79  		name:      "ValidateValueIntSuccessCase",
    80  		valueType: "int",
    81  		value:     "10",
    82  		wantErr:   nil,
    83  	}, {
    84  		// float success
    85  		name:      "ValidateValueFloatSuccessCase",
    86  		valueType: "float",
    87  		value:     "10.10",
    88  		wantErr:   nil,
    89  	}, {
    90  		// bool success true
    91  		name:      "ValidateValueBoolTrueSuccessCase",
    92  		valueType: "boolean",
    93  		value:     "true",
    94  		wantErr:   nil,
    95  	}, {
    96  		// bool success false
    97  		name:      "ValidateValueBoolFalseSuccessCase",
    98  		valueType: "boolean",
    99  		value:     "false",
   100  		wantErr:   nil,
   101  	},
   102  	}
   103  
   104  	// run the test cases
   105  	for _, test := range cases {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			err := ValidateValue(test.valueType, test.value)
   108  			if (err == nil && err != test.wantErr) || (err != nil && err.Error() != test.wantErr.Error()) {
   109  				t.Errorf("TestValidateValue Case failed: wanted %v and got %v", test.wantErr, err)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  // TestValidateTwinKey is function to test ValidateTwinKey
   116  func TestValidateTwinKey(t *testing.T) {
   117  	cases := []struct {
   118  		// name is name of the testcase
   119  		name string
   120  		// key is key to be validated, parameter to ValidateTwinKey function
   121  		key string
   122  		// want is expected boolean in test case, returned by ValidateTwinKey function
   123  		want bool
   124  	}{{
   125  		// Failure case
   126  		name: "ValidateTwinKeyFailCase",
   127  		key:  "test^",
   128  		want: false,
   129  	}, {
   130  		// Success case
   131  		name: "ValidateTwinKeySuccessCase",
   132  		key:  "test123",
   133  		want: true,
   134  	},
   135  	}
   136  
   137  	// run the test cases
   138  	for _, test := range cases {
   139  		t.Run(test.name, func(t *testing.T) {
   140  			bool := ValidateTwinKey(test.key)
   141  			if test.want != bool {
   142  				t.Errorf("ValidateTwinKey Case failed: wanted %v and got %v", test.want, bool)
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  // TestValidateTwinValue is function to test ValidateTwinValue
   149  func TestValidateTwinValue(t *testing.T) {
   150  	cases := []struct {
   151  		// name is name of the testcase
   152  		name string
   153  		// key is key to be validated, parameter to ValidateTwinKey function
   154  		key string
   155  		// want is expected boolean in test case, returned by ValidateTwinKey function
   156  		want bool
   157  	}{{
   158  		// Failure case
   159  		name: "ValidateTwinValueFailCase",
   160  		key:  "test^",
   161  		want: false,
   162  	}, {
   163  		// Success case
   164  		name: "ValidateTwinValueSuccessCase",
   165  		key:  "test123",
   166  		want: true,
   167  	},
   168  	}
   169  
   170  	// run the test cases
   171  	for _, test := range cases {
   172  		t.Run(test.name, func(t *testing.T) {
   173  			bool := ValidateTwinValue(test.key)
   174  			if test.want != bool {
   175  				t.Errorf("ValidateTwinValue Case failed: wanted %v and got %v", test.want, bool)
   176  			}
   177  		})
   178  	}
   179  }