github.com/oam-dev/kubevela@v1.9.11/pkg/utils/schema/ui_schema_test.go (about)

     1  /*
     2  Copyright 2022 The KubeVela 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 schema
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("Test ui schema utils", func() {
    27  	It("Test GetDefaultUIType function", func() {
    28  		testCase := []map[string]interface{}{{
    29  			"apiType":     "string",
    30  			"haveOptions": true,
    31  			"subType":     "",
    32  			"haveSub":     true,
    33  			"result":      "Select",
    34  		}, {
    35  			"apiType":     "string",
    36  			"haveOptions": false,
    37  			"subType":     "",
    38  			"haveSub":     true,
    39  			"result":      "Input",
    40  		}, {
    41  			"apiType":     "number",
    42  			"haveOptions": false,
    43  			"subType":     "",
    44  			"haveSub":     true,
    45  			"result":      "Number",
    46  		}, {
    47  			"apiType":     "integer",
    48  			"haveOptions": false,
    49  			"subType":     "",
    50  			"haveSub":     true,
    51  			"result":      "Number",
    52  		}, {
    53  			"apiType":     "boolean",
    54  			"haveOptions": false,
    55  			"subType":     "",
    56  			"haveSub":     true,
    57  			"result":      "Switch",
    58  		},
    59  			{
    60  				"apiType":     "array",
    61  				"haveOptions": false,
    62  				"subType":     "string",
    63  				"haveSub":     true,
    64  				"result":      "Strings",
    65  			},
    66  			{
    67  				"apiType":     "array",
    68  				"haveOptions": false,
    69  				"subType":     "",
    70  				"haveSub":     true,
    71  				"result":      "Structs",
    72  			},
    73  			{
    74  				"apiType":     "object",
    75  				"haveOptions": false,
    76  				"subType":     "",
    77  				"haveSub":     true,
    78  				"result":      "Group",
    79  			},
    80  			{
    81  				"apiType":     "object",
    82  				"haveOptions": false,
    83  				"subType":     "",
    84  				"haveSub":     false,
    85  				"result":      "KV",
    86  			},
    87  		}
    88  		for _, tc := range testCase {
    89  			uiType := GetDefaultUIType(tc["apiType"].(string), tc["haveOptions"].(bool), tc["subType"].(string), tc["haveSub"].(bool))
    90  			Expect(uiType).Should(Equal(tc["result"]))
    91  		}
    92  	})
    93  })
    94  
    95  func TestUISchema_Validate(t *testing.T) {
    96  	tests := []struct {
    97  		name    string
    98  		u       UISchema
    99  		wantErr bool
   100  	}{
   101  		{
   102  			name: "test validate ui schema error",
   103  			u: UISchema{
   104  				{
   105  					Conditions: []Condition{
   106  						{
   107  							JSONKey: "",
   108  						},
   109  					},
   110  				},
   111  			},
   112  			wantErr: true,
   113  		},
   114  		{
   115  			name: "test validate ui schema success",
   116  			u: UISchema{
   117  				{
   118  					Conditions: []Condition{
   119  						{
   120  							JSONKey: "key",
   121  							Op:      "==",
   122  							Action:  "enable",
   123  						},
   124  					},
   125  				},
   126  			},
   127  			wantErr: false,
   128  		},
   129  	}
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			if err := tt.u.Validate(); (err != nil) != tt.wantErr {
   133  				t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
   134  			}
   135  		})
   136  	}
   137  }