github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/updater/resultstore/query/query_test.go (about)

     1  /*
     2  Copyright 2024 The TestGrid 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 query
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestTranslateAtom(t *testing.T) {
    24  	cases := []struct {
    25  		name      string
    26  		atom      string
    27  		want      string
    28  		wantError bool
    29  	}{
    30  		{
    31  			name: "empty",
    32  			atom: "",
    33  			want: "",
    34  		},
    35  		{
    36  			name: "basic",
    37  			atom: `target:"//my-target"`,
    38  			want: `id.target_id="//my-target"`,
    39  		},
    40  		{
    41  			name:      "case-sensitive key",
    42  			atom:      `TARGET:"//MY-TARGET"`,
    43  			wantError: true,
    44  		},
    45  		{
    46  			name: "multiple colons",
    47  			atom: `target:"//path/to:my-target"`,
    48  			want: `id.target_id="//path/to:my-target"`,
    49  		},
    50  		{
    51  			name: "unquoted",
    52  			atom: `target://my-target`,
    53  			want: `id.target_id="//my-target"`,
    54  		},
    55  		{
    56  			name: "partial quotes",
    57  			atom: `target://my-target"`,
    58  			want: `id.target_id="//my-target"`,
    59  		},
    60  		{
    61  			name:      "not enough parts",
    62  			atom:      "target",
    63  			wantError: true,
    64  		},
    65  		{
    66  			name:      "unknown atom",
    67  			atom:      "label:foo",
    68  			wantError: true,
    69  		},
    70  	}
    71  
    72  	for _, tc := range cases {
    73  		t.Run(tc.name, func(t *testing.T) {
    74  			got, err := translateAtom(tc.atom)
    75  			if tc.want != got {
    76  				t.Errorf("translateAtom(%q) differed; got %q, want %q", tc.atom, got, tc.want)
    77  			}
    78  			if err == nil && tc.wantError {
    79  				t.Errorf("translateAtom(%q) did not error as expected", tc.atom)
    80  			} else if err != nil && !tc.wantError {
    81  				t.Errorf("translateAtom(%q) errored unexpectedly: %v", tc.atom, err)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestTranslateQuery(t *testing.T) {
    88  	cases := []struct {
    89  		name      string
    90  		query     string
    91  		want      string
    92  		wantError bool
    93  	}{
    94  		{
    95  			name:  "empty",
    96  			query: "",
    97  			want:  "",
    98  		},
    99  		{
   100  			name:  "basic",
   101  			query: `target:"//my-target"`,
   102  			want:  `id.target_id="//my-target"`,
   103  		},
   104  		{
   105  			name:      "case-sensitive key",
   106  			query:     `TARGET:"//MY-TARGET"`,
   107  			wantError: true,
   108  		},
   109  		{
   110  			name:  "multiple colons",
   111  			query: `target:"//path/to:my-target"`,
   112  			want:  `id.target_id="//path/to:my-target"`,
   113  		},
   114  		{
   115  			name:      "unquoted",
   116  			query:     `target://my-target`,
   117  			wantError: true,
   118  		},
   119  		{
   120  			name:      "partial quotes",
   121  			query:     `target://my-target"`,
   122  			wantError: true,
   123  		},
   124  		{
   125  			name:      "invalid query",
   126  			query:     `label:foo`,
   127  			wantError: true,
   128  		},
   129  		{
   130  			name:      "partial match",
   131  			query:     `some_target:foo`,
   132  			wantError: true,
   133  		},
   134  	}
   135  
   136  	for _, tc := range cases {
   137  		t.Run(tc.name, func(t *testing.T) {
   138  			got, err := TranslateQuery(tc.query)
   139  			if tc.want != got {
   140  				t.Errorf("translateQuery(%q) differed; got %q, want %q", tc.query, got, tc.want)
   141  			}
   142  			if tc.wantError && err == nil {
   143  				t.Errorf("translateQuery(%q) did not error as expected", tc.query)
   144  			} else if !tc.wantError && err != nil {
   145  				t.Errorf("translateQuery(%q) errored unexpectedly: %v", tc.query, err)
   146  			}
   147  		})
   148  	}
   149  }