Skip to content

Commit

Permalink
Add test coverage for handling of numeric values by Plan.UnmarshalJSON (
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Dec 7, 2023
1 parent 970acde commit a2ced5d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
56 changes: 56 additions & 0 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,59 @@ func TestPlan_movedBlock(t *testing.T) {
t.Fatalf("unexpected previous address %s, expected is random_id.test", plan.ResourceChanges[0].PreviousAddress)
}
}

func TestPlan_UnmarshalJSON(t *testing.T) {
t.Parallel()

b, err := os.ReadFile("testdata/numerics/plan.json")
if err != nil {
t.Fatal(err)
}

testCases := map[string]struct {
useJSONNumber bool
expected any
}{
"float64": {
expected: 1.23,
},
"json-number": {
useJSONNumber: true,
expected: json.Number("1.23"),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

plan := &Plan{}

plan.UseJSONNumber(testCase.useJSONNumber)

err = plan.UnmarshalJSON(b)

if err != nil {
t.Fatal(err)
}

after, ok := plan.ResourceChanges[0].Change.After.(map[string]any)

if !ok {
t.Fatal("plan.ResourceChanges[0].Change.After cannot be asserted as map[string]any")
}

attr, ok := after["configurable_attribute"]

if !ok {
t.Fatal("configurable attribute not found")
}

if diff := cmp.Diff(attr, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
75 changes: 75 additions & 0 deletions testdata/numerics/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"format_version": "1.2",
"terraform_version": "1.6.5",
"planned_values": {
"root_module": {
"resources": [
{
"address": "example_resource.test",
"mode": "managed",
"type": "example_resource",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/example",
"schema_version": 0,
"values": {
"configurable_attribute": 1.23,
"id": "one"
},
"sensitive_values": {}
}
]
}
},
"resource_changes": [
{
"address": "example_resource.test",
"mode": "managed",
"type": "example_resource",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/example",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"configurable_attribute": 1.23,
"id": "one"
},
"after_unknown": {},
"before_sensitive": false,
"after_sensitive": {}
}
}
],
"configuration": {
"provider_config": {
"example": {
"name": "example",
"full_name": "registry.terraform.io/hashicorp/example"
}
},
"root_module": {
"resources": [
{
"address": "example_resource.test",
"mode": "managed",
"type": "example_resource",
"name": "test",
"provider_config_key": "example",
"expressions": {
"configurable_attribute": {
"constant_value": 1.23
},
"id": {
"constant_value": "one"
}
},
"schema_version": 0
}
]
}
},
"timestamp": "2023-12-07T13:55:56Z",
"errored": false
}

0 comments on commit a2ced5d

Please sign in to comment.