github.com/crossplane/upjet@v1.3.0/pkg/pipeline/crd_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package pipeline
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
    12  )
    13  
    14  func TestDeleteOmittedFields(t *testing.T) {
    15  	type args struct {
    16  		sch           map[string]*schema.Schema
    17  		omittedFields []string
    18  	}
    19  	type want struct {
    20  		sch map[string]*schema.Schema
    21  	}
    22  
    23  	cases := map[string]struct {
    24  		reason string
    25  		args
    26  		want
    27  	}{
    28  		"No-op": {
    29  			reason: "Should not make any changes if fields are not found.",
    30  			args: args{
    31  				sch: map[string]*schema.Schema{
    32  					"top_level_a": {},
    33  					"top_level_b": {},
    34  				},
    35  				omittedFields: []string{
    36  					"top_level_c",
    37  				},
    38  			},
    39  			want: want{
    40  				sch: map[string]*schema.Schema{
    41  					"top_level_a": {},
    42  					"top_level_b": {},
    43  				},
    44  			},
    45  		},
    46  		"OmitTopLevelFields": {
    47  			reason: "Should be able to omit top level fields.",
    48  			args: args{
    49  				sch: map[string]*schema.Schema{
    50  					"top_level_a": {},
    51  					"top_level_b": {},
    52  				},
    53  				omittedFields: []string{
    54  					"top_level_a",
    55  				},
    56  			},
    57  			want: want{
    58  				sch: map[string]*schema.Schema{
    59  					"top_level_b": {},
    60  				},
    61  			},
    62  		},
    63  		"OmitLeafNode": {
    64  			reason: "Should be able to omit a leaf field.",
    65  			args: args{
    66  				sch: map[string]*schema.Schema{
    67  					"top_level_a": {
    68  						Elem: &schema.Resource{
    69  							Schema: map[string]*schema.Schema{
    70  								"down_one": {},
    71  								"down_two": {},
    72  							},
    73  						},
    74  					},
    75  					"top_level_b": {
    76  						Elem: &schema.Resource{
    77  							Schema: map[string]*schema.Schema{
    78  								"down_another": {},
    79  							},
    80  						},
    81  					},
    82  				},
    83  				omittedFields: []string{
    84  					"top_level_a.down_one",
    85  				},
    86  			},
    87  			want: want{
    88  				sch: map[string]*schema.Schema{
    89  					"top_level_a": {
    90  						Elem: &schema.Resource{
    91  							Schema: map[string]*schema.Schema{
    92  								"down_two": {},
    93  							},
    94  						},
    95  					},
    96  					"top_level_b": {
    97  						Elem: &schema.Resource{
    98  							Schema: map[string]*schema.Schema{
    99  								"down_another": {},
   100  							},
   101  						},
   102  					},
   103  				},
   104  			},
   105  		},
   106  		"OmitLeafNodeMultiple": {
   107  			reason: "Should be able to omit multiple leaf fields.",
   108  			args: args{
   109  				sch: map[string]*schema.Schema{
   110  					"top_level_a": {
   111  						Elem: &schema.Resource{
   112  							Schema: map[string]*schema.Schema{
   113  								"down_one":        {},
   114  								"down_one_prefix": {},
   115  								"down_two":        {},
   116  							},
   117  						},
   118  					},
   119  					"top_level_b": {
   120  						Elem: &schema.Resource{
   121  							Schema: map[string]*schema.Schema{
   122  								"down_another": {},
   123  							},
   124  						},
   125  					},
   126  				},
   127  				omittedFields: []string{
   128  					"top_level_a.down_one",
   129  					"top_level_a.down_one_prefix",
   130  				},
   131  			},
   132  			want: want{
   133  				sch: map[string]*schema.Schema{
   134  					"top_level_a": {
   135  						Elem: &schema.Resource{
   136  							Schema: map[string]*schema.Schema{
   137  								"down_two": {},
   138  							},
   139  						},
   140  					},
   141  					"top_level_b": {
   142  						Elem: &schema.Resource{
   143  							Schema: map[string]*schema.Schema{
   144  								"down_another": {},
   145  							},
   146  						},
   147  					},
   148  				},
   149  			},
   150  		},
   151  	}
   152  
   153  	for name, tc := range cases {
   154  		t.Run(name, func(t *testing.T) {
   155  			deleteOmittedFields(tc.args.sch, tc.args.omittedFields)
   156  			if diff := cmp.Diff(tc.want.sch, tc.args.sch); diff != "" {
   157  				t.Errorf("\n%s\ndeleteOmittedFields(...): -want, +got:\n%s", tc.reason, diff)
   158  			}
   159  		})
   160  	}
   161  }