|  | 
|  | 1 | +package conversion_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"testing" | 
|  | 5 | + | 
|  | 6 | +	"github.com/hashicorp/terraform-plugin-framework/resource/schema" | 
|  | 7 | +	"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | 
|  | 8 | +	"github.com/stretchr/testify/assert" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +func TestUpdateSchemaDescription(t *testing.T) { | 
|  | 12 | +	s := schema.Schema{ | 
|  | 13 | +		Attributes: map[string]schema.Attribute{ | 
|  | 14 | +			"id": schema.StringAttribute{ | 
|  | 15 | +				Computed: true, | 
|  | 16 | +			}, | 
|  | 17 | +			"project_id": schema.StringAttribute{ | 
|  | 18 | +				Required:            true, | 
|  | 19 | +				MarkdownDescription: "mddesc project_id", | 
|  | 20 | +			}, | 
|  | 21 | +			"nested": schema.ListNestedAttribute{ | 
|  | 22 | +				Computed:            true, | 
|  | 23 | +				MarkdownDescription: "mddesc nested", | 
|  | 24 | +				NestedObject: schema.NestedAttributeObject{ | 
|  | 25 | +					Attributes: map[string]schema.Attribute{ | 
|  | 26 | +						"attr1": schema.StringAttribute{ | 
|  | 27 | +							Description: "desc attr1", | 
|  | 28 | +							Computed:    true, | 
|  | 29 | +						}, | 
|  | 30 | +						"attr2": schema.StringAttribute{ | 
|  | 31 | +							MarkdownDescription: "mddesc attr2", | 
|  | 32 | +							Computed:            true, | 
|  | 33 | +						}, | 
|  | 34 | +					}, | 
|  | 35 | +				}, | 
|  | 36 | +			}, | 
|  | 37 | +		}, | 
|  | 38 | +		Blocks: map[string]schema.Block{ | 
|  | 39 | +			"list": schema.ListNestedBlock{ | 
|  | 40 | +				MarkdownDescription: "mddesc list", | 
|  | 41 | +				NestedObject: schema.NestedBlockObject{ | 
|  | 42 | +					Attributes: map[string]schema.Attribute{ | 
|  | 43 | +						"attr3": schema.BoolAttribute{ | 
|  | 44 | +							Optional:            true, | 
|  | 45 | +							Computed:            true, | 
|  | 46 | +							MarkdownDescription: "mddesc attr3", | 
|  | 47 | +						}, | 
|  | 48 | +					}, | 
|  | 49 | +				}, | 
|  | 50 | +			}, | 
|  | 51 | +			"set": schema.SetNestedBlock{ | 
|  | 52 | +				MarkdownDescription: "mddesc set", | 
|  | 53 | +				NestedObject: schema.NestedBlockObject{ | 
|  | 54 | +					Attributes: map[string]schema.Attribute{ | 
|  | 55 | +						"attr4": schema.StringAttribute{ | 
|  | 56 | +							Optional:            true, | 
|  | 57 | +							MarkdownDescription: "mddesc attr4", | 
|  | 58 | +						}, | 
|  | 59 | +						"attr5": schema.Int64Attribute{ | 
|  | 60 | +							Required:            true, | 
|  | 61 | +							MarkdownDescription: "mddesc attr5", | 
|  | 62 | +						}, | 
|  | 63 | +					}, | 
|  | 64 | +				}, | 
|  | 65 | +			}, | 
|  | 66 | +		}, | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	expected := schema.Schema{ | 
|  | 70 | +		Attributes: map[string]schema.Attribute{ | 
|  | 71 | +			"id": schema.StringAttribute{ | 
|  | 72 | +				Computed: true, | 
|  | 73 | +			}, | 
|  | 74 | +			"project_id": schema.StringAttribute{ | 
|  | 75 | +				Required:            true, | 
|  | 76 | +				Description:         "mddesc project_id", | 
|  | 77 | +				MarkdownDescription: "mddesc project_id", | 
|  | 78 | +			}, | 
|  | 79 | +			"nested": schema.ListNestedAttribute{ | 
|  | 80 | +				Computed:            true, | 
|  | 81 | +				Description:         "mddesc nested", | 
|  | 82 | +				MarkdownDescription: "mddesc nested", | 
|  | 83 | +				NestedObject: schema.NestedAttributeObject{ | 
|  | 84 | +					Attributes: map[string]schema.Attribute{ | 
|  | 85 | +						"attr1": schema.StringAttribute{ | 
|  | 86 | +							Description:         "desc attr1", | 
|  | 87 | +							MarkdownDescription: "desc attr1", | 
|  | 88 | +							Computed:            true, | 
|  | 89 | +						}, | 
|  | 90 | +						"attr2": schema.StringAttribute{ | 
|  | 91 | +							Description:         "mddesc attr2", | 
|  | 92 | +							MarkdownDescription: "mddesc attr2", | 
|  | 93 | +							Computed:            true, | 
|  | 94 | +						}, | 
|  | 95 | +					}, | 
|  | 96 | +				}, | 
|  | 97 | +			}, | 
|  | 98 | +		}, | 
|  | 99 | +		Blocks: map[string]schema.Block{ | 
|  | 100 | +			"list": schema.ListNestedBlock{ | 
|  | 101 | +				Description:         "mddesc list", | 
|  | 102 | +				MarkdownDescription: "mddesc list", | 
|  | 103 | +				NestedObject: schema.NestedBlockObject{ | 
|  | 104 | +					Attributes: map[string]schema.Attribute{ | 
|  | 105 | +						"attr3": schema.BoolAttribute{ | 
|  | 106 | +							Optional:            true, | 
|  | 107 | +							Computed:            true, | 
|  | 108 | +							Description:         "mddesc attr3", | 
|  | 109 | +							MarkdownDescription: "mddesc attr3", | 
|  | 110 | +						}, | 
|  | 111 | +					}, | 
|  | 112 | +				}, | 
|  | 113 | +			}, | 
|  | 114 | +			"set": schema.SetNestedBlock{ | 
|  | 115 | +				Description:         "mddesc set", | 
|  | 116 | +				MarkdownDescription: "mddesc set", | 
|  | 117 | +				NestedObject: schema.NestedBlockObject{ | 
|  | 118 | +					Attributes: map[string]schema.Attribute{ | 
|  | 119 | +						"attr4": schema.StringAttribute{ | 
|  | 120 | +							Optional:            true, | 
|  | 121 | +							Description:         "mddesc attr4", | 
|  | 122 | +							MarkdownDescription: "mddesc attr4", | 
|  | 123 | +						}, | 
|  | 124 | +						"attr5": schema.Int64Attribute{ | 
|  | 125 | +							Required:            true, | 
|  | 126 | +							Description:         "mddesc attr5", | 
|  | 127 | +							MarkdownDescription: "mddesc attr5", | 
|  | 128 | +						}, | 
|  | 129 | +					}, | 
|  | 130 | +				}, | 
|  | 131 | +			}, | 
|  | 132 | +		}, | 
|  | 133 | +	} | 
|  | 134 | + | 
|  | 135 | +	conversion.UpdateSchemaDescription(&s) | 
|  | 136 | +	assert.Equal(t, expected, s) | 
|  | 137 | +} | 
|  | 138 | + | 
|  | 139 | +func TestUpdateAttrPanic(t *testing.T) { | 
|  | 140 | +	testCases := map[string]any{ | 
|  | 141 | +		"not ptr, please fix caller":    "no ptr", | 
|  | 142 | +		"not struct, please fix caller": conversion.Pointer("no struct"), | 
|  | 143 | +		"invalid desc fields, please fix caller": conversion.Pointer(struct { | 
|  | 144 | +			Description         int | 
|  | 145 | +			MarkdownDescription int | 
|  | 146 | +		}{}), | 
|  | 147 | +		"both descriptions exist, please fix caller: description": conversion.Pointer(struct { | 
|  | 148 | +			Description         string | 
|  | 149 | +			MarkdownDescription string | 
|  | 150 | +		}{ | 
|  | 151 | +			Description:         "description", | 
|  | 152 | +			MarkdownDescription: "markdown description", | 
|  | 153 | +		}), | 
|  | 154 | +		"not map, please fix caller: Attributes": conversion.Pointer(struct { | 
|  | 155 | +			Attributes string | 
|  | 156 | +		}{}), | 
|  | 157 | +	} | 
|  | 158 | + | 
|  | 159 | +	for name, tc := range testCases { | 
|  | 160 | +		t.Run(name, func(t *testing.T) { | 
|  | 161 | +			assert.PanicsWithValue(t, name, func() { | 
|  | 162 | +				conversion.UpdateAttr(tc) | 
|  | 163 | +			}) | 
|  | 164 | +		}) | 
|  | 165 | +	} | 
|  | 166 | +} | 
0 commit comments