github.com/greenpau/go-authcrunch@v1.1.4/pkg/tagging/tag_test.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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  package tagging
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/greenpau/go-authcrunch/internal/tests"
    22  )
    23  
    24  func TestExtractTags(t *testing.T) {
    25  
    26  	testcases := []struct {
    27  		name      string
    28  		input     string
    29  		want      []*Tag
    30  		shouldErr bool
    31  		err       error
    32  		disabled  bool
    33  	}{
    34  		{
    35  			name:     "test extract tags with one tag",
    36  			disabled: false,
    37  			input: `{
    38  				"tags": [
    39  					{
    40  						"key": "foo",
    41  						"value": "bar"
    42  					}
    43  				]
    44  			}`,
    45  			want: []*Tag{
    46  				{
    47  					Key:   "foo",
    48  					Value: "bar",
    49  				},
    50  			},
    51  		},
    52  		{
    53  			name:     "test extract tags with multiple tags",
    54  			disabled: false,
    55  			input: `{
    56  				"tags": [
    57  					{
    58  						"key": "foo",
    59  						"value": "bar"
    60  					},
    61  					{
    62  						"key": "bar",
    63  						"value": "baz"
    64  					}
    65  				]
    66  			}`,
    67  			want: []*Tag{
    68  				{
    69  					Key:   "foo",
    70  					Value: "bar",
    71  				},
    72  				{
    73  					Key:   "bar",
    74  					Value: "baz",
    75  				},
    76  			},
    77  		},
    78  		{
    79  			name:     "test extract tags without any tags",
    80  			disabled: false,
    81  			input: `{
    82  				"tags": [
    83  				]
    84  			}`,
    85  			want: []*Tag{},
    86  		},
    87  		{
    88  			name:     "test map without tags field",
    89  			disabled: false,
    90  			input:    `{}`,
    91  			want:     []*Tag{},
    92  		},
    93  		{
    94  			name:     "test tag without key field",
    95  			disabled: false,
    96  			input: `{
    97  				"tags": [
    98  					{
    99  						"foo": "foo",
   100  						"value": "bar"
   101  					}
   102  				]
   103  			}`,
   104  			shouldErr: true,
   105  			err:       fmt.Errorf("malformed extracted tags: %s", "tag has no key"),
   106  		},
   107  		{
   108  			name:     "test tag without value field",
   109  			disabled: false,
   110  			input: `{
   111  				"tags": [
   112  					{
   113  						"key": "foo",
   114  						"foo": "bar"
   115  					}
   116  				]
   117  			}`,
   118  			shouldErr: true,
   119  			err:       fmt.Errorf("malformed extracted tags: %s", "tag has no value"),
   120  		},
   121  	}
   122  	for _, tc := range testcases {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			if tc.disabled {
   125  				return
   126  			}
   127  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
   128  			msgs = append(msgs, fmt.Sprintf("input:\n%v", tc.input))
   129  			input, err := tests.UnpackDict(tc.input)
   130  			if err != nil {
   131  				t.Fatalf("prereq failed: %v", err)
   132  			}
   133  			got, err := ExtractTags(input)
   134  			if tests.EvalErrWithLog(t, err, "ExtractTags", tc.shouldErr, tc.err, msgs) {
   135  				return
   136  			}
   137  			tests.EvalObjectsWithLog(t, "ExtractTags", tc.want, got, msgs)
   138  		})
   139  	}
   140  }
   141  
   142  func TestExtractLabels(t *testing.T) {
   143  
   144  	testcases := []struct {
   145  		name      string
   146  		input     string
   147  		want      []string
   148  		shouldErr bool
   149  		err       error
   150  		disabled  bool
   151  	}{
   152  		{
   153  			name:     "test extract labels with one label",
   154  			disabled: false,
   155  			input: `{
   156  				"labels": ["foo"]
   157  			}`,
   158  			want: []string{"foo"},
   159  		},
   160  		{
   161  			name:     "test extract labels with multiple labels",
   162  			disabled: false,
   163  			input: `{
   164  				"labels": ["foo", "bar"]
   165  			}`,
   166  			want: []string{"foo", "bar"},
   167  		},
   168  		{
   169  			name:     "test extract labels without any labels",
   170  			disabled: false,
   171  			input: `{
   172  				"labels": []
   173  			}`,
   174  			want: []string{},
   175  		},
   176  	}
   177  	for _, tc := range testcases {
   178  		t.Run(tc.name, func(t *testing.T) {
   179  			if tc.disabled {
   180  				return
   181  			}
   182  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
   183  			msgs = append(msgs, fmt.Sprintf("input:\n%v", tc.input))
   184  			input, err := tests.UnpackDict(tc.input)
   185  			if err != nil {
   186  				t.Fatalf("prereq failed: %v", err)
   187  			}
   188  			got, err := ExtractLabels(input)
   189  			if tests.EvalErrWithLog(t, err, "ExtractLabels", tc.shouldErr, tc.err, msgs) {
   190  				return
   191  			}
   192  			tests.EvalObjectsWithLog(t, "ExtractLabels", tc.want, got, msgs)
   193  		})
   194  	}
   195  }