|
| 1 | +package hcloudutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTerraformLabelsToHCloud(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + inputLabels types.Map |
| 16 | + wantLabels *map[string]string |
| 17 | + wantDiagnostics bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Some Labels", |
| 21 | + inputLabels: types.MapValueMust(types.StringType, map[string]attr.Value{"key1": types.StringValue("value1")}), |
| 22 | + wantLabels: &map[string]string{"key1": "value1"}, |
| 23 | + wantDiagnostics: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Empty Labels", |
| 27 | + inputLabels: types.MapNull(types.StringType), |
| 28 | + wantLabels: &map[string]string{}, |
| 29 | + wantDiagnostics: false, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Invalid Map Labels", |
| 33 | + inputLabels: types.MapValueMust(types.BoolType, map[string]attr.Value{"key1": types.BoolValue(true)}), |
| 34 | + wantLabels: &map[string]string{}, |
| 35 | + wantDiagnostics: true, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + var outputLabels map[string]string |
| 42 | + diagnostics := TerraformLabelsToHCloud(context.Background(), tt.inputLabels, &outputLabels) |
| 43 | + assert.Equalf(t, tt.wantDiagnostics, diagnostics != nil, "Unexpected Diagnostics: %v", diagnostics) |
| 44 | + assert.Equalf(t, *tt.wantLabels, outputLabels, "Unexpected Labels") |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
0 commit comments