forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_test.go
127 lines (116 loc) · 5.8 KB
/
remove_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package json_test
import (
"testing"
"github.com/hashicorp/terraform-provider-aws/internal/json"
)
func TestRemoveFields(t *testing.T) {
t.Parallel()
testCases := []struct {
testName string
input string
want string
}{
{
testName: "empty JSON",
input: "{}",
want: "{}",
},
{
testName: "single field",
input: `{ "key": 42 }`,
want: `{"key":42}`,
},
{
testName: "with read-only field",
input: "{\"unifiedAlerting\": {\"enabled\": true}, \"plugins\": {\"pluginAdminEnabled\" :false}}",
want: "{\"unifiedAlerting\":{\"enabled\":true}}",
},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
t.Parallel()
if got, want := json.RemoveFields(testCase.input, `"plugins"`), testCase.want; got != want {
t.Errorf("RemoveReadOnlyFields(%q) = %q, want %q", testCase.input, got, want)
}
})
}
}
func TestRemoveEmptyFields(t *testing.T) {
t.Parallel()
testCases := []struct {
testName string
input string
want string
}{
{
testName: "empty JSON",
input: "{}",
want: "{}",
},
{
testName: "single non-empty simple field",
input: `{"key": 42}`,
want: `{"key":42}`,
},
{
testName: "single non-empty array field",
input: `{"key": [1, true, "answer"]}`,
want: `{"key":[1,true,"answer"]}`,
},
{
testName: "single non-empty object field",
input: `{"key": {"inner": true}}`,
want: `{"key":{"inner":true}}`,
},
{
testName: "single null field",
input: `{"key": null}`,
want: `{}`,
},
{
testName: "single empty array field",
input: `{"key": []}`,
want: `{}`,
},
{
testName: "single empty object field",
input: `{"key": {}}`,
want: `{}`,
},
{
testName: "empty fields deeply nested 1 pass",
input: `{"key": {"a": [1, 2], "b": [], "c": {"d": true, "e": null}}}`,
want: `{"key":{"a":[1,2],"c":{"d":true}}}`,
},
{
testName: "empty fields deeply nested 2 passes",
input: `{"key": {"a": [1, 2], "b": {}, "c": {"d": null}}}`,
want: `{"key":{"a":[1,2]}}`,
},
{
testName: "empty fields deeply nested many empty objects",
input: `{"key": {"a": [1, 2], "b": {}, "c": {"d": {}}, "e": {}, "f": 99}}`,
want: `{"key":{"a":[1,2],"f":99}}`,
},
{
testName: "empty fields nested empty arrays",
input: `{"key": {"a": [1, [2], [], [[]], 3]}}`,
want: `{"key":{"a":[1,[2],3]}}`,
},
{
testName: "real life example",
input: `{"TargetMetadata":{"SupportLobs":true,"LimitedSizeLobMode":true,"LobMaxSize":32},"FullLoadSettings":{"TargetTablePrepMode":"DROP_AND_CREATE","MaxFullLoadSubTasks":8,"TransactionConsistencyTimeout":600,"CommitRate":10000},"TTSettings":{"TTS3Settings":{},"TTRecordSettings":{}},"Logging":{},"ControlTablesSettings":{"HistoryTimeslotInMinutes":5},"StreamBufferSettings":{"StreamBufferCount":3,"StreamBufferSizeInMB":8},"ChangeProcessingTuning":{"BatchApplyPreserveTransaction":true,"BatchApplyTimeoutMin":1,"BatchApplyTimeoutMax":30,"BatchApplyMemoryLimit":500,"MinTransactionSize":1000,"CommitTimeout":1,"MemoryLimitTotal":1024,"MemoryKeepTime":60,"StatementCacheSize":50},"ChangeProcessingDdlHandlingPolicy":{"HandleSourceTableDropped":true,"HandleSourceTableTruncated":true,"HandleSourceTableAltered":true},"LoopbackPreventionSettings":{},"CharacterSetSettings":{"CharacterSetSupport":{}},"BeforeImageSettings":{},"ErrorBehavior":{"DataErrorPolicy":"LOG_ERROR","DataTruncationErrorPolicy":"LOG_ERROR","DataErrorEscalationPolicy":"SUSPEND_TABLE","TableErrorPolicy":"SUSPEND_TABLE","TableErrorEscalationPolicy":"STOP_TASK","RecoverableErrorCount":-1,"RecoverableErrorInterval":5,"RecoverableErrorThrottling":true,"RecoverableErrorThrottlingMax":1800,"ApplyErrorDeletePolicy":"IGNORE_RECORD","ApplyErrorInsertPolicy":"LOG_ERROR","ApplyErrorUpdatePolicy":"LOG_ERROR","ApplyErrorEscalationPolicy":"LOG_ERROR","FullLoadIgnoreConflicts":true},"ValidationSettings":{"ValidationMode":"ROW_LEVEL","ThreadCount":5,"PartitionSize":10000,"FailureMaxCount":10000,"TableFailureMaxCount":1000}}`,
want: `{"TargetMetadata":{"SupportLobs":true,"LimitedSizeLobMode":true,"LobMaxSize":32},"FullLoadSettings":{"TargetTablePrepMode":"DROP_AND_CREATE","MaxFullLoadSubTasks":8,"TransactionConsistencyTimeout":600,"CommitRate":10000},"ControlTablesSettings":{"HistoryTimeslotInMinutes":5},"StreamBufferSettings":{"StreamBufferCount":3,"StreamBufferSizeInMB":8},"ChangeProcessingTuning":{"BatchApplyPreserveTransaction":true,"BatchApplyTimeoutMin":1,"BatchApplyTimeoutMax":30,"BatchApplyMemoryLimit":500,"MinTransactionSize":1000,"CommitTimeout":1,"MemoryLimitTotal":1024,"MemoryKeepTime":60,"StatementCacheSize":50},"ChangeProcessingDdlHandlingPolicy":{"HandleSourceTableDropped":true,"HandleSourceTableTruncated":true,"HandleSourceTableAltered":true},"ErrorBehavior":{"DataErrorPolicy":"LOG_ERROR","DataTruncationErrorPolicy":"LOG_ERROR","DataErrorEscalationPolicy":"SUSPEND_TABLE","TableErrorPolicy":"SUSPEND_TABLE","TableErrorEscalationPolicy":"STOP_TASK","RecoverableErrorCount":-1,"RecoverableErrorInterval":5,"RecoverableErrorThrottling":true,"RecoverableErrorThrottlingMax":1800,"ApplyErrorDeletePolicy":"IGNORE_RECORD","ApplyErrorInsertPolicy":"LOG_ERROR","ApplyErrorUpdatePolicy":"LOG_ERROR","ApplyErrorEscalationPolicy":"LOG_ERROR","FullLoadIgnoreConflicts":true},"ValidationSettings":{"ValidationMode":"ROW_LEVEL","ThreadCount":5,"PartitionSize":10000,"FailureMaxCount":10000,"TableFailureMaxCount":1000}}`,
},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
t.Parallel()
if got, want := json.RemoveEmptyFields([]byte(testCase.input)), testCase.want; string(got) != want {
t.Errorf("RemoveEmptyFields(%q) = %q, want %q", testCase.input, got, want)
}
})
}
}