...

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

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

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