...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/experiment/experiment_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/experiment

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package experiment
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestConstructQueryArgs(t *testing.T) {
    24  	cases := []struct {
    25  		kind          string
    26  		ns            string
    27  		name          string
    28  		uid           string
    29  		expectedQuery string
    30  		expectedArgs  []string
    31  	}{
    32  		{
    33  			kind:          "",
    34  			ns:            "",
    35  			name:          "",
    36  			uid:           "",
    37  			expectedQuery: "",
    38  			expectedArgs:  []string{},
    39  		},
    40  		{
    41  			kind:          "PodChaos",
    42  			ns:            "",
    43  			name:          "",
    44  			uid:           "",
    45  			expectedQuery: "kind = ?",
    46  			expectedArgs:  []string{"PodChaos"},
    47  		},
    48  		{
    49  			kind:          "",
    50  			ns:            "test-ns",
    51  			name:          "",
    52  			uid:           "",
    53  			expectedQuery: "namespace = ?",
    54  			expectedArgs:  []string{"test-ns"},
    55  		},
    56  		{
    57  			kind:          "",
    58  			ns:            "",
    59  			name:          "test-name",
    60  			uid:           "",
    61  			expectedQuery: "name = ?",
    62  			expectedArgs:  []string{"test-name"},
    63  		},
    64  		{
    65  			kind:          "PodChaos",
    66  			ns:            "test-ns",
    67  			name:          "",
    68  			uid:           "",
    69  			expectedQuery: "kind = ? AND namespace = ?",
    70  			expectedArgs:  []string{"PodChaos", "test-ns"},
    71  		},
    72  		{
    73  			kind:          "PodChaos",
    74  			ns:            "test-ns",
    75  			name:          "test-name",
    76  			uid:           "",
    77  			expectedQuery: "kind = ? AND namespace = ? AND name = ?",
    78  			expectedArgs:  []string{"PodChaos", "test-ns", "test-name"},
    79  		},
    80  		{
    81  			kind:          "",
    82  			ns:            "",
    83  			name:          "",
    84  			uid:           "test-uid",
    85  			expectedQuery: "uid = ?",
    86  			expectedArgs:  []string{"test-uid"},
    87  		},
    88  		{
    89  			kind:          "PodChaos",
    90  			ns:            "",
    91  			name:          "",
    92  			uid:           "test-uid",
    93  			expectedQuery: "kind = ? AND uid = ?",
    94  			expectedArgs:  []string{"PodChaos", "test-uid"},
    95  		},
    96  		{
    97  			kind:          "",
    98  			ns:            "test-ns",
    99  			name:          "",
   100  			uid:           "test-uid",
   101  			expectedQuery: "namespace = ? AND uid = ?",
   102  			expectedArgs:  []string{"test-ns", "test-uid"},
   103  		},
   104  		{
   105  			kind:          "",
   106  			ns:            "",
   107  			name:          "test-name",
   108  			uid:           "test-uid",
   109  			expectedQuery: "name = ? AND uid = ?",
   110  			expectedArgs:  []string{"test-name", "test-uid"},
   111  		},
   112  		{
   113  			kind:          "PodChaos",
   114  			ns:            "test-ns",
   115  			name:          "",
   116  			uid:           "test-uid",
   117  			expectedQuery: "kind = ? AND namespace = ? AND uid = ?",
   118  			expectedArgs:  []string{"PodChaos", "test-ns", "test-uid"},
   119  		},
   120  		{
   121  			kind:          "PodChaos",
   122  			ns:            "test-ns",
   123  			name:          "test-name",
   124  			uid:           "test-uid",
   125  			expectedQuery: "kind = ? AND namespace = ? AND name = ? AND uid = ?",
   126  			expectedArgs:  []string{"PodChaos", "test-ns", "test-name", "test-uid"},
   127  		},
   128  	}
   129  
   130  	for _, c := range cases {
   131  		query, args := constructQueryArgs(c.kind, c.ns, c.name, c.uid)
   132  		if query != c.expectedQuery {
   133  			t.Errorf("expected query %s but got %s", c.expectedQuery, query)
   134  		}
   135  		if !reflect.DeepEqual(c.expectedArgs, args) {
   136  			t.Errorf("expected args %v but got %v", c.expectedArgs, args)
   137  		}
   138  	}
   139  }
   140