|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package privatestate_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-provider-aws/internal/framework/privatestate" |
| 14 | +) |
| 15 | + |
| 16 | +func TestWriteOnlyValueStore_HasValue(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + ctx := t.Context() |
| 20 | + store1 := privatestate.NewWriteOnlyValueStore(&privateState{}, "key") |
| 21 | + store2 := privatestate.NewWriteOnlyValueStore(&privateState{}, "key") |
| 22 | + store2.SetValue(ctx, types.StringValue("value1")) |
| 23 | + |
| 24 | + testCases := []struct { |
| 25 | + testName string |
| 26 | + store *privatestate.WriteOnlyValueStore |
| 27 | + wantValue bool |
| 28 | + }{ |
| 29 | + { |
| 30 | + testName: "empty state", |
| 31 | + store: store1, |
| 32 | + }, |
| 33 | + { |
| 34 | + testName: "has value", |
| 35 | + store: store2, |
| 36 | + wantValue: true, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + for _, testCase := range testCases { |
| 41 | + t.Run(testCase.testName, func(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + gotValue, diags := testCase.store.HasValue(ctx) |
| 44 | + if diags.HasError() { |
| 45 | + t.Fatal("unexpected error") |
| 46 | + } |
| 47 | + if got, want := gotValue, testCase.wantValue; !cmp.Equal(got, want) { |
| 48 | + t.Errorf("got %t, want %t", got, want) |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestWriteOnlyValueStore_EqualValue(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + ctx := t.Context() |
| 58 | + store1 := privatestate.NewWriteOnlyValueStore(&privateState{}, "key") |
| 59 | + store1.SetValue(ctx, types.StringValue("value1")) |
| 60 | + store2 := privatestate.NewWriteOnlyValueStore(&privateState{}, "key") |
| 61 | + store2.SetValue(ctx, types.StringValue("value2")) |
| 62 | + |
| 63 | + testCases := []struct { |
| 64 | + testName string |
| 65 | + store *privatestate.WriteOnlyValueStore |
| 66 | + wantEqual bool |
| 67 | + }{ |
| 68 | + { |
| 69 | + testName: "equal", |
| 70 | + store: store1, |
| 71 | + wantEqual: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + testName: "not equal", |
| 75 | + store: store2, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, testCase := range testCases { |
| 80 | + t.Run(testCase.testName, func(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + gotEqual, diags := testCase.store.EqualValue(ctx, types.StringValue("value1")) |
| 83 | + if diags.HasError() { |
| 84 | + t.Fatal("unexpected error") |
| 85 | + } |
| 86 | + if got, want := gotEqual, testCase.wantEqual; !cmp.Equal(got, want) { |
| 87 | + t.Errorf("got %t, want %t", got, want) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +type privateState struct { |
| 94 | + data map[string][]byte |
| 95 | +} |
| 96 | + |
| 97 | +func (p *privateState) GetKey(_ context.Context, key string) ([]byte, diag.Diagnostics) { |
| 98 | + var diags diag.Diagnostics |
| 99 | + bytes := p.data[key] |
| 100 | + return bytes, diags |
| 101 | +} |
| 102 | + |
| 103 | +func (p *privateState) SetKey(_ context.Context, key string, value []byte) diag.Diagnostics { |
| 104 | + var diags diag.Diagnostics |
| 105 | + |
| 106 | + if p.data == nil { |
| 107 | + p.data = make(map[string][]byte) |
| 108 | + } |
| 109 | + |
| 110 | + p.data[key] = value |
| 111 | + return diags |
| 112 | +} |
0 commit comments