|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package timeouts_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/ephemeral/timeouts" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/internal/validators" |
| 18 | +) |
| 19 | + |
| 20 | +func TestBlockWithOpts(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + type testCase struct { |
| 24 | + opts timeouts.Opts |
| 25 | + expected schema.Block |
| 26 | + } |
| 27 | + tests := map[string]testCase{ |
| 28 | + "empty-opts": { |
| 29 | + opts: timeouts.Opts{}, |
| 30 | + expected: schema.SingleNestedBlock{ |
| 31 | + CustomType: timeouts.Type{ |
| 32 | + ObjectType: types.ObjectType{ |
| 33 | + AttrTypes: map[string]attr.Type{ |
| 34 | + "open": types.StringType, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "open": schema.StringAttribute{ |
| 40 | + Optional: true, |
| 41 | + Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` + |
| 42 | + `consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` + |
| 43 | + `"s" (seconds), "m" (minutes), "h" (hours).`, |
| 44 | + Validators: []validator.String{ |
| 45 | + validators.TimeDuration(), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "open-opts-description": { |
| 52 | + opts: timeouts.Opts{ |
| 53 | + OpenDescription: "open description", |
| 54 | + }, |
| 55 | + expected: schema.SingleNestedBlock{ |
| 56 | + CustomType: timeouts.Type{ |
| 57 | + ObjectType: types.ObjectType{ |
| 58 | + AttrTypes: map[string]attr.Type{ |
| 59 | + "open": types.StringType, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Attributes: map[string]schema.Attribute{ |
| 64 | + "open": schema.StringAttribute{ |
| 65 | + Optional: true, |
| 66 | + Description: "open description", |
| 67 | + Validators: []validator.String{ |
| 68 | + validators.TimeDuration(), |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for name, test := range tests { |
| 77 | + name, test := name, test |
| 78 | + t.Run(name, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + actual := timeouts.BlockWithOpts(context.Background(), test.opts) |
| 81 | + |
| 82 | + if diff := cmp.Diff(actual, test.expected); diff != "" { |
| 83 | + t.Errorf("unexpected block difference: %s", diff) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestBlock(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + type testCase struct { |
| 93 | + expected schema.Block |
| 94 | + } |
| 95 | + tests := map[string]testCase{ |
| 96 | + "open": { |
| 97 | + expected: schema.SingleNestedBlock{ |
| 98 | + CustomType: timeouts.Type{ |
| 99 | + ObjectType: types.ObjectType{ |
| 100 | + AttrTypes: map[string]attr.Type{ |
| 101 | + "open": types.StringType, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + Attributes: map[string]schema.Attribute{ |
| 106 | + "open": schema.StringAttribute{ |
| 107 | + Optional: true, |
| 108 | + Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` + |
| 109 | + `consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` + |
| 110 | + `"s" (seconds), "m" (minutes), "h" (hours).`, |
| 111 | + Validators: []validator.String{ |
| 112 | + validators.TimeDuration(), |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for name, test := range tests { |
| 121 | + name, test := name, test |
| 122 | + t.Run(name, func(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + actual := timeouts.Block(context.Background()) |
| 125 | + |
| 126 | + if diff := cmp.Diff(actual, test.expected); diff != "" { |
| 127 | + t.Errorf("unexpected block difference: %s", diff) |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestAttributesWithOpts(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + |
| 136 | + type testCase struct { |
| 137 | + opts timeouts.Opts |
| 138 | + expected schema.Attribute |
| 139 | + } |
| 140 | + tests := map[string]testCase{ |
| 141 | + "empty-opts": { |
| 142 | + opts: timeouts.Opts{}, |
| 143 | + expected: schema.SingleNestedAttribute{ |
| 144 | + Attributes: map[string]schema.Attribute{ |
| 145 | + "open": schema.StringAttribute{ |
| 146 | + Optional: true, |
| 147 | + Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` + |
| 148 | + `consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` + |
| 149 | + `"s" (seconds), "m" (minutes), "h" (hours).`, |
| 150 | + Validators: []validator.String{ |
| 151 | + validators.TimeDuration(), |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + CustomType: timeouts.Type{ |
| 156 | + ObjectType: types.ObjectType{ |
| 157 | + AttrTypes: map[string]attr.Type{ |
| 158 | + "open": types.StringType, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + Optional: true, |
| 163 | + }, |
| 164 | + }, |
| 165 | + "open-opts-description": { |
| 166 | + opts: timeouts.Opts{ |
| 167 | + OpenDescription: "open description", |
| 168 | + }, |
| 169 | + expected: schema.SingleNestedAttribute{ |
| 170 | + Attributes: map[string]schema.Attribute{ |
| 171 | + "open": schema.StringAttribute{ |
| 172 | + Optional: true, |
| 173 | + Description: "open description", |
| 174 | + Validators: []validator.String{ |
| 175 | + validators.TimeDuration(), |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + CustomType: timeouts.Type{ |
| 180 | + ObjectType: types.ObjectType{ |
| 181 | + AttrTypes: map[string]attr.Type{ |
| 182 | + "open": types.StringType, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + Optional: true, |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + for name, test := range tests { |
| 192 | + name, test := name, test |
| 193 | + t.Run(name, func(t *testing.T) { |
| 194 | + t.Parallel() |
| 195 | + actual := timeouts.AttributesWithOpts(context.Background(), test.opts) |
| 196 | + |
| 197 | + if diff := cmp.Diff(actual, test.expected); diff != "" { |
| 198 | + t.Errorf("unexpected block difference: %s", diff) |
| 199 | + } |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func TestAttributes(t *testing.T) { |
| 205 | + t.Parallel() |
| 206 | + |
| 207 | + type testCase struct { |
| 208 | + expected schema.Attribute |
| 209 | + } |
| 210 | + tests := map[string]testCase{ |
| 211 | + "open": { |
| 212 | + expected: schema.SingleNestedAttribute{ |
| 213 | + Attributes: map[string]schema.Attribute{ |
| 214 | + "open": schema.StringAttribute{ |
| 215 | + Optional: true, |
| 216 | + Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` + |
| 217 | + `consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` + |
| 218 | + `"s" (seconds), "m" (minutes), "h" (hours).`, |
| 219 | + Validators: []validator.String{ |
| 220 | + validators.TimeDuration(), |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + CustomType: timeouts.Type{ |
| 225 | + ObjectType: types.ObjectType{ |
| 226 | + AttrTypes: map[string]attr.Type{ |
| 227 | + "open": types.StringType, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + Optional: true, |
| 232 | + }, |
| 233 | + }, |
| 234 | + } |
| 235 | + |
| 236 | + for name, test := range tests { |
| 237 | + name, test := name, test |
| 238 | + t.Run(name, func(t *testing.T) { |
| 239 | + t.Parallel() |
| 240 | + actual := timeouts.Attributes(context.Background()) |
| 241 | + |
| 242 | + if diff := cmp.Diff(actual, test.expected); diff != "" { |
| 243 | + t.Errorf("unexpected block difference: %s", diff) |
| 244 | + } |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
0 commit comments