github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/identity/identity_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package identity
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func Test_Identity_From(t *testing.T) {
    28  	testCases := []struct {
    29  		name    string
    30  		input   []byte
    31  		wantErr bool
    32  		want    []byte
    33  	}{
    34  		{
    35  			name:    "Nil input",
    36  			input:   nil,
    37  			wantErr: false,
    38  			want:    nil,
    39  		},
    40  		{
    41  			name:    "Empty input",
    42  			input:   []byte{},
    43  			wantErr: false,
    44  			want:    []byte{},
    45  		},
    46  		{
    47  			name:    "Something",
    48  			input:   []byte("foo"),
    49  			wantErr: false,
    50  			want:    []byte("foo"),
    51  		},
    52  	}
    53  	for _, tC := range testCases {
    54  		testCase := tC
    55  		t.Run(testCase.name, func(t *testing.T) {
    56  			t.Parallel()
    57  
    58  			ctx := context.Background()
    59  
    60  			underTest := Transformer()
    61  
    62  			// Do the call
    63  			got, err := underTest.From(ctx, testCase.input)
    64  
    65  			// Assert results expectations
    66  			if (err != nil) != testCase.wantErr {
    67  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
    68  				return
    69  			}
    70  			if testCase.wantErr {
    71  				return
    72  			}
    73  			if diff := cmp.Diff(got, testCase.input); diff != "" {
    74  				t.Errorf("%q. Identity.From():\n-got/+want\ndiff %s", testCase.name, diff)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func Test_Identity_To(t *testing.T) {
    81  	testCases := []struct {
    82  		name    string
    83  		input   []byte
    84  		wantErr bool
    85  		want    []byte
    86  	}{
    87  		{
    88  			name:    "Nil input",
    89  			input:   nil,
    90  			wantErr: false,
    91  			want:    nil,
    92  		},
    93  		{
    94  			name:    "Empty input",
    95  			input:   []byte{},
    96  			wantErr: false,
    97  			want:    []byte{},
    98  		},
    99  		{
   100  			name:    "Something",
   101  			input:   []byte("foo"),
   102  			wantErr: false,
   103  			want:    []byte("foo"),
   104  		},
   105  	}
   106  	for _, tC := range testCases {
   107  		testCase := tC
   108  		t.Run(testCase.name, func(t *testing.T) {
   109  			t.Parallel()
   110  
   111  			ctx := context.Background()
   112  
   113  			underTest := Transformer()
   114  
   115  			// Do the call
   116  			got, err := underTest.To(ctx, testCase.input)
   117  
   118  			// Assert results expectations
   119  			if (err != nil) != testCase.wantErr {
   120  				t.Errorf("error during the call, error = %v, wantErr %v", err, testCase.wantErr)
   121  				return
   122  			}
   123  			if testCase.wantErr {
   124  				return
   125  			}
   126  			if diff := cmp.Diff(got, testCase.input); diff != "" {
   127  				t.Errorf("%q. Identity.To():\n-got/+want\ndiff %s", testCase.name, diff)
   128  			}
   129  		})
   130  	}
   131  }