Skip to content

Commit aac05da

Browse files
committed
fix: phantom diff, along with some acct test fixes
1 parent b94fce9 commit aac05da

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed

graphql/keys_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
var datablob = `{"data": {"someField": "someValue", "items": ["itemValueOne", "itemValueTwo"], "otherItems": [{"field1": "value1", "field2": "value2"}, {"field1": "value3", "field2": "value4"}]}}`
12+
var datablob = `{"data": {"someField": "someValue", "items": ["itemValueOne", "itemValueTwo"], "otherItems": [{"field1": "value1", "field2": "value2"}, {"field1": "value3", "field2": "value4"}, {"nestedList": ["nestedListValue"]}]}}`
1313

1414
func TestComputeMutationVariableKeys(t *testing.T) {
1515
cases := []struct {
@@ -28,6 +28,16 @@ func TestComputeMutationVariableKeys(t *testing.T) {
2828
computeKeys: map[string]interface{}{"id_key": "todos[1].id"},
2929
expectedValues: map[string]interface{}{"id_key": "second_id"},
3030
},
31+
{
32+
body: `{"data": {"todos": ["stringval"]}}`,
33+
computeKeys: map[string]interface{}{"id_key": "todos[0]"},
34+
expectedValues: map[string]interface{}{"id_key": "stringval"},
35+
},
36+
{
37+
body: `{"data": {"todos": ["notanobject", "another"]}}`,
38+
computeKeys: map[string]interface{}{"id_key": "todos[1]"},
39+
expectedValues: map[string]interface{}{"id_key": "another"},
40+
},
3141
{
3242
body: `{"data": {"todos": [{"id": "computed_id"}, {"id": "second_id"}]}}`,
3343
computeKeys: map[string]interface{}{"id_key": "todos[3].id"},
@@ -43,11 +53,6 @@ func TestComputeMutationVariableKeys(t *testing.T) {
4353
computeKeys: map[string]interface{}{"id_key": "todo[0].notreal"},
4454
expectedErrorMsg: "structure at key [todo] must be an array",
4555
},
46-
{
47-
body: `{"data": {"todos": ["notanobject"]}}`,
48-
computeKeys: map[string]interface{}{"id_key": "todos[0].id"},
49-
expectedErrorMsg: "malformed structure at provided index: 0",
50-
},
5156
{
5257
body: `{"data": {"todos": [{"id": "computed_id"}, {"id": "second_id"}]}}`,
5358
computeKeys: map[string]interface{}{"id_key": "todos.id"},
@@ -108,8 +113,8 @@ func TestMapQueryResponse(t *testing.T) {
108113
expectKey: "data.someField",
109114
},
110115
{
111-
value: "anotherListValue1",
112-
expectKey: "data.funItems[0].anotherList[0]",
116+
value: "nestedListValue",
117+
expectKey: "data.otherItems[2].nestedList[0]",
113118
},
114119
}
115120

graphql/mocks.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ package graphql
22

33
import (
44
"net/http"
5+
"os"
56

67
"github.com/jarcoal/httpmock"
78
)
89

910
const (
10-
readDataResponse = `{"data": {"todo": {"id": "1", "text": "something todo", "userId": "900"}}}`
11-
createDataResponse = `{"data": {"createTodo": {"id": "2"}}}`
12-
errDataResponse = `{"data": {}, errors: [{message: "bad things happened"}]}`
13-
queryUrl = "http://mock-gql-url.io"
11+
readDataResponse = `{"data": {"todo": {"id": "1", "text": "something todo", "userId": "900"}}}`
12+
readDataResponseUpdate = `{"data": {"todo": {"id": "1", "text": "something else", "userId": "500"}}}`
13+
readDataResponseResponseForceReplace = `{"data": {"todo": {"id": "1", "text": "forced replacement", "userId": "500"}}}`
14+
readDataResponseUpdateRemoteStateVerifyDisabled = `{"data": {"todo": {"id": "1", "text": "something else", "userId": "500"}}}`
15+
createDataResponse = `{"data": {"createTodo": {"id": "2"}}}`
16+
errDataResponse = `{"data": {}, errors: [{message: "bad things happened"}]}`
17+
queryUrl = "http://mock-gql-url.io"
18+
queryUrlUpdate = "http://mock-gql-url-update.io"
1419

1520
dataSourceConfig = `
1621
data "graphql_query" "basic_query" {
@@ -382,10 +387,30 @@ const (
382387
`
383388
)
384389

390+
func setupBasicCreate() {
391+
os.Setenv("TF_GRAPHQL_URL", queryUrl)
392+
}
393+
394+
func setupBasicUpdate() {
395+
os.Setenv("TF_GRAPHQL_URL", queryUrlUpdate)
396+
}
397+
385398
func mockGqlServerResponse(req *http.Request) (*http.Response, error) {
386399
return httpmock.NewStringResponse(200, readDataResponse), nil
387400
}
388401

402+
func mockGqlServerResponseUpdate(req *http.Request) (*http.Response, error) {
403+
return httpmock.NewStringResponse(200, readDataResponseUpdate), nil
404+
}
405+
406+
func mockGqlServerResponseForceReplace(req *http.Request) (*http.Response, error) {
407+
return httpmock.NewStringResponse(200, readDataResponseResponseForceReplace), nil
408+
}
409+
410+
func mockGqlServerResponseRemoteStateVerifyDisabled(req *http.Request) (*http.Response, error) {
411+
return httpmock.NewStringResponse(200, readDataResponseUpdateRemoteStateVerifyDisabled), nil
412+
}
413+
389414
func mockGqlServerResponseError(req *http.Request) (*http.Response, error) {
390415
return httpmock.NewStringResponse(200, errDataResponse), nil
391416
}

graphql/resource_graphql_mutation.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func resourceGraphqlRead(ctx context.Context, d *schema.ResourceData, m interfac
163163

164164
queryVariables := d.Get("read_query_variables").(map[string]interface{})
165165
computedVariables := d.Get("computed_read_operation_variables").(map[string]interface{})
166-
queryResponseInputKeyMap := d.Get("query_response_input_key_map").(map[string]interface{})
167166
enableRemoteStateReconciliation := d.Get("enable_remote_state_verification").(bool)
168167

169168
for k, v := range queryVariables {
@@ -188,6 +187,8 @@ func resourceGraphqlRead(ctx context.Context, d *schema.ResourceData, m interfac
188187
}
189188

190189
if enableRemoteStateReconciliation {
190+
queryResponseInputKeyMap := d.Get("query_response_input_key_map").(map[string]interface{})
191+
191192
mappedKeys := make(map[string]interface{})
192193
mutationVars := d.Get("mutation_variables").(map[string]interface{})
193194

@@ -225,6 +226,11 @@ func resourceGraphqlRead(ctx context.Context, d *schema.ResourceData, m interfac
225226
if err := d.Set("mutation_variables", mutationVars); err != nil {
226227
return diag.FromErr(err)
227228
}
229+
} else {
230+
// If enable_remote_state_verification = false, we need to set this to an empty map to prevent phantom diff
231+
if err := d.Set("query_response_input_key_map", make(map[string]interface{})); err != nil {
232+
return diag.FromErr(err)
233+
}
228234
}
229235

230236
computeFromCreate := d.Get("compute_from_create").(bool)

graphql/resource_graphql_mutation_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func init() {
14-
os.Setenv("TF_GRAPHQL_URL", queryUrl)
14+
// os.Setenv("TF_GRAPHQL_URL", queryUrl)
1515
os.Setenv("TF_ACC", "1")
1616
}
1717

@@ -20,14 +20,16 @@ func TestAccGraphqlMutation_full(t *testing.T) {
2020
defer httpmock.DeactivateAndReset()
2121

2222
httpmock.RegisterResponder("POST", queryUrl, mockGqlServerResponse)
23+
httpmock.RegisterResponder("POST", queryUrlUpdate, mockGqlServerResponseUpdate)
2324

2425
resource.Test(t, resource.TestCase{
2526
PreCheck: func() { testAccPreCheck(t) },
2627
Providers: testAccProviders,
2728
CheckDestroy: testAccGraphqlMutationResourceDestroy,
2829
Steps: []resource.TestStep{
2930
{
30-
Config: resourceConfigCreate,
31+
Config: resourceConfigCreate,
32+
PreConfig: setupBasicCreate,
3133
Check: resource.ComposeTestCheckFunc(
3234
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
3335
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "something todo"),
@@ -38,7 +40,8 @@ func TestAccGraphqlMutation_full(t *testing.T) {
3840
),
3941
},
4042
{
41-
Config: resourceConfigUpdate,
43+
Config: resourceConfigUpdate,
44+
PreConfig: setupBasicUpdate,
4245
Check: resource.ComposeTestCheckFunc(
4346
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
4447
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "something else"),
@@ -64,6 +67,7 @@ func TestAccGraphqlMutation_expectError(t *testing.T) {
6467
Steps: []resource.TestStep{
6568
{
6669
Config: resourceConfigCreate,
70+
PreConfig: setupBasicCreate,
6771
ExpectError: regexp.MustCompile("bad things happened"),
6872
},
6973
},
@@ -82,7 +86,9 @@ func TestAccGraphqlMutation_computefromcreate(t *testing.T) {
8286
CheckDestroy: testAccGraphqlMutationResourceDestroy,
8387
Steps: []resource.TestStep{
8488
{
85-
Config: resourceConfigComputeMutationKeysOnCreate,
89+
Config: resourceConfigComputeMutationKeysOnCreate,
90+
PreConfig: setupBasicCreate,
91+
ExpectNonEmptyPlan: true,
8692
Check: resource.ComposeTestCheckFunc(
8793
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "2"),
8894
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_delete_operation_variables.id", "2"),
@@ -100,14 +106,16 @@ func TestAccGraphqlMutation_force_replace(t *testing.T) {
100106
defer httpmock.DeactivateAndReset()
101107

102108
httpmock.RegisterResponder("POST", queryUrl, mockGqlServerResponse)
109+
httpmock.RegisterResponder("POST", queryUrlUpdate, mockGqlServerResponseForceReplace)
103110

104111
resource.Test(t, resource.TestCase{
105112
PreCheck: func() { testAccPreCheck(t) },
106113
Providers: testAccProviders,
107114
CheckDestroy: testAccGraphqlMutationResourceDestroy,
108115
Steps: []resource.TestStep{
109116
{
110-
Config: resourceConfigCreate,
117+
Config: resourceConfigCreate,
118+
PreConfig: setupBasicCreate,
111119
Check: resource.ComposeTestCheckFunc(
112120
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
113121
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "something todo"),
@@ -118,7 +126,8 @@ func TestAccGraphqlMutation_force_replace(t *testing.T) {
118126
),
119127
},
120128
{
121-
Config: resourceConfigUpdateForceReplace,
129+
Config: resourceConfigUpdateForceReplace,
130+
PreConfig: setupBasicUpdate,
122131
Check: resource.ComposeTestCheckFunc(
123132
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
124133
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "forced replacement"),
@@ -136,14 +145,16 @@ func TestAccGraphqlMutation_remote_verify_disable(t *testing.T) {
136145
defer httpmock.DeactivateAndReset()
137146

138147
httpmock.RegisterResponder("POST", queryUrl, mockGqlServerResponse)
148+
httpmock.RegisterResponder("POST", queryUrlUpdate, mockGqlServerResponseRemoteStateVerifyDisabled)
139149

140150
resource.Test(t, resource.TestCase{
141151
PreCheck: func() { testAccPreCheck(t) },
142152
Providers: testAccProviders,
143153
CheckDestroy: testAccGraphqlMutationResourceDestroy,
144154
Steps: []resource.TestStep{
145155
{
146-
Config: resourceConfigCreateRemoteStateVerificationDisabled,
156+
Config: resourceConfigCreateRemoteStateVerificationDisabled,
157+
PreConfig: setupBasicCreate,
147158
Check: resource.ComposeTestCheckFunc(
148159
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
149160
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "something todo"),
@@ -153,6 +164,18 @@ func TestAccGraphqlMutation_remote_verify_disable(t *testing.T) {
153164
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "query_response", readDataResponse),
154165
),
155166
},
167+
{
168+
Config: resourceConfigCreateRemoteStateVerificationDisabled,
169+
PreConfig: setupBasicUpdate,
170+
Check: resource.ComposeTestCheckFunc(
171+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.id", "1"),
172+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.text", "something todo"),
173+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_update_operation_variables.userId", "900"),
174+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_delete_operation_variables.id", "1"),
175+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "computed_delete_operation_variables.testvar1", "testval1"),
176+
resource.TestCheckResourceAttr("graphql_mutation.basic_mutation", "query_response", readDataResponseUpdateRemoteStateVerifyDisabled),
177+
),
178+
},
156179
},
157180
})
158181
}

0 commit comments

Comments
 (0)